1 <?php
2
3 /* --------------------------------------------------------------
4 FilenameStringType.php 2016-02-19
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2015 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 /**
14 * Class FilenameStringType
15 *
16 * @category System
17 * @package Shared
18 * @subpackage FileSystem
19 */
20 class FilenameStringType extends StringType
21 {
22
23 /**
24 * FilenameStringType constructor.
25 *
26 * @param string $filename
27 *
28 * @throws InvalidArgumentException if $filename contains invalid characters
29 */
30 public function __construct($filename)
31 {
32 parent::__construct($filename);
33 $this->_validateFilename($filename);
34 }
35
36
37 /**
38 * Validates file name.
39 *
40 * @param string $filename
41 *
42 * @throws InvalidArgumentException if $filename contains invalid characters
43 *
44 * @return FilenameStringType Same instance for chained method calls.
45 */
46 protected function _validateFilename($filename)
47 {
48 // backup locale setting
49 $locale = setlocale(LC_ALL, 0);
50
51 // change locale to multibyte character charset allowing characters like umlauts
52 // en_US.UTF8 should always be available
53 setlocale(LC_ALL, 'en_US.UTF8');
54
55 if($filename !== basename((string)$filename))
56 {
57 throw new InvalidArgumentException('Filename "' . (string)$filename . '" is not valid');
58 }
59
60 // restore locale setting
61 setlocale(LC_ALL, $locale);
62
63 return $this;
64 }
65 }