PropertyAccessorInterface.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\PropertyAccess;
  11. /**
  12. * Writes and reads values to/from an object/array graph.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. interface PropertyAccessorInterface
  17. {
  18. /**
  19. * Sets the value at the end of the property path of the object graph.
  20. *
  21. * Example:
  22. *
  23. * use Symfony\Component\PropertyAccess\PropertyAccess;
  24. *
  25. * $propertyAccessor = PropertyAccess::createPropertyAccessor();
  26. *
  27. * echo $propertyAccessor->setValue($object, 'child.name', 'Fabien');
  28. * // equals echo $object->getChild()->setName('Fabien');
  29. *
  30. * This method first tries to find a public setter for each property in the
  31. * path. The name of the setter must be the camel-cased property name
  32. * prefixed with "set".
  33. *
  34. * If the setter does not exist, this method tries to find a public
  35. * property. The value of the property is then changed.
  36. *
  37. * If neither is found, an exception is thrown.
  38. *
  39. * @param object|array $objectOrArray The object or array to modify
  40. * @param string|PropertyPathInterface $propertyPath The property path to modify
  41. * @param mixed $value The value to set at the end of the property path
  42. *
  43. * @throws Exception\InvalidArgumentException If the property path is invalid
  44. * @throws Exception\AccessException If a property/index does not exist or is not public
  45. * @throws Exception\UnexpectedTypeException If a value within the path is neither object nor array
  46. */
  47. public function setValue(&$objectOrArray, $propertyPath, $value);
  48. /**
  49. * Returns the value at the end of the property path of the object graph.
  50. *
  51. * Example:
  52. *
  53. * use Symfony\Component\PropertyAccess\PropertyAccess;
  54. *
  55. * $propertyAccessor = PropertyAccess::createPropertyAccessor();
  56. *
  57. * echo $propertyAccessor->getValue($object, 'child.name');
  58. * // equals echo $object->getChild()->getName();
  59. *
  60. * This method first tries to find a public getter for each property in the
  61. * path. The name of the getter must be the camel-cased property name
  62. * prefixed with "get", "is", or "has".
  63. *
  64. * If the getter does not exist, this method tries to find a public
  65. * property. The value of the property is then returned.
  66. *
  67. * If none of them are found, an exception is thrown.
  68. *
  69. * @param object|array $objectOrArray The object or array to traverse
  70. * @param string|PropertyPathInterface $propertyPath The property path to read
  71. *
  72. * @return mixed The value at the end of the property path
  73. *
  74. * @throws Exception\InvalidArgumentException If the property path is invalid
  75. * @throws Exception\AccessException If a property/index does not exist or is not public
  76. * @throws Exception\UnexpectedTypeException If a value within the path is neither object
  77. * nor array
  78. */
  79. public function getValue($objectOrArray, $propertyPath);
  80. /**
  81. * Returns whether a value can be written at a given property path.
  82. *
  83. * Whenever this method returns true, {@link setValue()} is guaranteed not
  84. * to throw an exception when called with the same arguments.
  85. *
  86. * @param object|array $objectOrArray The object or array to check
  87. * @param string|PropertyPathInterface $propertyPath The property path to check
  88. *
  89. * @return bool Whether the value can be set
  90. *
  91. * @throws Exception\InvalidArgumentException If the property path is invalid
  92. */
  93. public function isWritable($objectOrArray, $propertyPath);
  94. /**
  95. * Returns whether a property path can be read from an object graph.
  96. *
  97. * Whenever this method returns true, {@link getValue()} is guaranteed not
  98. * to throw an exception when called with the same arguments.
  99. *
  100. * @param object|array $objectOrArray The object or array to check
  101. * @param string|PropertyPathInterface $propertyPath The property path to check
  102. *
  103. * @return bool Whether the property path can be read
  104. *
  105. * @throws Exception\InvalidArgumentException If the property path is invalid
  106. */
  107. public function isReadable($objectOrArray, $propertyPath);
  108. }