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
@@ -38,8 +39,8 @@ class Database extends Endpoint
3839 /**
3940 * Database constructor.
4041 *
41- * @param string $databaseId
42- * @param Notion $notion
42+ * @param string $databaseId
43+ * @param Notion $notion
4344 *
4445 * @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException
4546 * @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
@@ -61,15 +62,26 @@ public function __construct(string $databaseId, Notion $notion)
6162 */
6263 public function query (): PageCollection
6364 {
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 {
6476 $ postData = [];
6577
6678 if ($ this ->sorts ->isNotEmpty ()) {
6779 $ postData ['sorts ' ] = Sorting::sortQuery ($ this ->sorts );
6880 }
6981
70- if ($ this ->filter !== null && ! is_null ($ this ->filterBag )) {
82+ if ($ this ->filter !== null && !is_null ($ this ->filterBag )) {
7183 throw new HandlingException ('Please provide either a filter bag or a single filter. ' );
72- } elseif ($ this ->filter !== null || ! is_null ($ this ->filterBag )) {
84+ } elseif ($ this ->filter !== null || !is_null ($ this ->filterBag )) {
7385 $ postData ['filter ' ] = $ this ->filterData ;
7486 }
7587
@@ -81,14 +93,7 @@ public function query(): PageCollection
8193 $ postData ['page_size ' ] = $ this ->pageSize ;
8294 }
8395
84- $ response = $ this
85- ->post (
86- $ this ->url (Endpoint::DATABASES ."/ {$ this ->databaseId }/query " ),
87- $ postData
88- )
89- ->json ();
90-
91- return new PageCollection ($ response );
96+ return $ postData ;
9297 }
9398
9499 /**
@@ -97,12 +102,12 @@ public function query(): PageCollection
97102 *
98103 * @throws HandlingException
99104 *
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,11 @@ 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+ * @throws HandlingException
125+ */
116126 public function filterBySingleFilter (Filter $ filter ): Database
117127 {
118128 $ this ->filter = $ filter ;
@@ -122,8 +132,8 @@ public function filterBySingleFilter(Filter $filter): Database
122132 }
123133
124134 /**
125- * @param FilterBag $filterBag
126- * @return $this
135+ * @param FilterBag $filterBag
136+ * @return Database $this
127137 */
128138 public function filterByBag (FilterBag $ filterBag ): Database
129139 {
@@ -134,7 +144,18 @@ public function filterByBag(FilterBag $filterBag): Database
134144 }
135145
136146 /**
137- * @param Collection|Sorting $sorts
147+ * @param Collection $filterCollection
148+ * @return Database $this
149+ */
150+ public function filterByCollection (Collection $ filterCollection ): Database {
151+ $ filterBag = new FilterBag (Operators::OR );
152+ $ filterBag ->addFilters ($ filterCollection );
153+
154+ return $ this ->filterByBag ($ filterBag );
155+ }
156+
157+ /**
158+ * @param Collection|Sorting $sorts
138159 * @return Database $this
139160 *
140161 * @throws HandlingException
@@ -150,14 +171,14 @@ public function sortBy(Sorting|Collection $sorts): Database
150171 $ this ->sorts = $ sorts ;
151172 break ;
152173 default :
153- throw new HandlingException ("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings . " );
174+ throw new HandlingException ("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of sortings . " );
154175 }
155176
156177 return $ this ;
157178 }
158179
159180 /**
160- * @param EntityCollection $entityCollection
181+ * @param EntityCollection $entityCollection
161182 * @return $this
162183 */
163184 public function offsetByResponse (EntityCollection $ entityCollection ): Database
@@ -166,11 +187,4 @@ public function offsetByResponse(EntityCollection $entityCollection): Database
166187
167188 return $ this ;
168189 }
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- }
176190}
0 commit comments