1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* (c) FSi sp. z o.o. <[email protected]> |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FSi\Bundle\AdminBundle\Display; |
11
|
|
|
|
12
|
|
|
use FSi\Bundle\AdminBundle\Display\Property\ValueFormatter; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
|
15
|
|
|
class Property |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var mixed |
19
|
|
|
*/ |
20
|
|
|
private $value; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $label; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param mixed $value |
29
|
|
|
* @param string $label |
30
|
|
|
* @param array|\FSi\Bundle\AdminBundle\Display\Property\ValueFormatter[] $valueFormatters |
31
|
|
|
*/ |
32
|
|
|
public function __construct($value, $label, array $valueFormatters = []) |
33
|
|
|
{ |
34
|
|
|
$this->validateLabel($label); |
35
|
|
|
$this->validateFormatters($valueFormatters); |
36
|
|
|
|
37
|
|
|
$this->value = $this->formatValue($value, $valueFormatters); |
38
|
|
|
$this->label = $label; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getLabel() |
45
|
|
|
{ |
46
|
|
|
return $this->label; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
|
|
public function getValue() |
53
|
|
|
{ |
54
|
|
|
return $this->value; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param mixed $value |
59
|
|
|
* @param ValueFormatter[] $valueFormatters |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
|
|
private function formatValue($value, array $valueFormatters) |
63
|
|
|
{ |
64
|
|
|
foreach ($valueFormatters as $formatter) { |
65
|
|
|
$value = $formatter->format($value); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $label |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
*/ |
75
|
|
|
private function validateLabel($label) |
76
|
|
|
{ |
77
|
|
|
if (!is_string($label)) { |
78
|
|
|
throw new InvalidArgumentException(sprintf( |
79
|
|
|
'Property label must be a string, got "%s"', |
80
|
|
|
gettype($label) |
81
|
|
|
)); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param ValueFormatter[] $valueFormatters |
87
|
|
|
* @throws InvalidArgumentException |
88
|
|
|
*/ |
89
|
|
|
private function validateFormatters(array $valueFormatters) |
90
|
|
|
{ |
91
|
|
|
foreach ($valueFormatters as $formatter) { |
92
|
|
|
if (!$formatter instanceof ValueFormatter) { |
93
|
|
|
throw new InvalidArgumentException(sprintf( |
94
|
|
|
'Expected property formatter to be an instance of' |
95
|
|
|
. ' FSi\Bundle\AdminBundle\Display\Property\ValueFormatter,' |
96
|
|
|
. ' got "%s" instead', |
97
|
|
|
is_object($formatter) ? get_class($formatter) : gettype($formatter) |
98
|
|
|
)); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|