7e4844d168df3ec6e3e77e91f78d1c9300d1a62a
[GitHub/WoltLab/WCF.git] /
1 <?php
2
3 namespace wcf\system\form\builder\field\dependency;
4
5 use wcf\system\form\builder\field\IFormField;
6 use wcf\system\form\builder\IFormNode;
7 use wcf\system\WCF;
8
9 /**
10 * Abstract implementation of a form field dependency.
11 *
12 * @author Matthias Schmidt
13 * @copyright 2001-2019 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package WoltLabSuite\Core\System\Form\Builder\Field\Dependency
16 * @since 5.2
17 */
18 abstract class AbstractFormFieldDependency implements IFormFieldDependency
19 {
20 /**
21 * node whose availability depends on the value of a field
22 * @var IFormNode
23 */
24 protected $dependentNode;
25
26 /**
27 * field the availability of the node dependents on
28 * @var IFormField
29 */
30 protected $field;
31
32 /**
33 * id of the field the availability of the node dependents on
34 * @var string
35 */
36 protected $fieldId;
37
38 /**
39 * id of the dependency
40 * @var string
41 */
42 protected $id;
43
44 /**
45 * name of the template containing the dependency JavaScript code
46 * @var null|string
47 */
48 protected $templateName;
49
50 /**
51 * @inheritDoc
52 */
53 public function dependentNode(IFormNode $node)
54 {
55 $this->dependentNode = $node;
56
57 return $this;
58 }
59
60 /**
61 * @inheritDoc
62 */
63 public function field(IFormField $field)
64 {
65 $this->field = $field;
66
67 return $this;
68 }
69
70 /**
71 * @inheritDoc
72 */
73 public function fieldId($fieldId)
74 {
75 if ($this->getField() !== null) {
76 throw new \BadMethodCallException("Cannot set field id after field has been set.");
77 }
78
79 $this->fieldId = $fieldId;
80
81 return $this;
82 }
83
84 /**
85 * @inheritDoc
86 */
87 public function getDependentNode()
88 {
89 if ($this->dependentNode === null) {
90 throw new \BadMethodCallException("Dependent node has not been set.");
91 }
92
93 return $this->dependentNode;
94 }
95
96 /**
97 * @inheritDoc
98 */
99 public function getField()
100 {
101 return $this->field;
102 }
103
104 /**
105 * @inheritDoc
106 */
107 public function getFieldId()
108 {
109 if ($this->getField() !== null) {
110 return $this->getField()->getId();
111 }
112
113 if ($this->fieldId === null) {
114 throw new \BadMethodCallException("Neither the field nor the field id has been set.");
115 }
116
117 return $this->fieldId;
118 }
119
120 /**
121 * @inheritDoc
122 */
123 public function getId()
124 {
125 return $this->id;
126 }
127
128 /**
129 * @inheritDoc
130 */
131 public function getHtml()
132 {
133 if ($this->templateName === null) {
134 throw new \LogicException("Template name is not set.");
135 }
136
137 return WCF::getTPL()->fetch($this->templateName, 'wcf', [
138 'dependency' => $this,
139 ], true);
140 }
141
142 /**
143 * Sets the id of this dependency and returns this dependency.
144 *
145 * @param string $id id of the dependency
146 * @return static $this this dependency
147 *
148 * @throws \InvalidArgumentException if given id no or otherwise invalid
149 */
150 protected function id($id)
151 {
152 if (\preg_match('~^[a-z][A-z0-9-]*$~', $id) !== 1) {
153 throw new \InvalidArgumentException("Invalid id '{$id}' given.");
154 }
155
156 $this->id = $id;
157
158 return $this;
159 }
160
161 /**
162 * @inheritDoc
163 * @return static
164 */
165 public static function create($id)
166 {
167 return (new static())->id($id);
168 }
169 }