collection.rst 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. ==========
  2. Collection
  3. ==========
  4. Collections are exactly what they sound like - collections of items. In DiscordPHP collections are based around the idea of parts, but they can be used for any type of item.
  5. .. container::
  6. Collections implement interfaces allowing them to be accessed like arrays, such as:
  7. .. code:: php
  8. // square bracket index access
  9. $collec[123] = 'asdf';
  10. echo $collec[123]; // asdf
  11. // foreach loops
  12. foreach ($collec as $item) {
  13. // ...
  14. }
  15. // json serialization
  16. json_encode($collec);
  17. // array serialization
  18. $collecArray = (array) $collec;
  19. // string serialization
  20. $jsonCollec = (string) $collec; // same as json_encode($collec)
  21. Creating a collection
  22. =====================
  23. +---------+----------------+--------------------------------------------------------------------+
  24. | name | type | description |
  25. +=========+================+====================================================================+
  26. | items | array | Array of items for the collection. Default is empty collection |
  27. +---------+----------------+--------------------------------------------------------------------+
  28. | discrim | string or null | The discriminator used to discriminate between parts. Default ‘id’ |
  29. +---------+----------------+--------------------------------------------------------------------+
  30. | class | string or null | The type of class contained in the collection. Default null |
  31. +---------+----------------+--------------------------------------------------------------------+
  32. .. code:: php
  33. // Creates an empty collection with discriminator of 'id' and no class type.
  34. // Any item can be inserted into this collection.
  35. $collec = new Collection();
  36. // Creates an empty collection with no discriminator and no class type.
  37. // Similar to a laravel collection.
  38. $collec = new Collection([], null, null);
  39. Getting an item
  40. ===============
  41. Gets an item from the collection, with a key and value.
  42. ===== ==== ===================================
  43. name type description
  44. ===== ==== ===================================
  45. key any The key to search with
  46. value any The value that the key should match
  47. ===== ==== ===================================
  48. .. code:: php
  49. // Collection with 3 items, discriminator is 'id', no class type
  50. $collec = new Collection([
  51. [
  52. 'id' => 1,
  53. 'text' => 'My ID is 1.'
  54. ],
  55. [
  56. 'id' => 2,
  57. 'text' => 'My ID is 2.'
  58. ],
  59. [
  60. 'id' => 3,
  61. 'text' => 'My ID is 3.'
  62. ]
  63. ]);
  64. // [
  65. // 'id' => 1,
  66. // 'text' => 'My ID is 1.'
  67. // ]
  68. $item = $collec->get('id', 1);
  69. // [
  70. // 'id' => 1,
  71. // 'text' => 'My ID is 1.'
  72. // ]
  73. $item = $collec->get('text', 'My ID is 1.');
  74. Adding an item
  75. ==============
  76. Adds an item to the collection. Note that if ``class`` is set in the constructor and the class of the item inserted is not the same, it will not insert.
  77. ===== ==== ==================
  78. name type description
  79. ===== ==== ==================
  80. $item any The item to insert
  81. ===== ==== ==================
  82. .. code:: php
  83. // empty, no discrim, no class
  84. $collec = new Collection([], null, null);
  85. $collec->push(1);
  86. $collec->push('asdf');
  87. $collec->push(true);
  88. // ---
  89. class X
  90. {
  91. public $y;
  92. public function __construct($y)
  93. {
  94. $this->y = $y;
  95. }
  96. }
  97. // empty, discrim 'y', class X
  98. $collec = new Collection([], 'y', X::class);
  99. $collec->push(new X(123));
  100. $collec->push(123); // won't insert
  101. // new X(123)
  102. $collec->get('y', 123);
  103. Pulling an item
  104. ===============
  105. Removes an item from the collection and returns it.
  106. ======= ==== =========================================
  107. name type description
  108. ======= ==== =========================================
  109. key any The key to look for
  110. default any Default if key is not found. Default null
  111. ======= ==== =========================================
  112. .. code:: php
  113. $collec = new Collection([], null, null);
  114. $collec->push(1);
  115. $collec->push(2);
  116. $collec->push(3);
  117. $collec->pull(1); // returns at 1 index - which is actually 2
  118. $collec->pull(100); // returns null
  119. $collec->pull(100, 123); // returns 123
  120. Filling the collection
  121. ======================
  122. Fills the collection with an array of items.
  123. .. code:: php
  124. $collec = new Collection([], null, null);
  125. $collec->fill([
  126. 1, 2, 3, 4,
  127. ]);
  128. Number of items
  129. ===============
  130. Returns the number of items in the collection.
  131. .. code:: php
  132. $collec = new Collection([
  133. 1, 2, 3
  134. ], null, null);
  135. echo $collec->count(); // 3
  136. Getting the first item
  137. ======================
  138. Gets the first item of the collection.
  139. .. code:: php
  140. $collec = new Collection([
  141. 1, 2, 3
  142. ], null, null);
  143. echo $collec->first(); // 1
  144. Filtering a collection
  145. ======================
  146. Filters the collection with a given callback function. The callback function is called for every item and is called with the item. If the callback returns true, the item is added to the new collection. Returns a new collection.
  147. ======== ======== =================================
  148. name type description
  149. ======== ======== =================================
  150. callback callable The callback called on every item
  151. ======== ======== =================================
  152. .. code:: php
  153. $collec = new Collection([
  154. 1, 2, 3, 100, 101, 102
  155. ], null, null);
  156. // [ 101, 102 ]
  157. $newCollec = $collec->filter(function ($item) {
  158. return $item > 100;
  159. });
  160. Clearing a collection
  161. =====================
  162. Clears the collection.
  163. .. code:: php
  164. $collec->clear(); // $collec = []
  165. Mapping a collection
  166. ====================
  167. A given callback function is called on each item in the collection, and the result is inserted into a new collection.
  168. ======== ======== =================================
  169. name type description
  170. ======== ======== =================================
  171. callback callable The callback called on every item
  172. ======== ======== =================================
  173. .. code:: php
  174. $collec = new Collection([
  175. 1, 2, 3, 100, 101, 102
  176. ], null, null);
  177. // [ 100, 200, 300, 10000, 10100, 10200 ]
  178. $newCollec = $collec->map(function ($item) {
  179. return $item * 100;
  180. });
  181. Converting to array
  182. ===================
  183. Converts a collection to an array.
  184. .. code:: php
  185. $arr = $collec->toArray();