Exemplo n.º 1
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());
 }
Exemplo n.º 2
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 = $this->encodeToBase64 ? strlen($this->encode($value, $field)) : 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());
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function decode($value, Field $field, Codec $codec = null)
 {
     if (empty($value)) {
         return null;
     }
     /** @var UuidIdentifier $className */
     $className = $field->getClassName() ?: 'Gdbots\\Pbj\\WellKnown\\UuidIdentifier';
     if ($value instanceof $className) {
         return $value;
     }
     return $className::fromString((string) $value);
 }
Exemplo n.º 4
0
 /**
  * {@inheritdoc}
  */
 public function decode($value, Field $field, Codec $codec = null)
 {
     if (null === $value) {
         return null;
     }
     /** @var Enum $className */
     $className = $field->getClassName();
     try {
         return $className::create((int) $value);
     } catch (\Exception $e) {
         throw new DecodeValueFailed($value, $field, $e->getMessage());
     }
 }
Exemplo n.º 5
0
 /**
  * {@inheritdoc}
  */
 public function decode($value, Field $field, Codec $codec = null)
 {
     if (empty($value)) {
         return null;
     }
     /** @var Identifier $className */
     $className = $field->getClassName();
     try {
         return $className::fromString((string) $value);
     } catch (\Exception $e) {
         throw new DecodeValueFailed($value, $field, $e->getMessage());
     }
 }
Exemplo n.º 6
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());
 }
Exemplo n.º 7
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());
 }
Exemplo n.º 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());
 }
Exemplo n.º 9
0
 /**
  * @return string
  */
 public function getFieldName()
 {
     return $this->field->getName();
 }
Exemplo n.º 10
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var MessageRef $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\MessageRef', null, $field->getName());
 }
Exemplo n.º 11
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());
 }
Exemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::boolean($value, null, $field->getName());
 }
Exemplo n.º 13
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     Assertion::choice($value, [0, 1, 2], null, $field->getName());
 }
Exemplo n.º 14
0
 /**
  * @param Field $field
  * @param \stdClass $rootObject
  * @param string $path
  * @return array
  */
 protected function mapString(Field $field, \stdClass $rootObject, $path = null)
 {
     switch ($field->getFormat()->getValue()) {
         case Format::DATE:
         case Format::DATE_TIME:
             return $this->types['date-time'];
             /**
              * String fields with these formats should use "pbj_keyword_analyzer" (or something similar)
              * so searches on these fields are not case sensitive.
              *
              * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html
              * @link http://stackoverflow.com/questions/15079064/how-to-setup-a-tokenizer-in-elasticsearch
              */
         /**
          * String fields with these formats should use "pbj_keyword_analyzer" (or something similar)
          * so searches on these fields are not case sensitive.
          *
          * @link http://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-custom-analyzer.html
          * @link http://stackoverflow.com/questions/15079064/how-to-setup-a-tokenizer-in-elasticsearch
          */
         case Format::SLUG:
         case Format::EMAIL:
         case Format::HOSTNAME:
         case Format::IPV6:
         case Format::UUID:
         case Format::URI:
         case Format::HASHTAG:
             return ['type' => 'string', 'analyzer' => 'pbj_keyword_analyzer', 'include_in_all' => false];
         case Format::IPV4:
             return ['type' => 'ip', 'include_in_all' => false];
         case Format::URL:
             return ['type' => 'string', 'index' => 'no', 'include_in_all' => false];
         default:
             if ($field->getPattern()) {
                 return ['type' => 'string', 'analyzer' => 'pbj_keyword_analyzer', 'include_in_all' => false];
             }
             return $this->applyAnalyzer(['type' => 'string'], $field, $rootObject, $path);
     }
 }
Exemplo n.º 15
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var Microtime $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\Microtime', null, $field->getName());
 }
Exemplo n.º 16
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var \DateTime $value */
     Assertion::isInstanceOf($value, 'DateTime', null, $field->getName());
 }
Exemplo n.º 17
0
 /**
  * {@inheritdoc}
  */
 public function decode($value, Field $field, Codec $codec = null)
 {
     return (double) bcadd((double) $value, '0', $field->getScale());
 }
Exemplo n.º 18
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;
 }
Exemplo n.º 19
0
 /**
  * {@inheritdoc}
  */
 public function guard($value, Field $field)
 {
     /** @var GeoPoint $value */
     Assertion::isInstanceOf($value, 'Gdbots\\Pbj\\WellKnown\\GeoPoint', null, $field->getName());
 }
Exemplo n.º 20
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;
     }
 }
Exemplo n.º 21
0
 /**
  * @return string
  */
 public function getFieldName()
 {
     return $this->existingField->getName();
 }
Exemplo n.º 22
0
 /**
  * @param array $value
  * @param Field $field
  * @return mixed
  *
  * @throws EncodeValueFailed
  */
 private function encodeASetValue(array $value, Field $field)
 {
     $type = $field->getType();
     /*
      * A MessageRefType is the only object/map value that can be
      * used in a set.  In this case of DynamoDb, we can store it as
      * a list of maps.
      */
     if ($type->getTypeName() === TypeName::MESSAGE_REF()) {
         $list = [];
         foreach ($value as $v) {
             $list[] = $type->encode($v, $field, $this);
         }
         return ['L' => $list];
     }
     if ($type->isString()) {
         $dynamoType = self::TYPE_STRING_SET;
     } elseif ($type->isNumeric()) {
         $dynamoType = self::TYPE_NUMBER_SET;
     } elseif ($type->isBinary()) {
         $dynamoType = self::TYPE_BINARY_SET;
     } else {
         throw new EncodeValueFailed($value, $field, sprintf('%s::%s has no handling for this value.', get_called_class(), __FUNCTION__));
     }
     $result = [];
     foreach ($value as $v) {
         if ($type->encodesToScalar()) {
             $result[] = (string) $type->encode($v, $field, $this);
         } else {
             $result[] = (string) $v;
         }
     }
     return [$dynamoType => $result];
 }