1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the FiveLab Resource package |
7
|
|
|
* |
8
|
|
|
* (c) FiveLab |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace FiveLab\Component\Resource\Serializer\Resolver; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Simple supportable for accept via format. |
18
|
|
|
* |
19
|
|
|
* @author Vitaliy Zhuk <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class AcceptFormatSupportable implements ResourceSerializerSupportableInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $acceptedMediaTypes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
private $supportClasses; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $notSupportedClasses; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
* |
41
|
|
|
* @param array $acceptedMediaTypes |
42
|
|
|
* @param array $supportClasses |
43
|
|
|
* @param array $notSupportedClasses |
44
|
|
|
*/ |
45
|
4 |
|
public function __construct(array $acceptedMediaTypes, array $supportClasses = [], array $notSupportedClasses = []) |
46
|
|
|
{ |
47
|
4 |
|
$this->acceptedMediaTypes = $acceptedMediaTypes; |
48
|
4 |
|
$this->supportClasses = $supportClasses; |
49
|
4 |
|
$this->notSupportedClasses = $notSupportedClasses; |
50
|
4 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
4 |
|
public function supports(string $resourceClass, string $mediaType): bool |
56
|
|
|
{ |
57
|
4 |
|
if (!\in_array($mediaType, $this->acceptedMediaTypes, true)) { |
58
|
1 |
|
return false; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
if (\count($this->supportClasses)) { |
62
|
2 |
|
foreach ($this->supportClasses as $supportClass) { |
63
|
2 |
|
if (\is_a($resourceClass, $supportClass, true)) { |
64
|
2 |
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
foreach ($this->notSupportedClasses as $notSupportedClass) { |
72
|
1 |
|
if (\is_a($resourceClass, $notSupportedClass, true)) { |
73
|
1 |
|
return false; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
1 |
|
return true; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|