Issues (39)

src/Metadata/ClassMetadata.php (3 issues)

1
<?php
2
3
namespace Bdf\Serializer\Metadata;
4
5
/**
6
 * ClassMetadata
7
 *
8
 * @author  Seb
9
 *
10
 * @template T as object
11
 */
12
class ClassMetadata
13
{
14
    /**
15
     * The class name
16
     *
17
     * @var class-string<T>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
18
     */
19
    public $name;
20
21
    /**
22
     * The properties normalizer
23
     *
24
     * @var PropertyMetadata[]
25
     */
26
    public $properties = [];
27
28
    /**
29
     * The property aliases
30
     *
31
     * @var string[]
32
     */
33
    private $propertyAliases = [];
34
35
    /**
36
     * The post normalization method
37
     *
38
     * @var string|null
39
     */
40
    public $postDenormalization;
41
42
    /**
43
     * ClassMetadata constructor.
44
     *
45
     * @param class-string<T> $class
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
46
     */
47 196
    public function __construct(string $class)
48
    {
49 196
        $this->name = $class;
50
    }
51
52
    /**
53
     * Add a property normalizer
54
     *
55
     * @param PropertyMetadata $normalizer
56
     */
57 190
    public function addProperty(PropertyMetadata $normalizer): void
58
    {
59 190
        $this->properties[$normalizer->name()] = $normalizer;
60
    }
61
62
    /**
63
     * Get a property normalizer
64
     *
65
     * @param string $name
66
     *
67
     * @return null|PropertyMetadata
68
     */
69 104
    public function property(string $name): ?PropertyMetadata
70
    {
71 104
        if (isset($this->properties[$name])) {
72 102
            return $this->properties[$name];
73
        }
74
75 20
        if (isset($this->propertyAliases[$name])) {
76 16
            return $this->property($this->propertyAliases[$name]);
77
        }
78
79 4
        return null;
80
    }
81
82
    /**
83
     * Get all properties normalizer
84
     *
85
     * @return PropertyMetadata[]
86
     */
87
    public function properties(): array
88
    {
89
        return $this->properties;
90
    }
91
92
    /**
93
     * Get the class name
94
     *
95
     * @return class-string<T>
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string<T> at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string<T>.
Loading history...
96
     */
97 10
    public function name(): string
98
    {
99 10
        return $this->name;
100
    }
101
102
    /**
103
     * Set the property aliases
104
     *
105
     * @param array $aliases
106
     */
107 196
    public function setPropertyAliases(array $aliases): void
108
    {
109 196
        $this->propertyAliases = $aliases;
110
    }
111
112
    /**
113
     * Set the post normalization method
114
     *
115
     * @param string $postDenormalization
116
     */
117 196
    public function setPostDenormalization(?string $postDenormalization): void
118
    {
119 196
        $this->postDenormalization = $postDenormalization;
120
    }
121
122
    /**
123
     * Set the post normalization method
124
     *
125
     * @param object $object
126
     */
127 64
    public function postDenormalization($object): void
128
    {
129 64
        if ($this->postDenormalization === null) {
130 62
            return;
131
        }
132
133 2
        $method = $this->postDenormalization;
134 2
        $object->$method();
135
    }
136
}
137