Passed
Push — Showing-Posts ( 4125ee...65a5c0 )
by Stone
02:18
created

StringFunctions::isAlphaNum()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
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
84
        $text = str_replace("  ", " ", $text);
85
        $string = explode(" ", $text);
86
        if (count($string) <= $count) {
87
            return $text;
88
        }
89
        $trimed = '';
90
        for ($wordCounter = 0; $wordCounter < $count; $wordCounter++) {
91
            //TODO Take into account the "read more" tag
92
            $trimed .= $string[$wordCounter];
93
            if ($wordCounter < $count - 1) {
94
                $trimed .= " ";
95
            } else {
96
                $trimed .= "...";
97
            }
98
        }
99
        $trimed = trim($trimed);
100
        return $trimed;
101
    }
102
103
    public function isAlphaNum(string $string): bool
104
    {
105
        if (preg_match("/^[A-Za-z0-9_-]+$/", $string)) {
106
            return true;
107
        }
108
        return false;
109
    }
110
}