Пример #1
0
 /**
  * @test
  * @expectedException \InvalidArgumentException
  */
 public function shouldThrowIfValueNotConfiguredInDiscriminatorMap()
 {
     $metadata = new ClassMetadata('MyClass');
     $metadata->setDiscriminatorMap(['child' => 'ChildClass']);
     $this->assertSame('ChildClass', $metadata->getDiscriminatorClass('child'));
     $metadata->getDiscriminatorClass('unknown');
 }
Пример #2
0
 /**
  * Load discriminator metadata
  *
  * @param ApiObject $object
  * @param ClassMetadata $metadata
  */
 private function loadDiscriminatorMetadata(ApiObject $object, ClassMetadata $metadata)
 {
     if (!$object->discField) {
         return;
     }
     $fieldMeta = null;
     $field = $object->discField;
     if ($metadata instanceof ObjectMetadataInterface) {
         $properties = $metadata->getProperties();
         if (array_key_exists($field, $properties)) {
             $fieldMeta = $properties[$field];
         }
     } elseif ($metadata instanceof ResourceMetadata) {
         $attributes = $metadata->getAttributes();
         if (array_key_exists($field, $attributes)) {
             $fieldMeta = $attributes[$field];
         }
     }
     if (null === $fieldMeta) {
         throw new \InvalidArgumentException("Specified discriminator field not found in object properties");
     } elseif ('scalar' !== $fieldMeta->getDataType() || 'string' !== $fieldMeta->getDataTypeParams()) {
         throw new \InvalidArgumentException("Discriminator field must point to property that contain string value");
     }
     $metadata->setDiscriminatorField($fieldMeta);
     $metadata->setDiscriminatorMap($object->discMap);
 }