Site du proximo, utilisé pour gérer le stock.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Collection.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace OpenFoodFacts;
  3. use Iterator;
  4. class Collection implements Iterator
  5. {
  6. private $listDocuments = null;
  7. private $count = null;
  8. private $page = null;
  9. private $skip = null;
  10. private $pageSize = null;
  11. /**
  12. * initialization of the collection
  13. * @param array|null $data the raw data
  14. * @param string|null $api this information help to type the collection (not use yet)
  15. */
  16. public function __construct(array $data = null, string $api = null)
  17. {
  18. $data = $data ?? [
  19. 'products' => [],
  20. 'count' => 0,
  21. 'page' => 0,
  22. 'skip' => 0,
  23. 'page_size' => 0,
  24. ];
  25. $this->listDocuments = [];
  26. if (!empty($data['products'])) {
  27. $currentApi = '';
  28. if (null !== $api) {
  29. $currentApi = $api;
  30. }
  31. foreach ($data['products'] as $document) {
  32. if($document instanceof Document){
  33. $this->listDocuments[] = $document;
  34. }elseif (is_array($document)){
  35. $this->listDocuments[] = Document::createSpecificDocument($currentApi, $document);
  36. }else {
  37. throw new \InvalidArgumentException(sprintf('Would expect an OpenFoodFacts\Document Interface or Array here. Got: %s', gettype($document)));
  38. }
  39. }
  40. }
  41. $this->count = $data['count'];
  42. $this->page = $data['page'];
  43. $this->skip = $data['skip'];
  44. $this->pageSize = $data['page_size'];
  45. }
  46. /**
  47. * @return int get the current page
  48. */
  49. public function getPage() : int
  50. {
  51. return $this->page;
  52. }
  53. /**
  54. * @return int get the number of element skipped
  55. */
  56. public function getSkip() : int
  57. {
  58. return $this->skip;
  59. }
  60. /**
  61. * @return int get the number of element by page for this collection
  62. */
  63. public function getPageSize() : int
  64. {
  65. return $this->pageSize;
  66. }
  67. /**
  68. * @return int the number of element in this Collection
  69. */
  70. public function pageCount() : int
  71. {
  72. return count($this->listDocuments);
  73. }
  74. /**
  75. * @return int the number of element for this search
  76. */
  77. public function searchCount() : int
  78. {
  79. return $this->count;
  80. }
  81. /**
  82. * Implementation of Iterator
  83. */
  84. /**
  85. * @inheritDoc
  86. */
  87. public function rewind()
  88. {
  89. reset($this->listDocuments);
  90. }
  91. /**
  92. * @inheritDoc
  93. */
  94. public function current()
  95. {
  96. return current($this->listDocuments);
  97. }
  98. /**
  99. * @inheritDoc
  100. */
  101. public function key()
  102. {
  103. return key($this->listDocuments);
  104. }
  105. /**
  106. * @inheritDoc
  107. */
  108. public function next()
  109. {
  110. return next($this->listDocuments);
  111. }
  112. /**
  113. * @inheritDoc
  114. */
  115. public function valid()
  116. {
  117. $key = key($this->listDocuments);
  118. return ($key !== null && $key !== false);
  119. }
  120. }