PrefixSuffix::getSuffix()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cecil\Collection\Page;
15
16
use Cecil\Config;
17
18
/**
19
 * PrefixSuffix class.
20
 *
21
 * Handles prefixes and suffixes in page filenames.
22
 * Prefixes can be dates or numbers, and suffixes are typically language codes.
23
 */
24
class PrefixSuffix
25
{
26
    // https://regex101.com/r/tJWUrd/6
27
    // ie: "blog/2017-10-19_post-1.md" prefix is "2017-10-19"
28
    // ie: "projet/1-projet-a.md" prefix is "1"
29
    public const PREFIX_PATTERN = '(|.*\/)(([0-9]{4})-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])|[0-9]+)(-|_)(.*)';
30
31
    // https://regex101.com/r/GlgBdT/7
32
    // ie: "blog/2017-10-19_post-1.en.md" suffix is "en"
33
    // ie: "projet/1-projet-a.fr-FR.md" suffix is "fr-FR"
34
    public const SUFFIX_PATTERN = '(.*)\.' . Config::LANG_CODE_PATTERN;
35
36
    /**
37
     * Returns true if the string contains a prefix or a suffix.
38
     */
39
    protected static function has(string $string, string $type): bool
40
    {
41
        return (bool) preg_match('/^' . self::getPattern($type) . '$/', $string);
42
    }
43
44
    /**
45
     * Returns true if the string contains a prefix.
46
     */
47
    public static function hasPrefix(string $string): bool
48
    {
49
        return self::has($string, 'prefix');
50
    }
51
52
    /**
53
     * Returns true if the string contains a suffix.
54
     */
55
    public static function hasSuffix(string $string): bool
56
    {
57
        return self::has($string, 'suffix');
58
    }
59
60
    /**
61
     * Returns the prefix or the suffix if exists.
62
     */
63
    protected static function get(string $string, string $type): ?string
64
    {
65
        if (self::has($string, $type)) {
66
            preg_match('/^' . self::getPattern($type) . '$/', $string, $matches);
67
            switch ($type) {
68
                case 'prefix':
69
                    return $matches[2];
70
                case 'suffix':
71
                    return $matches[2];
72
            }
73
        }
74
75
        return null;
76
    }
77
78
    /**
79
     * Returns the prefix if exists.
80
     */
81
    public static function getPrefix(string $string): ?string
82
    {
83
        return self::get($string, 'prefix');
84
    }
85
86
    /**
87
     * Returns the suffix if exists.
88
     */
89
    public static function getSuffix(string $string): ?string
90
    {
91
        return self::get($string, 'suffix');
92
    }
93
94
    /**
95
     * Returns string without the prefix and the suffix (if exists).
96
     */
97
    public static function sub(string $string): string
98
    {
99
        if (self::hasPrefix($string)) {
100
            preg_match('/^' . self::getPattern('prefix') . '$/', $string, $matches);
101
102
            $string = $matches[1] . $matches[7];
103
        }
104
        if (self::hasSuffix($string)) {
105
            preg_match('/^' . self::getPattern('suffix') . '$/', $string, $matches);
106
107
            $string = $matches[1];
108
        }
109
110
        return $string;
111
    }
112
113
    /**
114
     * Returns string without the prefix (if exists).
115
     */
116
    public static function subPrefix(string $string): string
117
    {
118
        if (self::hasPrefix($string)) {
119
            preg_match('/^' . self::getPattern('prefix') . '$/', $string, $matches);
120
121
            return $matches[1] . $matches[7];
122
        }
123
124
        return $string;
125
    }
126
127
    /**
128
     * Returns expreg pattern by $type.
129
     *
130
     * @throws \InvalidArgumentException
131
     */
132
    protected static function getPattern(string $type): string
133
    {
134
        switch ($type) {
135
            case 'prefix':
136
                return self::PREFIX_PATTERN;
137
            case 'suffix':
138
                return self::SUFFIX_PATTERN;
139
            default:
140
                throw new \InvalidArgumentException('Argument must be "prefix" or "suffix".');
141
        }
142
    }
143
}
144