Code Duplication    Length = 78-78 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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