Completed
Push — master ( e5b8a7...dc704c )
by Albert
05:34
created

AbstractApi::getMeta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Vultr PHP library.
5
 *
6
 * (c) Albert Leitato <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Vultr\Api;
13
14
use Vultr\Adapter\AdapterInterface;
15
use Vultr\Entity\Meta;
16
17
/**
18
 * @author Albert Leitato <[email protected]>
19
 */
20
abstract class AbstractApi
21
{
22
    /**
23
     * @var string
24
     */
25
    protected $endpoint = 'https://api.vultr.com/v1';
26
27
    /**
28
     * @var AdapterInterface
29
     */
30
    protected $adapter;
31
32
    /**
33
     * @var Meta
34
     */
35
    protected $meta;
36
37
    /**
38
     * @param AdapterInterface $adapter
39
     */
40
    public function __construct(AdapterInterface $adapter)
41
    {
42
        $this->adapter = $adapter;
43
    }
44
45
    /**
46
     * Bind response to Entity.
47
     *
48
     * Decode API response and bind the response to the respective entity
49
     *
50
     * @param string $response the response from the API
51
     * @param string $class    Entity to bind the response to
52
     * @param bool   $isArray  If the response is a collection
53
     *
54
     * @return object Entity instance
55
     **/
56
    protected function handleResponse($response, $class, $isArray = false)
57
    {
58
        $object = json_decode($response, true);
59
        if ($isArray) {
60
            return array_map(function ($entity) use ($class) {
61
                return new $class($entity);
62
            }, $object);
63
        }
64
65
        return new $class($object);
66
    }
67
}
68