Completed
Push — master ( 985338...fab147 )
by Beñat
03:54
created

FileName::fromHash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\File\Domain\Model;
14
15
use Ramsey\Uuid\Uuid;
16
17
/**
18
 * File name domain class.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 * @author Gorka Laucirica <[email protected]>
22
 */
23
final class FileName
24
{
25
    /**
26
     * The name.
27
     *
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * The file extension.
34
     *
35
     * @var string
36
     */
37
    private $extension;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param string $aName The name
43
     *
44
     * @throws FileNameInvalidException when given file name is invalid
45
     */
46
    public function __construct($aName)
47
    {
48
        if (null === $aName) {
49
            throw new FileNameInvalidException();
50
        }
51
        $this->name = $this->sanitize(pathinfo($aName, PATHINFO_FILENAME));
52
        $this->extension = pathinfo($aName, PATHINFO_EXTENSION);
53
    }
54
55
    /**
56
     * Factory method that returns an instance of file name with hash as a name.
57
     *
58
     * @param string $aName The name
59
     *
60
     * @return FileName
61
     */
62
    public static function fromHash($aName)
63
    {
64
        $instance = new self($aName);
65
        $instance->name = Uuid::uuid4()->toString();
66
67
        return $instance;
68
    }
69
70
    /**
71
     * Gets the name.
72
     *
73
     * @return string
74
     */
75
    public function name()
76
    {
77
        return $this->name;
78
    }
79
80
    /**
81
     *  Gets the extension.
82
     *
83
     * @return string
84
     */
85
    public function extension()
86
    {
87
        return $this->extension;
88
    }
89
90
    /**
91
     *  Gets the extension.
92
     *
93
     * @return string
94
     */
95
    public function filename()
96
    {
97
        return $this->name . '.' . $this->extension;
98
    }
99
100
    /**
101
     * Method that checks if the name given is equal to the current.
102
     *
103
     * @param FileName $aName
104
     *
105
     * @return bool
106
     */
107
    public function equals(FileName $aName)
108
    {
109
        return $this->filename() === $aName->filename();
110
    }
111
112
    /**
113
     * Magic method that represents the file name in string format.
114
     *
115
     * @return string
116
     */
117
    public function __toString()
118
    {
119
        return $this->filename();
120
    }
121
122
    /**
123
     * Sanitizes the given file name.
124
     *
125
     * @param string $aName The file name
126
     *
127
     * @return string
128
     */
129
    private function sanitize($aName)
130
    {
131
        $characters = [
132
            // Latin
133
            'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä' => 'A',
134
            'Å' => 'A', 'Æ' => 'AE', 'Ç' => 'C', 'È' => 'E', 'É' => 'E',
135
            'Ê' => 'E', 'Ë' => 'E', 'Ì' => 'I', 'Í' => 'I', 'Î' => 'I',
136
            'Ï' => 'I', 'Ð' => 'D', 'Ñ' => 'N', 'Ò' => 'O', 'Ó' => 'O',
137
            'Ô' => 'O', 'Õ' => 'O', 'Ö' => 'O', 'Ő' => 'O', 'Ø' => 'O',
138
            'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'U', 'Ű' => 'U',
139
            'Ũ' => 'U', 'Ý' => 'Y', 'Þ' => 'TH', 'ß' => 'ss',
140
            'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a', 'ä' => 'a',
141
            'å' => 'a', 'æ' => 'ae', 'ç' => 'c', 'è' => 'e', 'é' => 'e',
142
            'ê' => 'e', 'ë' => 'e', 'ì' => 'i', 'í' => 'i', 'î' => 'i',
143
            'ï' => 'i', 'ð' => 'd', 'ñ' => 'n', 'ò' => 'o', 'ó' => 'o',
144
            'ô' => 'o', 'õ' => 'o', 'ö' => 'o', 'ő' => 'o', 'ø' => 'o',
145
            'ù' => 'u', 'ú' => 'u', 'û' => 'u', 'ü' => 'u', 'ű' => 'u',
146
            'ũ' => 'u', 'ý' => 'y', 'þ' => 'th', 'ÿ' => 'y',
147
            // Croatian
148
            'Đ' => 'D', 'đ' => 'd',
149
            // Czech
150
            'Č' => 'C', 'Ď' => 'D', 'Ě' => 'E', 'Ň' => 'N', 'Ř' => 'R',
151
            'Š' => 'S', 'Ť' => 'T', 'Ů' => 'U', 'Ž' => 'Z', 'č' => 'c',
152
            'ď' => 'd', 'ě' => 'e', 'ň' => 'n', 'ř' => 'r', 'š' => 's',
153
            'ť' => 't', 'ů' => 'u', 'ž' => 'z',
154
            // Esperanto
155
            'Ĉ' => 'C', 'ĉ' => 'c', 'Ĝ' => 'G', 'ĝ' => 'g', 'Ĥ' => 'H',
156
            'ĥ' => 'h', 'Ĵ' => 'J', 'ĵ' => 'j', 'Ŝ' => 'S', 'ŝ' => 's',
157
            'Ŭ' => 'U', 'ŭ' => 'u',
158
            // Greek
159
            'Α' => 'A', 'Β' => 'B', 'Γ' => 'G', 'Δ' => 'D', 'Ε' => 'E',
160
            'Ζ' => 'Z', 'Η' => 'H', 'Θ' => '8', 'Ι' => 'I', 'Κ' => 'K',
161
            'Λ' => 'L', 'Μ' => 'M', 'Ν' => 'N', 'Ξ' => '3', 'Ο' => 'O',
162
            'Π' => 'P', 'Ρ' => 'R', 'Σ' => 'S', 'Τ' => 'T', 'Υ' => 'Y',
163
            'Φ' => 'F', 'Χ' => 'X', 'Ψ' => 'PS', 'Ω' => 'W', 'Ά' => 'A',
164
            'Έ' => 'E', 'Ί' => 'I', 'Ό' => 'O', 'Ύ' => 'Y', 'Ή' => 'H',
165
            'Ώ' => 'W', 'Ϊ' => 'I', 'Ϋ' => 'Y', 'α' => 'a', 'β' => 'b',
166
            'γ' => 'g', 'δ' => 'd', 'ε' => 'e', 'ζ' => 'z', 'η' => 'h',
167
            'θ' => '8', 'ι' => 'i', 'κ' => 'k', 'λ' => 'l', 'μ' => 'm',
168
            'ν' => 'n', 'ξ' => '3', 'ο' => 'o', 'π' => 'p', 'ρ' => 'r',
169
            'σ' => 's', 'τ' => 't', 'υ' => 'y', 'φ' => 'f', 'χ' => 'x',
170
            'ψ' => 'ps', 'ω' => 'w', 'ά' => 'a', 'έ' => 'e', 'ί' => 'i',
171
            'ό' => 'o', 'ύ' => 'y', 'ή' => 'h', 'ώ' => 'w', 'ς' => 's',
172
            'ϊ' => 'i', 'ΰ' => 'y', 'ϋ' => 'y', 'ΐ' => 'i',
173
            // Latvian
174
            'Ā' => 'A', 'Č' => 'C', 'Ē' => 'E', 'Ģ' => 'G', 'Ī' => 'i',
175
            'Ķ' => 'k', 'Ļ' => 'L', 'Ņ' => 'N', 'Š' => 'S', 'Ū' => 'u',
176
            'Ž' => 'Z', 'ā' => 'a', 'č' => 'c', 'ē' => 'e', 'ģ' => 'g',
177
            'ī' => 'i', 'ķ' => 'k', 'ļ' => 'l', 'ņ' => 'n', 'Ŗ' => 'R',
178
            'ŗ' => 'r', 'š' => 's', 'ū' => 'u', 'ž' => 'z', 'Ō' => 'O',
179
            'ō' => 'o',
180
            // Lithuanian
181
            'Ė' => 'E', 'ė' => 'e', 'Ĩ' => 'I', 'ĩ' => 'i', 'Į' => 'I',
182
            'į' => 'i', 'Ų' => 'U', 'ų' => 'u',
183
            // Maltese
184
            'Ċ' => 'C', 'ċ' => 'c', 'Ġ' => 'G', 'ġ' => 'g', 'Ħ' => 'H',
185
            'ħ' => 'h',
186
            // Polish
187
            'Ą' => 'A', 'Ć' => 'C', 'Ę' => 'e', 'Ł' => 'L', 'Ń' => 'N',
188
            'Ó' => 'o', 'Ś' => 'S', 'Ź' => 'Z', 'Ż' => 'Z', 'ą' => 'a',
189
            'ć' => 'c', 'ę' => 'e', 'ł' => 'l', 'ń' => 'n', 'ó' => 'o',
190
            'ś' => 's', 'ź' => 'z', 'ż' => 'z',
191
            // Romanian
192
            'Ă' => 'A', 'ă' => 'a', 'ș' => 'S', 'ș' => 's', 'Ț' => 'T',
193
            'ț' => 't', 'Ţ' => 't', 'ţ' => 't',
194
            // Russian
195
            'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G', 'Д' => 'D',
196
            'Е' => 'E', 'Ё' => 'Yo', 'Ж' => 'Zh', 'З' => 'Z', 'И' => 'I',
197
            'Й' => 'J', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N',
198
            'О' => 'O', 'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T',
199
            'У' => 'U', 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', 'Ч' => 'Ch',
200
            'Ш' => 'Sh', 'Щ' => 'Sh', 'Ъ' => '', 'Ы' => 'Y', 'Ь' => '',
201
            'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', 'а' => 'a', 'б' => 'b',
202
            'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo',
203
            'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'j', 'к' => 'k',
204
            'л' => 'l', 'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p',
205
            'р' => 'r', 'с' => 's', 'т' => 't', 'у' => 'u', 'ф' => 'f',
206
            'х' => 'h', 'ц' => 'c', 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sh',
207
            'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e', 'ю' => 'yu',
208
            'я' => 'ya',
209
            // Sami
210
            'Ŋ' => 'N', 'ŋ' => 'n', 'Ŧ' => 'T', 'ŧ' => 't', 'Ǧ' => 'G',
211
            'ǧ' => 'g', 'Ǩ' => 'K', 'ǩ' => 'k', 'Ʒ' => 'Z', 'ʒ' => 'z',
212
            'Ǯ' => 'Z', 'ǯ' => 'z',
213
            // Slovak
214
            'Ľ' => 'L', 'ľ' => 'l', 'Ĺ' => 'L', 'ĺ' => 'l', 'Ŕ' => 'R',
215
            'ŕ' => 'r',
216
            // Turkish
217
            'Ş' => 'S', 'İ' => 'I', 'Ç' => 'C', 'Ü' => 'U', 'Ö' => 'O',
218
            'Ğ' => 'G', 'ş' => 's', 'ı' => 'i', 'ç' => 'c', 'ü' => 'u',
219
            'ö' => 'o', 'ğ' => 'g',
220
            // Ukrainian
221
            'Є' => 'Ye', 'І' => 'I', 'Ї' => 'Yi', 'Ґ' => 'G', 'є' => 'ye',
222
            'і' => 'i', 'ї' => 'yi', 'ґ' => 'g',
223
        ];
224
225
        $normalized = strtolower(strtr($aName, $characters));
226
        $cleaned = preg_replace('/([^a-z0-9]|-)+/', '-', $normalized);
227
228
        return trim($cleaned, '-');
229
    }
230
}
231