App\services\RESTfulService::buildUrl PHP Method

buildUrl() public method

Turn a URI segment into a full API URL.
public buildUrl ( string $uri, boolean $appendKey = true ) : string
$uri string
$appendKey boolean Whether to automatically append the API key into the URL.
return string
    public function buildUrl($uri, $appendKey = true)
    {
        if (!starts_with($uri, ['http://', 'https://'])) {
            if ($uri[0] !== '/') {
                $uri = "/{$uri}";
            }
            $uri = $this->endpoint . $uri;
        }
        if ($appendKey) {
            if (parse_url($uri, PHP_URL_QUERY)) {
                $uri .= "&{$this->keyParam}=" . $this->getKey();
            } else {
                $uri .= "?{$this->keyParam}=" . $this->getKey();
            }
        }
        return $uri;
    }

Usage Example

示例#1
0
 public function testUrlConstruction()
 {
     $api = new RESTfulService('bar', null, 'http://foo.com', \Mockery::mock(Client::class));
     $this->assertEquals('http://foo.com/get/param?key=bar', $api->buildUrl('get/param'));
     $this->assertEquals('http://foo.com/get/param?baz=moo&key=bar', $api->buildUrl('/get/param?baz=moo'));
     $this->assertEquals('http://baz.com/?key=bar', $api->buildUrl('http://baz.com/'));
 }