Exemple #1
0
 public final function __construct($uniqueIdentifier = null)
 {
     $this->modelName = SolutionSchema::getModelNameFromClass(get_class($this));
     if ($this->modelName === null) {
         // If our model hasn't been registered with the SolutionSchema - we can still use it
         // However we will assume that the model name would have been just the last part of the
         // full class name.
         $this->modelName = basename(str_replace("\\", "/", get_class($this)));
     }
     $schema = $this->getSchema();
     $this->uniqueIdentifierColumnName = $schema->uniqueIdentifierColumnName;
     if (!isset(self::$modelDataTransforms[$this->modelName])) {
         // Ensure we have our model transforms in place.
         self::$modelDataTransforms[$this->modelName] = [];
         $schema = $this->getSchema();
         $columns = $schema->getColumns();
         foreach ($columns as $column) {
             self::$modelDataTransforms[$this->modelName][$column->columnName] = [$column->getTransformFromModelData(), $column->getTransformIntoModelData()];
         }
     }
     if ($uniqueIdentifier !== null) {
         $this->defaultsSet = true;
         $repository = $this->getRepository();
         $repository->hydrateObject($this, $uniqueIdentifier);
     }
     parent::__construct();
 }
 public static function dispatchModelEvent($event, Model $model)
 {
     $event = SolutionSchema::getModelNameFromClass(get_class($model)) . ":" . $event;
     if (!isset(self::$eventHandlers[$event])) {
         return null;
     }
     $args = func_get_args();
     $args = array_slice($args, 1);
     // Check if the last argument is a callback.
     $count = count($args);
     $callBack = false;
     if ($count > 0 && is_object($args[$count - 1]) && is_callable($args[$count - 1])) {
         $callBack = $args[$count - 1];
         $args = array_slice($args, 0, -1);
     }
     $result = null;
     foreach (self::$eventHandlers[$event] as $delegate) {
         $answer = call_user_func_array($delegate, $args);
         if ($result === null) {
             $result = $answer;
         }
     }
     if ($callBack !== false) {
         call_user_func($callBack, $result);
     }
     return $result;
 }
 protected function getTitle()
 {
     if ($this->restModel !== null) {
         if ($this->restModel->isNewRecord()) {
             return "Adding a new " . strtolower(StringTools::wordifyStringByUpperCase(SolutionSchema::getModelNameFromClass(get_class($this->restModel)))) . " entry";
         } else {
             return ucfirst(strtolower(StringTools::wordifyStringByUpperCase(SolutionSchema::getModelNameFromClass(get_class($this->restModel)))) . " '" . $this->restModel->GetLabel() . "'");
         }
     } else {
         if ($this->restCollection !== null) {
             return StringTools::wordifyStringByUpperCase(StringTools::makePlural(SolutionSchema::getModelNameFromClass($this->restCollection->getModelClassName())));
         } else {
             return "Untitled";
         }
     }
 }
 public static final function getDecoratorForModel(Model $model)
 {
     $class = get_class($model);
     if (!isset(self::$decorators[$class])) {
         $decoratorClass = false;
         // Check for a concrete decorator for this exact class.
         if (isset(self::$decoratorClasses[$class])) {
             $decoratorClass = self::$decoratorClasses[$class];
         } else {
             // Perhaps the decorated was registered with an alias instead?
             $alias = SolutionSchema::getModelNameFromClass($class);
             if (isset(self::$decoratorClasses[$alias])) {
                 $decoratorClass = self::$decoratorClasses[$alias];
             } else {
                 // Descend through the parents to find the best matching decorator.
                 // This could have been much simpler by simply using the instanceof operator
                 // however there's no way to guarantee you would match the most specific decorator, i.e.
                 // the decorator targeting the sub class highest in the target model's hierarchy.
                 $currentClass = new \ReflectionClass($class);
                 do {
                     foreach (self::$decoratorClasses as $modelClassName => $decoratorClassName) {
                         if ($currentClass->getName() == $modelClassName) {
                             $decoratorClass = $decoratorClassName;
                             break 2;
                         }
                     }
                     $currentClass = $currentClass->getParentClass();
                 } while ($currentClass);
             }
         }
         if ($decoratorClass) {
             self::$decorators[$class] = new $decoratorClass($class);
         } else {
             self::$decorators[$class] = false;
         }
     }
     $decorator = self::$decorators[$class];
     if ($decorator) {
         $decorator->model = $model;
     }
     return $decorator;
 }