88use FiveamCode \LaravelNotionApi \Notion ;
99use FiveamCode \LaravelNotionApi \Query \Filters \Filter ;
1010use FiveamCode \LaravelNotionApi \Query \Filters \FilterBag ;
11+ use FiveamCode \LaravelNotionApi \Query \Filters \Operators ;
1112use FiveamCode \LaravelNotionApi \Query \Sorting ;
1213use Illuminate \Support \Collection ;
1314
@@ -60,6 +61,18 @@ public function __construct(string $databaseId, Notion $notion)
6061 * @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
6162 */
6263 public function query (): PageCollection
64+ {
65+ $ response = $ this
66+ ->post (
67+ $ this ->url (Endpoint::DATABASES ."/ {$ this ->databaseId }/query " ),
68+ $ this ->getPostData ()
69+ )
70+ ->json ();
71+
72+ return new PageCollection ($ response );
73+ }
74+
75+ public function getPostData (): array
6376 {
6477 $ postData = [];
6578
@@ -81,28 +94,20 @@ public function query(): PageCollection
8194 $ postData ['page_size ' ] = $ this ->pageSize ;
8295 }
8396
84- $ response = $ this
85- ->post (
86- $ this ->url (Endpoint::DATABASES ."/ {$ this ->databaseId }/query " ),
87- $ postData
88- )
89- ->json ();
90-
91- return new PageCollection ($ response );
97+ return $ postData ;
9298 }
9399
94100 /**
95101 * @param $filter
96102 * @return Database $this
97103 *
98104 * @throws HandlingException
99- *
100- * @todo As soon as this package drops PHP 7.4 support, we can use union types here (FilterBag and Filter)
101105 */
102- public function filterBy ($ filter ): Database // TODO that's a breaking change
106+ public function filterBy (Collection | Filter | FilterBag $ filter ): Database
103107 {
104- $ this ->checkFilterType ($ filter );
105-
108+ if ($ filter instanceof Collection) {
109+ return $ this ->filterByCollection ($ filter );
110+ }
106111 if ($ filter instanceof FilterBag) {
107112 return $ this ->filterByBag ($ filter );
108113 }
@@ -113,6 +118,12 @@ public function filterBy($filter): Database // TODO that's a breaking change
113118 return $ this ;
114119 }
115120
121+ /**
122+ * @param Filter $filter
123+ * @return $this
124+ *
125+ * @throws HandlingException
126+ */
116127 public function filterBySingleFilter (Filter $ filter ): Database
117128 {
118129 $ this ->filter = $ filter ;
@@ -123,7 +134,7 @@ public function filterBySingleFilter(Filter $filter): Database
123134
124135 /**
125136 * @param FilterBag $filterBag
126- * @return $this
137+ * @return Database $this
127138 */
128139 public function filterByBag (FilterBag $ filterBag ): Database
129140 {
@@ -133,6 +144,18 @@ public function filterByBag(FilterBag $filterBag): Database
133144 return $ this ;
134145 }
135146
147+ /**
148+ * @param Collection $filterCollection
149+ * @return Database $this
150+ */
151+ public function filterByCollection (Collection $ filterCollection ): Database
152+ {
153+ $ filterBag = new FilterBag (Operators::OR );
154+ $ filterBag ->addFilters ($ filterCollection );
155+
156+ return $ this ->filterByBag ($ filterBag );
157+ }
158+
136159 /**
137160 * @param Collection|Sorting $sorts
138161 * @return Database $this
@@ -150,7 +173,7 @@ public function sortBy(Sorting|Collection $sorts): Database
150173 $ this ->sorts = $ sorts ;
151174 break ;
152175 default :
153- throw new HandlingException ("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings . " );
176+ throw new HandlingException ("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of sortings . " );
154177 }
155178
156179 return $ this ;
@@ -166,11 +189,4 @@ public function offsetByResponse(EntityCollection $entityCollection): Database
166189
167190 return $ this ;
168191 }
169-
170- private function checkFilterType ($ filter ): void
171- {
172- if (! ($ filter instanceof Filter || $ filter instanceof FilterBag)) {
173- throw new HandlingException ('Please provide either a filter bag or a single filter. ' );
174- }
175- }
176192}
0 commit comments