1 | <?php |
||
7 | class Rules implements Formatter |
||
8 | { |
||
9 | private |
||
10 | $rules; |
||
11 | |||
12 | public function __construct(array $rules) |
||
16 | |||
17 | private function getSpecialValuesMappingTable() |
||
28 | |||
29 | private function convertRules(array $rules) |
||
30 | { |
||
31 | $this->rules = array(); |
||
32 | $mapping = $this->getSpecialValuesMappingTable(); |
||
33 | |||
34 | foreach($rules as $value => $result) |
||
35 | { |
||
36 | $value = trim($value); |
||
37 | |||
38 | if(is_string($value) && array_key_exists($value, $mapping)) |
||
39 | { |
||
40 | $result = $this->handleStringFormatting($value, $result); |
||
41 | $value = $mapping[$value]; |
||
42 | } |
||
43 | |||
44 | $this->rules[] = array($value, $result); |
||
45 | } |
||
46 | } |
||
47 | |||
48 | private function handleStringFormatting($value, $result) |
||
59 | |||
60 | public function format($value) |
||
61 | { |
||
62 | foreach($this->rules as $rule) |
||
63 | { |
||
64 | list($condition, $result) = $rule; |
||
65 | |||
66 | if($this->isRuleMatches($condition, $value)) |
||
67 | { |
||
68 | return $this->applyFormattingRule($result, $value); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | return $value; |
||
73 | } |
||
74 | |||
75 | private function isRuleMatches($condition, $value) |
||
76 | { |
||
77 | $hasMatched = ($condition === $value); |
||
78 | |||
79 | if($condition instanceof \Closure) |
||
80 | { |
||
81 | $hasMatched = $condition($value); |
||
82 | } |
||
83 | |||
84 | return $hasMatched; |
||
85 | } |
||
86 | |||
87 | private function applyFormattingRule($ruleResult, $value) |
||
96 | } |
||
97 |