66use Illuminate \Database \Eloquent \Builder ;
77use Illuminate \Contracts \Support \Jsonable ;
88use Illuminate \Contracts \Support \Arrayable ;
9- use Kettasoft \Filterable \Foundation \Resources ;
10- use Kettasoft \Filterable \Engines \Foundation \Mapper ;
11- use Kettasoft \Filterable \Support \ValidateTableColumns ;
129use Kettasoft \Filterable \Engines \Foundation \Parsers \Dissector ;
13- use Kettasoft \Filterable \Engines \Foundation \Mappers \OperatorMapper ;
1410use Kettasoft \Filterable \Engines \Foundation \Resolvers \RelationResolver ;
1511
1612class Clause implements Arrayable, Jsonable
1713{
18- /**
19- * Current registered engine.
20- * @var Resources
21- */
22- public Resources $ resources ;
2314
2415 /**
2516 * Original field.
@@ -40,43 +31,33 @@ class Clause implements Arrayable, Jsonable
4031 public readonly string |null $ value ;
4132
4233 /**
43- * Dissector instance.
44- * @var Dissector
45- */
46- public readonly Dissector $ dissector ;
47-
48- /**
49- * Strict mode.
34+ * Check if the clause is validated.
5035 * @var bool
5136 */
52- protected bool $ strict = false ;
37+ public bool $ validated = false ;
5338
5439 /**
5540 * Clause constructor.
5641 * @param \Kettasoft\Filterable\Foundation\Resources $resources
5742 * @param mixed $field
5843 * @param mixed $dissector
5944 */
60- public function __construct (Resources $ resources , $ field , $ dissector )
45+ public function __construct ($ field , $ operator , $ value )
6146 {
62- $ this ->resources = $ resources ;
6347 $ this ->field = $ field ;
64- $ this ->dissector = Dissector::parse ($ dissector , 'eq ' );
65-
66- $ this ->operator = $ this ->dissector ->operator ;
67- $ this ->value = $ this ->dissector ->value ;
48+ $ this ->operator = $ operator ;
49+ $ this ->value = $ value ;
6850 }
6951
7052 /**
7153 * Create Dissector instance.
72- * @param \Kettasoft\Filterable\Foundation\Resources $resources
7354 * @param mixed $field
7455 * @param mixed $dissector
7556 * @return Clause
7657 */
77- public static function make (Resources $ resources , $ field , $ dissector )
58+ public static function make ($ field , $ operator , $ value )
7859 {
79- return new self ($ resources , $ field , $ dissector );
60+ return new self ($ field , $ operator , $ value );
8061 }
8162
8263 /**
@@ -87,9 +68,10 @@ public function isRelational(): bool
8768 return is_string ($ this ->field ) && str_contains ($ this ->field , '. ' );
8869 }
8970
90- public function isEmptyValue (): bool
71+ public function setStatus ( bool $ status )
9172 {
92- return empty ($ this ->value ) || is_null ($ this ->value );
73+ $ this ->validated = $ status ;
74+ return $ this ;
9375 }
9476
9577 /**
@@ -105,73 +87,22 @@ public function getOriginalField()
10587 */
10688 public function getValue ()
10789 {
108- $ sanitizers = $ this ->resources ->sanitizers ;
109-
110- $ value = $ this ->getOriginalValue ();
111-
112- if (count ($ sanitizers )) {
113- foreach ($ sanitizers as $ sanitizer ) {
114- $ value = $ sanitizer ->handle ($ this ->getOriginalField (), $ value );
115- }
116- }
117-
118- return $ value ;
90+ return $ this ->value ;
11991 }
12092
121- /**
122- * @inheritDoc
123- */
124- public function getOriginalValue ()
93+ public function relation ($ bag )
12594 {
126- return $ this ->dissector ->value ;
95+ $ instance = new RelationResolver ($ bag , $ this ->field );
96+ return $ instance ;
12797 }
12898
129- /**
130- * @inheritDoc
131- */
132- public function getOperator ()
99+ public function apply (Builder $ builder )
133100 {
134- $ instance = $ this ->resources ->operators ;
135-
136- return Mapper::run (
137- OperatorMapper::init ($ instance , $ this ->strict ),
138- $ this ->operator
101+ return $ builder ->where (
102+ $ this ->field ,
103+ $ this ->operator ,
104+ $ this ->value
139105 );
140- // return $instance->get($this->operator, $instance->get($instance->default));
141- }
142-
143- /**
144- * @inheritDoc
145- */
146- public function getDatabaseColumnName ()
147- {
148- return $ this ->resources ->fieldMap ->get ($ this ->field , $ this ->field );
149- }
150-
151- /**
152- * Check if the field is allowed.
153- * @return bool
154- */
155- public function isAllowedField (): bool
156- {
157- return $ this ->resources ->fields ->has ($ this ->field );
158- }
159-
160- public function validateTableColumn ()
161- {
162- // return ValidateTableColumns::validate($this->engine->getContext()->getBuilder(), $this->getDatabaseColumnName());
163- }
164-
165- public function strict ($ enable = true ): self
166- {
167- $ this ->strict = $ enable ;
168- return $ this ;
169- }
170-
171- public function relation ($ bag )
172- {
173- $ instance = new RelationResolver ($ bag , $ this ->field );
174- return $ instance ;
175106 }
176107
177108 /**
@@ -180,9 +111,9 @@ public function relation($bag)
180111 public function toArray (): array
181112 {
182113 return [
183- 'field ' => $ this ->getDatabaseColumnName () ,
184- 'operator ' => $ this ->getOperator () ,
185- 'value ' => $ this ->getValue ()
114+ 'field ' => $ this ->field ,
115+ 'operator ' => $ this ->operator ,
116+ 'value ' => $ this ->value
186117 ];
187118 }
188119
0 commit comments