Str::to_s()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bavix\AdvancedHtmlDom;
4
5
class Str
6
{
7
    /**
8
     * @var
9
     */
10
    protected $text;
11
12
    /**
13
     * Str constructor.
14
     *
15
     * @param $str
16
     */
17
    public function __construct($str)
18
    {
19
        $this->text = $str;
20
    }
21
22
    /**
23
     * @param     $regex
24
     * @param int $group_number
25
     *
26
     * @return bool
27
     * @see https://github.com/hashsup/advanced_html_dom/commit/10fea1345cd1f096a3199e2760ad06e4931007f6
28
     */
29
    public function match($regex, $group_number = 0)
30
    {
31
        if (!\preg_match($regex, $this->text, $matches)) {
32
            return false;
33
        }
34
35
        if (!\array_key_exists($group_number, $matches)) {
36
            return false;
37
        }
38
39
        return $matches[$group_number];
40
    }
41
42
    /**
43
     * @param     $regex
44
     * @param int $group_number
45
     *
46
     * @return mixed
47
     * @see https://github.com/hashsup/advanced_html_dom/commit/10fea1345cd1f096a3199e2760ad06e4931007f6
48
     */
49
    public function scan($regex, $group_number = 0)
50
    {
51
        if (!\preg_match_all($regex, $this->text, $matches)) {
52
            return false;
53
        }
54
55
        if (!\array_key_exists($group_number, $matches)) {
56
            return false;
57
        }
58
59
        return $matches[$group_number];
60
    }
61
62
    /**
63
     * @param $regex
64
     * @param $replacement
65
     *
66
     * @return Str
67
     */
68
    public function sub($regex, $replacement)
69
    {
70
        $val = $this->gsub($regex, $replacement, 1);
71
72
        return new Str($val);
73
    }
74
75
    /**
76
     * @param     $regex
77
     * @param     $replacement
78
     * @param int $limit
79
     *
80
     * @return Str
81
     */
82
    public function gsub($regex, $replacement, $limit = -1)
83
    {
84
        if ($replacement instanceof \Closure) {
85
            $val = \preg_replace_callback($regex, $replacement, $this->text, $limit);
86
        } else {
87
            $val = \preg_replace($regex, $replacement, $this->text, $limit);
88
        }
89
90
        return new Str($val);
91
    }
92
93
    /**
94
     * @param     $regex
95
     * @param int $limit
96
     *
97
     * @return array
98
     */
99
    public function split($regex, $limit = -1)
100
    {
101
        return \preg_split($regex, $this->text, $limit);
102
    }
103
104
    /**
105
     * @return string
106
     */
107
    public function __toString()
108
    {
109
        return (string)$this->text;
110
    }
111
112
    /**
113
     * @return mixed
114
     */
115
    public function to_s()
116
    {
117
        return $this->text;
118
    }
119
120
}
121