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;
9 * Abstract implementation of a form field dependency.
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
17 abstract class AbstractFormFieldDependency implements IFormFieldDependency {
19 * node whose availability depends on the value of a field
22 protected $dependentNode;
25 * field the availability of the node dependents on
31 * id of the field the availability of the node dependents on
37 * id of the dependency
43 * name of the template containing the dependency JavaScript code
46 protected $templateName;
51 public function dependentNode(IFormNode $node) {
52 $this->dependentNode = $node;
60 public function field(IFormField $field) {
61 $this->field = $field;
69 public function fieldId($fieldId) {
70 if ($this->getField() !== null) {
71 throw new \BadMethodCallException("Cannot set field id after field has been set.");
74 $this->fieldId = $fieldId;
82 public function getDependentNode() {
83 if ($this->dependentNode === null) {
84 throw new \BadMethodCallException("Dependent node has not been set.");
87 return $this->dependentNode;
93 public function getField() {
100 public function getFieldId() {
101 if ($this->getField() !== null) {
102 return $this->getField()->getId();
105 if ($this->fieldId === null) {
106 throw new \BadMethodCallException("Neither the field nor the field id has been set.");
109 return $this->fieldId;
115 public function getId() {
122 public function getHtml() {
123 if ($this->templateName === null) {
124 throw new \LogicException("Template name is not set.");
127 return WCF::getTPL()->fetch($this->templateName, 'wcf', [
128 'dependency' => $this
133 * Sets the id of this dependency and returns this dependency.
135 * @param string $id id of the dependency
136 * @return static $this this dependency
138 * @throws \InvalidArgumentException if given id no or otherwise invalid
140 protected function id($id) {
141 if (preg_match('~^[a-z][A-z0-9-]*$~', $id) !== 1) {
142 throw new \InvalidArgumentException("Invalid id '{$id}' given.");
154 public static function create($id) {
155 return (new static)->id($id);