Example #1
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\UuidIdentifier', null, $field->getName());
     if ($field->hasClassName()) {
         Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     }
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var BigNumber $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\BigNumber', null, $field->getName());
     Assertion::true(!$value->isNegative(), sprintf('Field [%s] cannot be negative.', $field->getName()), $field->getName());
     Assertion::true($value->isLessThanOrEqualTo('18446744073709551615'), sprintf('Field [%s] cannot be greater than [18446744073709551615].', $field->getName()), $field->getName());
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var BigNumber $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\BigNumber', null, $field->getName());
     Assertion::true($value->isGreaterThanOrEqualTo('-9223372036854775808'), sprintf('Field [%s] cannot be less than [-9223372036854775808].', $field->getName()), $field->getName());
     Assertion::true($value->isLessThanOrEqualTo('9223372036854775807'), sprintf('Field [%s] cannot be greater than [9223372036854775807].', $field->getName()), $field->getName());
 }
Example #4
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Enum $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Common\\Enum', null, $field->getName());
     Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     Assertion::integer($value->getValue(), null, $field->getName());
     Assertion::range($value->getValue(), $this->getMin(), $this->getMax(), null, $field->getName());
 }
Example #5
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::integer($value, null, $field->getName());
     $intMin = $this->getMin();
     $intMax = $this->getMax();
     $min = NumberUtils::bound($field->getMin(), $intMin, $intMax);
     $max = NumberUtils::bound($field->getMax(), $intMin, $intMax);
     Assertion::range($value, $min, $max, null, $field->getName());
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::string($value, null, $field->getName());
     // intentionally using strlen to get byte length, not mb_strlen
     $length = strlen($value);
     $minLength = $field->getMinLength();
     $maxLength = NumberUtils::bound($field->getMaxLength(), $minLength, $this->getMaxBytes());
     $okay = $length >= $minLength && $length <= $maxLength;
     Assertion::true($okay, sprintf('Field [%s] must be between [%d] and [%d] bytes, [%d] bytes given.', $field->getName(), $minLength, $maxLength, $length), $field->getName());
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Identifier $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\Identifier', null, $field->getName());
     Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     $v = $value->toString();
     //Assertion::string($v, null, $field->getName());
     // intentionally using strlen to get byte length, not mb_strlen
     $length = strlen($v);
     $maxBytes = $this->getMaxBytes();
     $okay = $length > 0 && $length <= $maxBytes;
     Assertion::true($okay, sprintf('Field [%s] must be between [1] and [%d] bytes, [%d] bytes given.', $field->getName(), $maxBytes, $length), $field->getName());
 }
Example #8
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Message $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\Message', null, $field->getName());
     Assertion::isInstanceOf($value, $field->getClassName(), null, $field->getName());
     if (!$field->hasAnyOfClassNames()) {
         return;
     }
     $classNames = $field->getAnyOfClassNames();
     if (empty($classNames)) {
         // means it can be "any message"
         return;
     }
     foreach ($classNames as $className) {
         if ($value instanceof $className) {
             return;
         }
     }
     Assertion::true(false, sprintf('Field [%s] must be an instance of at least one of: %s.', $field->getName(), implode(',', $classNames)), $field->getName());
 }
Example #9
0
 /**
  * @param Field $field
  * @throws FieldAlreadyDefined
  * @throws FieldOverrideNotCompatible
  */
 private function addField(Field $field)
 {
     $fieldName = $field->getName();
     if ($this->hasField($fieldName)) {
         $existingField = $this->getField($fieldName);
         if (!$existingField->isOverridable()) {
             throw new FieldAlreadyDefined($this, $fieldName);
         }
         if (!$existingField->isCompatibleForOverride($field)) {
             throw new FieldOverrideNotCompatible($this, $fieldName, $field);
         }
     }
     $this->fields[$fieldName] = $field;
     if ($field->isRequired()) {
         $this->requiredFields[$fieldName] = $field;
     }
 }
Example #10
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var GeoPoint $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\GeoPoint', null, $field->getName());
 }
Example #11
0
 /**
  * @return string
  */
 public function getFieldName()
 {
     return $this->field->getName();
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var MessageRef $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\MessageRef', null, $field->getName());
 }
Example #13
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::integer($value, null, $field->getName());
     Assertion::true(DateUtils::isValidTimestamp($value), sprintf('Field [%s] value [%d] is not a valid unix timestamp.', $field->getName(), $value), $field->getName());
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::boolean($value, null, $field->getName());
 }
Example #15
0
 /**
  * Populates the default on a single field if it's not already set
  * and the default generated is not a null value or empty array.
  *
  * @param Field $field
  * @return bool Returns true if a non null/empty default was applied or already present.
  */
 private function populateDefault(Field $field)
 {
     if ($this->has($field->getName())) {
         return true;
     }
     $default = $field->getDefault($this);
     if (null === $default) {
         return false;
     }
     if ($field->isASingleValue()) {
         $this->data[$field->getName()] = $default;
         unset($this->clearedFields[$field->getName()]);
         return true;
     }
     if (empty($default)) {
         return false;
     }
     /*
      * sets have a special handling to deal with unique values
      */
     if ($field->isASet()) {
         $this->addToSet($field->getName(), $default);
         return true;
     }
     $this->data[$field->getName()] = $default;
     unset($this->clearedFields[$field->getName()]);
     return true;
 }
 /**
  * @return string
  */
 public function getFieldName()
 {
     return $this->existingField->getName();
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::choice($value, [0, 1, 2], null, $field->getName());
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Microtime $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\Microtime', null, $field->getName());
 }
Example #19
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var \DateTime $value */
     Assertion::isInstanceOf($value, 'DateTime', null, $field->getName());
 }