22
33namespace FiveamCode \LaravelNotionApi \Query ;
44
5+ use FiveamCode \LaravelNotionApi \Exceptions \WrapperException ;
6+ use Illuminate \Support \Collection ;
7+
58class Filter extends QueryHelper
69{
10+ private ?string $ filterType = null ;
11+ private ?array $ filterConditions = null ;
12+ private ?array $ filterDefinition = null ;
13+
714
8- public function __construct (string $ property )
15+ public function __construct (
16+ string $ property ,
17+ string $ filterType = null ,
18+ array $ filterConditions = null ,
19+ array $ filterDefinition = null
20+ )
921 {
1022 parent ::__construct ();
1123
1224 $ this ->property = $ property ;
25+ $ this ->filterType = $ filterType ;
26+ $ this ->filterConditions = $ filterConditions ;
27+ $ this ->filterDefinition = $ filterDefinition ;
28+ }
1329
30+ /**
31+ * Returns a text filter instance.
32+ *
33+ * @see https://developers.notion.com/reference/post-database-query#text-filter-condition
34+ *
35+ * @param string $property
36+ * @param array $filterConditions
37+ * @return Filter
38+ */
39+ public static function textFilter (string $ property , array $ filterConditions ): Filter
40+ {
41+ return new Filter ($ property , "text " , $ filterConditions );
42+ }
1443
44+ /**
45+ * This method allows you to define every filter that is offered
46+ * by Notion but not implemented in this package yet. Provide the
47+ * filter definition as an array like explained in the Notion docs.
48+ * Use with caution; this method will be removed in the future and
49+ * is marked as deprecated from the start!
50+ *
51+ * @see https://developers.notion.com/reference/post-database-query#post-database-query-filter
52+ *
53+ * @param string $property
54+ * @param array $filterDefinition
55+ *
56+ * @deprecated
57+ */
58+ public static function rawFilter (string $ property , array $ filterDefinition ): Filter
59+ {
60+ return new Filter ($ property , null , null , $ filterDefinition );
1561 }
1662
17- // public static function textFilter(string $property, ) {
18- //
19- // }
63+ public function toArray (): array
64+ {
65+ if ($ this ->filterDefinition !== null && $ this ->filterType === null && $ this ->filterConditions === null ) {
66+ return array_merge (
67+ ["property " => $ this ->property ],
68+ $ this ->filterDefinition
69+ );
70+ }
71+ elseif ($ this ->filterType !== null && $ this ->filterConditions !== null && $ this ->filterDefinition === null ) {
72+ return [
73+ "property " => $ this ->property ,
74+ $ this ->filterType => $ this ->filterConditions
75+ ];
76+ }
77+ else
78+ throw WrapperException::instance ("Invalid filter definition. " , ["invalidFilter " => $ this ]);
79+
80+ }
81+
82+
83+ public static function filterQuery (Collection $ filter ): array
84+ {
85+
86+ $ queryFilter = new Collection ();
87+
88+ $ filter ->each (function (Filter $ filter ) use ($ queryFilter ) {
89+ $ queryFilter ->add ($ filter ->toArray ());
90+ });
91+
92+ return $ queryFilter ->toArray ();
93+
94+ }
2095
2196
2297}
0 commit comments