leonam-carlos

Extended TRecord __callStatic method

Jul 18th, 2023
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.74 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Base class for Active Records
  5.  *
  6.  * @version    7.5
  7.  * @package    database
  8.  * @author     Pablo Dall'Oglio
  9.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  10.  * @license    http://www.adianti.com.br/framework-license
  11.  */
  12. class TRecordExpanded extends TRecord
  13. {
  14.     /**
  15.      * Executed whenever an unknown method is executed
  16.      * @param $method Method name
  17.      * @param $parameter Method parameters
  18.      */
  19.     public static function __callStatic($method, $parameters)
  20.     {
  21.         $class_name = get_called_class();
  22.         if (substr($method,-13) == 'InTransaction')
  23.         {
  24.             $method = substr($method,0,-13);
  25.             if (method_exists($class_name, $method))
  26.             {                
  27.                 // Interact wiht WHERE
  28.                 if ($method == 'where' && is_array($parameters[1])) {
  29.  
  30.                     $database = array_shift($parameters);
  31.                     $where_field          = $parameters[0][0];
  32.                     $where_operator       = $parameters[0][1];
  33.                     $where_value          = $parameters[0][2];
  34.                     $where_return         = $parameters[0][3] ?? 0;
  35.                     $where_field_return   = $parameters[0][4] ?? 0;
  36.                     TTransaction::open($database);
  37.                     if ($where_return) {
  38.  
  39.                         switch ($where_return) {
  40.                             case 'load':
  41.                                 $content = $class_name::where( $where_field, $where_operator, $where_value )->load();
  42.                                 break;
  43.  
  44.                             case 'count':
  45.                                 $content = $class_name::where( $where_field, $where_operator, $where_value )->count();
  46.                                 break;
  47.  
  48.                             case 'first':
  49.                                 $content = $class_name::where( $where_field, $where_operator, $where_value )->first();
  50.                                 if ($content && $where_field_return) {
  51.                                     $content = $content->$where_field_return;
  52.                                 }
  53.                                 break;
  54.  
  55.                             default:
  56.                                 $content = $class_name::where( $where_field, $where_operator, $where_value )->$where_return;
  57.                                 break;
  58.                         }
  59.                     } else {
  60.  
  61.                         $content = $class_name::where( $where_field, $where_operator, $where_value )->load();
  62.                     }
  63.                     TTransaction::close();
  64.  
  65.                     return $content;
  66.  
  67.                 } else {
  68.                
  69.                     // Interact with FIND
  70.                     $database = array_shift($parameters);
  71.                     TTransaction::open($database);
  72.                     $content = forward_static_call_array( array($class_name, $method), $parameters);
  73.                     TTransaction::close();
  74.                     return $content;
  75.                
  76.                 }
  77.             }
  78.             else
  79.             {
  80.                 throw new Exception(AdiantiCoreTranslator::translate('Method ^1 not found', $class_name.'::'.$method.'()'));
  81.             }
  82.         }
  83.         else if (method_exists('TRepository', $method))
  84.         {
  85.             $class = get_called_class(); // get the Active Record class name
  86.             $repository = new TRepository( $class ); // create the repository
  87.             return call_user_func_array( array($repository, $method), $parameters );
  88.         }
  89.         else
  90.         {
  91.             throw new Exception(AdiantiCoreTranslator::translate('Method ^1 not found', $class_name.'::'.$method.'()'));
  92.         }
  93.     }
  94.    
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment