Repository navigation
Expand file tree
/
Copy pathApiObject.php
More file actions
100 lines (93 loc) · 2.47 KB
/
Copy pathApiObject.php
File metadata and controls
100 lines (93 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
/**
* This file contains code about \CloudConvert\ApiObject class
*/
namespace CloudConvert;
/**
* Base class for Objects returned from the CloudConvert API
*
* @package CloudConvert
* @category CloudConvert
* @author Josias Montag <josias@montag.info>
*/
class ApiObject
{
/** @var Api */
protected $api;
/** @var string */
public $url;
/**
* Contains the object data returned from the CloudConvert API
* @var array
*/
protected $data = array();
/**
* Construct a new ApiObject instance
*
* @param Api $api
* @param string $url The Object URL
*
* @throws Exceptions\InvalidParameterException If one parameter is missing or with bad value
*/
public function __construct(Api $api, $url)
{
if (!isset($api)) {
throw new Exceptions\InvalidParameterException("API parameter is not set");
}
if (!isset($url)) {
throw new Exceptions\InvalidParameterException("Object URL parameter is not set");
}
$this->api = $api;
$this->url = $url;
return $this;
}
/**
* Refresh Object Data
*
* @param array $parameters Parameters for refreshing the Object.
*
* @return \CloudConvert\ApiObject
*
* @throws \CloudConvert\Exceptions\ApiException if the CloudConvert API returns an error
* @throws \GuzzleHttp\Exception\GuzzleException if there is a general HTTP / network error
*
*/
public function refresh($parameters = null)
{
$this->data = $this->api->get($this->url, $parameters, false);
return $this;
}
/**
* Access Object data via $object->prop->subprop
*
* @param string $name
* @return null|object
*/
public function __get($name)
{
if (is_array($this->data) && array_key_exists($name, $this->data)) {
return self::arrayToObject($this->data[$name]);
}
return null;
}
/**
* Converts multi dimensional arrays into objects
*
* @param array $d
* @return object
*/
private static function arrayToObject($d)
{
if (is_array($d)) {
/*
* Return array converted to object
* Using [__CLASS__, __METHOD__] (Magic constant)
* for recursive call
*/
return (object)array_map([__CLASS__, __METHOD__], $d);
} else {
// Return object
return $d;
}
}
}