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