This repository has been archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AbstractPropertyMetaData.php
102 lines (87 loc) · 2.47 KB
/
AbstractPropertyMetaData.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
declare(strict_types = 1);
/*
* @author Martin Fris <rasta@lj.sk>
*/
namespace BetterSerializer\DataBind\MetaData\Model\PropertyModel;
use BetterSerializer\DataBind\MetaData\Annotations\AnnotationInterface;
use BetterSerializer\DataBind\MetaData\Annotations\Groups;
use BetterSerializer\DataBind\MetaData\Annotations\Property;
use BetterSerializer\DataBind\MetaData\Annotations\PropertyInterface;
use BetterSerializer\DataBind\MetaData\Type\TypeInterface;
use LogicException;
use RuntimeException;
/**
* ClassModel PropertyMetadata
*
* @author mfris
* @package BetterSerializer\DataBind\MetaData
*/
abstract class AbstractPropertyMetaData implements PropertyMetaDataInterface
{
/**
* @var AnnotationInterface[]
*/
private $annotations;
/**
* @var TypeInterface
*/
private $type;
/**
* PropertyMetadata constructor.
*
* @param AnnotationInterface[] $annotations
* @param TypeInterface $type
* @throws RuntimeException
*/
public function __construct(array $annotations, TypeInterface $type)
{
$this->setAnnotations($annotations);
$this->type = $type;
}
/**
* @return TypeInterface
*/
public function getType(): TypeInterface
{
return $this->type;
}
/**
* @return string
* @throws LogicException
* @throws RuntimeException
*/
public function getOutputKey(): string
{
if (!isset($this->annotations[Property::ANNOTATION_NAME])) {
throw new LogicException('Property annotation missing.');
}
/* @var $propertyAnnotation PropertyInterface */
$propertyAnnotation = $this->annotations[Property::ANNOTATION_NAME];
$outputKey = $propertyAnnotation->getName();
if (!$outputKey) {
throw new RuntimeException('Missing property name.');
}
return $outputKey;
}
/**
* @return string[]
*/
public function getGroups(): array
{
/* @var $groupsAnnotation Groups */
$groupsAnnotation = $this->annotations[Groups::ANNOTATION_NAME];
return $groupsAnnotation->getGroups();
}
/**
* @param array $annotations
* @throws RuntimeException
*/
private function setAnnotations(array $annotations): void
{
if (!isset($annotations[Groups::ANNOTATION_NAME])) {
throw new RuntimeException('Groups annotation missing.');
}
$this->annotations = $annotations;
}
}