1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Psi\Component\ContentType\Metadata; |
4
|
|
|
|
5
|
|
|
use Metadata\MergeableClassMetadata; |
6
|
|
|
use Metadata\PropertyMetadata; |
|
|
|
|
7
|
|
|
use Psi\Component\ContentType\Metadata\PropertyMetadata as PsiPropertyMetadata; |
8
|
|
|
|
9
|
|
|
class ClassMetadata extends MergeableClassMetadata |
10
|
|
|
{ |
11
|
|
|
private $roles = []; |
12
|
|
|
|
13
|
|
|
public function __construct( |
14
|
|
|
$name |
15
|
|
|
) { |
16
|
|
|
parent::__construct($name); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function getName() |
20
|
|
|
{ |
21
|
|
|
return $this->name; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getPropertyMetadata() |
25
|
|
|
{ |
26
|
|
|
return $this->propertyMetadata; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function addPropertyMetadata(PropertyMetadata $metadata) |
30
|
|
|
{ |
31
|
|
|
$this->doAddPropertyMetadata($metadata); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function hasPropertyByRole($role) |
35
|
|
|
{ |
36
|
|
|
return isset($this->roles[$role]); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getPropertyByRole($role) |
40
|
|
|
{ |
41
|
|
|
if (!isset($this->roles[$role])) { |
42
|
|
|
throw new \InvalidArgumentException(sprintf( |
43
|
|
|
'No property exists with role "%s"', |
44
|
|
|
$role |
45
|
|
|
)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return $this->propertyMetadata[$this->roles[$role]]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function doAddPropertyMetadata(PsiPropertyMetadata $metadata) |
52
|
|
|
{ |
53
|
|
|
parent::addPropertyMetadata($metadata); |
|
|
|
|
54
|
|
|
if (!$metadata->getRole()) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (isset($this->roles[$metadata->getRole()])) { |
59
|
|
|
$propertyName = $this->roles[$metadata->getRole()]; |
60
|
|
|
throw new \InvalidArgumentException(sprintf( |
61
|
|
|
'Role "%s" has already been assigned to property "%s" (on property "%s")', |
62
|
|
|
$metadata->getRole(), $propertyName, $metadata->getName() |
63
|
|
|
)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
$this->roles[$metadata->getRole()] = $metadata->name; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: