View source
<?php
namespace Drupal\schemata_json_schema\Normalizer\hal;
use Drupal\schemata_json_schema\Normalizer\json\SchemataSchemaNormalizer as JsonSchemataSchemaNormalizer;
class SchemataSchemaNormalizer extends JsonSchemataSchemaNormalizer {
protected $format = 'schema_json';
protected $describedFormat = 'hal_json';
public function normalize($entity, $format = NULL, array $context = []) {
$normalized = parent::normalize($entity, $format, $context);
$items = [];
if (!empty($normalized['properties']['_links'])) {
$items = $normalized['properties']['_links'];
}
$items['self'] = [
'$ref' => '#/definitions/linkObject',
];
$items['type'] = [
'$ref' => '#/definitions/linkObject',
];
$normalized['properties']['_links'] = [
'title' => 'HAL Links',
'description' => 'Object of links with the rels as the keys',
'type' => 'object',
'properties' => $items,
];
if (!empty($normalized['properties']['_embedded'])) {
$items = $normalized['properties']['_embedded'];
$normalized['properties']['_embedded'] = [
'title' => 'HAL Embedded Resource',
'description' => 'An embedded HAL resource',
'type' => 'object',
'properties' => $items,
];
}
$normalized['definitions']['linkArray'] = [
'title' => 'HAL Link Array',
'description' => 'An array of linkObjects of the same link relation',
'type' => 'array',
'items' => [
'$ref' => '#/definitions/linkObject',
],
];
$normalized['definitions']['linkObject'] = [
'title' => 'HAL Link Object',
'description' => 'An object with link information.',
'type' => 'object',
'properties' => [
'name' => [
'title' => 'Name',
'description' => 'Name of a resource, link, action, etc.',
'type' => 'string',
],
'title' => [
'title' => 'Title',
'description' => 'A title for a resource, link, action, etc.',
'type' => 'string',
],
'href' => [
'title' => 'HREF',
'description' => 'A hyperlink URL.',
'type' => 'string',
'format' => 'uri',
],
],
'required' => [
'href',
],
];
return $normalized;
}
}