|
3 | 3 | namespace Karriere\JsonDecoder\Bindings; |
4 | 4 |
|
5 | 5 | use Karriere\JsonDecoder\Binding; |
6 | | -use Karriere\JsonDecoder\Exceptions\JsonValueException; |
7 | | -use Karriere\JsonDecoder\JsonDecoder; |
8 | | -use Karriere\JsonDecoder\PropertyAccessor; |
9 | 6 |
|
10 | | -class FieldBinding implements Binding |
| 7 | +class FieldBinding extends Binding |
11 | 8 | { |
12 | 9 | /** |
13 | | - * @var string |
14 | | - */ |
15 | | - private $property; |
16 | | - |
17 | | - /** |
18 | | - * @var string |
19 | | - */ |
20 | | - private $jsonField; |
21 | | - |
22 | | - /** |
23 | | - * @var string |
24 | | - */ |
25 | | - private $type; |
26 | | - |
27 | | - /** |
28 | | - * @var bool |
29 | | - */ |
30 | | - private $isRequired; |
31 | | - |
32 | | - /** |
33 | | - * FieldBinding constructor. |
34 | | - * |
35 | | - * @param string $property the property to bind to |
36 | | - * @param string $jsonField the json field |
37 | | - * @param string $type the desired type of the property |
38 | | - * @param bool $isRequired defines if the field value is required during decoding |
39 | | - */ |
40 | | - public function __construct($property, $jsonField, $type, $isRequired = false) |
41 | | - { |
42 | | - $this->property = $property; |
43 | | - $this->jsonField = $jsonField; |
44 | | - $this->type = $type; |
45 | | - $this->isRequired = $isRequired; |
46 | | - } |
47 | | - |
48 | | - /** |
49 | | - * executes the defined binding method on the class instance. |
50 | | - * |
51 | | - * @param JsonDecoder $jsonDecoder |
52 | | - * @param array $jsonData |
53 | | - * @param PropertyAccessor $propertyAccessor the class instance to bind to |
54 | | - * |
55 | | - * @throws JsonValueException if given json field is not available |
56 | | - * |
57 | | - * @return mixed |
| 10 | + * {@inheritdoc} |
58 | 11 | */ |
59 | 12 | public function bind($jsonDecoder, $jsonData, $propertyAccessor) |
60 | 13 | { |
61 | 14 | if (array_key_exists($this->jsonField, $jsonData)) { |
62 | 15 | $data = $jsonData[$this->jsonField]; |
63 | 16 | $propertyAccessor->set($jsonDecoder->decodeArray($data, $this->type)); |
64 | 17 | } |
65 | | - |
66 | | - if ($this->isRequired) { |
67 | | - throw new JsonValueException( |
68 | | - sprintf('the value "%s" for property "%s" does not exist', $this->jsonField, $this->property) |
69 | | - ); |
70 | | - } |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * @return string the name of the property to bind |
75 | | - */ |
76 | | - public function property() |
77 | | - { |
78 | | - return $this->property; |
79 | 18 | } |
80 | 19 | } |
0 commit comments