ReflectionEmbeddedProperty.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Mapping;
  20. use Doctrine\Instantiator\Instantiator;
  21. use ReflectionProperty;
  22. /**
  23. * Acts as a proxy to a nested Property structure, making it look like
  24. * just a single scalar property.
  25. *
  26. * This way value objects "just work" without UnitOfWork, Persisters or Hydrators
  27. * needing any changes.
  28. *
  29. * TODO: Move this class into Common\Reflection
  30. */
  31. class ReflectionEmbeddedProperty extends ReflectionProperty
  32. {
  33. /** @var ReflectionProperty reflection property of the class where the embedded object has to be put */
  34. private $parentProperty;
  35. /** @var ReflectionProperty reflection property of the embedded object */
  36. private $childProperty;
  37. /** @var string name of the embedded class to be eventually instantiated */
  38. private $embeddedClass;
  39. /** @var Instantiator|null */
  40. private $instantiator;
  41. /**
  42. * @param string $embeddedClass
  43. */
  44. public function __construct(ReflectionProperty $parentProperty, ReflectionProperty $childProperty, $embeddedClass)
  45. {
  46. $this->parentProperty = $parentProperty;
  47. $this->childProperty = $childProperty;
  48. $this->embeddedClass = (string) $embeddedClass;
  49. parent::__construct($childProperty->getDeclaringClass()->getName(), $childProperty->getName());
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function getValue($object = null)
  55. {
  56. $embeddedObject = $this->parentProperty->getValue($object);
  57. if ($embeddedObject === null) {
  58. return null;
  59. }
  60. return $this->childProperty->getValue($embeddedObject);
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function setValue($object, $value = null)
  66. {
  67. $embeddedObject = $this->parentProperty->getValue($object);
  68. if ($embeddedObject === null) {
  69. $this->instantiator = $this->instantiator ?: new Instantiator();
  70. $embeddedObject = $this->instantiator->instantiate($this->embeddedClass);
  71. $this->parentProperty->setValue($object, $embeddedObject);
  72. }
  73. $this->childProperty->setValue($embeddedObject, $value);
  74. }
  75. }