PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / sdk / google / google / gax / src / Page.php

Page.php in Media Cloud Sync 1.4.2, at includes/sdk/google/google/gax/src/Page.php

221 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * Copyright 2016 Google LLC
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 namespace Dudlewebs\WPMCS\GCP\Google\ApiCore;
34
35 use Generator;
36 use Dudlewebs\WPMCS\GCP\Google\Protobuf\Internal\MapField;
37 use Dudlewebs\WPMCS\GCP\Google\Protobuf\Internal\Message;
38 use IteratorAggregate;
39 /**
40 * A Page object wraps an API list method response and provides methods
41 * to retrieve additional pages using the page token.
42 */
43 class Page implements IteratorAggregate
44 {
45 const FINAL_PAGE_TOKEN = '';
46 private $call;
47 private $callable;
48 private $options;
49 private $pageStreamingDescriptor;
50 private $pageToken;
51 // @phpstan-ignore-line
52 private $response;
53 /**
54 * Page constructor.
55 *
56 * @param Call $call
57 * @param array $options
58 * @param callable $callable
59 * @param PageStreamingDescriptor $pageStreamingDescriptor
60 * @param Message $response
61 */
62 public function __construct(Call $call, array $options, callable $callable, PageStreamingDescriptor $pageStreamingDescriptor, Message $response)
63 {
64 $this->call = $call;
65 $this->options = $options;
66 $this->callable = $callable;
67 $this->pageStreamingDescriptor = $pageStreamingDescriptor;
68 $this->response = $response;
69 $requestPageTokenGetMethod = $this->pageStreamingDescriptor->getRequestPageTokenGetMethod();
70 $this->pageToken = $this->call->getMessage()->{$requestPageTokenGetMethod}();
71 }
72 /**
73 * Returns true if there are more pages that can be retrieved from the
74 * API.
75 *
76 * @return bool
77 */
78 public function hasNextPage()
79 {
80 return \strcmp($this->getNextPageToken(), Page::FINAL_PAGE_TOKEN) != 0;
81 }
82 /**
83 * Returns the next page token from the response.
84 *
85 * @return string
86 */
87 public function getNextPageToken()
88 {
89 $responsePageTokenGetMethod = $this->pageStreamingDescriptor->getResponsePageTokenGetMethod();
90 return $this->getResponseObject()->{$responsePageTokenGetMethod}();
91 }
92 /**
93 * Retrieves the next Page object using the next page token.
94 *
95 * @param int|null $pageSize
96 * @throws ValidationException if there are no pages remaining, or if pageSize is supplied but
97 * is not supported by the API
98 * @throws ApiException if the call to fetch the next page fails.
99 * @return Page
100 */
101 public function getNextPage(?int $pageSize = null)
102 {
103 if (!$this->hasNextPage()) {
104 throw new ValidationException('Could not complete getNextPage operation: ' . 'there are no more pages to retrieve.');
105 }
106 $oldRequest = $this->getRequestObject();
107 $requestClass = \get_class($oldRequest);
108 $newRequest = new $requestClass();
109 $newRequest->mergeFrom($oldRequest);
110 $requestPageTokenSetMethod = $this->pageStreamingDescriptor->getRequestPageTokenSetMethod();
111 $newRequest->{$requestPageTokenSetMethod}($this->getNextPageToken());
112 if (isset($pageSize)) {
113 if (!$this->pageStreamingDescriptor->requestHasPageSizeField()) {
114 throw new ValidationException('pageSize argument was defined, but the method does not ' . 'support a page size parameter in the optional array argument');
115 }
116 $requestPageSizeSetMethod = $this->pageStreamingDescriptor->getRequestPageSizeSetMethod();
117 $newRequest->{$requestPageSizeSetMethod}($pageSize);
118 }
119 $this->call = $this->call->withMessage($newRequest);
120 $callable = $this->callable;
121 $response = $callable($this->call, $this->options)->wait();
122 return new Page($this->call, $this->options, $this->callable, $this->pageStreamingDescriptor, $response);
123 }
124 /**
125 * Return the number of elements in the response.
126 *
127 * @return int
128 */
129 public function getPageElementCount()
130 {
131 $resourcesGetMethod = $this->pageStreamingDescriptor->getResourcesGetMethod();
132 return \count($this->getResponseObject()->{$resourcesGetMethod}());
133 }
134 /**
135 * Return an iterator over the elements in the response.
136 *
137 * @return Generator
138 */
139 #[\ReturnTypeWillChange]
140 public function getIterator()
141 {
142 $resourcesGetMethod = $this->pageStreamingDescriptor->getResourcesGetMethod();
143 $items = $this->getResponseObject()->{$resourcesGetMethod}();
144 foreach ($items as $key => $element) {
145 if ($items instanceof MapField) {
146 (yield $key => $element);
147 } else {
148 (yield $element);
149 }
150 }
151 }
152 /**
153 * Return an iterator over Page objects, beginning with this object.
154 * Additional Page objects are retrieved lazily via API calls until
155 * all elements have been retrieved.
156 *
157 * @return Generator|array<Page>
158 * @throws ValidationException
159 * @throws ApiException
160 */
161 public function iteratePages()
162 {
163 $currentPage = $this;
164 (yield $this);
165 while ($currentPage->hasNextPage()) {
166 $currentPage = $currentPage->getNextPage();
167 (yield $currentPage);
168 }
169 }
170 /**
171 * Gets the request object used to generate the Page.
172 *
173 * @return mixed|Message
174 */
175 public function getRequestObject()
176 {
177 return $this->call->getMessage();
178 }
179 /**
180 * Gets the API response object.
181 *
182 * @return mixed|Message
183 */
184 public function getResponseObject()
185 {
186 return $this->response;
187 }
188 /**
189 * Returns a collection of elements with a fixed size set by
190 * the collectionSize parameter. The collection will only contain
191 * fewer than collectionSize elements if there are no more
192 * pages to be retrieved from the server.
193 *
194 * NOTE: it is an error to call this method if an optional parameter
195 * to set the page size is not supported or has not been set in the
196 * API call that was used to create this page. It is also an error
197 * if the collectionSize parameter is less than the page size that
198 * has been set.
199 *
200 * @param int $collectionSize
201 * @throws ValidationException if a FixedSizeCollection of the specified size cannot be constructed
202 * @return FixedSizeCollection
203 */
204 public function expandToFixedSizeCollection($collectionSize)
205 {
206 if (!$this->pageStreamingDescriptor->requestHasPageSizeField()) {
207 throw new ValidationException('FixedSizeCollection is not supported for this method, because ' . 'the method does not support an optional argument to set the ' . 'page size.');
208 }
209 $request = $this->getRequestObject();
210 $pageSizeGetMethod = $this->pageStreamingDescriptor->getRequestPageSizeGetMethod();
211 $pageSize = $request->{$pageSizeGetMethod}();
212 if (\is_null($pageSize)) {
213 throw new ValidationException('Error while expanding Page to FixedSizeCollection: No page size ' . 'parameter found. The page size parameter must be set in the API ' . 'optional arguments array, and must be less than the collectionSize ' . 'parameter, in order to create a FixedSizeCollection object.');
214 }
215 if ($pageSize > $collectionSize) {
216 throw new ValidationException('Error while expanding Page to FixedSizeCollection: collectionSize ' . 'parameter is less than the page size optional argument specified in ' . "the API call. collectionSize: {$collectionSize}, page size: {$pageSize}");
217 }
218 return new FixedSizeCollection($this, $collectionSize);
219 }
220 }
221