Webiny\Component\Entity\Attribute\Validation\ValidationException::setAttribute PHP Method

setAttribute() public method

public setAttribute ( null $attribute )
$attribute null
    public function setAttribute($attribute)
    {
        $this->attribute = $attribute;
    }

Usage Example

 /**
  * Normalize given value to be a valid array of entity instances
  *
  * @param mixed $value
  *
  * @return array
  * @throws ValidationException
  */
 protected function normalizeValue($value)
 {
     if (is_null($value)) {
         return $value;
     }
     $entityClass = $this->getEntity();
     $entityCollectionClass = '\\Webiny\\Component\\Entity\\EntityCollection';
     // Validate Many2many attribute value
     if (!$this->isArray($value) && !$this->isArrayObject($value) && !$this->isInstanceOf($value, $entityCollectionClass)) {
         $exception = new ValidationException(ValidationException::DATA_TYPE, ['array, ArrayObject or EntityCollection', gettype($value)]);
         $exception->setAttribute($this->getName());
         throw $exception;
     }
     /* @var $entityAttribute One2ManyAttribute */
     $values = [];
     foreach ($value as $item) {
         $itemEntity = false;
         // $item can be an array of data, AbstractEntity or a simple mongo ID string
         if ($this->isInstanceOf($item, $entityClass)) {
             $itemEntity = $item;
         } elseif ($this->isArray($item) || $this->isArrayObject($item)) {
             $itemEntity = $entityClass::findById(isset($item['id']) ? $item['id'] : false);
         } elseif ($this->isString($item) && Entity::getInstance()->getDatabase()->isId($item)) {
             $itemEntity = $entityClass::findById($item);
         }
         // If instance was not found, create a new entity instance
         if (!$itemEntity) {
             $itemEntity = new $entityClass();
         }
         // If $item is an array - use it to populate the entity instance
         if ($this->isArray($item) || $this->isArrayObject($item)) {
             $itemEntity->populate($item);
         }
         $values[] = $itemEntity;
     }
     return new EntityCollection($this->getEntity(), $values);
 }