Example #1
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);
 }
Example #2
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());
     }
 }
Example #3
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());
     }
 }
Example #4
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 #5
0
 /**
  * @link https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
  *
  * @param Field $field
  * @param \stdClass $rootObject
  * @param string $path
  * @return array
  */
 protected function mapMessage(Field $field, \stdClass $rootObject, $path = null)
 {
     /** @var Message $class */
     $class = $field->getClassName();
     if (!empty($class) && !class_exists($class)) {
         /*
          * gdbots/pbjc compiler generates an interface and a concrete class with
          * a V# suffix.  v1 would of course generally exist so we have a good chance
          * of finding a class and thus a schema using this strategy.  we will however
          * need to get fancier as versions increase and when mixins are used.
          */
         $class = $class . 'V1';
     }
     if (!empty($class) && class_exists($class)) {
         $schema = $class::schema();
         return ['type' => $field->isAList() ? 'nested' : 'object', 'properties' => $this->mapSchema($schema, $rootObject, $path)];
     }
     return ['type' => $field->isAList() ? 'nested' : 'object', 'properties' => [Schema::PBJ_FIELD_NAME => ['type' => 'string', 'index' => 'not_analyzed', 'include_in_all' => false]]];
 }