22
33namespace FiveamCode \LaravelNotionApi \Endpoints ;
44
5+ use FiveamCode \LaravelNotionApi \Entities \Collections \EntityCollection ;
6+ use FiveamCode \LaravelNotionApi \Entities \Collections \PageCollection ;
7+ use Illuminate \Support \Collection ;
58use FiveamCode \LaravelNotionApi \Notion ;
9+ use FiveamCode \LaravelNotionApi \Query \Filter ;
10+ use FiveamCode \LaravelNotionApi \Query \Sorting ;
11+ use FiveamCode \LaravelNotionApi \Query \StartCursor ;
12+ use FiveamCode \LaravelNotionApi \Exceptions \WrapperException ;
13+ use Symfony \Component \VarDumper \Cloner \Data ;
614
715class Search extends Endpoint
816{
17+ private string $ searchText ;
18+ private ?string $ filter = null ;
19+ private ?Sorting $ sort = null ;
920
10- // toDo: Think about it. 🧐
11- }
21+
22+ public function __construct (Notion $ notion , string $ searchText = "" )
23+ {
24+ $ this ->searchText = $ searchText ;
25+ parent ::__construct ($ notion );
26+ }
27+
28+ public function query (): Collection
29+ {
30+ $ postData = [];
31+
32+ if ($ this ->sort !== null )
33+ $ postData ["sort " ] = $ this ->sort ->toArray ();
34+
35+ if ($ this ->filter !== null )
36+ $ postData ["filter " ] = ['property ' => 'object ' , 'value ' => $ this ->filter ];
37+
38+ if ($ this ->startCursor !== null )
39+ $ postData ["start_cursor " ] = $ this ->startCursor ;
40+
41+ if ($ this ->pageSize !== null )
42+ $ postData ["page_size " ] = $ this ->pageSize ;
43+
44+ if ($ this ->searchText !== null )
45+ $ postData ['query ' ] = $ this ->searchText ;
46+
47+
48+
49+ $ response = $ this
50+ ->post (
51+ $ this ->url (Endpoint::SEARCH ),
52+ $ postData
53+ )
54+ ->json ();
55+
56+ $ pageCollection = new EntityCollection ($ response );
57+
58+ return $ pageCollection ->getResults ();
59+ }
60+
61+ public function sortByLastEditedTime (string $ direction = "ascending " ): Search
62+ {
63+ $ this ->sort = Sorting::timestampSort ("last_edited_time " , $ direction );
64+ return $ this ;
65+ }
66+
67+ public function onlyDatabases () : Search
68+ {
69+ $ this ->filter = "database " ;
70+ return $ this ;
71+ }
72+
73+ public function onlyPages () : Search
74+ {
75+ $ this ->filter = "page " ;
76+ return $ this ;
77+ }
78+
79+ public function getTitles ()
80+ {
81+ $ titleCollection = new Collection ();
82+ $ results = $ this ->query ();
83+
84+ foreach ($ results as $ result ) {
85+ $ titleCollection ->add ($ result ->getTitle ());
86+ }
87+
88+ return $ titleCollection ;
89+ }
90+
91+ public function getIds (){
92+ $ idCollection = new Collection ();
93+ $ results = $ this ->query ();
94+
95+ foreach ($ results as $ result ) {
96+ $ idCollection ->add ($ result ->getId ());
97+ }
98+ return $ idCollection ;
99+ }
100+
101+ public function filterBy (string $ filter )
102+ {
103+ $ this ->filter = $ filter ;
104+ return $ this ;
105+ }
106+
107+ public function sortBy (Sorting $ sort )
108+ {
109+ $ this ->sort = $ sort ;
110+ return $ this ;
111+ }
112+ }
0 commit comments