ResourceFieldResource.php in RESTful 7.2
File
src/Plugin/resource/Field/ResourceFieldResource.php
View source
<?php
namespace Drupal\restful\Plugin\resource\Field;
use Drupal\restful\Exception\ServerConfigurationException;
use Drupal\restful\Http\RequestInterface;
use Drupal\restful\Plugin\resource\DataInterpreter\DataInterpreterInterface;
use Drupal\restful\Plugin\resource\Field\PublicFieldInfo\PublicFieldInfoInterface;
use Drupal\restful\Plugin\resource\ResourceInterface;
use Drupal\restful\Util\ExplorableDecoratorInterface;
class ResourceFieldResource implements ResourceFieldResourceInterface, ExplorableDecoratorInterface {
protected $decorated;
protected $resourceId;
protected $resourceMachineName;
protected $resourcePlugin;
protected $targetColumn;
public function __construct(array $field, RequestInterface $request) {
if ($this->decorated) {
$this
->setRequest($request);
}
$this->resourceMachineName = $field['resource']['name'];
if (!empty($field['targetColumn'])) {
$this->targetColumn = $field['targetColumn'];
}
}
public function getResourceId(DataInterpreterInterface $interpreter) {
if (isset($this->resourceId)) {
return $this->resourceId;
}
$this->resourceId = $this
->compoundDocumentId($interpreter);
return $this->resourceId;
}
public function getResourceMachineName() {
return $this->resourceMachineName;
}
public function getResourcePlugin() {
if (isset($this->resourcePlugin)) {
return $this->resourcePlugin;
}
$resource_info = $this
->getResource();
$this->resourcePlugin = restful()
->getResourceManager()
->getPlugin(sprintf('%s:%d.%d', $resource_info['name'], $resource_info['majorVersion'], $resource_info['minorVersion']));
return $this->resourcePlugin;
}
public function getCardinality() {
if ($this->decorated instanceof ResourceFieldEntityInterface) {
return $this->decorated
->getCardinality();
}
return 1;
}
public function setCardinality($cardinality) {
$this->decorated
->setCardinality($cardinality);
}
public static function create(array $field, RequestInterface $request = NULL) {
$request = $request ?: restful()
->getRequest();
$resource_field = ResourceField::create($field, $request);
$output = new static($field, $request);
$output
->decorate($resource_field);
return $output;
}
public static function isArrayNumeric(array $input) {
return ResourceFieldBase::isArrayNumeric($input);
}
public function value(DataInterpreterInterface $interpreter) {
return $this->decorated
->value($interpreter);
}
public function access($op, DataInterpreterInterface $interpreter) {
return $this->decorated
->access($op, $interpreter);
}
public function addDefaults() {
$this->decorated
->addDefaults();
}
public function set($value, DataInterpreterInterface $interpreter) {
$this->decorated
->set($value, $interpreter);
}
public function decorate(ResourceFieldInterface $decorated) {
$this->decorated = $decorated;
}
public function addMetadata($key, $value) {
$this->decorated
->addMetadata($key, $value);
}
public function getMetadata($key) {
return $this->decorated
->getMetadata($key);
}
public function executeProcessCallbacks($value) {
return $this->decorated
->executeProcessCallbacks($value);
}
public function getPublicName() {
return $this->decorated
->getPublicName();
}
public function setPublicName($public_name) {
$this->decorated
->setPublicName($public_name);
}
public function getAccessCallbacks() {
return $this->decorated
->getAccessCallbacks();
}
public function setAccessCallbacks($access_callbacks) {
$this->decorated
->setAccessCallbacks($access_callbacks);
}
public function getProperty() {
return $this->decorated
->getProperty();
}
public function setProperty($property) {
$this->decorated
->setProperty($property);
}
public function getCallback() {
return $this->decorated
->getCallback();
}
public function setCallback($callback) {
$this->decorated
->setCallback($callback);
}
public function getProcessCallbacks() {
return $this->decorated
->getProcessCallbacks();
}
public function setProcessCallbacks($process_callbacks) {
$this->decorated
->setProcessCallbacks($process_callbacks);
}
public function getResource() {
return $this->decorated
->getResource();
}
public function setResource($resource) {
$this->decorated
->setResource($resource);
}
public function getMethods() {
return $this->decorated
->getMethods();
}
public function setMethods($methods) {
$this->decorated
->setMethods($methods);
}
public function id() {
return $this->decorated
->id();
}
public function isComputed() {
return $this->decorated
->isComputed();
}
public function compoundDocumentId(DataInterpreterInterface $interpreter) {
return $this->decorated
->compoundDocumentId($interpreter);
}
public function render(DataInterpreterInterface $interpreter) {
return $this
->executeProcessCallbacks($this
->value($interpreter));
}
public function __call($name, $arguments) {
return call_user_func_array(array(
$this->decorated,
$name,
), $arguments);
}
public function getRequest() {
return $this->decorated
->getRequest();
}
public function setRequest(RequestInterface $request) {
$this->decorated
->setRequest($request);
}
public function getDefinition() {
return $this->decorated
->getDefinition();
}
public function getPublicFieldInfo() {
return $this->decorated
->getPublicFieldInfo();
}
public function setPublicFieldInfo(PublicFieldInfoInterface $public_field_info) {
$this->decorated
->setPublicFieldInfo($public_field_info);
}
public function autoDiscovery() {
if (method_exists($this->decorated, 'autoDiscovery')) {
return $this->decorated
->autoDiscovery();
}
return ResourceFieldBase::emptyDiscoveryInfo($this
->getPublicName());
}
public function getTargetColumn() {
if (empty($this->targetColumn)) {
$definition = $this->decorated
->getDefinition();
if (!empty($definition['targetColumn'])) {
$this->targetColumn = $definition['targetColumn'];
}
elseif ($this
->isInstanceOf(ResourceFieldEntityReferenceInterface::class)) {
$entity_info = entity_get_info($this
->getResourcePlugin()
->getEntityType());
$this->targetColumn = $entity_info['entity keys']['id'];
}
else {
throw new ServerConfigurationException(sprintf('Target column could not be found for field "%s".', $this
->getPublicName()));
}
}
return $this->targetColumn;
}
public function isInstanceOf($class) {
if ($this instanceof $class || $this->decorated instanceof $class) {
return TRUE;
}
if ($this->decorated instanceof ExplorableDecoratorInterface) {
return $this->decorated
->isInstanceOf($class);
}
return FALSE;
}
}