|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Copyright (c) 2016-2016} Andreas Heigl<[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
* |
|
24
|
|
|
* @author Andreas Heigl<[email protected]> |
|
25
|
|
|
* @copyright 2016-2016 Andreas Heigl |
|
26
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT-License |
|
27
|
|
|
* @version 0.0 |
|
28
|
|
|
* @since 24.03.2016 |
|
29
|
|
|
* @link http://github.com/heiglandreas/org.heigl.DateFormatter |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
namespace Org_Heigl\DateFormatter\Service; |
|
33
|
|
|
|
|
34
|
|
|
use Org_Heigl\DateFormatter\UnknownFormatException; |
|
35
|
|
|
use Org_Heigl\FileFinder\ClassMapList; |
|
36
|
|
|
use Org_Heigl\FileFinder\FileFinder; |
|
37
|
|
|
use Org_Heigl\FileFinder\Filter\ClassIsInstanceof; |
|
38
|
|
|
|
|
39
|
|
|
class FormatterService |
|
40
|
|
|
{ |
|
41
|
|
|
protected static $list; |
|
42
|
|
|
|
|
43
|
|
|
protected static $formatterDirectories = [ |
|
44
|
|
|
__DIR__ . '/../Formatter/', |
|
45
|
|
|
]; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return \Org_Heigl\FileFinder\FileListInterface |
|
49
|
|
|
*/ |
|
50
|
5 |
|
protected static function getList() |
|
51
|
|
|
{ |
|
52
|
5 |
|
if (! self::$list) { |
|
53
|
1 |
|
$finder = new FileFinder(); |
|
54
|
1 |
|
$finder->addFilter(new ClassIsInstanceof(['Org_Heigl\DateFormatter\Formatter\FormatterInterface'])); |
|
55
|
1 |
|
$finder->setFileList(new ClassMapList()); |
|
56
|
1 |
|
foreach (self::$formatterDirectories as $directory) { |
|
57
|
1 |
|
$finder->addDirectory($directory); |
|
58
|
1 |
|
} |
|
59
|
1 |
|
self::$list = $finder->find(); |
|
60
|
1 |
|
} |
|
61
|
|
|
|
|
62
|
5 |
|
return self::$list; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $format |
|
67
|
|
|
* |
|
68
|
|
|
* @return Org_Heigl\DateFormatter\Formatter\FormatterInterface |
|
69
|
|
|
*/ |
|
70
|
5 |
|
public static function getFormatter($format) |
|
71
|
|
|
{ |
|
72
|
5 |
|
foreach (self::getList() as $class => $path) { |
|
|
|
|
|
|
73
|
5 |
|
if (! in_array(strtolower($format), array_map('strtolower', $class::getFormatString()))) { |
|
74
|
4 |
|
continue; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
3 |
|
return new $class(); |
|
78
|
2 |
|
} |
|
79
|
|
|
|
|
80
|
2 |
|
throw new UnknownFormatException('There is no formater for this format'); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param string $folder |
|
85
|
|
|
* |
|
86
|
|
|
* @return void |
|
87
|
|
|
*/ |
|
88
|
1 |
|
public static function addFormatterFolder($folder) |
|
89
|
|
|
{ |
|
90
|
1 |
|
if (! is_dir($folder)) { |
|
91
|
1 |
|
return; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
1 |
|
array_unshift(self::$formatterDirectories, $folder); |
|
95
|
1 |
|
self::$list = null; |
|
96
|
1 |
|
} |
|
97
|
|
|
|
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
|