/**
  * Merge the given object's validation results the response feedback.
  *
  * @param  ModelInterface       $obj     The validated object.
  * @param  string[]|string|null $filters Filter the levels to merge.
  * @throws InvalidArgumentException If the filters are invalid.
  * @return SaveAction Chainable
  */
 public function addFeedbackFromValidation(ModelInterface $obj, $filters = null)
 {
     $validator = $obj->validator();
     $levels = [ModelValidator::ERROR, ModelValidator::WARNING, ModelValidator::NOTICE];
     if (is_string($filters) && in_array($filters, $levels)) {
         $results = call_user_func([$validator, $filters . 'Results']);
         foreach ($results as $result) {
             $this->addFeedback($result->level(), $result->message());
         }
         return $this;
     }
     if (!is_array($filters) && $filters !== null) {
         throw new InvalidArgumentException('Filters must be an array of validation levels or NULL');
     }
     $validation = $validator->results();
     foreach ($validation as $level => $results) {
         if ($filters === null || in_array($level, $filters)) {
             foreach ($results as $result) {
                 $this->addFeedback($result->level(), $result->message());
             }
         }
     }
     return $this;
 }
 /**
  * Filter the object before its assigned to the row.
  *
  * This method is useful for classes using this trait.
  *
  * @param  ModelInterface $object           The current row's object.
  * @param  array          $objectProperties The $object's display properties.
  * @return array
  */
 protected function parseObjectRow(ModelInterface $object, array $objectProperties)
 {
     return ['object' => $object, 'objectId' => $object->id(), 'objectProperties' => $objectProperties];
 }
 /**
  * Render the choice from the object.
  *
  * @param ModelInterface|ViewableInterface $obj The object or view to render as a label.
  * @return string
  */
 protected function objPattern($obj)
 {
     $pattern = (string) $this->pattern();
     if ($obj instanceof ViewableInterface && $obj->view() !== null) {
         return $obj->renderTemplate($pattern);
     } else {
         $cb = function ($matches) use($obj) {
             $method = trim($matches[1]);
             if (method_exists($obj, $method)) {
                 return call_user_func([$obj, $method]);
             } elseif (isset($obj[$method])) {
                 return $obj[$method];
             } else {
                 return '';
             }
         };
         return preg_replace_callback('~\\{\\{\\s*(.*?)\\s*\\}\\}~i', $cb, $pattern);
     }
 }
Ejemplo n.º 4
0
 /**
  * Get all the fields of a model.
  *
  * @param ModelInterface $model      The model to get fields from.
  * @param array|null     $properties Optional list of properties to get. If null, retrieve all (from metadata).
  * @return array
  * @todo Move this method in StorableTrait or AbstractModel
  */
 private function getModelFields(ModelInterface $model, $properties = null)
 {
     if ($properties === null) {
         // No custom properties; use all (from model metadata)
         $properties = array_keys($model->metadata()->properties());
     } else {
         // Ensure the key is always in the required fields.
         $properties = array_merge($properties, [$model->key()]);
     }
     $fields = [];
     foreach ($properties as $propertyIdent) {
         $p = $model->p($propertyIdent);
         if (!$p || !$p->active() || !$p->storable()) {
             continue;
         }
         $v = $model->propertyValue($propertyIdent);
         foreach ($p->fields($v) as $fieldIdent => $field) {
             $fields[$field->ident()] = $field;
         }
     }
     return $fields;
 }
Ejemplo n.º 5
0
 /**
  * Manually add an object to the list
  *
  * @param ModelInterface $obj The object to add.
  * @return \Charcoal\Collection (Chainable)
  */
 public function add(ModelInterface $obj)
 {
     $this->objects[] = $obj;
     $this->map[$obj->id()] = $obj;
     // Chainable
     return $this;
 }
 /**
  * Add an object to the cache store.
  *
  * @param ModelInterface $obj The object to store.
  * @return HierarchicalInterface Chainable
  */
 private function addObjectToCache(ModelInterface $obj)
 {
     static::$objectCache[$this->objType()][$obj->id()] = $obj;
     return $this;
 }