Completed
Push — internationalization ( 1dc6e7...bf1b4f )
by Arnaud
01:41
created

PrefixSuffix::getSuffix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
/*
3
 * Copyright (c) Arnaud Ligny <[email protected]>
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Cecil\Collection\Page;
10
11
/**
12
 * Class PrefixSuffix.
13
 */
14
class PrefixSuffix
0 ignored issues
show
Coding Style introduced by
PrefixSuffix does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
15
{
16
    // https://regex101.com/r/tJWUrd/6
17
    // ie: "blog/2017-10-19-post-1.md" prefix is "2017-10-19"
18
    // ie: "projet/1-projet-a.md" prefix is "1"
19
    const PREFIX_PATTERN = '^(|.*\/)(([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])|[0-9]+)(-|_|\.)(.*)$';
20
    // https://regex101.com/r/GlgBdT/7
21
    // ie: "blog/2017-10-19-post-1.en.md" suffix is "en"
22
    // ie: "projet/1-projet-a.fr-FR.md" suffix is "fr-FR"
23
    const SUFFIX_PATTERN = '^(.*)\.([a-z]{2}(-[A-Z]{2})?)$';
24
25
    /**
26
     * Return true if the string contains a prefix or a suffix.
27
     *
28
     * @param string $string
29
     * @param string $type
30
     *
31
     * @return bool
32
     */
33
    protected static function has(string $string, string $type): bool
34
    {
35
        return (bool) preg_match('/'.self::getPattern($type).'/', $string);
36
    }
37
38
    /**
39
     * Return true if the string contains a prefix.
40
     *
41
     * @param string $string
42
     *
43
     * @return bool
44
     */
45
    public static function hasPrefix(string $string): bool
46
    {
47
        return self::has($string, 'prefix');
48
    }
49
50
    /**
51
     * Return true if the string contains a suffix.
52
     *
53
     * @param string $string
54
     *
55
     * @return bool
56
     */
57
    public static function hasSuffix(string $string): bool
58
    {
59
        return self::has($string, 'suffix');
60
    }
61
62
    /**
63
     * Return the prefix or the suffix if exists.
64
     *
65
     * @param string $string
66
     * @param string $type
67
     *
68
     * @return string[]|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
69
     */
70
    protected static function get(string $string, string $type): ?string
71
    {
72
        if (self::has($string, $type)) {
73
            preg_match('/'.self::getPattern($type).'/', $string, $matches);
74
            switch ($type) {
75
                case 'prefix':
76
                    return $matches[2];
77
                case 'suffix':
78
                    return $matches[2];
79
            }
80
        }
81
82
        return null;
83
    }
84
85
    /**
86
     * Return the prefix if exists.
87
     *
88
     * @param string $string
89
     *
90
     * @return string[]|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
91
     */
92
    public static function getPrefix(string $string): ?string
93
    {
94
        return self::get($string, 'prefix');
95
    }
96
97
    /**
98
     * Return the suffix if exists.
99
     *
100
     * @param string $string
101
     *
102
     * @return string[]|null
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
103
     */
104
    public static function getSuffix(string $string): ?string
105
    {
106
        return self::get($string, 'suffix');
107
    }
108
109
    /**
110
     * Return string without the prefix and the suffix (if exists).
111
     *
112
     * @param string $string
113
     *
114
     * @return string
115
     */
116
    public static function sub(string $string): string
117
    {
118
        if (self::hasPrefix($string)) {
119
            preg_match('/'.self::getPattern('prefix').'/', $string, $matches);
120
121
            $string = $matches[1].$matches[7];
122
        }
123
        if (self::hasSuffix($string)) {
124
            preg_match('/'.self::getPattern('suffix').'/', $string, $matches);
125
126
            $string = $matches[1];
127
        }
128
129
        return $string;
130
    }
131
132
    /**
133
     * Return string without the prefix (if exists).
134
     *
135
     * @param string $string
136
     *
137
     * @return string
138
     */
139
    public static function subPrefix(string $string): string
140
    {
141
        if (self::hasPrefix($string)) {
142
            preg_match('/'.self::getPattern('prefix').'/', $string, $matches);
143
144
            return $matches[1].$matches[7];
145
        }
146
147
        return $string;
148
    }
149
150
    /**
151
     * Return expreg pattern by $type.
152
     *
153
     * @param string $type
154
     *
155
     * @return string
156
     */
157
    protected static function getPattern(string $type): string
158
    {
159
        switch ($type) {
160
            case 'prefix':
161
                return self::PREFIX_PATTERN;
162
            case 'suffix':
163
                return self::SUFFIX_PATTERN;
164
            default:
165
                throw new Exception(\sprintf('%s must be "prefix" or "suffix"', $type));
166
        }
167
    }
168
}
169