Skip to content
This repository was archived by the owner on Mar 12, 2025. It is now read-only.

Commit 91780e1

Browse files
committed
use Accessor as a argument for DateTimeFieldNormalizer
1 parent 7142338 commit 91780e1

File tree

5 files changed

+112
-13
lines changed

5 files changed

+112
-13
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ A simple serialization.
3131
Through [Composer](http://getcomposer.org) as [chubbyphp/chubbyphp-serialization][1].
3232

3333
```sh
34-
composer require chubbyphp/chubbyphp-serialization "~2.1"
34+
composer require chubbyphp/chubbyphp-serialization "~2.2"
3535
```
3636

3737
## Usage

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
},
2929
"extra": {
3030
"branch-alias": {
31-
"dev-master": "2.1-dev"
31+
"dev-master": "2.2-dev"
3232
}
3333
}
3434
}

doc/Normalizer/DateTimeFieldNormalizer.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ $model = new Model;
1212
$context = ...;
1313

1414
$fieldNormalizer = new DateTimeFieldNormalizer(
15-
new FieldNormalizer(
16-
new PropertyAccessor('at')
17-
),
15+
new PropertyAccessor('at'),
1816
'Y-m-d H:i:s'
1917
);
2018

src/Normalizer/DateTimeFieldNormalizer.php

+62-4
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,64 @@
44

55
namespace Chubbyphp\Serialization\Normalizer;
66

7+
use Chubbyphp\Serialization\Accessor\AccessorInterface;
8+
79
final class DateTimeFieldNormalizer implements FieldNormalizerInterface
810
{
911
/**
1012
* @var FieldNormalizerInterface
1113
*/
1214
private $fieldNormalizer;
1315

16+
/**
17+
* @var AccessorInterface
18+
*/
19+
private $accessor;
20+
1421
/**
1522
* @var string
1623
*/
1724
private $format;
1825

1926
/**
20-
* @param FieldNormalizerInterface $fieldNormalizer
27+
* @param AccessorInterface|FieldNormalizerInterface $fieldNormalizer
2128
* @param string $format
2229
*/
23-
public function __construct(FieldNormalizerInterface $fieldNormalizer, string $format = 'c')
30+
public function __construct($accessor, string $format = 'c')
2431
{
25-
$this->fieldNormalizer = $fieldNormalizer;
32+
2633
$this->format = $format;
34+
35+
if ($accessor instanceof AccessorInterface) {
36+
$this->accessor = $accessor;
37+
38+
return;
39+
}
40+
41+
if ($accessor instanceof FieldNormalizerInterface) {
42+
@trigger_error(
43+
sprintf(
44+
'Use "%s" instead of "%s" as __construct argument',
45+
AccessorInterface::class,
46+
FieldNormalizerInterface::class
47+
),
48+
E_USER_DEPRECATED
49+
);
50+
51+
$this->fieldNormalizer = $accessor;
52+
53+
return;
54+
}
55+
56+
throw new \TypeError(
57+
sprintf(
58+
'%s::__construct() expects parameter 1 to be %s|%s, %s given',
59+
self::class,
60+
AccessorInterface::class,
61+
FieldNormalizerInterface::class,
62+
is_object($accessor) ? get_class($accessor) : gettype($accessor)
63+
)
64+
);
2765
}
2866

2967
/**
@@ -40,7 +78,7 @@ public function normalizeField(
4078
NormalizerContextInterface $context,
4179
NormalizerInterface $normalizer = null
4280
) {
43-
$value = $this->fieldNormalizer->normalizeField($path, $object, $context, $normalizer);
81+
$value = $this->getValue($path, $object, $context, $normalizer);
4482

4583
if (is_string($value)) {
4684
try {
@@ -55,4 +93,24 @@ public function normalizeField(
5593

5694
return $value->format($this->format);
5795
}
96+
97+
/**
98+
* @param string $path
99+
* @param $object
100+
* @param NormalizerContextInterface $context
101+
* @param NormalizerInterface|null $normalizer
102+
* @return mixed
103+
*/
104+
private function getValue(
105+
string $path,
106+
$object,
107+
NormalizerContextInterface $context,
108+
NormalizerInterface $normalizer = null
109+
) {
110+
if (null !== $this->accessor) {
111+
return $this->accessor->getValue($object);
112+
}
113+
114+
return $this->fieldNormalizer->normalizeField($path, $object, $context, $normalizer);
115+
}
58116
}

tests/Normalizer/DateTimeFieldNormalizerTest.php

+47-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Chubbyphp\Tests\Serialization\Normalizer;
66

7+
use Chubbyphp\Serialization\Accessor\AccessorInterface;
78
use Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer;
89
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
910
use Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface;
@@ -14,7 +15,15 @@
1415
*/
1516
class DateTimeFieldNormalizerTest extends TestCase
1617
{
17-
public function testNormalizeField()
18+
public function testNormalizeFieldWithInvalidConstructArgument()
19+
{
20+
self::expectException(\TypeError::class);
21+
self::expectExceptionMessage('Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer::__construct() expects parameter 1 to be Chubbyphp\Serialization\Accessor\AccessorInterface|Chubbyphp\Serialization\Normalizer\FieldNormalizerInterface, DateTime given');
22+
23+
new DateTimeFieldNormalizer(new \DateTime());
24+
}
25+
26+
public function testNormalizeFieldWithFieldNormalizer()
1827
{
1928
$object = $this->getObject();
2029
$object->setDate(new \DateTime('2017-01-01 22:00:00+01:00'));
@@ -30,13 +39,30 @@ public function testNormalizeField()
3039
)
3140
);
3241
}
42+
43+
public function testNormalizeField()
44+
{
45+
$object = $this->getObject();
46+
$object->setDate(new \DateTime('2017-01-01 22:00:00+01:00'));
47+
48+
$fieldNormalizer = new DateTimeFieldNormalizer($this->getAccessor());
49+
50+
self::assertSame(
51+
'2017-01-01T22:00:00+01:00',
52+
$fieldNormalizer->normalizeField(
53+
'date',
54+
$object,
55+
$this->getNormalizerContext()
56+
)
57+
);
58+
}
3359

3460
public function testNormalizeWithValidDateString()
3561
{
3662
$object = $this->getObject();
3763
$object->setDate('2017-01-01 22:00:00+01:00');
3864

39-
$fieldNormalizer = new DateTimeFieldNormalizer($this->getFieldNormalizer());
65+
$fieldNormalizer = new DateTimeFieldNormalizer($this->getAccessor());
4066

4167
self::assertSame(
4268
'2017-01-01T22:00:00+01:00',
@@ -53,7 +79,7 @@ public function testNormalizeWithInvalidDateString()
5379
$object = $this->getObject();
5480
$object->setDate('2017-01-01 25:00:00');
5581

56-
$fieldNormalizer = new DateTimeFieldNormalizer($this->getFieldNormalizer());
82+
$fieldNormalizer = new DateTimeFieldNormalizer($this->getAccessor());
5783

5884
self::assertSame(
5985
'2017-01-01 25:00:00',
@@ -69,7 +95,7 @@ public function testNormalizeWithNull()
6995
{
7096
$object = $this->getObject();
7197

72-
$fieldNormalizer = new DateTimeFieldNormalizer($this->getFieldNormalizer());
98+
$fieldNormalizer = new DateTimeFieldNormalizer($this->getAccessor());
7399

74100
self::assertNull(
75101
$fieldNormalizer->normalizeField('date', $object, $this->getNormalizerContext())
@@ -105,6 +131,23 @@ public function setDate($date): self
105131
}
106132
};
107133
}
134+
135+
/**
136+
* @return AccessorInterface
137+
*/
138+
private function getAccessor(): AccessorInterface
139+
{
140+
/** @var AccessorInterface|\PHPUnit_Framework_MockObject_MockObject $accessor */
141+
$accessor = $this->getMockBuilder(AccessorInterface::class)->getMockForAbstractClass();
142+
143+
$accessor->expects(self::any())->method('getValue')->willReturnCallback(
144+
function ($object) {
145+
return $object->getDate();
146+
}
147+
);
148+
149+
return $accessor;
150+
}
108151

109152
/**
110153
* @return FieldNormalizerInterface

0 commit comments

Comments
 (0)