1 | <?php |
||
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) |
|
97 | |||
98 | |||
99 | } |
||
100 |