예제 #1
0
 public function __call($method, array $args = null)
 {
     if (!strlen($method) < 4) {
         $args = $args ? $args : [];
         $token = substr($method, 0, 3);
         if (in_array($token, ['has', 'get', 'set', 'add'])) {
             $name = substr($method, 3);
             $suffix = '';
             if (strlen($name) > 5 && substr($name, -5) === 'Array') {
                 $name = substr($name, 0, -5);
                 $suffix = 'Array';
             }
             $plural = StringUtil::pluralize($name);
             $singular = $plural !== $name;
             $table = $this->getDatabase()->{$plural};
             array_unshift($args, $table);
             switch (substr($method, 0, 3)) {
                 case 'has':
                     if ($singular) {
                         return call_user_func_array([$this, 'countOne'], $args) ? true : false;
                     }
                     return call_user_func_array([$this, 'countMany'], $args) ? true : false;
                 case 'get':
                     if ($singular) {
                         return call_user_func_array([$this, 'selectOne'], $args);
                     }
                     return call_user_func_array([$this, 'selectMany'], $args);
                 case 'set':
                     if ($singular) {
                         return call_user_func_array([$this, 'setOne'], $args);
                     }
                     return call_user_func_array([$this, 'setMany' . $suffix], $args);
                 case 'add':
                     if ($singular) {
                         return call_user_func_array([$this, 'addManyOne'], $args);
                     }
                     throw new Exception("Failed to add {$table}: No plural action available");
             }
         }
         if (strlen($method) > 5) {
             $token = substr($method, 0, 5);
             if (in_array($token, ['count'])) {
                 $name = substr($method, 5);
                 $plural = StringUtil::pluralize($name);
                 $singular = $plural !== $name;
                 $table = $this->getDatabase()->{$plural};
                 array_unshift($args, $table);
                 if ($singular) {
                     return call_user_func_array([$this, 'countOne'], $args);
                 }
                 return call_user_func_array([$this, 'countMany'], $args);
             }
         }
     }
     throw new BadMethodCallException("Failed to call method {$method}: Method doesnt exist");
 }