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.

126 lines
3.2 KiB

4 years ago
  1. <?php
  2. /*
  3. * GetTransactionsResponse.php
  4. * Copyright (c) 2021 james@firefly-iii.org
  5. *
  6. * This file is part of the Firefly III Data Importer
  7. * (https://github.com/firefly-iii/data-importer).
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. declare(strict_types=1);
  23. namespace App\Services\Nordigen\Response;
  24. use App\Services\Nordigen\Model\Transaction;
  25. use App\Services\Shared\Response\Response;
  26. use Countable;
  27. use Illuminate\Support\Collection;
  28. use Iterator;
  29. /**
  30. * Class GetTransactionsResponse
  31. */
  32. class GetTransactionsResponse extends Response implements \Iterator, \Countable
  33. {
  34. private Collection $collection;
  35. private int $position = 0;
  36. public function __construct(array $data)
  37. {
  38. $this->collection = new Collection();
  39. /** @var array $array */
  40. foreach ($data as $array) {
  41. $this->collection->push(Transaction::fromArray($array));
  42. }
  43. }
  44. /**
  45. * Count elements of an object.
  46. *
  47. * @see https://php.net/manual/en/countable.count.php
  48. *
  49. * @return int The custom count as an integer.
  50. * </p>
  51. * <p>
  52. * The return value is cast to an integer.
  53. *
  54. * @since 5.1.0
  55. */
  56. public function count(): int
  57. {
  58. return $this->collection->count();
  59. }
  60. /**
  61. * Return the current element.
  62. *
  63. * @see https://php.net/manual/en/iterator.current.php
  64. * @since 5.0.0
  65. */
  66. public function current(): Transaction
  67. {
  68. return $this->collection->get($this->position);
  69. }
  70. /**
  71. * Return the key of the current element.
  72. *
  73. * @see https://php.net/manual/en/iterator.key.php
  74. * @since 5.0.0
  75. */
  76. public function key(): int
  77. {
  78. return $this->position;
  79. }
  80. /**
  81. * Move forward to next element.
  82. *
  83. * @see https://php.net/manual/en/iterator.next.php
  84. * @since 5.0.0
  85. */
  86. public function next(): void
  87. {
  88. ++$this->position;
  89. }
  90. /**
  91. * Rewind the Iterator to the first element.
  92. *
  93. * @see https://php.net/manual/en/iterator.rewind.php
  94. * @since 5.0.0
  95. */
  96. public function rewind(): void
  97. {
  98. $this->position = 0;
  99. }
  100. /**
  101. * Checks if current position is valid.
  102. *
  103. * @see https://php.net/manual/en/iterator.valid.php
  104. *
  105. * @return bool The return value will be casted to boolean and then evaluated.
  106. * Returns true on success or false on failure.
  107. *
  108. * @since 5.0.0
  109. */
  110. public function valid(): bool
  111. {
  112. return $this->collection->has($this->position);
  113. }
  114. }