Skip to content

Commit bb720a7

Browse files
authored
Merge pull request #27 from fetzi/master
Fixes isRequired check in bindings
2 parents cf0b3f9 + a7e3d81 commit bb720a7

14 files changed

+175
-235
lines changed

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Changelog
2+
All notable changes to this project will be documented in this file.
3+
4+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6+
7+
## [2.2.1] - 2019-03-05
8+
### Added
9+
- `validate` function for checking the `isRequired` flag for Bindings
10+
11+
### Changed
12+
- transform `Binding` to an abstract class and unify functionality from `AliasBinding`, `ArrayBinding` & `FieldBinding`
13+
14+
### Fixed
15+
- check for `isRequired` flag is only executed when applicable
16+
17+
## [2.2.0] - 2019-01-11
18+
### Added
19+
- `JsonDecoder` instance as second parameter to callback function signature for CallbackBindings

src/Binding.php

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,64 @@
22

33
namespace Karriere\JsonDecoder;
44

5-
interface Binding
5+
abstract class Binding
66
{
7+
/**
8+
* @var string
9+
*/
10+
protected $property;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $jsonField;
16+
17+
/**
18+
* @var string
19+
*/
20+
protected $type;
21+
22+
/**
23+
* @var bool
24+
*/
25+
protected $isRequired;
26+
27+
/**
28+
* FieldBinding constructor.
29+
*
30+
* @param string $property the property to bind to
31+
* @param string $jsonField the json field
32+
* @param string $type the desired type of the property
33+
* @param bool $isRequired defines if the field value is required during decoding
34+
*/
35+
public function __construct($property, $jsonField, $type, $isRequired = false)
36+
{
37+
$this->property = $property;
38+
$this->jsonField = $jsonField;
39+
$this->type = $type;
40+
$this->isRequired = $isRequired;
41+
}
42+
43+
/**
44+
* validates the given binding data.
45+
*
46+
* @param mixed $jsonData
47+
*
48+
* @return bool
49+
*/
50+
public function validate($jsonData) : bool
51+
{
52+
return !$this->isRequired || array_key_exists($this->jsonField, $jsonData);
53+
}
54+
55+
/**
56+
* @return string the name of the property to bind
57+
*/
58+
public function property()
59+
{
60+
return $this->property;
61+
}
62+
763
/**
864
* executes the defined binding method on the class instance.
965
*
@@ -13,10 +69,5 @@ interface Binding
1369
*
1470
* @return mixed
1571
*/
16-
public function bind($jsonDecoder, $jsonData, $propertyAccessor);
17-
18-
/**
19-
* @return string the name of the property to bind
20-
*/
21-
public function property();
72+
abstract public function bind($jsonDecoder, $jsonData, $propertyAccessor);
2273
}

src/Bindings/AliasBinding.php

Lines changed: 3 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,9 @@
33
namespace Karriere\JsonDecoder\Bindings;
44

55
use Karriere\JsonDecoder\Binding;
6-
use Karriere\JsonDecoder\Exceptions\JsonValueException;
7-
use Karriere\JsonDecoder\JsonDecoder;
8-
use Karriere\JsonDecoder\PropertyAccessor;
96

10-
class AliasBinding implements Binding
7+
class AliasBinding extends Binding
118
{
12-
/**
13-
* @var string
14-
*/
15-
private $property;
16-
17-
/**
18-
* @var string
19-
*/
20-
private $jsonField;
21-
22-
/**
23-
* @var bool
24-
*/
25-
private $isRequired;
26-
279
/**
2810
* AliasBinding constructor.
2911
*
@@ -33,42 +15,16 @@ class AliasBinding implements Binding
3315
*/
3416
public function __construct($property, $jsonField, $isRequired = false)
3517
{
36-
$this->property = $property;
37-
$this->jsonField = $jsonField;
38-
$this->isRequired = $isRequired;
18+
parent::__construct($property, $jsonField, null, $isRequired);
3919
}
4020

4121
/**
42-
* executes the defined binding method on the class instance.
43-
*
44-
* @param JsonDecoder $jsonDecoder
45-
* @param mixed $jsonData
46-
* @param PropertyAccessor $propertyAccessor the class instance to bind to
47-
*
48-
* @throws JsonValueException if given json field is not available
49-
*
50-
* @return mixed
22+
* {@inheritdoc}
5123
*/
5224
public function bind($jsonDecoder, $jsonData, $propertyAccessor)
5325
{
5426
if (array_key_exists($this->jsonField, $jsonData)) {
5527
$propertyAccessor->set($jsonData[$this->jsonField]);
56-
57-
return;
58-
}
59-
60-
if ($this->isRequired) {
61-
throw new JsonValueException(
62-
sprintf('the value "%s" for property "%s" does not exist', $this->jsonField, $this->property)
63-
);
6428
}
6529
}
66-
67-
/**
68-
* @return string the name of the property to bind
69-
*/
70-
public function property()
71-
{
72-
return $this->property;
73-
}
7430
}

src/Bindings/ArrayBinding.php

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,11 @@
33
namespace Karriere\JsonDecoder\Bindings;
44

55
use Karriere\JsonDecoder\Binding;
6-
use Karriere\JsonDecoder\Exceptions\JsonValueException;
7-
use Karriere\JsonDecoder\JsonDecoder;
8-
use Karriere\JsonDecoder\PropertyAccessor;
96

10-
class ArrayBinding implements Binding
7+
class ArrayBinding extends Binding
118
{
129
/**
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-
* ArrayBinding 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}
5811
*/
5912
public function bind($jsonDecoder, $jsonData, $propertyAccessor)
6013
{
@@ -70,19 +23,5 @@ public function bind($jsonDecoder, $jsonData, $propertyAccessor)
7023
$propertyAccessor->set($values);
7124
}
7225
}
73-
74-
if ($this->isRequired) {
75-
throw new JsonValueException(
76-
sprintf('the value "%s" for property "%s" does not exist', $this->jsonField, $this->property)
77-
);
78-
}
79-
}
80-
81-
/**
82-
* @return string the name of the property to bind
83-
*/
84-
public function property()
85-
{
86-
return $this->property;
8726
}
8827
}

src/Bindings/CallbackBinding.php

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,9 @@
33
namespace Karriere\JsonDecoder\Bindings;
44

55
use Karriere\JsonDecoder\Binding;
6-
use Karriere\JsonDecoder\JsonDecoder;
7-
use Karriere\JsonDecoder\PropertyAccessor;
86

9-
class CallbackBinding implements Binding
7+
class CallbackBinding extends Binding
108
{
11-
/**
12-
* @var string
13-
*/
14-
private $property;
15-
169
/**
1710
* @var callable
1811
*/
@@ -26,29 +19,23 @@ class CallbackBinding implements Binding
2619
*/
2720
public function __construct($property, $callback)
2821
{
29-
$this->property = $property;
22+
parent::__construct($property, null, null, false);
3023
$this->callback = $callback;
3124
}
3225

3326
/**
34-
* executes the defined binding method on the class instance.
35-
*
36-
* @param JsonDecoder $jsonDecoder
37-
* @param mixed $jsonData
38-
* @param PropertyAccessor $propertyAccessor the class instance to bind to
39-
*
40-
* @return mixed
27+
* {@inheritdoc}
4128
*/
42-
public function bind($jsonDecoder, $jsonData, $propertyAccessor)
29+
public function validate($jsonData) : bool
4330
{
44-
$propertyAccessor->set($this->callback->__invoke($jsonData, $jsonDecoder));
31+
return true;
4532
}
4633

4734
/**
48-
* @return string the name of the property to bind
35+
* {@inheritdoc}
4936
*/
50-
public function property()
37+
public function bind($jsonDecoder, $jsonData, $propertyAccessor)
5138
{
52-
return $this->property;
39+
$propertyAccessor->set($this->callback->__invoke($jsonData, $jsonDecoder));
5340
}
5441
}

src/Bindings/FieldBinding.php

Lines changed: 2 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -3,78 +3,17 @@
33
namespace Karriere\JsonDecoder\Bindings;
44

55
use Karriere\JsonDecoder\Binding;
6-
use Karriere\JsonDecoder\Exceptions\JsonValueException;
7-
use Karriere\JsonDecoder\JsonDecoder;
8-
use Karriere\JsonDecoder\PropertyAccessor;
96

10-
class FieldBinding implements Binding
7+
class FieldBinding extends Binding
118
{
129
/**
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}
5811
*/
5912
public function bind($jsonDecoder, $jsonData, $propertyAccessor)
6013
{
6114
if (array_key_exists($this->jsonField, $jsonData)) {
6215
$data = $jsonData[$this->jsonField];
6316
$propertyAccessor->set($jsonDecoder->decodeArray($data, $this->type));
6417
}
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;
7918
}
8019
}

0 commit comments

Comments
 (0)