-
Couldn't load subscription status.
- Fork 3
Add page iterator support. #70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e2b35ae
START: Add page iterator support.
SilasKenneth 2a68648
Write test for the Page iterator.
SilasKenneth 2ca04c6
Fix minor bug with page iterator.
SilasKenneth 944904d
Update the tests to cover more cases.
SilasKenneth 6861e62
Make minor changes to serialization.
SilasKenneth 63bbd8a
Work on feedback.
SilasKenneth 2688ae9
Update src/Tasks/PageIterator.php
SilasKenneth 6523210
Update src/Tasks/PageIterator.php
SilasKenneth 85867a0
Wrap inside exception.
SilasKenneth e6c2811
Just let the exception be thrown.
SilasKenneth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| <?php | ||
|
|
||
| namespace Microsoft\Graph\Core\Models; | ||
|
|
||
| class PageResult | ||
| { | ||
| /** @var string|null $odataNextLink */ | ||
| private ?string $odataNextLink; | ||
| /** @var array<mixed>|null $value */ | ||
| private ?array $value; | ||
|
|
||
| /** | ||
| * @return string|null | ||
| */ | ||
| public function getOdataNextLink(): ?string { | ||
| return $this->odataNextLink; | ||
| } | ||
|
|
||
| /** | ||
| * @return array<mixed>|null | ||
| */ | ||
| public function getValue(): ?array { | ||
| return $this->value; | ||
| } | ||
|
|
||
| /** | ||
| * @param string|null $nextLink | ||
| */ | ||
| public function setOdataNextLink(?string $nextLink): void{ | ||
| $this->odataNextLink = $nextLink; | ||
| } | ||
|
|
||
| /** | ||
| * @param array|null $value | ||
| */ | ||
| public function setValue(?array $value): void { | ||
| $this->value = $value; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| <?php | ||
|
|
||
| namespace Microsoft\Graph\Core\Tasks; | ||
|
|
||
| use Exception; | ||
| use Http\Promise\FulfilledPromise; | ||
| use Http\Promise\Promise; | ||
| use Http\Promise\RejectedPromise; | ||
| use InvalidArgumentException; | ||
| use JsonException; | ||
| use Microsoft\Graph\Core\Models\PageResult; | ||
| use Microsoft\Kiota\Abstractions\HttpMethod; | ||
| use Microsoft\Kiota\Abstractions\NativeResponseHandler; | ||
| use Microsoft\Kiota\Abstractions\RequestAdapter; | ||
| use Microsoft\Kiota\Abstractions\RequestInformation; | ||
| use Microsoft\Kiota\Abstractions\RequestOption; | ||
| use Microsoft\Kiota\Abstractions\Serialization\Parsable; | ||
|
|
||
| class PageIterator | ||
| { | ||
| private PageResult $currentPage; | ||
| private RequestAdapter $requestAdapter; | ||
| private bool $hasNext = false; | ||
| private int $pauseIndex; | ||
| /** @var array{string, string} $constructorFunc */ | ||
| private array $constructorCallable; | ||
| private array $headers = []; | ||
| /** @var array<RequestOption>|null */ | ||
| private ?array $requestOptions = []; | ||
|
|
||
| /** | ||
| * @param Parsable|array|object $response paged collection response | ||
| * @param RequestAdapter $requestAdapter | ||
| * @param array{string,string} $constructorCallable The method to construct a paged response object. | ||
| * @throws JsonException | ||
| */ | ||
| public function __construct($response, RequestAdapter $requestAdapter, array $constructorCallable) { | ||
| $this->requestAdapter = $requestAdapter; | ||
| $this->constructorCallable = $constructorCallable; | ||
| $this->pauseIndex = 0; | ||
| $page = self::convertToPage($response); | ||
|
|
||
| if ($page !== null) { | ||
| $this->currentPage = $page; | ||
| $this->hasNext = true; | ||
| } | ||
| $this->headers = []; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $headers | ||
| */ | ||
| public function setHeaders(array $headers): void | ||
| { | ||
| $this->headers = $headers; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $requestOptions | ||
| */ | ||
| public function setRequestOptions(array $requestOptions): void | ||
| { | ||
| $this->requestOptions = $requestOptions; | ||
| } | ||
|
|
||
| /** | ||
| * @param int $pauseIndex | ||
| */ | ||
| public function setPauseIndex(int $pauseIndex): void | ||
Ndiritu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| $this->pauseIndex = $pauseIndex; | ||
| } | ||
|
|
||
| /** | ||
| * @param callable(Parsable|array|object): bool $callback The callback function to apply on every entity. Pauses iteration if false is returned | ||
| * @throws Exception | ||
| */ | ||
| public function iterate(callable $callback): void { | ||
Ndiritu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| while(true) { | ||
| $keepIterating = $this->enumerate($callback); | ||
|
|
||
| if (!$keepIterating) { | ||
| return; | ||
| } | ||
| $nextPage = $this->next(); | ||
|
|
||
| if (empty($nextPage)) { | ||
| $this->hasNext = false; | ||
| return; | ||
| } | ||
| $this->currentPage = $nextPage; | ||
| $this->pauseIndex = 0; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @throws Exception | ||
| */ | ||
| public function next(): ?PageResult { | ||
| if (empty($this->currentPage->getOdataNextLink())) { | ||
| return null; | ||
| } | ||
|
|
||
| $response = $this->fetchNextPage(); | ||
| $result = $response->wait(); | ||
| return self::convertToPage($result); | ||
| } | ||
|
|
||
| /** | ||
| * @param $response | ||
| * @return PageResult|null | ||
| * @throws JsonException | ||
| */ | ||
| public static function convertToPage($response): ?PageResult { | ||
| $page = new PageResult(); | ||
| if ($response === null) { | ||
| throw new InvalidArgumentException('$response cannot be null'); | ||
| } | ||
|
|
||
| if (is_array($response)) { | ||
| $value = $response['value']; | ||
| } else if(is_a($response, Parsable::class) && method_exists($response, 'getValue')) { | ||
| $value = $response->getValue(); | ||
| } else { | ||
| $value = $response->value; | ||
| } | ||
|
|
||
| if ($value === null) { | ||
| throw new InvalidArgumentException('The response does not contain a value.'); | ||
| } | ||
|
|
||
| $parsablePage = is_a($response, Parsable::class) ? $response : json_decode(json_encode($response,JSON_THROW_ON_ERROR), true); | ||
| if (is_array($parsablePage)) { | ||
| $page->setOdataNextLink($parsablePage['@odata.nextLink'] ?? ''); | ||
| } else { | ||
| $page->setOdataNextLink($parsablePage->getOdataNextLink()); | ||
Ndiritu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| $page->setValue($value); | ||
| return $page; | ||
| } | ||
| private function fetchNextPage(): ?Promise { | ||
| /** @var Parsable $graphResponse */ | ||
| $graphResponse = null; | ||
|
|
||
| $nextLink = $this->currentPage->getOdataNextLink(); | ||
|
|
||
| if ($nextLink === null) { | ||
| return new RejectedPromise(new InvalidArgumentException('The response does not have a nextLink')); | ||
| } | ||
|
|
||
| if (!filter_var($nextLink, FILTER_VALIDATE_URL)) { | ||
| throw new InvalidArgumentException('Could not parse the nextLink url.'); | ||
| } | ||
|
|
||
| $requestInfo = new RequestInformation(); | ||
| $requestInfo->httpMethod = HttpMethod::GET; | ||
| $requestInfo->setUri($nextLink); | ||
| $requestInfo->headers = $this->headers; | ||
| if ($this->requestOptions !== null) { | ||
| $requestInfo->addRequestOptions(...$this->requestOptions); | ||
| } | ||
|
|
||
| return $this->requestAdapter->sendAsync($requestInfo, $this->constructorCallable); | ||
| } | ||
|
|
||
| public function enumerate(?callable $callback): ?bool { | ||
| $keepIterating = true; | ||
|
|
||
| $pageItems = $this->currentPage->getValue(); | ||
| if (empty($pageItems)) { | ||
| return false; | ||
| } | ||
| for ($i = $this->pauseIndex; $i < count($pageItems); $i++){ | ||
| $keepIterating = $callback($pageItems[$i]); | ||
|
|
||
| if (!$keepIterating) { | ||
| $this->pauseIndex = $i + 1; | ||
| break; | ||
| } | ||
| } | ||
| return $keepIterating; | ||
| } | ||
|
|
||
| public function hasNext(): bool { | ||
| return $this->hasNext; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.