Code Duplication    Length = 78-78 lines in 2 locations

src/voku/helper/SimpleHtmlDom.php 1 location

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

src/voku/helper/SimpleXmlDom.php 1 location

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