diff options
| author | Eole <EoleDev@outlook.fr> | 2016-04-27 16:42:28 +0200 |
|---|---|---|
| committer | Eole <EoleDev@outlook.fr> | 2016-04-27 16:42:28 +0200 |
| commit | 49f416dc5061032e0514ea0cfeceaca37d13e432 (patch) | |
| tree | 1202ac2a6fa860b8929afdc886c94fc50bd0a1de /server/vendor/php-opencloud/common/src/Common/Resource/Iterator.php | |
| parent | c7edd70b5e5b0f5159c78ce3d924d4e7f60db816 (diff) | |
| parent | c9202d9113210981ae47df40511645da2ee140df (diff) | |
Merge branch 'develop' into Eole_Graph
Conflicts:
client/index.html
client/js/controllers/home/home.js
client/partials/home/home.html
Diffstat (limited to 'server/vendor/php-opencloud/common/src/Common/Resource/Iterator.php')
| -rw-r--r-- | server/vendor/php-opencloud/common/src/Common/Resource/Iterator.php | 97 |
1 files changed, 0 insertions, 97 deletions
diff --git a/server/vendor/php-opencloud/common/src/Common/Resource/Iterator.php b/server/vendor/php-opencloud/common/src/Common/Resource/Iterator.php deleted file mode 100644 index 63d4455..0000000 --- a/server/vendor/php-opencloud/common/src/Common/Resource/Iterator.php +++ /dev/null @@ -1,97 +0,0 @@ -<?php - -namespace OpenCloud\Common\Resource; - -use OpenCloud\Common\Transport\Utils; - -class Iterator -{ - private $requestFn; - private $resourceFn; - private $limit; - private $count; - private $resourcesKey; - private $markerKey; - private $mapFn; - private $currentMarker; - - public function __construct(array $options, callable $requestFn, callable $resourceFn) - { - $this->limit = isset($options['limit']) ? $options['limit'] : false; - $this->count = 0; - - if (isset($options['resourcesKey'])) { - $this->resourcesKey = $options['resourcesKey']; - } - - if (isset($options['markerKey'])) { - $this->markerKey = $options['markerKey']; - } - - if (isset($options['mapFn']) && is_callable($options['mapFn'])) { - $this->mapFn = $options['mapFn']; - } - - $this->requestFn = $requestFn; - $this->resourceFn = $resourceFn; - } - - private function fetchResources() - { - if ($this->shouldNotSendAnotherRequest()) { - return false; - } - - $response = call_user_func($this->requestFn, $this->currentMarker); - - $json = Utils::flattenJson(Utils::jsonDecode($response), $this->resourcesKey); - - if ($response->getStatusCode() === 204 || empty($json)) { - return false; - } - - return $json; - } - - private function assembleResource(array $data) - { - $resource = call_user_func($this->resourceFn, $data); - - // Invoke user-provided fn if provided - if ($this->mapFn) { - call_user_func_array($this->mapFn, [&$resource]); - } - - // Update marker if operation supports it - if ($this->markerKey) { - $this->currentMarker = $resource->{$this->markerKey}; - } - - return $resource; - } - - private function totalReached() - { - return $this->limit && $this->count >= $this->limit; - } - - private function shouldNotSendAnotherRequest() - { - return $this->totalReached() || ($this->count > 0 && !$this->markerKey); - } - - public function __invoke() - { - while ($resources = $this->fetchResources()) { - foreach ($resources as $resourceData) { - if ($this->totalReached()) { - break; - } - - $this->count++; - - yield $this->assembleResource($resourceData); - } - } - } -} |
