diff --git a/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldEncoder.php b/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldEncoder.php deleted file mode 100644 index 33dab76..0000000 --- a/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldEncoder.php +++ /dev/null @@ -1,27 +0,0 @@ -entity->getTranslationLanguages() as $langcode => $language) { - foreach ($this->entity->getTranslation($langcode) as $name => $field) { - $definition = $this->entity->getPropertyDefinition($name); - $langKey = empty($definition['translatable']) ? 'und' : $langcode; - if (!$field->isEmpty()) { - $properties[$name][$langKey] = $field->getValue(); - } - } - } - - // Only return properties which are not in the $skip array. - return array_diff_key($properties, array_fill_keys($skip, '')); - } - -} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldNormalizer.php deleted file mode 100644 index fec373b..0000000 --- a/core/modules/jsonld/lib/Drupal/jsonld/DrupalJsonldNormalizer.php +++ /dev/null @@ -1,32 +0,0 @@ -register('serializer.normalizer.jsonld', 'Drupal\jsonld\JsonldNormalizer')->addTag('normalizer', array('priority' => $priority)); - $container->register('serializer.encoder.jsonld', 'Drupal\jsonld\JsonldEncoder')->addTag('encoder', array('priority' => $priority)); - $container->register('serializer.normalizer.drupal_jsonld', 'Drupal\jsonld\DrupalJsonldNormalizer')->addTag('normalizer', array('priority' => $priority)); - $container->register('serializer.encoder.drupal_jsonld', 'Drupal\jsonld\DrupalJsonldEncoder')->addTag('encoder', array('priority' => $priority)); + // Normalizers can be specified to support a particular class and format in + // Normalizer::supportsNormalization(). Since the first matching Normalizer + // is used, Normalizers should be ordered from most specific to least + // specific. + $normalizers = array( + // Field Item. + 'entity_reference' => array( + 'jsonld' => 'Drupal\jsonld\JsonldEntityReferenceNormalizer', + ), + 'field_item' => array( + 'jsonld' => 'Drupal\jsonld\JsonldFieldItemNormalizer', + ), + // Field. + 'field' => array( + 'jsonld' => 'Drupal\jsonld\JsonldFieldNormalizer', + ), + // Entity. + 'entity' => array( + 'jsonld' => 'Drupal\jsonld\JsonldEntityNormalizer', + ), + ); + // Encoders can only specify which format they support in + // Encoder::supportsEncoding(). + $encoders = array( + 'jsonld' => 'Drupal\jsonld\JsonldEncoder', + ); + + // Add Normalizers to service container. + foreach ($normalizers as $supported_class => $formats) { + foreach ($formats as $format => $normalizer_class) { + $container->register("serializer.normalizer.{$supported_class}.{$format}", $normalizer_class)->addTag('normalizer', array('priority' => $priority)); + } + } + + // Add Encoders to service container. + foreach ($encoders as $format => $encoder_class) { + $container->register("serializer.encoder.{$format}", $encoder_class)->addTag('encoder', array('priority' => $priority)); + } } } diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEncoder.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEncoder.php index cd76a3c..1d8fe2c 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEncoder.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEncoder.php @@ -18,11 +18,11 @@ class JsonldEncoder extends JsonEncoder implements EncoderInterface { /** - * The format that this Encoder supports. + * The formats that this Normalizer supports. * - * @var string + * @var array */ - static protected $format = 'jsonld'; + static protected $format = array('jsonld', 'drupal_jsonld'); /** * Check whether the request is for JSON-LD. @@ -34,6 +34,6 @@ class JsonldEncoder extends JsonEncoder implements EncoderInterface { * Returns TRUE if the encoder can handle the request. */ public function supportsEncoding($format) { - return static::$format === $format; + return in_array($format, static::$format); } } diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php new file mode 100644 index 0000000..5b2c5bf --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityNormalizer.php @@ -0,0 +1,37 @@ +serializer); + + $attributes = $entityWrapper->getProperties(); + $attributes = array('@id' => $entityWrapper->getId()) + $attributes; + return $attributes; + } + + /** + * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization() + */ + public function supportsNormalization($data, $format = NULL) { + return parent::supportsNormalization($data, $format) && ($data instanceof EntityNG); + } + +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php new file mode 100644 index 0000000..1e6d27c --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php @@ -0,0 +1,41 @@ +entity, $format, $this->serializer); + return array( + '@id' => $entityWrapper->getId(), + ); + } + + /** + * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization() + */ + public function supportsNormalization($data, $format = NULL) { + return parent::supportsNormalization($data, $format) && ($data instanceof EntityReferenceItem); + } + +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php index 16cd980..838fdb5 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityWrapper.php @@ -1,38 +1,57 @@ entity = $entity; + $this->format = $format; + $this->serializer = $serializer; } - + /** * Get the Entity's URI for the @id attribute. + * + * @return string + * The URI of the entity. */ public function getId() { $uri_info = $this->entity->uri(); @@ -42,11 +61,26 @@ public function getId() { /** * Get properties, excluding JSON-LD specific properties. * - * Formats Entity properties in the JSON-LD array structure and removes - * unwanted values. + * @return array + * An array of properties structured as in JSON-LD. */ public function getProperties() { - // @todo Add property handling based on http://drupal.org/node/1813328. - return array(); + // Properties to skip. + $skip = array('id'); + + // Create language map property structure. + foreach ($this->entity->getTranslationLanguages() as $langcode => $language) { + foreach ($this->entity->getTranslation($langcode) as $name => $field) { + $definition = $this->entity->getPropertyDefinition($name); + $langKey = empty($definition['translatable']) ? 'und' : $langcode; + if (!$field->isEmpty()) { + $properties[$name][$langKey] = $this->serializer->normalize($field, $this->format); + } + } + } + + // Only return properties which are not in the $skip array. + return array_diff_key($properties, array_fill_keys($skip, '')); } + } diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldFieldItemNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldFieldItemNormalizer.php new file mode 100644 index 0000000..d0c2fdf --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldFieldItemNormalizer.php @@ -0,0 +1,33 @@ +getValue(); + } + + /** + * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization() + */ + public function supportsNormalization($data, $format = NULL) { + return parent::supportsNormalization($data, $format) && ($data instanceof FieldItemBase); + } + +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldFieldNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldFieldNormalizer.php new file mode 100644 index 0000000..9544fa0 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldFieldNormalizer.php @@ -0,0 +1,38 @@ +getIterator(); + foreach ($list as $delta => $item) { + $fieldValues[$delta] = $this->serializer->normalize($item, $format); + } + return $fieldValues; + } + + /** + * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() + */ + public function supportsNormalization($data, $format = NULL) { + return parent::supportsNormalization($data, $format) && ($data instanceof Field); + } + +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizer.php deleted file mode 100644 index 61c654a..0000000 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizer.php +++ /dev/null @@ -1,69 +0,0 @@ -entityWrapperClass($object); - - $attributes = $entityWrapper->getProperties(); - $attributes = array('@id' => $entityWrapper->getId()) + $attributes; - return $attributes; - } - - /** - * Checks whether the data and format are supported by this normalizer. - * - * @param mixed $data - * Data to normalize. - * @param string $format - * Format the normalization result will be encoded as. - * - * @return bool - * Returns TRUE if the normalizer can handle the request. - */ - public function supportsNormalization($data, $format = NULL) { - // If this is an Entity object and the request is for JSON-LD. - return is_object($data) && ($data instanceof EntityNG) && static::$format === $format; - } - -} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizerBase.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizerBase.php new file mode 100644 index 0000000..2508459 --- /dev/null +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldNormalizerBase.php @@ -0,0 +1,34 @@ + 'Drupal JSON-LD Normalizer', + 'name' => 'vnd.drupal.ld+json Normalization', 'description' => "Test Drupal's vendor specific JSON-LD normalizer.", 'group' => 'JSON-LD', ); @@ -35,25 +45,38 @@ public static function getInfo() { function setUp() { parent::setUp(); - $this->normalizer = new DrupalJsonldNormalizer(); + $this->normalizers = array( + 'entityreference' => new JsonldEntityReferenceNormalizer(), + 'field_item' => new JsonldFieldItemNormalizer(), + 'field' => new JsonldFieldNormalizer(), + 'entity' => new JsonldEntityNormalizer(), + ); + $serializer = new Serializer($this->normalizers); + $this->normalizers['entity']->setSerializer($serializer); } /** * Tests the supportsNormalization function. */ public function testSupportsNormalization() { - $function = 'DrupalJsonldNormalizer::supportsNormlization'; - $supportedFormat = 'drupal_jsonld'; - $unsupportedFormat = 'jsonld'; + $format = static::$format; $supportedEntity = entity_create('entity_test', array()); $unsupportedEntity = new ConfigEntityTest(); + $field = $supportedEntity->get('uuid'); + $entityreferenceField = $supportedEntity->get('user_id'); + + // Supported entity. + $this->assertTrue($this->normalizers['entity']->supportsNormalization($supportedEntity, static::$format), "Entity normalization is supported for $format on content entities."); + // Unsupported entity. + $this->assertFalse($this->normalizers['entity']->supportsNormalization($unsupportedEntity, static::$format), "Normalization is not supported for other entity types."); + + // Field. + $this->assertTrue($this->normalizers['field']->supportsNormalization($field, static::$format), "Field normalization is supported for $format."); - // Supported entity, supported format. - $this->assertTrue($this->normalizer->supportsNormalization($supportedEntity, $supportedFormat), "$function returns TRUE for supported format."); - // Supported entity, unsupported format. - $this->assertFalse($this->normalizer->supportsNormalization($supportedEntity, $unsupportedFormat), "$function returns FALSE for unsupported format."); - // Unsupported entity, supported format. - $this->assertFalse($this->normalizer->supportsNormalization($unsupportedEntity, $supportedFormat), "$function returns FALSE for unsupported entity type."); + // Field item. + $this->assertTrue($this->normalizers['field_item']->supportsNormalization($field->offsetGet(0), static::$format), "Field item normalization is supported for $format."); + // Entity reference field item. + $this->assertTrue($this->normalizers['entityreference']->supportsNormalization($entityreferenceField->offsetGet(0), static::$format), "Entity reference field item normalization is supported for $format."); } /** @@ -92,14 +115,14 @@ public function testNormalize() { 'uuid' => array( 'und' => array( array( - 'value' => $entity->uuid() + 'value' => $entity->uuid(), ), ), ), 'user_id' => array( 'de' => array( array( - 'value' => 1, + '@id' => url('user/' . $values['user_id'], array('absolute' => TRUE)), ), ), ), @@ -125,7 +148,7 @@ public function testNormalize() { ), ); - $normalized = $this->normalizer->normalize($entity); + $normalized = $this->normalizers['entity']->normalize($entity, static::$format); // Test ordering. The @context and @id properties should always be first. $keys = array_keys($normalized); $this->assertEqual($keys[0], '@id', '@id and @context attributes placed correctly.'); diff --git a/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTest.php b/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTest.php deleted file mode 100644 index 2ab5261..0000000 --- a/core/modules/jsonld/lib/Drupal/jsonld/Tests/JsonldNormalizerTest.php +++ /dev/null @@ -1,58 +0,0 @@ - 'JSON-LD Normalizer', - 'description' => "Test the JSON-LD normalizer.", - 'group' => 'JSON-LD', - ); - } - - /** - * Add the normalizer to be tested. - */ - function setUp() { - parent::setUp(); - - $this->normalizer = new JsonldNormalizer(); - } - - /** - * Tests the supportsNormalization function. - */ - public function testSupportsNormalization() { - $function = 'JsonldNormalizer::supportsNormlization'; - $supportedFormat = 'jsonld'; - $unsupportedFormat = 'drupal_jsonld'; - $supportedEntity = entity_create('entity_test', array()); - $unsupportedEntity = new ConfigEntityTest(); - - // Supported entity, supported format. - $this->assertTrue($this->normalizer->supportsNormalization($supportedEntity, $supportedFormat), "$function returns TRUE for supported format."); - // Supported entity, unsupported format. - $this->assertFalse($this->normalizer->supportsNormalization($supportedEntity, $unsupportedFormat), "$function returns FALSE for unsupported format."); - // Unsupported entity, supported format. - $this->assertFalse($this->normalizer->supportsNormalization($unsupportedEntity, $supportedFormat), "$function returns FALSE for unsupported entity type."); - } - -}