TableModelCollection.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // "Hackware Web Services Core" is released under the MIT License terms.
  4. namespace Hawese\Core;
  5. use Illuminate\Support\Collection;
  6. /**
  7. * Colleciton of TableModels
  8. *
  9. * Currently only supports appendForeignObjects() and get()
  10. */
  11. class TableModelCollection
  12. {
  13. private $collection;
  14. private $model;
  15. public function __construct($collection, $model)
  16. {
  17. $this->model = $model;
  18. $this->collection = $collection->map(function ($object) {
  19. return new $this->model($object);
  20. });
  21. }
  22. private function mapForeignCollections(array $attributes) : array
  23. {
  24. return array_combine(
  25. $attributes,
  26. array_map(function ($attribute) {
  27. $foreign_key = $this->model::guessFK($attribute);
  28. return $this->getForeignCollection($foreign_key);
  29. }, $attributes)
  30. );
  31. }
  32. private function getForeignCollection(
  33. string $foreign_key
  34. ) : TableModelCollection {
  35. $ids = $this->collection->pluck($foreign_key)->unique()->values();
  36. $foreign_model = $this->model::$foreign_keys[$foreign_key];
  37. return $foreign_model::processCollection(
  38. $foreign_model::select()
  39. ->whereIn($foreign_model::$primary_key, $ids)
  40. ->get()
  41. );
  42. }
  43. /**
  44. * Match $foreign_collections element PK with $object FK and return it.
  45. *
  46. * @param TableModel $object Single object to compare with.
  47. * @param TableModelCollection[] $foregin_collections map of collections.
  48. * @param array $match Associative array containing `attribute`, `key` &
  49. * `model`.
  50. */
  51. private function findForeignObject(
  52. TableModel $object,
  53. array $foreign_collections,
  54. array $match
  55. ) : object {
  56. return $foreign_collections[$match['attribute']]->get()->first(
  57. function ($foreign_object) use ($object, $match) {
  58. $object_fk = $object->{$match['key']};
  59. $foreign_pk = $foreign_object->{
  60. $match['model']::$primary_key
  61. };
  62. return $object_fk === $foreign_pk;
  63. }
  64. );
  65. }
  66. /**
  67. * Appends $foreign_keys based objects to each object on collection.
  68. *
  69. * This method edits the original $collection and is 'fluent'.
  70. * This avoids the N+1 queries antipattern.
  71. */
  72. public function appendForeignObjects(array $attributes) : self
  73. {
  74. $foreign_collections = $this->mapForeignCollections($attributes);
  75. $this->collection = $this->collection->map(
  76. function ($object) use ($attributes, $foreign_collections) {
  77. foreach ($attributes as $attribute) {
  78. $object->append($attribute);
  79. $foreign_key = $this->model::guessFK($attribute);
  80. $object->{$attribute} = $this->findForeignObject(
  81. $object,
  82. $foreign_collections,
  83. [
  84. 'attribute' => $attribute,
  85. 'key' => $foreign_key,
  86. 'model' => $this->model::$foreign_keys[$foreign_key]
  87. ]
  88. );
  89. }
  90. return $object;
  91. }
  92. );
  93. return $this;
  94. }
  95. public function get() : Collection
  96. {
  97. return $this->collection;
  98. }
  99. }