Guest User

Untitled

a guest
Jun 20th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Generic;
  4.  
  5. class Collection implements Contract
  6. {
  7. /**
  8. * Untyped items in collection.
  9. *
  10. * @var array
  11. */
  12. private $items = [];
  13.  
  14. /**
  15. * Make a new generic based on class.
  16. *
  17. * @param mixed $item for generic collection type
  18. *
  19. * @return \Generic\Contract
  20. */
  21. public static function generic() : Contract
  22. {
  23. list($item) = func_get_args();
  24.  
  25. return new Generic(static::class, $item);
  26. }
  27.  
  28. /**
  29. * Get all the untyped items in the collection.
  30. *
  31. * @return array
  32. */
  33. public function all() : array
  34. {
  35. return $this->items;
  36. }
  37.  
  38. /**
  39. * Add untyped item to collection.
  40. *
  41. * @param mixed $item
  42. *
  43. * @return self
  44. */
  45. public function add($item)
  46. {
  47. $this->items[] = $item;
  48.  
  49. return $this;
  50. }
  51.  
  52. /**
  53. * Remove untyped item from collection.
  54. *
  55. * @param mixed $item
  56. *
  57. * @return self
  58. */
  59. public function remove($item)
  60. {
  61. $index = array_search($item, $this->items, true);
  62.  
  63. if( false !== $index ) {
  64. unset($this->items, $index);
  65. }
  66.  
  67. return $this;
  68. }
  69. }
Add Comment
Please, Sign In to add comment