1515
1616namespace InitPHP \Database ;
1717
18- use InitPHP \Database \Exceptions \ DatabaseInvalidArgumentException ;
18+ use InitPHP \Database \Validation \ Validation ;
1919use \PDO ;
2020use \InitPHP \Database \Connection \{Connection , ConnectionInterface };
2121use \InitPHP \Database \DataMapper \{DataMapper , DataMapperInterface };
@@ -57,10 +57,19 @@ class DB
5757 'updatedField ' => null ,
5858 'deletedField ' => null ,
5959 'timestampFormat ' => 'c ' ,
60+ 'validation ' => [
61+ 'methods ' => [],
62+ 'messages ' => [],
63+ 'labels ' => [],
64+ ],
6065 ];
6166
6267 private bool $ isOnlyDeletes = false ;
6368
69+ private Validation $ _validation ;
70+
71+ private array $ errors = [];
72+
6473 public function __construct (array $ configurations )
6574 {
6675 $ this ->configurations = \array_merge ($ this ->configurations , $ configurations );
@@ -85,6 +94,7 @@ public function __construct(array $configurations)
8594 'schema ' => ($ this ->configurations ['tableSchema ' ] ?? null ),
8695 'schemaID ' => ($ this ->configurations ['tableSchemaID ' ] ?? null ),
8796 ]);
97+ $ this ->_validation = new Validation ($ this ->configurations ['validation ' ]['methods ' ], $ this ->configurations ['validation ' ]['messages ' ], $ this ->configurations ['validation ' ]['labels ' ], $ this );
8898 }
8999
90100 public function __call ($ name , $ arguments )
@@ -153,6 +163,18 @@ public function getSchemaID()
153163 return $ this ->configurations ['tableSchemaID ' ];
154164 }
155165
166+ public function isError (): bool
167+ {
168+ $ this ->get_error_merge ();
169+ return !empty ($ this ->errors );
170+ }
171+
172+ public function getError (): array
173+ {
174+ $ this ->get_error_merge ();
175+ return $ this ->errors ;
176+ }
177+
156178 /**
157179 * @return ConnectionInterface
158180 */
@@ -174,34 +196,55 @@ public function getDataMapper(): DataMapperInterface
174196 */
175197 public function getQueryBuilder (): QueryBuilderInterface
176198 {
177- $ this ->is_get_execute = false ;
178199 return $ this ->_queryBuilder ;
179200 }
180201
202+ public function getValidation (): Validation
203+ {
204+ return $ this ->_validation ;
205+ }
206+
181207 /**
182208 * @param array $fields
183209 * @return bool
210+ * @throws Exceptions\ValidationException
184211 */
185212 public function create (array $ fields )
186213 {
187214 $ data = []; $ parameters = [];
188215 $ isCreatedField = !empty ($ this ->configurations ['createdField ' ]);
189216 if (\count ($ fields ) === \count ($ fields , \COUNT_RECURSIVE )){
217+ $ this ->getValidation ()->setData ($ fields );
190218 foreach ($ fields as $ column => $ value ) {
219+ if ($ this ->getValidation ()->validation ($ column , null ) === FALSE ){
220+ $ this ->errors [] = $ this ->getValidation ()->getError ();
221+ return false ;
222+ }
191223 $ data [$ column ] = ': ' .$ column ;
192224 $ parameters [': ' . $ column ] = $ value ;
193225 }
226+ if (empty ($ data )){
227+ return false ;
228+ }
194229 if ($ isCreatedField ){
195230 $ data [$ this ->configurations ['createdField ' ]] = ': ' . $ this ->configurations ['createdField ' ];
196231 }
197232 }else {
198233 $ i = 0 ;
199234 foreach ($ fields as $ row ) {
200235 $ data [$ i ] = [];
236+ $ this ->getValidation ()->setData ($ row );
201237 foreach ($ row as $ column => $ value ) {
238+ if ($ this ->getValidation ()->validation ($ column , null ) === FALSE ){
239+ $ this ->errors [] = $ this ->getValidation ()->getError ();
240+ return false ;
241+ }
202242 $ data [$ i ][$ column ] = ': ' . $ column . '_ ' . $ i ;
203243 $ parameters [': ' . $ column . '_ ' . $ i ] = $ value ;
204244 }
245+ if (empty ($ data )){
246+ continue ;
247+ }
205248 if ($ isCreatedField ){
206249 $ data [$ i ][$ this ->configurations ['createdField ' ]] = ': ' . $ this ->configurations ['createdField ' ];
207250 }
@@ -250,16 +293,23 @@ public function readOne(array $selector = [], array $conditions = [], array $par
250293 */
251294 public function update (array $ fields )
252295 {
296+ $ schemaID = null ;
253297 if (!empty ($ this ->getSchemaID ()) && isset ($ fields [$ this ->getSchemaID ()])){
298+ $ schemaID = $ fields [$ this ->getSchemaID ()];
254299 $ this ->getQueryBuilder ()->where ($ this ->getSchemaID (), ': ' . $ this ->getSchemaID ());
255- $ this ->getDataMapper ()->setParameter (': ' . $ this ->getSchemaID (), $ fields [ $ this -> getSchemaID ()] );
300+ $ this ->getDataMapper ()->setParameter (': ' . $ this ->getSchemaID (), $ schemaID );
256301 unset($ fields [$ this ->getSchemaID ()]);
257302 }
258303 if (empty ($ fields )){
259304 return false ;
260305 }
306+ $ this ->getValidation ()->setData ($ fields );
261307 $ data = [];
262308 foreach ($ fields as $ column => $ value ) {
309+ if ($ this ->getValidation ()->validation ($ column , $ schemaID ) === FALSE ){
310+ $ this ->errors [] = $ this ->getValidation ()->getError ();
311+ return false ;
312+ }
263313 $ data [$ column ] = ': ' . $ column ;
264314 $ this ->getDataMapper ()->setParameter (': ' . $ column , $ value );
265315 }
@@ -430,4 +480,12 @@ private function deletedFieldBuild(bool $reset = true): void
430480 }
431481 }
432482
483+ private function get_error_merge (): void
484+ {
485+ $ error = $ this ->getDataMapper ()->lastError ();
486+ if (!empty ($ error ) && !\in_array ($ error , $ this ->errors )){
487+ $ this ->errors [] = $ error ;
488+ }
489+ }
490+
433491}
0 commit comments