2 declare(strict_types=1);
3 namespace wcf\system\form\builder\field\dependency;
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-2018 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 dependency
37 * name of the template containing the dependency JavaScript code
40 protected $templateName;
45 public function dependentNode(IFormNode $node): IFormFieldDependency {
46 $this->__dependentNode = $node;
54 public function field(IFormField $field): IFormFieldDependency {
55 $this->__field = $field;
63 public function getDependentNode(): IFormNode {
64 if ($this->__dependentNode === null) {
65 throw new \BadMethodCallException("Dependent node has not been set.");
68 return $this->__dependentNode;
74 public function getField(): IFormField {
75 if ($this->__field === null) {
76 throw new \BadMethodCallException("Field has not been set.");
79 return $this->__field;
85 public function getId(): string {
92 public function getHtml(): string {
93 if ($this->templateName === null) {
94 throw new \LogicException("Template name is not set.");
97 return WCF::getTPL()->fetch($this->templateName, 'wcf', [
103 * Sets the id of this dependency and returns this dependency.
105 * @param string $id id of the dependency
106 * @return static $this this dependency
108 * @throws \InvalidArgumentException if given id no string or otherwise invalid
110 protected function id(string $id): IFormFieldDependency {
111 if (preg_match('~^[a-z][A-z0-9-]*$~', $id) !== 1) {
112 throw new \InvalidArgumentException("Invalid id '{$id}' given.");
124 public static function create(string $id): IFormFieldDependency {
125 return (new static)->id($id);