|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of the Symfony package. |
|
5 * |
|
6 * (c) Fabien Potencier <fabien@symfony.com> |
|
7 * |
|
8 * For the full copyright and license information, please view the LICENSE |
|
9 * file that was distributed with this source code. |
|
10 */ |
|
11 |
|
12 namespace Symfony\Component\Security\Acl\Domain; |
|
13 |
|
14 use Symfony\Component\Security\Acl\Exception\InvalidDomainObjectException; |
|
15 use Symfony\Component\Security\Acl\Model\DomainObjectInterface; |
|
16 use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface; |
|
17 |
|
18 /** |
|
19 * ObjectIdentity implementation |
|
20 * |
|
21 * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
|
22 */ |
|
23 final class ObjectIdentity implements ObjectIdentityInterface |
|
24 { |
|
25 private $identifier; |
|
26 private $type; |
|
27 |
|
28 /** |
|
29 * Constructor. |
|
30 * |
|
31 * @param string $identifier |
|
32 * @param string $type |
|
33 * @return void |
|
34 */ |
|
35 public function __construct($identifier, $type) |
|
36 { |
|
37 if (empty($identifier)) { |
|
38 throw new \InvalidArgumentException('$identifier cannot be empty.'); |
|
39 } |
|
40 if (empty($type)) { |
|
41 throw new \InvalidArgumentException('$type cannot be empty.'); |
|
42 } |
|
43 |
|
44 $this->identifier = $identifier; |
|
45 $this->type = $type; |
|
46 } |
|
47 |
|
48 /** |
|
49 * Constructs an ObjectIdentity for the given domain object |
|
50 * |
|
51 * @param object $domainObject |
|
52 * @throws \InvalidArgumentException |
|
53 * @return ObjectIdentity |
|
54 */ |
|
55 static public function fromDomainObject($domainObject) |
|
56 { |
|
57 if (!is_object($domainObject)) { |
|
58 throw new InvalidDomainObjectException('$domainObject must be an object.'); |
|
59 } |
|
60 |
|
61 try { |
|
62 if ($domainObject instanceof DomainObjectInterface) { |
|
63 return new self($domainObject->getObjectIdentifier(), get_class($domainObject)); |
|
64 } else if (method_exists($domainObject, 'getId')) { |
|
65 return new self($domainObject->getId(), get_class($domainObject)); |
|
66 } |
|
67 } catch (\InvalidArgumentException $invalid) { |
|
68 throw new InvalidDomainObjectException($invalid->getMessage(), 0, $invalid); |
|
69 } |
|
70 |
|
71 throw new InvalidDomainObjectException('$domainObject must either implement the DomainObjectInterface, or have a method named "getId".'); |
|
72 } |
|
73 |
|
74 /** |
|
75 * {@inheritDoc} |
|
76 */ |
|
77 public function getIdentifier() |
|
78 { |
|
79 return $this->identifier; |
|
80 } |
|
81 |
|
82 /** |
|
83 * {@inheritDoc} |
|
84 */ |
|
85 public function getType() |
|
86 { |
|
87 return $this->type; |
|
88 } |
|
89 |
|
90 /** |
|
91 * {@inheritDoc} |
|
92 */ |
|
93 public function equals(ObjectIdentityInterface $identity) |
|
94 { |
|
95 // comparing the identifier with === might lead to problems, so we |
|
96 // waive this restriction |
|
97 return $this->identifier == $identity->getIdentifier() |
|
98 && $this->type === $identity->getType(); |
|
99 } |
|
100 |
|
101 /** |
|
102 * Returns a textual representation of this object identity |
|
103 * |
|
104 * @return string |
|
105 */ |
|
106 public function __toString() |
|
107 { |
|
108 return sprintf('ObjectIdentity(%s, %s)', $this->identifier, $this->type); |
|
109 } |
|
110 } |