Psecio\Gatekeeper\Gatekeeper::buildModel PHP Method

buildModel() public static method

Build the model instance with data given
public static buildModel ( string $action = 'find', string $name, array $args ) : object
$action string Action called (ex: "delete" or "create")
$name string Function nname
$args array Arguments set
return object Model instance
    public static function buildModel($action = 'find', $name, array $args)
    {
        $name = str_replace($action, '', $name);
        preg_match('/By(.+)/', $name, $matches);
        if (empty($matches) && $args[0] instanceof \Modler\Model) {
            $model = $name;
            $data = $args[0]->toArray();
        } else {
            $property = lcfirst($matches[1]);
            $model = str_replace($matches[0], '', $name);
            $data = array($property => $args[0]);
        }
        $modelNs = '\\Psecio\\Gatekeeper\\' . $model . 'Model';
        if (!class_exists($modelNs)) {
            throw new Exception\ModelNotFoundException('Model type ' . $model . ' could not be found');
        }
        $instance = new $modelNs(self::$datasource);
        $instance = self::$datasource->find($instance, $data);
        return $instance;
    }

Usage Example

Example #1
0
 /**
  * Execute the deletion handling
  *
  * @return boolean Success/failure of delete
  */
 public function execute()
 {
     $args = $this->getArguments();
     $name = $this->getName();
     $model = g::buildModel('delete', $name, $args);
     return $this->getDb()->delete($model);
 }