PropertyAccessorInterface.php 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @return void
  40. *
  41. * @throws Exception\InvalidArgumentException If the property path is invalid
  42. * @throws Exception\AccessException If a property/index does not exist or is not public
  43. * @throws Exception\UnexpectedTypeException If a value within the path is neither object nor array
  44. */
  45. public function setValue(object|array &$objectOrArray, string|PropertyPathInterface $propertyPath, mixed $value);
  46. /**
  47. * Returns the value at the end of the property path of the object graph.
  48. *
  49. * Example:
  50. *
  51. * use Symfony\Component\PropertyAccess\PropertyAccess;
  52. *
  53. * $propertyAccessor = PropertyAccess::createPropertyAccessor();
  54. *
  55. * echo $propertyAccessor->getValue($object, 'child.name');
  56. * // equals echo $object->getChild()->getName();
  57. *
  58. * This method first tries to find a public getter for each property in the
  59. * path. The name of the getter must be the camel-cased property name
  60. * prefixed with "get", "is", or "has".
  61. *
  62. * If the getter does not exist, this method tries to find a public
  63. * property. The value of the property is then returned.
  64. *
  65. * If none of them are found, an exception is thrown.
  66. *
  67. * @throws Exception\InvalidArgumentException If the property path is invalid
  68. * @throws Exception\AccessException If a property/index does not exist or is not public
  69. * @throws Exception\UnexpectedTypeException If a value within the path is neither object
  70. * nor array
  71. */
  72. public function getValue(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): mixed;
  73. /**
  74. * Returns whether a value can be written at a given property path.
  75. *
  76. * Whenever this method returns true, {@link setValue()} is guaranteed not
  77. * to throw an exception when called with the same arguments.
  78. *
  79. * @throws Exception\InvalidArgumentException If the property path is invalid
  80. */
  81. public function isWritable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool;
  82. /**
  83. * Returns whether a property path can be read from an object graph.
  84. *
  85. * Whenever this method returns true, {@link getValue()} is guaranteed not
  86. * to throw an exception when called with the same arguments.
  87. *
  88. * @throws Exception\InvalidArgumentException If the property path is invalid
  89. */
  90. public function isReadable(object|array $objectOrArray, string|PropertyPathInterface $propertyPath): bool;
  91. }