client, $this->api); } /** * @return \GuzzleHttp\Psr7\Uri:null */ protected function getHttpBaseUrl() { return $this->client->getConfig('base_uri'); } /** * @return mixed */ public function executeWithState(array $definition) { return $this->execute($definition, $this->getAttrs(array_keys($definition['params']))); } private function getResourcesKey(): string { $resourcesKey = $this->resourcesKey; if (!$resourcesKey) { $class = substr(static::class, strrpos(static::class, '\\') + 1); $resourcesKey = strtolower(preg_replace('/([a-z])([A-Z])/', '$1_$2', $class)).'s'; } return $resourcesKey; } /** * {@inheritdoc} */ public function enumerate(array $def, array $userVals = [], callable $mapFn = null): \Generator { $operation = $this->getOperation($def); $requestFn = function ($marker) use ($operation, $userVals) { if ($marker) { $userVals['marker'] = $marker; } return $this->sendRequest($operation, $userVals); }; $resourceFn = function (array $data) { $resource = $this->newInstance(); $resource->populateFromArray($data); return $resource; }; $opts = [ 'limit' => isset($userVals['limit']) ? $userVals['limit'] : null, 'resourcesKey' => $this->getResourcesKey(), 'markerKey' => $this->markerKey, 'mapFn' => $mapFn, ]; $iterator = new Iterator($opts, $requestFn, $resourceFn); return $iterator(); } public function extractMultipleInstances(ResponseInterface $response, string $key = null): array { $key = $key ?: $this->getResourcesKey(); $resourcesData = Utils::jsonDecode($response)[$key]; $resources = []; foreach ($resourcesData as $resourceData) { $resources[] = $this->newInstance()->populateFromArray($resourceData); } return $resources; } protected function getService() { $class = static::class; $service = substr($class, 0, strpos($class, 'Models') - 1).'\\Service'; return new $service($this->client, $this->api); } /** * {@inheritdoc} */ public function model(string $class, $data = null): ResourceInterface { $model = new $class($this->client, $this->api); // @codeCoverageIgnoreStart if (!$model instanceof ResourceInterface) { throw new \RuntimeException(sprintf('%s does not implement %s', $class, ResourceInterface::class)); } // @codeCoverageIgnoreEnd if ($data instanceof ResponseInterface) { $model->populateFromResponse($data); } elseif (is_array($data)) { $model->populateFromArray($data); } return $model; } }