1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the StfalconApiBundle. |
4
|
|
|
* |
5
|
|
|
* (c) Stfalcon LLC <stfalcon.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace StfalconStudio\ApiBundle\Annotation; |
14
|
|
|
|
15
|
|
|
use Doctrine\Common\Annotations\Annotation; |
16
|
|
|
use StfalconStudio\ApiBundle\DTO\DtoInterface; |
17
|
|
|
use StfalconStudio\ApiBundle\Exception\InvalidArgumentException; |
18
|
|
|
use StfalconStudio\ApiBundle\Exception\LogicException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Data Transfer Object Annotation. |
22
|
|
|
* |
23
|
|
|
* @Annotation |
24
|
|
|
* |
25
|
|
|
* @Target({"CLASS"}) |
26
|
|
|
*/ |
27
|
|
|
class DTO implements DtoAnnotationInterface |
28
|
|
|
{ |
29
|
|
|
private const DTO_SUFFIX = 'Dto'; |
30
|
|
|
|
31
|
|
|
/** @var string */ |
32
|
|
|
private $class; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param mixed[] $options |
36
|
|
|
* |
37
|
|
|
* @throws LogicException |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
public function __construct(array $options) |
41
|
|
|
{ |
42
|
|
|
if (!\array_key_exists('value', $options)) { |
43
|
|
|
throw new LogicException('DTO class must be set.'); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$class = $options['value']; |
47
|
|
|
|
48
|
|
|
if (!\is_string($class)) { |
49
|
|
|
throw new InvalidArgumentException('Value should be string'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
if (!\class_exists($class)) { |
53
|
|
|
throw new InvalidArgumentException(\sprintf('Class %s does not exist.', $class)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
if (!\is_subclass_of($class, DtoInterface::class)) { |
|
|
|
|
57
|
|
|
throw new InvalidArgumentException(\sprintf('Class %s does not implement %s interface.', $class, DtoInterface::class)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if (self::DTO_SUFFIX !== \mb_substr($class, -3)) { |
61
|
|
|
throw new InvalidArgumentException(\sprintf('Class name %s must be suffixed with "%s".', $class, self::DTO_SUFFIX)); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$this->class = $class; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function getClass(): string |
71
|
|
|
{ |
72
|
|
|
return $this->class; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|