ObjectIdentity.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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\Core\Util\ClassUtils;
  12. use Symfony\Component\Security\Acl\Exception\InvalidDomainObjectException;
  13. use Symfony\Component\Security\Acl\Model\DomainObjectInterface;
  14. use Symfony\Component\Security\Acl\Model\ObjectIdentityInterface;
  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 (empty($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. * @throws InvalidDomainObjectException
  48. * @return ObjectIdentity
  49. */
  50. public static function fromDomainObject($domainObject)
  51. {
  52. if (!is_object($domainObject)) {
  53. throw new InvalidDomainObjectException('$domainObject must be an object.');
  54. }
  55. try {
  56. if ($domainObject instanceof DomainObjectInterface) {
  57. return new self($domainObject->getObjectIdentifier(), ClassUtils::getRealClass($domainObject));
  58. } elseif (method_exists($domainObject, 'getId')) {
  59. return new self($domainObject->getId(), ClassUtils::getRealClass($domainObject));
  60. }
  61. } catch (\InvalidArgumentException $invalid) {
  62. throw new InvalidDomainObjectException($invalid->getMessage(), 0, $invalid);
  63. }
  64. throw new InvalidDomainObjectException('$domainObject must either implement the DomainObjectInterface, or have a method named "getId".');
  65. }
  66. /**
  67. * {@inheritDoc}
  68. */
  69. public function getIdentifier()
  70. {
  71. return $this->identifier;
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. public function getType()
  77. {
  78. return $this->type;
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. public function equals(ObjectIdentityInterface $identity)
  84. {
  85. // comparing the identifier with === might lead to problems, so we
  86. // waive this restriction
  87. return $this->identifier == $identity->getIdentifier()
  88. && $this->type === $identity->getType();
  89. }
  90. /**
  91. * Returns a textual representation of this object identity
  92. *
  93. * @return string
  94. */
  95. public function __toString()
  96. {
  97. return sprintf('ObjectIdentity(%s, %s)', $this->identifier, $this->type);
  98. }
  99. }