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