271afcb01685369e4aedc89fe9588a44921f9d43
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Classes / Domain / Model / Renderable / RenderableVariant.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Form\Domain\Model\Renderable;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Form\Domain\Condition\ConditionResolver;
19 use TYPO3\CMS\Form\Domain\Exception\IdentifierNotValidException;
20
21 /**
22 * Scope: frontend
23 * **This class is NOT meant to be sub classed by developers.**
24 * @internal
25 */
26 class RenderableVariant implements RenderableVariantInterface
27 {
28
29 /**
30 * @var string
31 */
32 protected $identifier;
33
34 /**
35 * @var array
36 */
37 protected $options;
38
39 /**
40 * @var VariableRenderableInterface
41 */
42 protected $renderable;
43
44 /**
45 * @var string
46 */
47 protected $condition = '';
48
49 /**
50 * @var bool
51 */
52 protected $applied = false;
53
54 /**
55 * @param string $identifier
56 * @param array $options
57 * @param VariableRenderableInterface $renderable
58 * @throws IdentifierNotValidException
59 */
60 public function __construct(
61 string $identifier,
62 array $options,
63 VariableRenderableInterface $renderable
64 ) {
65 if ('' === $identifier) {
66 throw new IdentifierNotValidException('The given variant identifier was empty.', 1519998923);
67 }
68 $this->identifier = $identifier;
69 $this->renderable = $renderable;
70
71 if (isset($options['condition']) && is_string($options['condition'])) {
72 $this->condition = $options['condition'];
73 }
74
75 unset($options['condition'], $options['identifier'], $options['variants']);
76
77 $this->options = $options;
78 }
79
80 /**
81 * Apply the specified variant to this form element
82 * regardless of their conditions
83 */
84 public function apply(): void
85 {
86 $this->renderable->setOptions($this->options, true);
87 $this->applied = true;
88 }
89
90 /**
91 * @param ConditionResolver $conditionResolver
92 * @return bool
93 */
94 public function conditionMatches(ConditionResolver $conditionResolver): bool
95 {
96 if (empty($this->condition)) {
97 return false;
98 }
99
100 return $conditionResolver->resolveCondition($this->condition);
101 }
102
103 /**
104 * @return string
105 */
106 public function getIdentifier(): string
107 {
108 return $this->identifier;
109 }
110
111 /**
112 * @return bool
113 */
114 public function isApplied(): bool
115 {
116 return $this->applied;
117 }
118 }