FooArrayAccess.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. $value = $this->offsetGet($offset);
  8. return $value !== null;
  9. }
  10. public function offsetGet($offset) {
  11. $offset = str_replace('_', '', $offset); // method names always use camels, field names can use snakes
  12. $methodName = "get$offset";
  13. if (method_exists($this, $methodName)) {
  14. return $this->$methodName();
  15. } else {
  16. return null;
  17. }
  18. }
  19. public function offsetSet($offset, $value) {
  20. throw new \BadMethodCallException ("Array access of class " . get_class($this) . " is read-only!");
  21. }
  22. public function offsetUnset($offset) {
  23. throw new \BadMethodCallException("Array access of class " . get_class($this) . " is read-only!");
  24. }
  25. private $bar;
  26. private $baz;
  27. public function getBar()
  28. {
  29. return $this->bar;
  30. }
  31. public function setBar($bar)
  32. {
  33. $this->bar = $bar;
  34. }
  35. public function getBaz()
  36. {
  37. return $this->baz;
  38. }
  39. public function setBaz($baz)
  40. {
  41. $this->baz = $baz;
  42. }
  43. public function __toString()
  44. {
  45. return (string) $this->bar;
  46. }
  47. }