@@ -237,38 +237,40 @@ final public function withPrimaryKey(string $column): self
237237 }
238238
239239 /**
240- * @param array $data
240+ * @param array $set
241241 * @return array|false
242242 */
243- final public function create (array $ data )
243+ final public function create (array $ set )
244244 {
245- return $ this ->insert ($ data );
245+ return $ this ->insert ($ set );
246246 }
247247
248248 /**
249- * @param array $data
249+ * @param array $set
250250 * @return array|false
251251 */
252- final public function insert (array $ data )
252+ final public function insert (array $ set )
253253 {
254254 if ($ this ->isWritable () === FALSE ){
255255 throw new WritableException ('" ' . \get_called_class () . '" is not a writable model. ' );
256256 }
257- if (($ data = $ this ->callbacksFunctionHandler ($ data , 'beforeInsert ' )) === FALSE ){
257+
258+ $ data = $ this ->isCallbacksFunction ('beforeInsert ' , 'afterInsert ' ) ? $ this ->callbacksFunctionHandler ($ set , 'beforeInsert ' ) : $ set ;
259+
260+ if ($ data === FALSE ){
258261 return false ;
259262 }
260263
261- $ create = parent ::create ($ data );
262-
263- if ($ create === FALSE ){
264+ if (parent ::create ($ data ) === FALSE ){
264265 return false ;
265266 }
266- return $ data = $ this ->callbacksFunctionHandler ($ data , 'afterInsert ' );
267+
268+ return $ this ->isCallbacksFunction ('afterInsert ' ) ? $ this ->callbacksFunctionHandler ($ data , 'afterInsert ' ) : true ;
267269 }
268270
269271 /**
270272 * @param Entity $entity
271- * @return array|false
273+ * @return array|bool
272274 */
273275 final public function save (Entity $ entity )
274276 {
@@ -310,20 +312,22 @@ final public function readOne(array $selector = [], array $conditions = [], arra
310312
311313 /**
312314 * @param array $set
313- * @return array|false
315+ * @return array|bool
314316 */
315317 final public function update (array $ set )
316318 {
317319 if ($ this ->isUpdatable () === FALSE ){
318320 throw new UpdatableException ('" ' . \get_called_class () . '" is not a updatable model. ' );
319321 }
320- if (($ data = $ this ->callbacksFunctionHandler ($ set , 'beforeUpdate ' )) === FALSE ){
322+ $ data = $ this ->isCallbacksFunction ('beforeUpdate ' , 'afterUpdate ' ) ? $ this ->callbacksFunctionHandler ($ set , 'beforeUpdate ' ) : $ set ;
323+ if ($ data === FALSE ){
321324 return false ;
322325 }
323326 if (parent ::update ($ data ) === FALSE ){
324327 return false ;
325328 }
326- return $ data = $ this ->callbacksFunctionHandler ($ data , 'afterUpdate ' );
329+
330+ return $ this ->isCallbacksFunction ('afterUpdate ' ) ? $ this ->callbacksFunctionHandler ($ data , 'afterUpdate ' ) : true ;
327331 }
328332
329333 /**
@@ -337,21 +341,25 @@ final public function updateBatch(array $set, string $referenceColumn)
337341 throw new UpdatableException ('" ' . \get_called_class () . '" is not a updatable model. ' );
338342 }
339343
340- foreach ($ set as &$ data ) {
341- $ data = $ this ->callbacksFunctionHandler ($ data , 'beforeUpdate ' );
342- if ($ data === FALSE ){
343- return false ;
344+ if ($ this ->isCallbacksFunction ('beforeUpdate ' , 'afterUpdate ' )){
345+ foreach ($ set as &$ data ) {
346+ $ data = $ this ->callbacksFunctionHandler ($ data , 'beforeUpdate ' );
347+ if ($ data === FALSE ){
348+ return false ;
349+ }
344350 }
345351 }
346352
347353 if (parent ::updateBatch ($ set , $ referenceColumn ) === FALSE ){
348354 return false ;
349355 }
350356
351- foreach ($ set as &$ row ) {
352- $ row = $ this ->callbacksFunctionHandler ($ row , 'afterUpdate ' );
353- if ($ row === FALSE ){
354- return false ;
357+ if ($ this ->isCallbacksFunction ('afterUpdate ' )){
358+ foreach ($ set as &$ row ) {
359+ $ row = $ this ->callbacksFunctionHandler ($ row , 'afterUpdate ' );
360+ if ($ row === FALSE ){
361+ return false ;
362+ }
355363 }
356364 }
357365
@@ -370,26 +378,29 @@ final public function delete($id = null)
370378 if ($ id !== null && !empty ($ this ->getSchemaID ())){
371379 $ this ->where ($ this ->getSchemaID (), $ id );
372380 }
373- $ clone = clone $ this ;
374- $ parameters = Parameters::get ();
375- $ res = $ clone ->query ($ clone ->_readQuery (), $ parameters );
376- $ data = $ res ->asAssoc ()->results ();
377- Parameters::merge ($ parameters );
378- unset($ clone , $ parameters );
379-
380- if ($ data === null ){
381- return true ;
382- }
383- $ data = $ this ->callbacksFunctionHandler ($ data , 'beforeDelete ' );
384- if ($ data === FALSE ){
385- return false ;
381+
382+ if ($ this ->isCallbacksFunction ('beforeDelete ' , 'afterDelete ' )) {
383+ $ clone = clone $ this ;
384+ $ parameters = Parameters::get ();
385+ $ res = $ clone ->query ($ clone ->_readQuery (), $ parameters );
386+ $ data = $ res ->asAssoc ()->results ();
387+ Parameters::merge ($ parameters );
388+ unset($ clone , $ parameters );
389+
390+ if ($ data === null ){
391+ return true ;
392+ }
393+ $ data = $ this ->callbacksFunctionHandler ($ data , 'beforeDelete ' );
394+ if ($ data === FALSE ){
395+ return false ;
396+ }
386397 }
387398
388399 if (parent ::delete () === FALSE ){
389400 return false ;
390401 }
391402
392- return $ data = $ this ->callbacksFunctionHandler ($ data , 'afterDelete ' );
403+ return $ this -> isCallbacksFunction ( ' afterDelete ' ) ? $ this ->callbacksFunctionHandler ($ data , 'afterDelete ' ) : true ;
393404 }
394405
395406 final public function purgeDeleted (): bool
@@ -489,20 +500,35 @@ final public function isDeletable(): bool
489500 return $ this ->deletable ?? true ;
490501 }
491502
503+ /**
504+ * @param string ...$methods
505+ * @return bool
506+ */
507+ private function isCallbacksFunction (string ...$ methods ): bool
508+ {
509+ if (($ this ->allowedCallbacks ?? false ) === FALSE ){
510+ return false ;
511+ }
512+ foreach ($ methods as $ method ) {
513+ $ callbacks = $ this ->{$ method } ?? null ;
514+ if (!empty ($ callbacks )){
515+ return true ;
516+ }
517+ }
518+ return false ;
519+ }
520+
492521 /**
493522 * @param array $data
494523 * @param string $method
495524 * @return array|false
496525 */
497526 private function callbacksFunctionHandler (array $ data , string $ method )
498527 {
499- if (( $ this ->allowedCallbacks ?? false ) === FALSE ){
528+ if (! $ this ->isCallbacksFunction ( $ method ) ){
500529 return $ data ;
501530 }
502531 $ callbacks = $ this ->{$ method };
503- if (!\is_array ($ callbacks )){
504- return $ data ;
505- }
506532
507533 foreach ($ callbacks as $ callback ) {
508534 if (\is_string ($ callback )){
0 commit comments