Example #1
0
 /**
  * Magic methods for readable method names.
  *
  * @param string $methodName Name of the method.
  * @param array $methodParams Method parameters.
  *
  * @return mixed
  * @throws DBSelectorException
  */
 public function __call($methodName, $methodParams)
 {
     /*
      * Selects DBObject record by some field value.
      *
      * @param <mixed> Value of the field
      *
      * @return DBObject
      */
     if (preg_match("#^select([[:alpha:]]+)By([[:alpha:]]+)#", $methodName, $matches)) {
         if (empty($methodParams[0])) {
             return null;
         }
         $this->validateClassName($matches[1]);
         $fieldName = substr(strtolower(preg_replace("#([A-Z]{1})#", "_\$1", $matches[2])), 1);
         $fieldValue = $methodParams[0];
         if ($fieldName == "id") {
             return $this->selectDBObjectById($fieldValue);
         }
         return $this->selectDBObjectByField($fieldName, $fieldValue);
     } elseif (preg_match("#^selectAll([A-Z]{1}[[:alpha:]]+)s#", $methodName, $matches)) {
         $this->validateClassName(preg_replace("#ie\$#", "y", $matches[1]));
         $this->order = "`" . $this->dbObject->getIdFieldName() . "` DESC";
         if (isset($methodParams[0])) {
             $this->order = (string) $methodParams[0];
         }
         $dbObjects = $this->selectDBObjects();
         $this->reset();
         return $dbObjects;
     } elseif (preg_match("#^select([[:alpha:]]+)s#", $methodName, $matches)) {
         $this->validateClassName(preg_replace("#ie\$#", "y", $matches[1]));
         return $this->selectDBObjects();
     } elseif (preg_match("#^select([[:alpha:]]+)#", $methodName, $matches)) {
         $this->validateClassName($matches[1]);
         return $this->selectDBObject();
     }
     /*
      * Try to call parent method __call() with same params by default
      */
     $method = substr($methodName, 0, 3);
     $fieldName = $this->getFieldName(substr($methodName, 3));
     switch ($method) {
         case "set":
             $fieldValue = $methodParams[0];
             return $this->setFieldValue($fieldName, $fieldValue);
         case "get":
             return $this->getFieldValue($fieldName);
         default:
             throw new DBSelectorException("No method with name '" . $methodName . "'");
     }
 }