Passed
Pull Request — Showing-Posts (#54)
by Stone
04:55 queued 02:28
created

StringFunctions::endsWith()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Core\Traits;
4
5
use Core\Constant;
6
use Twig\Error\Error;
7
8
/**
9
 * a trait with some string related helpers
10
 * Trait StringFunctions
11
 * @package Core\Traits
12
 */
13
trait StringFunctions
14
{
15
    /**
16
     * does a haystack start with a needle ?
17
     * @param $haystack string the string to search in
18
     * @param $needle string the string to search for
19
     * @return bool
20
     */
21
    public function startsWith(string $haystack, string $needle): bool
22
    {
23
        $length = strlen($needle);
24
        return (substr($haystack, 0, $length) === $needle);
25
    }
26
27
    /**
28
     * Does a haystack end with a needle
29
     * @param $haystack string the string to search in
30
     * @param $needle string the string to search for
31
     * @return bool
32
     */
33
    public function endsWith(string $haystack, string $needle): bool
34
    {
35
        $length = strlen($needle);
36
        if ($length == 0) {
37
            return true;
38
        }
39
40
        return (substr($haystack, -$length) === $needle);
41
    }
42
43
    /**
44
     * Remove the tail of a string
45
     * @param $string string to slice apart
46
     * @param $tail string the string to remove
47
     * @return string
48
     */
49
    public function removeFromEnd(string $string, string $tail): string
50
    {
51
        if ($this->endsWith($string, $tail)) {
52
            $string = substr($string, 0, -strlen($tail));
53
        }
54
        return $string;
55
    }
56
57
    /**
58
     * remove some characters from the front of the string
59
     * @param string $string the string to be decapitated
60
     * @param string $head the head to be cut off ("OFF WITH HIS HEAD")
61
     * @return string
62
     */
63
    public function removeFromBeginning(string $string, string $head): string
64
    {
65
        if ($this->startsWith($string, $head)) {
66
            $string = substr($string, strlen($head));
67
        }
68
        return $string;
69
    }
70
71
    /**
72
     * create an excerpt, shortening the text to a specific number of words
73
     * @param string $text the text to shorten
74
     * @param int $count number of words
75
     * @return string the shortend text
76
     * @throws \ErrorException
77
     */
78
    public function getExcerpt(string $text, int $count = Constant::EXCERPT_WORD_COUNT)
79
    {
80
        if ($count < 1) {
81
            throw new \ErrorException('excerpt length too low');
82
        }
83
        $text = str_replace("  ", " ", $text);
84
85
        //Searching for the page break tag
86
        $breakTagPosition = strpos($text, "<!-- EndOfExcerptBlogOc -->");
87
        if($breakTagPosition > 0){
88
            return $this->completeDom(substr($text, 0, $breakTagPosition));
89
        }
90
91
        //exploding on space except for in img, p and span.
92
        $string = preg_split('/(<img[^>]+\>)|(<p[^>]+\>)|(<span[^>]+\>)|\s/', $text, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
93
94
        //The preg split can return false, probably will never happen but just in case.
95
        if(!$string)
96
        {
97
            throw new \Error("excerpt generation failed");
98
        }
99
100
        if (count($string) <= $count) {
101
            return $text;
102
        }
103
        $trimed = '';
104
        for ($wordCounter = 0; $wordCounter < $count; $wordCounter++) {
105
            $trimed .= $string[$wordCounter];
106
            if ($wordCounter < $count - 1) {
107
                $trimed .= " ";
108
            } else {
109
                $trimed .= "[...]";
110
            }
111
        }
112
113
        return $this->completeDom($trimed);
114
    }
115
116
    /**
117
     * Close the dom in given $text
118
     * @param string $text unclean html
119
     * @return string cleaned up html
120
     */
121
    private function completeDom(string $text): string
122
    {
123
        //grabbed from https://gist.github.com/JayWood/348752b568ecd63ae5ce#gistcomment-2310550
124
        libxml_use_internal_errors(true);
125
126
        $dom = new \DOMDocument;
127
        $dom->loadHTML($text);
128
129
        // Strip wrapping <html> and <body> tags
130
        $mock = new \DOMDocument;
131
        $body = $dom->getElementsByTagName('body')->item(0);
132
        foreach ($body->childNodes as $child) {
133
            $mock->appendChild($mock->importNode($child, true));
134
        }
135
136
        return trim($mock->saveHTML());
137
    }
138
139
    /**
140
     * check passed string, returns true if the string contains alphaNum or _ or -. use for checking database tables, column names or slugs
141
     * @param string $string the string to analyse
142
     * @return bool
143
     */
144
    public function isAlphaNum(string $string): bool
145
    {
146
        if (preg_match("/^[A-Za-z0-9_-]+$/", $string)) {
147
            return true;
148
        }
149
        return false;
150
    }
151
152
    /**
153
     * check if each string in passed array is valid
154
     * @param array $strings
155
     * @return bool
156
     */
157
    public function isAllAlphaNum(array $strings):bool
158
    {
159
        $result = true;
160
        foreach ($strings as $string)
161
        {
162
            if(!$this->isAlphaNum($string))
163
            {
164
                $result = false;
165
            }
166
        }
167
        return $result;
168
    }
169
}