Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Component\Plugin\Context;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Plugin\Exception\ContextException;
|
Chris@0
|
6 use Symfony\Component\Validator\Constraints\Type;
|
Chris@0
|
7 use Symfony\Component\Validator\Validation;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * A generic context class for wrapping data a plugin needs to operate.
|
Chris@0
|
11 */
|
Chris@0
|
12 class Context implements ContextInterface {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * The value of the context.
|
Chris@0
|
16 *
|
Chris@0
|
17 * @var mixed
|
Chris@0
|
18 */
|
Chris@0
|
19 protected $contextValue;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * The definition to which a context must conform.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var \Drupal\Component\Plugin\Context\ContextDefinitionInterface
|
Chris@0
|
25 */
|
Chris@0
|
26 protected $contextDefinition;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Create a context object.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @param \Drupal\Component\Plugin\Context\ContextDefinitionInterface $context_definition
|
Chris@0
|
32 * The context definition.
|
Chris@0
|
33 * @param mixed|null $context_value
|
Chris@0
|
34 * The value of the context.
|
Chris@0
|
35 */
|
Chris@0
|
36 public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
|
Chris@0
|
37 $this->contextDefinition = $context_definition;
|
Chris@0
|
38 $this->contextValue = $context_value;
|
Chris@0
|
39 }
|
Chris@0
|
40
|
Chris@0
|
41 /**
|
Chris@0
|
42 * {@inheritdoc}
|
Chris@0
|
43 */
|
Chris@0
|
44 public function getContextValue() {
|
Chris@0
|
45 // Support optional contexts.
|
Chris@0
|
46 if (!isset($this->contextValue)) {
|
Chris@0
|
47 $definition = $this->getContextDefinition();
|
Chris@0
|
48 $default_value = $definition->getDefaultValue();
|
Chris@0
|
49
|
Chris@0
|
50 if (!isset($default_value) && $definition->isRequired()) {
|
Chris@0
|
51 $type = $definition->getDataType();
|
Chris@0
|
52 throw new ContextException(sprintf("The %s context is required and not present.", $type));
|
Chris@0
|
53 }
|
Chris@0
|
54 // Keep the default value here so that subsequent calls don't have to look
|
Chris@0
|
55 // it up again.
|
Chris@0
|
56 $this->contextValue = $default_value;
|
Chris@0
|
57 }
|
Chris@0
|
58 return $this->contextValue;
|
Chris@0
|
59 }
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * {@inheritdoc}
|
Chris@0
|
63 */
|
Chris@0
|
64 public function hasContextValue() {
|
Chris@0
|
65 return (bool) $this->contextValue || (bool) $this->getContextDefinition()->getDefaultValue();
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * {@inheritdoc}
|
Chris@0
|
70 */
|
Chris@0
|
71 public function getContextDefinition() {
|
Chris@0
|
72 return $this->contextDefinition;
|
Chris@0
|
73 }
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * {@inheritdoc}
|
Chris@0
|
77 */
|
Chris@0
|
78 public function getConstraints() {
|
Chris@0
|
79 if (empty($this->contextDefinition['class'])) {
|
Chris@0
|
80 throw new ContextException("An error was encountered while trying to validate the context.");
|
Chris@0
|
81 }
|
Chris@0
|
82 return [new Type($this->contextDefinition['class'])];
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * {@inheritdoc}
|
Chris@0
|
87 */
|
Chris@0
|
88 public function validate() {
|
Chris@0
|
89 $validator = Validation::createValidatorBuilder()
|
Chris@0
|
90 ->getValidator();
|
Chris@0
|
91 return $validator->validateValue($this->getContextValue(), $this->getConstraints());
|
Chris@0
|
92 }
|
Chris@0
|
93
|
Chris@0
|
94 }
|