1<?php
2
3/**
4 * @see       https://github.com/laminas/laminas-oauth for the canonical source repository
5 * @copyright https://github.com/laminas/laminas-oauth/blob/master/COPYRIGHT.md
6 * @license   https://github.com/laminas/laminas-oauth/blob/master/LICENSE.md New BSD License
7 */
8
9namespace Laminas\OAuth\Token;
10
11use Laminas\OAuth\Client;
12use Laminas\OAuth\Config\ConfigInterface as Config;
13use Laminas\OAuth\Exception;
14use Laminas\Uri;
15
16/**
17 * @category   Laminas
18 * @package    Laminas_OAuth
19 */
20class Access extends AbstractToken
21{
22    /**
23     * Cast to HTTP header
24     *
25     * @param  string $url
26     * @param  Config $config
27     * @param  null|array $customParams
28     * @param  null|string $realm
29     * @return string
30     * @throws Exception\InvalidArgumentException
31     */
32    public function toHeader(
33        $url, Config $config, array $customParams = null, $realm = null
34    ) {
35        $uri = Uri\UriFactory::factory($url);
36        if (!$uri->isValid()
37            || !in_array($uri->getScheme(), array('http', 'https'))
38        ) {
39            throw new Exception\InvalidArgumentException(
40                '\'' . $url . '\' is not a valid URI'
41            );
42        }
43        $params = $this->_httpUtility->assembleParams($url, $config, $customParams);
44        return $this->_httpUtility->toAuthorizationHeader($params, $realm);
45    }
46
47    /**
48     * Cast to HTTP query string
49     *
50     * @param  mixed $url
51     * @param  Laminas\OAuth\Config $config
52     * @param  null|array $params
53     * @return string
54     * @throws Exception\InvalidArgumentException
55     */
56    public function toQueryString($url, Config $config, array $params = null)
57    {
58        $uri = Uri\UriFactory::factory($url);
59        if (!$uri->isValid()
60            || !in_array($uri->getScheme(), array('http', 'https'))
61        ) {
62            throw new Exception\InvalidArgumentException(
63                '\'' . $url . '\' is not a valid URI'
64            );
65        }
66        $params = $this->_httpUtility->assembleParams($url, $config, $params);
67        return $this->_httpUtility->toEncodedQueryString($params);
68    }
69
70    /**
71     * Get OAuth client
72     *
73     * @param  array $oauthOptions
74     * @param  null|string $uri
75     * @param  null|array|\Traversable $config
76     * @param  bool $excludeCustomParamsFromHeader
77     * @return Client
78     */
79    public function getHttpClient(array $oauthOptions, $uri = null, $config = null, $excludeCustomParamsFromHeader = true)
80    {
81        $client = new Client($oauthOptions, $uri, $config, $excludeCustomParamsFromHeader);
82        $client->setToken($this);
83        return $client;
84    }
85}
86