AbstractWrapper.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace Gedmo\Tool\Wrapper;
  3. use Doctrine\ODM\MongoDB\DocumentManager;
  4. use Doctrine\Common\Persistence\ObjectManager;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use Gedmo\Tool\WrapperInterface;
  7. use Gedmo\Exception\UnsupportedObjectManagerException;
  8. /**
  9. * Wraps entity or proxy for more convenient
  10. * manipulation
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. abstract class AbstractWrapper implements WrapperInterface
  16. {
  17. /**
  18. * Object metadata
  19. *
  20. * @var object
  21. */
  22. protected $meta;
  23. /**
  24. * Wrapped object
  25. *
  26. * @var object
  27. */
  28. protected $object;
  29. /**
  30. * Object manager instance
  31. *
  32. * @var \Doctrine\Common\Persistence\ObjectManager
  33. */
  34. protected $om;
  35. /**
  36. * List of wrapped object references
  37. *
  38. * @var array
  39. */
  40. private static $wrappedObjectReferences;
  41. /**
  42. * Wrap object factory method
  43. *
  44. * @param object $object
  45. * @param ObjectManager $om
  46. *
  47. * @throws \Gedmo\Exception\UnsupportedObjectManagerException
  48. *
  49. * @return \Gedmo\Tool\WrapperInterface
  50. */
  51. public static function wrap($object, ObjectManager $om)
  52. {
  53. if ($om instanceof EntityManagerInterface) {
  54. return new EntityWrapper($object, $om);
  55. } elseif ($om instanceof DocumentManager) {
  56. return new MongoDocumentWrapper($object, $om);
  57. }
  58. throw new UnsupportedObjectManagerException('Given object manager is not managed by wrapper');
  59. }
  60. public static function clear()
  61. {
  62. self::$wrappedObjectReferences = array();
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getObject()
  68. {
  69. return $this->object;
  70. }
  71. /**
  72. * {@inheritDoc}
  73. */
  74. public function getMetadata()
  75. {
  76. return $this->meta;
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. public function populate(array $data)
  82. {
  83. foreach ($data as $field => $value) {
  84. $this->setPropertyValue($field, $value);
  85. }
  86. return $this;
  87. }
  88. }