Text::determineEndingWhitespace()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Kevintweber\HtmlTokenizer\Tokens;
4
5
class Text extends AbstractToken
6
{
7
    /**
8
     * Constructor
9
     *
10
     * @param Token|null $parent        The parent Token
11
     * @param bool       $throwOnError
12
     * @param string     $forcedValue   For when contents could be parsed but will be interpreted as TEXT instead.
13
     *                                  When forcing a value, do not call parse.
14
     */
15 32
    public function __construct(Token $parent = null, bool $throwOnError = true, string $forcedValue = '')
16
    {
17 32
        parent::__construct(Token::TEXT, $parent, $throwOnError);
18
19 32
        $this->value = $forcedValue;
20 32
        if ($forcedValue !== '') {
21 7
            $this->setTokenPosition($forcedValue);
22
        }
23 32
    }
24
25 25
    public function parse(string $html) : string
26
    {
27 25
        $this->setTokenPosition($html);
28 25
        $startingWhitespace = $this->determineStartingWhitespace($html);
29
30 25
        $posOfNextElement = mb_strpos($html, '<');
31 25
        if ($posOfNextElement === false) {
32 3
            $this->value = $startingWhitespace . trim($html);
33
34 3
            return '';
35
        }
36
37
        // Find full length of TEXT.
38 22
        $text = mb_substr($html, 0, $posOfNextElement);
39 22
        if (trim($text) === '') {
40 3
            $this->value = ' ';
41
42 3
            return mb_substr($html, $posOfNextElement);
43
        }
44
45 21
        $endingWhitespace = $this->determineEndingWhitespace($text);
46 21
        $this->value = $startingWhitespace . trim($text) . $endingWhitespace;
47
48 21
        return mb_substr($html, $posOfNextElement);
49
    }
50
51 14
    public function toArray() : array
52
    {
53
        return array(
54 14
            'type' => 'text',
55 14
            'value' => $this->value,
56 14
            'line' => $this->getLine(),
57 14
            'position' => $this->getPosition()
58
        );
59
    }
60
61
    /**
62
     * If whitespace exists at the start of the HTML, collapse it to a single space.
63
     *
64
     * @param string $html
65
     *
66
     * @return string
67
     */
68 25
    private function determineStartingWhitespace(string $html) : string
69
    {
70 25
        $startingWhitespace = '';
71 25
        if (preg_match("/(^\s)/", $html) === 1) {
72 6
            $startingWhitespace = ' ';
73
        }
74
75 25
        return $startingWhitespace;
76
    }
77
78
    /**
79
     * If whitespace exists at the end of the HTML, collapse it to a single space.
80
     *
81
     * @param string $text
82
     *
83
     * @return string
84
     */
85 21
    private function determineEndingWhitespace(string $text) : string
86
    {
87 21
        $endingWhitespace = '';
88 21
        if (preg_match("/(\s$)/", $text) === 1) {
89 8
            $endingWhitespace = ' ';
90
        }
91
92 21
        return $endingWhitespace;
93
    }
94
}
95