3 namespace wcf\system\form\builder\field\dependency;
5 use wcf\system\form\builder\field\IFormField;
6 use wcf\system\form\builder\IFormNode;
10 * Abstract implementation of a form field dependency.
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
18 abstract class AbstractFormFieldDependency implements IFormFieldDependency
21 * node whose availability depends on the value of a field
24 protected $dependentNode;
27 * field the availability of the node dependents on
33 * id of the field the availability of the node dependents on
39 * id of the dependency
45 * name of the template containing the dependency JavaScript code
48 protected $templateName;
53 public function dependentNode(IFormNode $node)
55 $this->dependentNode = $node;
63 public function field(IFormField $field)
65 $this->field = $field;
73 public function fieldId($fieldId)
75 if ($this->getField() !== null) {
76 throw new \BadMethodCallException("Cannot set field id after field has been set.");
79 $this->fieldId = $fieldId;
87 public function getDependentNode()
89 if ($this->dependentNode === null) {
90 throw new \BadMethodCallException("Dependent node has not been set.");
93 return $this->dependentNode;
99 public function getField()
107 public function getFieldId()
109 if ($this->getField() !== null) {
110 return $this->getField()->getId();
113 if ($this->fieldId === null) {
114 throw new \BadMethodCallException("Neither the field nor the field id has been set.");
117 return $this->fieldId;
123 public function getId()
131 public function getHtml()
133 if ($this->templateName === null) {
134 throw new \LogicException("Template name is not set.");
137 return WCF::getTPL()->fetch($this->templateName, 'wcf', [
138 'dependency' => $this,
143 * Sets the id of this dependency and returns this dependency.
145 * @param string $id id of the dependency
146 * @return static $this this dependency
148 * @throws \InvalidArgumentException if given id no or otherwise invalid
150 protected function id($id)
152 if (\preg_match('~^[a-z][A-z0-9-]*$~', $id) !== 1) {
153 throw new \InvalidArgumentException("Invalid id '{$id}' given.");
165 public static function create($id)
167 return (new static())->id($id);