-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetadataManager.php
More file actions
112 lines (89 loc) · 2.63 KB
/
Copy pathMetadataManager.php
File metadata and controls
112 lines (89 loc) · 2.63 KB
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
103
104
105
106
107
108
109
110
111
112
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Metadata;
use Klipper\Component\Metadata\Exception\ObjectMetadataNotFoundException;
/**
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class MetadataManager implements MetadataManagerInterface
{
protected MetadataFactoryInterface $factory;
/**
* @var null|ObjectMetadataInterface[]
*/
protected ?array $objects = null;
/**
* @var null|ChoiceInterface[]
*/
protected ?array $choices = null;
/**
* @param MetadataFactoryInterface $factory The metadata factory
*/
public function __construct(MetadataFactoryInterface $factory)
{
$this->factory = $factory;
}
public function all(): array
{
if (null === $this->objects) {
$this->objects = [];
foreach ($this->factory->getManagedClasses()->all() as $class) {
$this->get($class);
}
}
return $this->objects;
}
public function has(string $class): bool
{
return $this->factory->isManagedClass($class);
}
public function hasByName(string $name): bool
{
return $this->factory->isManagedByName($name);
}
public function get(string $class): ObjectMetadataInterface
{
$class = $this->factory->getManagedClass($class);
if (!isset($this->objects[$class])) {
$meta = $this->factory->create($class);
$this->objects[$class] = $meta;
}
return $this->objects[$class];
}
public function getByName(string $name): ObjectMetadataInterface
{
if ($this->hasByName($name)) {
return $this->get($name);
}
throw new ObjectMetadataNotFoundException($name);
}
public function allChoices(): array
{
if (null === $this->choices) {
$this->choices = [];
foreach ($this->factory->getChoiceNames()->all() as $name) {
$this->getChoice($name);
}
}
return $this->choices;
}
public function hasChoice(string $name): bool
{
return $this->factory->isChoiceManaged($name);
}
public function getChoice(string $name): ChoiceInterface
{
if (!isset($this->choices[$name])) {
$choice = $this->factory->createChoice($name);
$this->choices[$name] = $choice;
}
return $this->choices[$name];
}
}