withPath() public method

public withPath ( $path )
Example #1
0
 /**
  * @param array $params {
  *      @var string             $resource Flora resource
  *      @var int|string         $id             optional    Unique item identifier
  *      @var string             $format         optional    Output format (default json)
  *      @var string             $action         optional    API action (default: retrieve)
  *      @var string             $select         optional    Retrieve only specified attributes
  *      @var string             $filter         optional    Filter items by criteria
  *      @var int                $limit          optional    Limit result set
  *      @var int                $page           optional    Paginate through result set
  *      @var string             $search         optional    Search items by full-text search
  *      @var bool               $cache          optional    En-/disable caching (default: true)
  *      @var bool               $authenticate   optional    Use authentication provider to add some authentication information to request
  *      @var string             $httpMethod     optional    Explicitly set/override HTTP (GET, POST,...) method
  *      @var array|\stdClass    $data           optional    Send $data as JSON
  * }
  * @return \stdClass
  * @throws \Flora\Exception
  */
 public function execute(array $params)
 {
     if (!isset($params['resource']) || empty($params['resource'])) {
         throw new Exception('Resource must be set');
     }
     $uri = $this->uri->withPath($this->getPath($params));
     foreach (['resource', 'id', 'format'] as $param) {
         // remove path params from request params
         if (isset($params[$param])) {
             unset($params[$param]);
         }
     }
     if (array_key_exists('cache', $params)) {
         if ((bool) $params['cache'] === false) {
             $params['_'] = time();
         }
         unset($params['cache']);
     }
     if (isset($params['action']) && $params['action'] == 'retrieve') {
         unset($params['action']);
     }
     $httpMethod = $this->getHttpMethod($params);
     $request = new Request($httpMethod, $uri, ['Referer' => $this->getCurrentUri()]);
     if (isset($params['authenticate'])) {
         if ((bool) $params['authenticate']) {
             if ($this->authProvider === null) {
                 throw new Exception('Authentication provider is not configured');
             }
             $request = $this->authProvider->authenticate($request);
         }
         unset($params['authenticate']);
     }
     if (!empty($this->defaultParams)) {
         $params = array_merge($this->defaultParams, $params);
     }
     if (!empty($params)) {
         $request = $this->applyParameters($request, $params);
     }
     try {
         $response = $this->httpClient->send($request, $this->httpOptions);
     } catch (RequestException $e) {
         throw new Exception($e->getMessage());
     }
     $result = $response->getBody();
     $contentType = $response->getHeaderLine('Content-Type');
     if (strpos($contentType, 'application/json') !== false) {
         $result = json_decode($result);
     }
     $statusCode = $response->getStatusCode();
     if ($statusCode < 400) {
         return $result;
     }
     $this->throwError($statusCode, $result->error);
 }
Example #2
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testPathMustBeValid()
 {
     $uri = new Uri('');
     $uri->withPath(array());
 }