1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Apie\CarbonPlugin\Normalizers; |
5
|
|
|
|
6
|
|
|
use Carbon\Carbon; |
7
|
|
|
use Carbon\CarbonImmutable; |
8
|
|
|
use Carbon\CarbonInterface; |
9
|
|
|
use DateTime; |
10
|
|
|
use DateTimeImmutable; |
11
|
|
|
use DateTimeInterface; |
12
|
|
|
use DateTimeZone; |
13
|
|
|
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Extension on DateTimeNormalizer to use Carbon over the regular DateTime class instances. |
17
|
|
|
*/ |
18
|
|
|
class CarbonNormalizer extends DateTimeNormalizer |
19
|
|
|
{ |
20
|
|
|
private $allowedTypes = [ |
21
|
|
|
Carbon::class, |
22
|
|
|
CarbonInterface::class |
23
|
|
|
]; |
24
|
|
|
|
25
|
|
|
private $before = [ |
26
|
|
|
CarbonInterface::class => DateTimeInterface::class, |
27
|
|
|
Carbon::class => DateTime::class, |
28
|
|
|
]; |
29
|
|
|
|
30
|
|
|
private $after = [ |
31
|
|
|
DateTime::class => Carbon::class, |
32
|
|
|
DateTimeInterface::class => Carbon::class, |
33
|
|
|
Carbon::class => Carbon::class, |
34
|
|
|
CarbonInterface::class => Carbon::class, |
35
|
|
|
]; |
36
|
|
|
|
37
|
|
|
public function __construct($defaultContext = [], DateTimeZone $timezone = null) |
38
|
|
|
{ |
39
|
|
|
parent::__construct($defaultContext, $timezone); |
|
|
|
|
40
|
|
|
// carbon 2 support. |
41
|
|
|
if (class_exists(CarbonImmutable::class)) { |
42
|
|
|
$this->allowedTypes[] = CarbonImmutable::class; |
43
|
|
|
$this->before[CarbonImmutable::class] = DateTimeImmutable::class; |
44
|
|
|
$this->after[DateTimeInterface::class] = CarbonImmutable::class; |
45
|
|
|
$this->after[CarbonInterface::class] = CarbonImmutable::class; |
46
|
|
|
$this->after[CarbonImmutable::class] = CarbonImmutable::class; |
47
|
|
|
$this->after[DateTimeImmutable::class] = CarbonImmutable::class; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function denormalize($data, $type, $format = null, array $context = []) |
55
|
|
|
{ |
56
|
|
|
$internalType = $type; |
57
|
|
|
if (isset($this->before[$type])) { |
58
|
|
|
$internalType = $this->before[$type]; |
59
|
|
|
} |
60
|
|
|
$result = parent::denormalize($data, $internalType, $format, $context); |
61
|
|
|
if (isset($this->after[$type])) { |
62
|
|
|
return $this->after[$type]::make($result); |
63
|
|
|
} |
64
|
|
|
return $result; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function supportsDenormalization($data, $type, $format = null) |
71
|
|
|
{ |
72
|
|
|
return in_array($type, $this->allowedTypes) || parent::supportsDenormalization($data, $type, $format); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
|
public function hasCacheableSupportsMethod(): bool |
79
|
|
|
{ |
80
|
|
|
return true; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.