Passed
Pull Request — master (#1013)
by lee
07:38
created

PrefixSuffix::getPattern()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
1
<?php
2
/**
3
 * This file is part of the Cecil/Cecil package.
4
 *
5
 * Copyright (c) Arnaud Ligny <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Cecil\Collection\Page;
12
13
use Cecil\Exception\Exception;
14
15
/**
16
 * Class PrefixSuffix.
17
 */
18
class PrefixSuffix
19
{
20
    // https://regex101.com/r/tJWUrd/6
21
    // ie: "blog/2017-10-19-post-1.md" prefix is "2017-10-19"
22
    // ie: "projet/1-projet-a.md" prefix is "1"
23
    const PREFIX_PATTERN = '^(|.*\/)(([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])|[0-9]+)(-|_|\.)(.*)$';
24
    // https://regex101.com/r/GlgBdT/7
25
    // ie: "blog/2017-10-19-post-1.en.md" suffix is "en"
26
    // ie: "projet/1-projet-a.fr-FR.md" suffix is "fr-FR"
27
    const SUFFIX_PATTERN = '^(.*)\.([a-z]{2}(-[A-Z]{2})?)$';
28
29
    /**
30
     * Returns true if the string contains a prefix or a suffix.
31
     *
32
     * @param string $string
33
     * @param string $type
34
     *
35
     * @return bool
36
     */
37 1
    protected static function has(string $string, string $type): bool
38
    {
39 1
        return (bool) preg_match('/'.self::getPattern($type).'/', $string);
40
    }
41
42
    /**
43
     * Returns true if the string contains a prefix.
44
     *
45
     * @param string $string
46
     *
47
     * @return bool
48
     */
49 1
    public static function hasPrefix(string $string): bool
50
    {
51 1
        return self::has($string, 'prefix');
52
    }
53
54
    /**
55
     * Returns true if the string contains a suffix.
56
     *
57
     * @param string $string
58
     *
59
     * @return bool
60
     */
61 1
    public static function hasSuffix(string $string): bool
62
    {
63 1
        return self::has($string, 'suffix');
64
    }
65
66
    /**
67
     * Returns the prefix or the suffix if exists.
68
     *
69
     * @param string $string
70
     * @param string $type
71
     *
72
     * @return string|null
73
     */
74 1
    protected static function get(string $string, string $type): ?string
75
    {
76 1
        if (self::has($string, $type)) {
77 1
            preg_match('/'.self::getPattern($type).'/', $string, $matches);
78
            switch ($type) {
79 1
                case 'prefix':
80 1
                    return $matches[2];
81 1
                case 'suffix':
82 1
                    return $matches[2];
83
            }
84
        }
85
86
        return null;
87
    }
88
89
    /**
90
     * Returns the prefix if exists.
91
     *
92
     * @param string $string
93
     *
94
     * @return string[]|null
95
     */
96 1
    public static function getPrefix(string $string): ?string
97
    {
98 1
        return self::get($string, 'prefix');
99
    }
100
101
    /**
102
     * Returns the suffix if exists.
103
     *
104
     * @param string $string
105
     *
106
     * @return string[]|null
107
     */
108 1
    public static function getSuffix(string $string): ?string
109
    {
110 1
        return self::get($string, 'suffix');
111
    }
112
113
    /**
114
     * Returns string without the prefix and the suffix (if exists).
115
     *
116
     * @param string $string
117
     *
118
     * @return string
119
     */
120 1
    public static function sub(string $string): string
121
    {
122 1
        if (self::hasPrefix($string)) {
123 1
            preg_match('/'.self::getPattern('prefix').'/', $string, $matches);
124
125 1
            $string = $matches[1].$matches[7];
126
        }
127 1
        if (self::hasSuffix($string)) {
128 1
            preg_match('/'.self::getPattern('suffix').'/', $string, $matches);
129
130 1
            $string = $matches[1];
131
        }
132
133 1
        return $string;
134
    }
135
136
    /**
137
     * Returns string without the prefix (if exists).
138
     *
139
     * @param string $string
140
     *
141
     * @return string
142
     */
143 1
    public static function subPrefix(string $string): string
144
    {
145 1
        if (self::hasPrefix($string)) {
146 1
            preg_match('/'.self::getPattern('prefix').'/', $string, $matches);
147
148 1
            return $matches[1].$matches[7];
149
        }
150
151 1
        return $string;
152
    }
153
154
    /**
155
     * Returns expreg pattern by $type.
156
     *
157
     * @param string $type
158
     *
159
     * @throws Exception
160
     *
161
     * @return string
162
     */
163 1
    protected static function getPattern(string $type): string
164
    {
165
        switch ($type) {
166 1
            case 'prefix':
167 1
                return self::PREFIX_PATTERN;
168 1
            case 'suffix':
169 1
                return self::SUFFIX_PATTERN;
170
            default:
171
                throw new Exception(\sprintf('%s must be "prefix" or "suffix"', $type));
172
        }
173
    }
174
}
175