friendship ended with social-app. php is my new best friend
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\DomCrawler\Field;
13
14/**
15 * FormField is the abstract class for all form fields.
16 *
17 * @author Fabien Potencier <fabien@symfony.com>
18 */
19abstract class FormField
20{
21 protected string $name;
22 protected string|array|null $value = null;
23 protected \DOMDocument $document;
24 protected \DOMXPath $xpath;
25 protected bool $disabled = false;
26
27 /**
28 * @param \DOMElement $node The node associated with this field
29 */
30 public function __construct(
31 protected \DOMElement $node,
32 ) {
33 $this->name = $node->getAttribute('name');
34 $this->xpath = new \DOMXPath($node->ownerDocument);
35
36 $this->initialize();
37 }
38
39 /**
40 * Returns the label tag associated to the field or null if none.
41 */
42 public function getLabel(): ?\DOMElement
43 {
44 $xpath = new \DOMXPath($this->node->ownerDocument);
45
46 if ($this->node->hasAttribute('id')) {
47 $labels = $xpath->query(\sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id')));
48 if ($labels->length > 0) {
49 return $labels->item(0);
50 }
51 }
52
53 $labels = $xpath->query('ancestor::label[1]', $this->node);
54
55 return $labels->length > 0 ? $labels->item(0) : null;
56 }
57
58 /**
59 * Returns the name of the field.
60 */
61 public function getName(): string
62 {
63 return $this->name;
64 }
65
66 /**
67 * Gets the value of the field.
68 */
69 public function getValue(): string|array|null
70 {
71 return $this->value;
72 }
73
74 /**
75 * Sets the value of the field.
76 */
77 public function setValue(?string $value): void
78 {
79 $this->value = $value ?? '';
80 }
81
82 /**
83 * Returns true if the field should be included in the submitted values.
84 */
85 public function hasValue(): bool
86 {
87 return true;
88 }
89
90 /**
91 * Check if the current field is disabled.
92 */
93 public function isDisabled(): bool
94 {
95 return $this->node->hasAttribute('disabled');
96 }
97
98 /**
99 * Initializes the form field.
100 */
101 abstract protected function initialize(): void;
102}