33namespace Tempest \Database \Builder \QueryBuilders ;
44
55use Closure ;
6- use Tempest \Database \Builder \ModelInspector ;
76use Tempest \Database \Builder \WhereOperator ;
8- use Tempest \Database \QueryStatements \HasWhereStatements ;
97use Tempest \Database \QueryStatements \WhereStatement ;
10- use Tempest \Support \Str ;
118
129use function Tempest \Support \str ;
1310
1411/**
1512 * @template TModel of object
1613 * @phpstan-require-implements \Tempest\Database\Builder\QueryBuilders\BuildsQuery
14+ * @phpstan-require-implements \Tempest\Database\Builder\QueryBuilders\SupportsWhereConditions
1715 * @use \Tempest\Database\Builder\QueryBuilders\HasConvenientWhereMethods<TModel>
1816 */
1917trait HasWhereQueryBuilderMethods
2018{
2119 use HasConvenientWhereMethods;
2220
23- abstract private function getModel (): ModelInspector ;
24-
25- abstract private function getStatementForWhere (): HasWhereStatements ;
26-
2721 /**
2822 * Adds a SQL `WHERE` condition to the query. If the `$statement` looks like raw SQL, the method will assume it is and call `whereRaw`. Otherwise, `whereField` will be called.
2923 *
@@ -51,14 +45,14 @@ public function where(string $statement, mixed ...$bindings): self
5145 public function whereField (string $ field , mixed $ value , string |WhereOperator $ operator = WhereOperator::EQUALS ): self
5246 {
5347 $ operator = WhereOperator::fromOperator ($ operator );
54- $ fieldDefinition = $ this ->getModel () ->getFieldDefinition ($ field );
48+ $ fieldDefinition = $ this ->model ->getFieldDefinition ($ field );
5549 $ condition = $ this ->buildCondition ((string ) $ fieldDefinition , $ operator , $ value );
5650
57- if ($ this ->getStatementForWhere ()-> where ->isNotEmpty ()) {
51+ if ($ this ->wheres ->isNotEmpty ()) {
5852 return $ this ->andWhere ($ field , $ value , $ operator );
5953 }
6054
61- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ($ condition ['sql ' ]);
55+ $ this ->wheres [] = new WhereStatement ($ condition ['sql ' ]);
6256 $ this ->bind (...$ condition ['bindings ' ]);
6357
6458 return $ this ;
@@ -72,10 +66,10 @@ public function whereField(string $field, mixed $value, string|WhereOperator $op
7266 public function andWhere (string $ field , mixed $ value , WhereOperator $ operator = WhereOperator::EQUALS ): self
7367 {
7468 $ operator = WhereOperator::fromOperator ($ operator );
75- $ fieldDefinition = $ this ->getModel () ->getFieldDefinition ($ field );
69+ $ fieldDefinition = $ this ->model ->getFieldDefinition ($ field );
7670 $ condition = $ this ->buildCondition ((string ) $ fieldDefinition , $ operator , $ value );
7771
78- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ("AND {$ condition ['sql ' ]}" );
72+ $ this ->wheres [] = new WhereStatement ("AND {$ condition ['sql ' ]}" );
7973 $ this ->bind (...$ condition ['bindings ' ]);
8074
8175 return $ this ;
@@ -89,10 +83,10 @@ public function andWhere(string $field, mixed $value, WhereOperator $operator =
8983 public function orWhere (string $ field , mixed $ value , WhereOperator $ operator = WhereOperator::EQUALS ): self
9084 {
9185 $ operator = WhereOperator::fromOperator ($ operator );
92- $ fieldDefinition = $ this ->getModel () ->getFieldDefinition ($ field );
86+ $ fieldDefinition = $ this ->model ->getFieldDefinition ($ field );
9387 $ condition = $ this ->buildCondition ((string ) $ fieldDefinition , $ operator , $ value );
9488
95- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ("OR {$ condition ['sql ' ]}" );
89+ $ this ->wheres [] = new WhereStatement ("OR {$ condition ['sql ' ]}" );
9690 $ this ->bind (...$ condition ['bindings ' ]);
9791
9892 return $ this ;
@@ -105,11 +99,11 @@ public function orWhere(string $field, mixed $value, WhereOperator $operator = W
10599 */
106100 public function whereRaw (string $ statement , mixed ...$ bindings ): self
107101 {
108- if ($ this ->getStatementForWhere ()-> where ->isNotEmpty () && ! str ($ statement )->trim ()->startsWith (['AND ' , 'OR ' ])) {
102+ if ($ this ->wheres ->isNotEmpty () && ! str ($ statement )->trim ()->startsWith (['AND ' , 'OR ' ])) {
109103 return $ this ->andWhereRaw ($ statement , ...$ bindings );
110104 }
111105
112- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ($ statement );
106+ $ this ->wheres [] = new WhereStatement ($ statement );
113107 $ this ->bind (...$ bindings );
114108
115109 return $ this ;
@@ -122,7 +116,7 @@ public function whereRaw(string $statement, mixed ...$bindings): self
122116 */
123117 public function andWhereRaw (string $ rawCondition , mixed ...$ bindings ): self
124118 {
125- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ("AND {$ rawCondition }" );
119+ $ this ->wheres [] = new WhereStatement ("AND {$ rawCondition }" );
126120 $ this ->bind (...$ bindings );
127121
128122 return $ this ;
@@ -135,7 +129,7 @@ public function andWhereRaw(string $rawCondition, mixed ...$bindings): self
135129 */
136130 public function orWhereRaw (string $ rawCondition , mixed ...$ bindings ): self
137131 {
138- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ("OR {$ rawCondition }" );
132+ $ this ->wheres [] = new WhereStatement ("OR {$ rawCondition }" );
139133 $ this ->bind (...$ bindings );
140134
141135 return $ this ;
@@ -149,12 +143,12 @@ public function orWhereRaw(string $rawCondition, mixed ...$bindings): self
149143 */
150144 public function whereGroup (Closure $ callback ): self
151145 {
152- $ groupBuilder = new WhereGroupBuilder ($ this ->getModel () );
146+ $ groupBuilder = new WhereGroupBuilder ($ this ->model );
153147 $ callback ($ groupBuilder );
154148 $ group = $ groupBuilder ->build ();
155149
156150 if (! $ group ->conditions ->isEmpty ()) {
157- $ this ->getStatementForWhere ()-> where [] = $ group ;
151+ $ this ->wheres [] = $ group ;
158152 $ this ->bind (...$ groupBuilder ->getBindings ());
159153 }
160154
@@ -169,8 +163,8 @@ public function whereGroup(Closure $callback): self
169163 */
170164 public function andWhereGroup (Closure $ callback ): self
171165 {
172- if ($ this ->getStatementForWhere ()-> where ->isNotEmpty ()) {
173- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ('AND ' );
166+ if ($ this ->wheres ->isNotEmpty ()) {
167+ $ this ->wheres [] = new WhereStatement ('AND ' );
174168 }
175169
176170 return $ this ->whereGroup ($ callback );
@@ -184,8 +178,8 @@ public function andWhereGroup(Closure $callback): self
184178 */
185179 public function orWhereGroup (Closure $ callback ): self
186180 {
187- if ($ this ->getStatementForWhere ()-> where ->isNotEmpty ()) {
188- $ this ->getStatementForWhere ()-> where [] = new WhereStatement ('OR ' );
181+ if ($ this ->wheres ->isNotEmpty ()) {
182+ $ this ->wheres [] = new WhereStatement ('OR ' );
189183 }
190184
191185 return $ this ->whereGroup ($ callback );
0 commit comments