ObjectIdentity.php 3.1 KB

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