PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Zikula/Core/Doctrine/EntityAccess.php

https://github.com/antoniom/core
PHP | 98 lines | 62 code | 16 blank | 20 comment | 2 complexity | 4d877e160bac1b31c37a6e9fc853a026 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, MIT
  1. <?php
  2. /**
  3. * Copyright 2010 Zikula Foundation
  4. *
  5. * This work is contributed to the Zikula Foundation under one or more
  6. * Contributor Agreements and licensed to You under the following license:
  7. *
  8. * @license GNU/LGPLv3 (or at your option, any later version).
  9. * @package Zikula
  10. *
  11. * Please see the NOTICE file distributed with this source code for further
  12. * information regarding copyright and licensing.
  13. */
  14. namespace Zikula\Core\Doctrine;
  15. class EntityAccess implements \ArrayAccess
  16. {
  17. /**
  18. * @var ReflectionObject
  19. */
  20. protected $reflection;
  21. /**
  22. * Get this reflection.
  23. *
  24. * @return ReflectionObject
  25. */
  26. public function getReflection()
  27. {
  28. if (!is_null($this->reflection)) {
  29. return $this->reflection;
  30. }
  31. $this->reflection = new \ReflectionObject($this);
  32. return $this->reflection;
  33. }
  34. public function offsetExists($key)
  35. {
  36. return method_exists($this, "get" . ucfirst($key));
  37. }
  38. public function offsetGet($key)
  39. {
  40. $method = "get" . ucfirst($key);
  41. return $this->$method();
  42. }
  43. public function offsetSet($key, $value)
  44. {
  45. $method = "set" . ucfirst($key);
  46. $this->$method($value);
  47. }
  48. public function offsetUnset($key)
  49. {
  50. $this->offsetSet($key, null);
  51. }
  52. public function toArray()
  53. {
  54. $r = $this->getReflection();
  55. $array = array();
  56. $excluded = array(
  57. 'reflection',
  58. '_entityPersister',
  59. '_identifier',
  60. '__isInitialized__'
  61. );
  62. while($r !== false) {
  63. $properties = $r->getProperties();
  64. $r = $r->getParentClass();
  65. foreach ($properties as $property) {
  66. if (in_array($property->name, $excluded)) {
  67. continue;
  68. }
  69. $method = "get" . ucfirst($property->name);
  70. $array[$property->name] = $this->$method();
  71. }
  72. }
  73. return $array;
  74. }
  75. public function merge(array $array)
  76. {
  77. foreach ($array as $key => $value) {
  78. $method = "set" . ucfirst($key);
  79. $this->$method($value);
  80. }
  81. }
  82. }