|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace FinanCalc\Interfaces\Calculator { |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use FinanCalc\Constants\ErrorMessages; |
|
7
|
|
|
use FinanCalc\Interfaces\Serializer\SerializerInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Interface CalculatorAbstract |
|
11
|
|
|
* @package FinanCalc\Interfaces |
|
12
|
|
|
*/ |
|
13
|
|
|
abstract class CalculatorAbstract |
|
14
|
|
|
{ |
|
15
|
|
|
protected $propResultArray = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @param string $name |
|
19
|
|
|
* @param $value |
|
20
|
|
|
* @param \Closure $callbackBefore |
|
21
|
|
|
* @throws Exception |
|
22
|
|
|
*/ |
|
23
|
|
|
final protected function setProperty($name, $value, $callbackBefore = null) |
|
24
|
|
|
{ |
|
25
|
|
|
if (is_callable($callbackBefore)) { |
|
26
|
|
|
$callbackBefore($value); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
if (property_exists($this, $name)) { |
|
30
|
|
|
if (is_object($value) || is_null($value)) { |
|
31
|
|
|
$this->$name = $value; |
|
32
|
|
|
} else { |
|
33
|
|
|
$this->$name = (string)$value; |
|
34
|
|
|
} |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
throw new Exception(ErrorMessages::getNonExistentPropertyMessage($name, get_class($this))); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param array $propResultArray |
|
43
|
|
|
* @return array |
|
44
|
|
|
*/ |
|
45
|
|
|
final public function getResultAsArray(array $propResultArray = null) |
|
46
|
|
|
{ |
|
47
|
|
|
if ($propResultArray === null) { |
|
48
|
|
|
if ($this->propResultArray !== null && is_array($this->propResultArray)) { |
|
49
|
|
|
$propResultArray = $this->propResultArray; |
|
50
|
|
|
} else { |
|
51
|
|
|
error_log(ErrorMessages::getPropresultarrayNotSuppliedMessage()); |
|
52
|
|
|
return []; |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$processArray = function ($inputArray) use (&$processArray) { |
|
57
|
|
|
$processedArray = array(); |
|
58
|
|
|
foreach ($inputArray as $key => $prop) { |
|
59
|
|
|
if (is_string($prop)) { |
|
60
|
|
|
$propGetter = "get" . ucfirst($prop); |
|
61
|
|
|
if (method_exists($this, $propGetter)) { |
|
62
|
|
|
$processedArray[is_string($key) ? $key : $prop] = call_user_func(array($this, $propGetter)); |
|
63
|
|
|
} else { |
|
64
|
|
|
error_log(ErrorMessages::getMethodDoesNotExistMessage($propGetter, get_class($this))); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
if (is_array($prop)) { |
|
68
|
|
|
$processedArray[$key] = $processArray($prop); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $processedArray; |
|
73
|
|
|
}; |
|
74
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
return $processArray($propResultArray); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param SerializerInterface $serializer |
|
81
|
|
|
* @return mixed |
|
82
|
|
|
*/ |
|
83
|
|
|
final public function getSerializedResult(SerializerInterface $serializer) |
|
84
|
|
|
{ |
|
85
|
|
|
return $serializer->serializeArray($this->getResultAsArray()); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|