Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\DomCrawler\Field; Chris@0: Chris@0: /** Chris@0: * FormField is the abstract class for all form fields. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: abstract class FormField Chris@0: { Chris@0: /** Chris@0: * @var \DOMElement Chris@0: */ Chris@0: protected $node; Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $name; Chris@0: /** Chris@0: * @var string Chris@0: */ Chris@0: protected $value; Chris@0: /** Chris@0: * @var \DOMDocument Chris@0: */ Chris@0: protected $document; Chris@0: /** Chris@0: * @var \DOMXPath Chris@0: */ Chris@0: protected $xpath; Chris@0: /** Chris@0: * @var bool Chris@0: */ Chris@0: protected $disabled; Chris@0: Chris@0: /** Chris@0: * @param \DOMElement $node The node associated with this field Chris@0: */ Chris@0: public function __construct(\DOMElement $node) Chris@0: { Chris@0: $this->node = $node; Chris@0: $this->name = $node->getAttribute('name'); Chris@0: $this->xpath = new \DOMXPath($node->ownerDocument); Chris@0: Chris@0: $this->initialize(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the label tag associated to the field or null if none. Chris@0: * Chris@0: * @return \DOMElement|null Chris@0: */ Chris@0: public function getLabel() Chris@0: { Chris@0: $xpath = new \DOMXPath($this->node->ownerDocument); Chris@0: Chris@0: if ($this->node->hasAttribute('id')) { Chris@0: $labels = $xpath->query(sprintf('descendant::label[@for="%s"]', $this->node->getAttribute('id'))); Chris@0: if ($labels->length > 0) { Chris@0: return $labels->item(0); Chris@0: } Chris@0: } Chris@0: Chris@0: $labels = $xpath->query('ancestor::label[1]', $this->node); Chris@0: if ($labels->length > 0) { Chris@0: return $labels->item(0); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the name of the field. Chris@0: * Chris@0: * @return string The name of the field Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return $this->name; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the value of the field. Chris@0: * Chris@0: * @return string|array The value of the field Chris@0: */ Chris@0: public function getValue() Chris@0: { Chris@0: return $this->value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the value of the field. Chris@0: * Chris@0: * @param string $value The value of the field Chris@0: */ Chris@0: public function setValue($value) Chris@0: { Chris@0: $this->value = (string) $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns true if the field should be included in the submitted values. Chris@0: * Chris@0: * @return bool true if the field should be included in the submitted values, false otherwise Chris@0: */ Chris@0: public function hasValue() Chris@0: { Chris@0: return true; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Check if the current field is disabled. Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function isDisabled() Chris@0: { Chris@0: return $this->node->hasAttribute('disabled'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the form field. Chris@0: */ Chris@0: abstract protected function initialize(); Chris@0: }