withHighlights = false; return $this; } /** * Should we ignore bookmarks during export ? By default, bookmarks will * be exported as well. * * @return self */ public function withoutBookmarks() { $this->withoutBookmarks = false; return $this; } /** * Defines which user to export data for. If not defined, user will be * extracted from request. * * @return self */ public function forUser(User $user) { $this->forUser = $user; return $this; } /** * Defines from which folder data will be exported. If not defined, root * folder attached to specified user will be used. * * @return self */ public function fromFolder(Folder $folder) { $this->fromFolder = $folder; return $this; } /** * Export specified user's data into a PHP array. * * @return array */ public function export() { if (empty($this->fromFolder)) { $this->fromFolder = $this->forUser->groups()->active()->first()->folders()->ofType('root')->first(); } $rootArray = [ 'documents' => [], 'folders' => $this->exportTree($this->fromFolder->children()->get()), ]; foreach ($this->fromFolder->documents()->get() as $document) { $documentArray = [ 'url' => $document->url, 'feeds' => [], ]; foreach ($document->feeds()->get() as $feed) { $documentArray['feeds'] = [ 'url' => $feed->url, 'is_ignored' => $feed->is_ignored, ]; } $rootArray['documents'][] = $documentArray; } return [ 'highlights' => $this->forUser->highlights()->select(['expression', 'color'])->get(), 'bookmarks' => $rootArray, ]; } /** * Export a single tree branch. * * @param mixed $folders */ protected function exportTree($folders) { $array = []; foreach ($folders as $folder) { $folderArray = [ 'title' => $folder->title, 'documents' => [], 'folders' => [], ]; foreach ($folder->documents()->get() as $document) { $documentArray = [ 'url' => $document->url, 'feeds' => [], ]; foreach ($document->feeds()->get() as $feed) { $documentArray['feeds'][] = [ 'url' => $feed->url, 'is_ignored' => $feed->is_ignored, ]; } $folderArray['documents'][] = $documentArray; } $folderArray['folders'] = $this->exportTree(($folder->children()->get())); $array[] = $folderArray; } return $array; } }