class EntityParameterFactory in JSON-RPC 8
Same name and namespace in other branches
- 2.x src/ParameterFactory/EntityParameterFactory.php \Drupal\jsonrpc\ParameterFactory\EntityParameterFactory
A factory to create loaded entities from entity type & UUID user input.
Hierarchy
- class \Drupal\jsonrpc\ParameterFactory\ParameterFactoryBase extends \Shaper\Transformation\TransformationBase implements ParameterFactoryInterface
- class \Drupal\jsonrpc\ParameterFactory\EntityParameterFactory
Expanded class hierarchy of EntityParameterFactory
File
- src/
ParameterFactory/ EntityParameterFactory.php, line 19
Namespace
Drupal\jsonrpc\ParameterFactoryView source
class EntityParameterFactory extends ParameterFactoryBase {
/**
* The entity repository.
*
* @var \Drupal\Core\Entity\EntityRepositoryInterface
*/
protected $entityRepository;
/**
* EntityParameterFactory constructor.
*
* @param \Drupal\jsonrpc\ParameterDefinitionInterface $definition
* The parameter definition.
* @param \JsonSchema\Validator $validator
* The validator to ensure the user input is valid.
* @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
* The entity type repository to load entities by UUID.
*/
public function __construct(ParameterDefinitionInterface $definition, Validator $validator, EntityRepositoryInterface $entity_repository) {
parent::__construct($definition, $validator);
$this->entityRepository = $entity_repository;
}
/**
* {@inheritdoc}
*/
public static function create(ParameterDefinitionInterface $definition, ContainerInterface $container) {
return new static($definition, $container
->get('jsonrpc.schema_validator'), $container
->get('entity.repository'));
}
/**
* {@inheritdoc}
*/
public static function schema(ParameterDefinitionInterface $parameter_definition = NULL) {
return [
'type' => 'object',
'properties' => [
'type' => [
'type' => 'string',
],
'uuid' => [
'type' => 'string',
],
],
];
}
/**
* {@inheritdoc}
*/
public function getOutputValidator() {
return new InstanceofValidator(EntityInterface::class);
}
/**
* {@inheritdoc}
*/
protected function doTransform($data, Context $context = NULL) {
try {
if ($entity = $this->entityRepository
->loadEntityByUuid($data['type'], $data['uuid'])) {
return $entity;
}
throw JsonRpcException::fromError(Error::invalidParams('The requested entity could not be found.'));
} catch (EntityStorageException $e) {
throw JsonRpcException::fromError(Error::invalidParams('This entity type is not supported. Error: ' . $e
->getMessage()));
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityParameterFactory:: |
protected | property | The entity repository. | |
EntityParameterFactory:: |
public static | function |
Instantiates a new instance of this class. Overrides ParameterFactoryBase:: |
|
EntityParameterFactory:: |
protected | function | ||
EntityParameterFactory:: |
public | function | ||
EntityParameterFactory:: |
public static | function |
An array representing the JSON Schema for acceptable input to the factory. Overrides ParameterFactoryInterface:: |
|
EntityParameterFactory:: |
public | function |
EntityParameterFactory constructor. Overrides ParameterFactoryBase:: |
|
ParameterFactoryBase:: |
protected | property | The parameter definition. | |
ParameterFactoryBase:: |
protected | property | The validation class for shaper interactions. | |
ParameterFactoryBase:: |
protected | property | The schema validator to ensure the input data adheres to the expectation. | |
ParameterFactoryBase:: |
public | function |