ArrayPropertyTrait.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @package Grav\Framework\Object
  4. *
  5. * @copyright Copyright (c) 2015 - 2023 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Framework\Object\Property;
  9. use InvalidArgumentException;
  10. use function array_key_exists;
  11. /**
  12. * Array Property Trait
  13. *
  14. * Stores all object properties into an array.
  15. *
  16. * @package Grav\Framework\Object\Property
  17. */
  18. trait ArrayPropertyTrait
  19. {
  20. /** @var array Properties of the object. */
  21. private $_elements;
  22. /**
  23. * @param array $elements
  24. * @param string|null $key
  25. * @throws InvalidArgumentException
  26. */
  27. public function __construct(array $elements = [], $key = null)
  28. {
  29. $this->setElements($elements);
  30. $this->setKey($key ?? '');
  31. }
  32. /**
  33. * @param string $property Object property name.
  34. * @return bool True if property has been defined (can be null).
  35. */
  36. protected function doHasProperty($property)
  37. {
  38. return array_key_exists($property, $this->_elements);
  39. }
  40. /**
  41. * @param string $property Object property to be fetched.
  42. * @param mixed $default Default value if property has not been set.
  43. * @param bool $doCreate Set true to create variable.
  44. * @return mixed Property value.
  45. */
  46. protected function &doGetProperty($property, $default = null, $doCreate = false)
  47. {
  48. if (!array_key_exists($property, $this->_elements)) {
  49. if ($doCreate) {
  50. $this->_elements[$property] = null;
  51. } else {
  52. return $default;
  53. }
  54. }
  55. return $this->_elements[$property];
  56. }
  57. /**
  58. * @param string $property Object property to be updated.
  59. * @param mixed $value New value.
  60. * @return void
  61. */
  62. protected function doSetProperty($property, $value)
  63. {
  64. $this->_elements[$property] = $value;
  65. }
  66. /**
  67. * @param string $property Object property to be unset.
  68. * @return void
  69. */
  70. protected function doUnsetProperty($property)
  71. {
  72. unset($this->_elements[$property]);
  73. }
  74. /**
  75. * @param string $property
  76. * @param mixed|null $default
  77. * @return mixed|null
  78. */
  79. protected function getElement($property, $default = null)
  80. {
  81. return array_key_exists($property, $this->_elements) ? $this->_elements[$property] : $default;
  82. }
  83. /**
  84. * @return array
  85. */
  86. protected function getElements()
  87. {
  88. return array_filter($this->_elements, static function ($val) {
  89. return $val !== null;
  90. });
  91. }
  92. /**
  93. * @param array $elements
  94. * @return void
  95. */
  96. protected function setElements(array $elements)
  97. {
  98. $this->_elements = $elements;
  99. }
  100. abstract protected function setKey($key);
  101. }