Inheritance: implements JsonSerializabl\JsonSerializable, use trait Webiny\Component\StdLib\StdLibTrait, use trait Webiny\Component\StdLib\FactoryLoaderTrait
Exemplo n.º 1
0
 /**
  * @inheritDoc
  */
 public function validate(AbstractAttribute $attribute, $data, $params = [])
 {
     if (empty($data)) {
         return;
     }
     $query = [$attribute->attr() => $data];
     $id = $attribute->getParent()->id;
     if ($id) {
         $query['id'] = ['$ne' => $id];
     }
     $exists = call_user_func_array([$attribute->getParent(), 'findOne'], [$query]);
     if ($exists) {
         throw new ValidationException('A record with this attribute value already exists.');
     }
 }
Exemplo n.º 2
0
 public function getValue($params = [], $asDateTimeObject = false)
 {
     if ($asDateTimeObject) {
         return $this->processGetValue(new DateTimeObject(parent::getValue()), $params);
     }
     return $this->processGetValue(parent::getValue(), $params);
 }
Exemplo n.º 3
0
 public function getValue($params = [], $asDateTimeObject = false, $processCallbacks = true)
 {
     $value = parent::getValue([], false);
     if ($asDateTimeObject) {
         $value = new DateTimeObject($value);
     }
     return $this->processGetValue($value, $params, $processCallbacks);
 }
Exemplo n.º 4
0
 /**
  * @inheritDoc
  */
 public function getDbValue()
 {
     $value = $this->getValue();
     if ($value instanceof AbstractEntity) {
         return $this->processToDbValue($value->id);
     }
     return parent::getDbValue();
 }
Exemplo n.º 5
0
 /**
  * Perform validation against given value
  *
  * @param $value
  *
  * @throws ValidationException
  * @return $this
  */
 protected function validate(&$value)
 {
     if ($this->str($value)->contains(',')) {
         $value = $this->str($value)->replace(',', '.')->val();
     }
     $value = floatval($value);
     if (!$this->isNumber($value)) {
         $this->expected('number', gettype($value));
     }
     parent::validate($value);
     return $this;
 }
Exemplo n.º 6
0
 /**
  * Perform validation against given value
  *
  * @param $value
  *
  * @throws ValidationException
  * @return $this
  */
 protected function validate(&$value)
 {
     if ($value instanceof AbstractEntity) {
         $value = $value->id;
     }
     if ($value != null && !$this->isString($value) && !$this->isNumber($value)) {
         $this->expected('string, number or AbstractEntity', gettype($value));
     }
     // Make sure it's a string even if it is a number (convert to numeric string)
     $value = '' . $value;
     parent::validate($value);
     return $this;
 }
Exemplo n.º 7
0
 /**
  * Perform validation against given value
  *
  * @param $value
  *
  * @throws ValidationException
  * @return $this
  */
 protected function validate(&$value)
 {
     if ($this->isString($value) && $this->isNumber($value)) {
         if (!$this->str($value)->contains('.') && !$this->str($value)->contains(',')) {
             $value = intval($value);
         }
     }
     if (!$this->isInteger($value)) {
         $this->expected('integer', gettype($value));
     }
     parent::validate($value);
     return $this;
 }
Exemplo n.º 8
0
 /**
  * Set attribute instance
  *
  * @param AbstractAttribute $attribute
  *
  * @return AbstractAttribute
  */
 public function smart(AbstractAttribute $attribute)
 {
     $attribute->setName($this->attribute)->setParent($this->entity);
     return $this->attributes[$this->attribute] = $attribute;
 }
Exemplo n.º 9
0
 /**
  * Perform validation against given value
  *
  * @param $value
  *
  * @return $this
  * @throws ValidationException
  */
 protected function validate(&$value)
 {
     if ($this->isNull($value)) {
         return $this;
     }
     if (!$this->isArray($value) && !$this->isArrayObject($value)) {
         $this->expected('array or ArrayObject', gettype($value));
     }
     // Call parent method
     parent::validate($value);
     // Validate array keys
     $this->validateNestedKeys($value);
     return $this;
 }
Exemplo n.º 10
0
 public function __construct($name = null, AbstractEntity $parent = null)
 {
     parent::__construct($name, $parent);
     $this->defaultValue = ['lat' => 0, 'lng' => 0];
 }
Exemplo n.º 11
0
 /**
  * @param AbstractAttribute   $attribute
  * @param ValidationException $e
  *
  * @return AttributeValidationException
  */
 public function attributeValidationException(AbstractAttribute $attribute, ValidationException $e)
 {
     $attrException = new AttributeValidationException($e->getMessage());
     $attrException->addError($attribute->getName(), $e->getMessage());
     return $attrException;
 }
Exemplo n.º 12
0
 private function populateAttribute($attributeName, AbstractAttribute $entityAttribute, $validation, $data, $fromDb)
 {
     // Skip population of protected attributes if data is not coming from DB
     if (!$fromDb && $entityAttribute->getSkipOnPopulate()) {
         return;
     }
     // Dynamic attributes from database should be populated without any checks, and skipped otherwise
     if ($this->isInstanceOf($entityAttribute, AttributeType::DYNAMIC) && isset($data[$attributeName])) {
         $entityAttribute->setValue($data[$attributeName], $fromDb);
         return;
     }
     /**
      * Check if attribute is required and it's value is set or maybe value was already assigned
      */
     if (!$fromDb && $entityAttribute->isRequired() && !isset($data[$attributeName]) && !$entityAttribute->hasValue()) {
         $message = $entityAttribute->getValidationMessages('required');
         if (!$message) {
             $message = ValidationException::REQUIRED;
         }
         $ex = new ValidationException(ValidationException::VALIDATION_FAILED);
         $ex->addError($attributeName, $message, []);
         $validation[$attributeName] = $ex;
         return;
     }
     /**
      * In case it is an update - if the attribute is not in new $data, it's no big deal, we already have the previous value.
      */
     $dataIsSet = array_key_exists($attributeName, $data);
     if (!$dataIsSet && $this->exists()) {
         return;
     }
     $canPopulate = !$this->exists() || $fromDb || !$entityAttribute->getOnce();
     if ($dataIsSet && $canPopulate) {
         $dataValue = $data[$attributeName];
         try {
             $entityAttribute->setValue($dataValue, $fromDb);
         } catch (ValidationException $e) {
             $validation[$attributeName] = $e;
         }
     }
 }
Exemplo n.º 13
0
 public function toArray($params = [])
 {
     return $this->processToArrayValue(StdObjectWrapper::toBool(parent::toArray($params)));
 }