StringHelper::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Subreality\Dilmun\Anshar\Utils;
3
4
class StringHelper
5
{
6
    protected $string;
7
8
    /**
9
     * StringHelper constructor. Accepts and stores a string.
10
     *
11
     * @throws \InvalidArgumentException if not initialized with a string
12
     *
13
     * @param string $string    The string needing help
14
     */
15 77
    public function __construct($string)
16
    {
17 77
        if (!is_string($string)) {
18 6
            throw new \InvalidArgumentException("Supplied string is not a string");
19
        }
20
21 71
        $this->string = $string;
22 71
    }
23
24 24
    public function __toString()
25
    {
26 24
        return $this->string;
27
    }
28
29
    /**
30
     * Given a delimited string, returns a copy of the string with chunks defined by the delimiter affected by a given
31
     * function.
32
     *
33
     * The supplied function must be callable and must accept a string as its only required parameter
34
     *
35
     * @param callable $function    The function to be called against the delimited chunks; must be callable and must
36
     *                              accept a string as its only parameter
37
     * @param string[] $delimiters  One or more strings delimiting the chunks
0 ignored issues
show
Documentation introduced by
Should the type for parameter $delimiters not be string[][]?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
38
     *
39
     * @return string               A delimited string with chunks affected by the supplied function
40
     */
41 35
    public function affectChunks(callable $function, ...$delimiters)
42
    {
43 35
        $delimiter_string = "";
44 35
        $replace_callback = function ($matches) use ($function) {
45
46 35
            $callback_string = $function($matches[0]);
47
48 35
            return $callback_string;
49 35
        };
50
51 35
        foreach ($delimiters as $delimiter) {
52 35
            $delimiter_string .= preg_quote($delimiter, "/");
53 35
        }
54
55 35
        $pattern = "/[^{$delimiter_string}]+/";
56
57 35
        $affected_string = preg_replace_callback($pattern, $replace_callback, $this->string);
58
59 35
        return $affected_string;
60
    }
61
62
    /**
63
     * Determines whether the string starts with a given string.
64
     *
65
     * @param string $string    A string the string may or may not start with
66
     *
67
     * @return bool             Returns true if the string starts with the given string
68
     *                          Returns false if the string does not start with the given string
69
     */
70 26
    public function startsWith($string)
71
    {
72 26
        $pattern = "/^" . preg_quote($string, "/") . "/";
73
74 26
        $starts_with_string = preg_match($pattern, $this->string);
75
76 26
        return (bool) $starts_with_string;
77
    }
78
79
    /**
80
     * Given a string, collapses any repetition at the start of the string into a single instance of the given string.
81
     * If the given string does not exist at the start of the string, returns an unmodified string.
82
     *
83
     * For example, if the StringHelper was constructed with "ffoo", StringHelper::collapseStartingRepetition("f") will
84
     * return "foo". If the StringHelper was constructed with "foo", StringHelper::collapseStartingRepetition("b") will
85
     * also return "foo".
86
     *
87
     * @param string $string    A string the string may or may not start with, with or without repetition
88
     *
89
     * @return string           Returns a potentially modified string with a repeating, starting string collapsed into
90
     *                          a single instance of the string
91
     */
92 8
    public function collapseStartingRepetition($string)
93
    {
94 8
        $matches = array();
95 8
        $pattern = "/^[" . preg_quote($string, "/") . "]+(?P<remainder>.*)/";
96
97 8
        $starts_with_string = preg_match($pattern, $this->string, $matches);
98
99 8
        if ($starts_with_string) {
100 5
            $collapsed_string = $string . $matches["remainder"];
101
102 5
            return $collapsed_string;
103
        } else {
104 3
            return $this->string;
105
        }
106
    }
107
}
108