1 | <?php |
||
17 | abstract class AbstractFormatterProvider implements FormatterProvider |
||
18 | { |
||
19 | protected $defaultFormatter; |
||
20 | /** |
||
21 | * method prefix |
||
22 | */ |
||
23 | const METHOD_PATTERN_MATCH = '/^as([A-Z]\w+)$/'; |
||
24 | |||
25 | public $nullValue = '<span>Not Set</span>'; |
||
26 | |||
27 | /** |
||
28 | * Provide a list of formatters this that are available from this provider |
||
29 | * |
||
30 | * @return array |
||
31 | */ |
||
32 | 7 | public function formats() |
|
33 | { |
||
34 | // get a list of all public non-static methods that start with |
||
35 | // METHOD_PREFIX |
||
36 | 7 | $reflect = new ReflectionClass($this); |
|
37 | 7 | $formats = []; |
|
38 | 7 | foreach ($reflect->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
39 | 7 | $name = $this->getFormatterName($method); |
|
40 | 7 | if ($name) { |
|
41 | 7 | $formats[] = $name; |
|
42 | 7 | } |
|
43 | 7 | } |
|
44 | |||
45 | 7 | return $formats; |
|
46 | } |
||
47 | |||
48 | /** |
||
49 | * Get the name of the formatter method |
||
50 | * |
||
51 | * @param ReflectionMethod $method |
||
52 | * @return string |
||
53 | */ |
||
54 | 7 | private function getFormatterName(ReflectionMethod $method) |
|
61 | |||
62 | /** |
||
63 | * Combine the value & params |
||
64 | * @param mixed $value the value (first argument of the format method) |
||
65 | * @param string|array $format the name of the formatter, or an array of |
||
66 | * the formatter and its addtional params |
||
67 | * @return array of two elements, format and the params for the formatter |
||
68 | * method |
||
69 | * @throws InvalidArgumentException if the format is incorrect |
||
70 | */ |
||
71 | 9 | protected function extractFormatAndParams($value, $format) |
|
88 | |||
89 | /** |
||
90 | * Format the supplied value, based on the desired format + configuration |
||
91 | * |
||
92 | * @param mixed $value The value to format |
||
93 | * @param string|array|null $format either the formatter name, or the formatter |
||
94 | * config as an array. If it's an array, the |
||
95 | * first item must be the same of the formatter |
||
96 | * @return mixed |
||
97 | * @throws InvalidArgumentException if the format is incorrect |
||
98 | */ |
||
99 | 4 | public function format($value, $format = null) |
|
117 | |||
118 | /** |
||
119 | * Check if the requested format exists in this provider |
||
120 | * |
||
121 | * @param string $format The formatter name you want to check for |
||
122 | * @return boolean |
||
123 | */ |
||
124 | 6 | public function hasFormat($format) |
|
135 | } |
||
136 |