Code Duplication    Length = 78-78 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

@@ 697-774 (lines=78) @@
694
     *
695
     * @return string|string[]|null
696
     */
697
    public function val($value = null)
698
    {
699
        if ($value === null) {
700
            if (
701
                $this->tag === 'input'
702
                &&
703
                (
704
                    $this->getAttribute('type') === 'hidden'
705
                    ||
706
                    $this->getAttribute('type') === 'text'
707
                    ||
708
                    !$this->hasAttribute('type')
709
                )
710
            ) {
711
                return $this->getAttribute('value');
712
            }
713
714
            if (
715
                $this->hasAttribute('checked')
716
                &&
717
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
718
            ) {
719
                return $this->getAttribute('value');
720
            }
721
722
            if ($this->node->nodeName === 'select') {
723
                $valuesFromDom = [];
724
                $options = $this->getElementsByTagName('option');
725
                if ($options instanceof SimpleHtmlDomNode) {
726
                    foreach ($options as $option) {
727
                        if ($this->hasAttribute('checked')) {
728
                            /** @noinspection UnnecessaryCastingInspection */
729
                            $valuesFromDom[] = (string) $option->getAttribute('value');
730
                        }
731
                    }
732
                }
733
734
                if (\count($valuesFromDom) === 0) {
735
                    return null;
736
                }
737
738
                return $valuesFromDom;
739
            }
740
741
            if ($this->node->nodeName === 'textarea') {
742
                return $this->node->nodeValue;
743
            }
744
        } else {
745
            /** @noinspection NestedPositiveIfStatementsInspection */
746
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
747
                if ($value === $this->getAttribute('value')) {
748
                    /** @noinspection UnusedFunctionResultInspection */
749
                    $this->setAttribute('checked', 'checked');
750
                } else {
751
                    /** @noinspection UnusedFunctionResultInspection */
752
                    $this->removeAttribute('checked');
753
                }
754
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
755
                foreach ($this->node->getElementsByTagName('option') as $option) {
756
                    /** @var \DOMElement $option */
757
                    if ($value === $option->getAttribute('value')) {
758
                        /** @noinspection UnusedFunctionResultInspection */
759
                        $option->setAttribute('selected', 'selected');
760
                    } else {
761
                        $option->removeAttribute('selected');
762
                    }
763
                }
764
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
765
                // Set value for input elements
766
                /** @noinspection UnusedFunctionResultInspection */
767
                $this->setAttribute('value', $value);
768
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
769
                $this->node->nodeValue = $value;
770
            }
771
        }
772
773
        return null;
774
    }
775
776
    /**
777
     * @param HtmlDomParser $newDocument

src/voku/helper/SimpleXmlDom.php 1 location

@@ 678-755 (lines=78) @@
675
     *
676
     * @return string|string[]|null
677
     */
678
    public function val($value = null)
679
    {
680
        if ($value === null) {
681
            if (
682
                $this->tag === 'input'
683
                &&
684
                (
685
                    $this->getAttribute('type') === 'hidden'
686
                    ||
687
                    $this->getAttribute('type') === 'text'
688
                    ||
689
                    !$this->hasAttribute('type')
690
                )
691
            ) {
692
                return $this->getAttribute('value');
693
            }
694
695
            if (
696
                $this->hasAttribute('checked')
697
                &&
698
                \in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)
699
            ) {
700
                return $this->getAttribute('value');
701
            }
702
703
            if ($this->node->nodeName === 'select') {
704
                $valuesFromDom = [];
705
                $options = $this->getElementsByTagName('option');
706
                if ($options instanceof SimpleXmlDomNode) {
707
                    foreach ($options as $option) {
708
                        if ($this->hasAttribute('checked')) {
709
                            /** @noinspection UnnecessaryCastingInspection */
710
                            $valuesFromDom[] = (string) $option->getAttribute('value');
711
                        }
712
                    }
713
                }
714
715
                if (\count($valuesFromDom) === 0) {
716
                    return null;
717
                }
718
719
                return $valuesFromDom;
720
            }
721
722
            if ($this->node->nodeName === 'textarea') {
723
                return $this->node->nodeValue;
724
            }
725
        } else {
726
            /** @noinspection NestedPositiveIfStatementsInspection */
727
            if (\in_array($this->getAttribute('type'), ['checkbox', 'radio'], true)) {
728
                if ($value === $this->getAttribute('value')) {
729
                    /** @noinspection UnusedFunctionResultInspection */
730
                    $this->setAttribute('checked', 'checked');
731
                } else {
732
                    /** @noinspection UnusedFunctionResultInspection */
733
                    $this->removeAttribute('checked');
734
                }
735
            } elseif ($this->node instanceof \DOMElement && $this->node->nodeName === 'select') {
736
                foreach ($this->node->getElementsByTagName('option') as $option) {
737
                    /** @var \DOMElement $option */
738
                    if ($value === $option->getAttribute('value')) {
739
                        /** @noinspection UnusedFunctionResultInspection */
740
                        $option->setAttribute('selected', 'selected');
741
                    } else {
742
                        $option->removeAttribute('selected');
743
                    }
744
                }
745
            } elseif ($this->node->nodeName === 'input' && \is_string($value)) {
746
                // Set value for input elements
747
                /** @noinspection UnusedFunctionResultInspection */
748
                $this->setAttribute('value', $value);
749
            } elseif ($this->node->nodeName === 'textarea' && \is_string($value)) {
750
                $this->node->nodeValue = $value;
751
            }
752
        }
753
754
        return null;
755
    }
756
757
    /**
758
     * Retrieve an external iterator.