2 namespace wcf\system\form\builder\field\dependency;
3 use wcf\system\form\builder\field\IFormField;
4 use wcf\system\form\builder\IFormNode;
8 * Abstract implementation of a form field dependency.
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
16 abstract class AbstractFormFieldDependency implements IFormFieldDependency {
18 * node whose availability depends on the value of a field
21 protected $__dependentNode;
24 * field the availability of the node dependents on
30 * id of the dependency
36 * name of the template containing the dependency JavaScript code
39 protected $templateName;
44 public function dependentNode(IFormNode $node) {
45 $this->__dependentNode = $node;
53 public function field(IFormField $field) {
54 $this->__field = $field;
62 public function getDependentNode() {
63 if ($this->__dependentNode === null) {
64 throw new \BadMethodCallException("Dependent node has not been set.");
67 return $this->__dependentNode;
73 public function getField() {
74 if ($this->__field === null) {
75 throw new \BadMethodCallException("Field has not been set.");
78 return $this->__field;
84 public function getId() {
91 public function getHtml() {
92 if ($this->templateName === null) {
93 throw new \LogicException("Template name is not set.");
96 return WCF::getTPL()->fetch($this->templateName, 'wcf', [
102 * Sets the id of this dependency and returns this dependency.
104 * @param string $id id of the dependency
105 * @return static $this this dependency
107 * @throws \InvalidArgumentException if given id no or otherwise invalid
109 protected function id($id) {
110 if (preg_match('~^[a-z][A-z0-9-]*$~', $id) !== 1) {
111 throw new \InvalidArgumentException("Invalid id '{$id}' given.");
123 public static function create($id) {
124 return (new static)->id($id);