FooArrayAccess.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace Sonata\AdminBundle\Tests\Fixtures\Entity;
  3. class FooArrayAccess implements \ArrayAccess
  4. {
  5. // methods to enable ArrayAccess
  6. public function offsetExists($offset)
  7. {
  8. $value = $this->offsetGet($offset);
  9. return $value !== null;
  10. }
  11. public function offsetGet($offset)
  12. {
  13. $offset = str_replace('_', '', $offset); // method names always use camels, field names can use snakes
  14. $methodName = "get$offset";
  15. if (method_exists($this, $methodName)) {
  16. return $this->$methodName();
  17. } else {
  18. return null;
  19. }
  20. }
  21. public function offsetSet($offset, $value)
  22. {
  23. throw new \BadMethodCallException ("Array access of class " . get_class($this) . " is read-only!");
  24. }
  25. public function offsetUnset($offset)
  26. {
  27. throw new \BadMethodCallException("Array access of class " . get_class($this) . " is read-only!");
  28. }
  29. private $bar;
  30. private $baz;
  31. public function getBar()
  32. {
  33. return $this->bar;
  34. }
  35. public function setBar($bar)
  36. {
  37. $this->bar = $bar;
  38. }
  39. public function getBaz()
  40. {
  41. return $this->baz;
  42. }
  43. public function setBaz($baz)
  44. {
  45. $this->baz = $baz;
  46. }
  47. public function __toString()
  48. {
  49. return (string) $this->bar;
  50. }
  51. }