PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/doctrine/vendors/Symfony/Component/Security/Acl/Domain/ObjectIdentity.php

https://bitbucket.org/NordLabs/yiidoctrine
PHP | 110 lines | 54 code | 13 blank | 43 comment | 6 complexity | a5ae69e83da01882b146b541034986d4 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  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. /**
  15. * ObjectIdentity implementation
  16. *
  17. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  18. */
  19. final class ObjectIdentity implements ObjectIdentityInterface
  20. {
  21. private $identifier;
  22. private $type;
  23. /**
  24. * Constructor.
  25. *
  26. * @param string $identifier
  27. * @param string $type
  28. * @return void
  29. */
  30. public function __construct($identifier, $type)
  31. {
  32. if (empty($identifier)) {
  33. throw new \InvalidArgumentException('$identifier cannot be empty.');
  34. }
  35. if (empty($type)) {
  36. throw new \InvalidArgumentException('$type cannot be empty.');
  37. }
  38. $this->identifier = $identifier;
  39. $this->type = $type;
  40. }
  41. /**
  42. * Constructs an ObjectIdentity for the given domain object
  43. *
  44. * @param object $domainObject
  45. * @throws \InvalidArgumentException
  46. * @return ObjectIdentity
  47. */
  48. static public function fromDomainObject($domainObject)
  49. {
  50. if (!is_object($domainObject)) {
  51. throw new InvalidDomainObjectException('$domainObject must be an object.');
  52. }
  53. try {
  54. if ($domainObject instanceof DomainObjectInterface) {
  55. return new self($domainObject->getObjectIdentifier(), get_class($domainObject));
  56. } elseif (method_exists($domainObject, 'getId')) {
  57. return new self($domainObject->getId(), get_class($domainObject));
  58. }
  59. } catch (\InvalidArgumentException $invalid) {
  60. throw new InvalidDomainObjectException($invalid->getMessage(), 0, $invalid);
  61. }
  62. throw new InvalidDomainObjectException('$domainObject must either implement the DomainObjectInterface, or have a method named "getId".');
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getIdentifier()
  68. {
  69. return $this->identifier;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getType()
  75. {
  76. return $this->type;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function equals(ObjectIdentityInterface $identity)
  82. {
  83. // comparing the identifier with === might lead to problems, so we
  84. // waive this restriction
  85. return $this->identifier == $identity->getIdentifier()
  86. && $this->type === $identity->getType();
  87. }
  88. /**
  89. * Returns a textual representation of this object identity
  90. *
  91. * @return string
  92. */
  93. public function __toString()
  94. {
  95. return sprintf('ObjectIdentity(%s, %s)', $this->identifier, $this->type);
  96. }
  97. }