ObjectStorage.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace PhpDevCommunity\PaperORM\Collection;
  3. use SplObjectStorage;
  4. class ObjectStorage extends SplObjectStorage
  5. {
  6. public function __construct(array $data = [])
  7. {
  8. foreach ($data as $item) {
  9. $this->attach($item);
  10. }
  11. }
  12. /**
  13. * Find the object with the given primary key value.
  14. *
  15. * @param mixed $pk The primary key value to search for.
  16. * @return object|null The object with the given primary key value, or null if not found.
  17. */
  18. public function findPk($pk): ?object
  19. {
  20. if ($pk === null) {
  21. return null;
  22. }
  23. foreach ($this as $object) {
  24. if (method_exists($object, 'getId') && $object->getId() === $pk) {
  25. return $object;
  26. }
  27. if (method_exists($object, 'getPrimaryKeyValue') && $object->getPrimaryKeyValue() === $pk) {
  28. return $object;
  29. }
  30. }
  31. return null;
  32. }
  33. /**
  34. * Finds and returns an object based on the specified method and value.
  35. *
  36. * @param string $method The method to search by
  37. * @param mixed $value The value to search for
  38. * @return object|null The found object or null if not found
  39. */
  40. public function findOneBy(string $method, $value): ?object
  41. {
  42. foreach ($this as $object) {
  43. if (method_exists($object, $method) && $object->$method() === $value) {
  44. return $object;
  45. }
  46. }
  47. return null;
  48. }
  49. /**
  50. * Finds an object in the collection using a callback.
  51. *
  52. * @param callable $callback The callback used for searching.
  53. * @return mixed|null The found object or null if no object matches the criteria.
  54. */
  55. public function find(callable $callback)
  56. {
  57. foreach ($this as $item) {
  58. if ($callback($item)) {
  59. return $item;
  60. }
  61. }
  62. return null;
  63. }
  64. /**
  65. * Finds all objects in the collection that match a given criteria.
  66. *
  67. * @param callable $callback The callback used for searching.
  68. * @return array An array containing all objects that match the criteria.
  69. */
  70. public function filter(callable $callback): array
  71. {
  72. $foundObjects = [];
  73. foreach ($this as $item) {
  74. if ($callback($item)) {
  75. $foundObjects[] = $item;
  76. }
  77. }
  78. return $foundObjects;
  79. }
  80. public function isEmpty(): bool
  81. {
  82. return count($this) === 0;
  83. }
  84. /**
  85. * Retrieves the first object in the collection.
  86. *
  87. * @return mixed|null The first object or null if the collection is empty.
  88. */
  89. public function first()
  90. {
  91. foreach ($this as $item) {
  92. return $item;
  93. }
  94. return null;
  95. }
  96. /**
  97. * Converts the collection to an array.
  98. *
  99. * @return array The collection converted to an array.
  100. */
  101. public function toArray(): array
  102. {
  103. return iterator_to_array($this);
  104. }
  105. /**
  106. * Retrieves the last item in the collection.
  107. *
  108. * @return mixed|null The last item in the collection, or null if the collection is empty.
  109. */
  110. public function last()
  111. {
  112. $last = null;
  113. foreach ($this as $item) {
  114. $last = $item;
  115. }
  116. return $last;
  117. }
  118. /**
  119. * Removes all objects from the collection.
  120. */
  121. public function clear(): void
  122. {
  123. foreach ($this as $item) {
  124. $this->detach($item);
  125. }
  126. }
  127. /**
  128. * Adds an object to the collection.
  129. *
  130. * @param object $object The object to be added.
  131. * @return self Returns the updated collection.
  132. */
  133. public function add(object $object): self
  134. {
  135. $this->attach($object);
  136. return $this;
  137. }
  138. /**
  139. * Removes an object from the collection.
  140. *
  141. * @param object $object The object to be removed.
  142. * @return self Returns the updated collection.
  143. */
  144. public function remove(object $object): self
  145. {
  146. $this->detach($object);
  147. return $this;
  148. }
  149. }