|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ArjanWestdorp\Imaginary; |
|
4
|
|
|
|
|
5
|
|
|
use ArjanWestdorp\Imaginary\Exceptions\InvalidConfigurationException; |
|
6
|
|
|
use ArjanWestdorp\Imaginary\Exceptions\UndefinedDefinitionException; |
|
7
|
|
|
use Closure; |
|
8
|
|
|
|
|
9
|
|
|
class Client |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var Builder |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $builder; |
|
15
|
|
|
/** |
|
16
|
|
|
* @var array; |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $definitions = []; |
|
19
|
|
|
/** |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $config; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var array |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $defaults = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Client constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param $config |
|
32
|
|
|
*/ |
|
33
|
18 |
|
public function __construct($config) |
|
34
|
|
|
{ |
|
35
|
18 |
|
$this->config = $config; |
|
36
|
18 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Check if we can call a method on the manipulations builder. |
|
40
|
|
|
* Else check if a predefined manipulation set need to be called. |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $method |
|
43
|
|
|
* @param array $arguments |
|
44
|
|
|
* @return $this |
|
45
|
|
|
* @throws UndefinedDefinitionException |
|
46
|
|
|
*/ |
|
47
|
13 |
|
public function __call($method, $arguments) |
|
48
|
|
|
{ |
|
49
|
13 |
|
if (method_exists($this->builder, $method)) { |
|
50
|
10 |
|
call_user_func_array([$this->builder, $method], $arguments); |
|
51
|
|
|
|
|
52
|
10 |
|
return $this; |
|
53
|
3 |
|
} elseif (isset($this->definitions[$method]) && is_callable($this->definitions[$method])) { |
|
54
|
2 |
|
array_unshift($arguments, $this->builder); |
|
55
|
|
|
|
|
56
|
2 |
|
call_user_func_array($this->definitions[$method], $arguments); |
|
57
|
|
|
|
|
58
|
2 |
|
return $this; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
1 |
|
throw UndefinedDefinitionException::definitionNotDefined($method); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Define a predefined set of manipulations. |
|
66
|
|
|
* |
|
67
|
|
|
* @param string $key |
|
68
|
|
|
* @param Closure $callback |
|
69
|
|
|
* @return $this |
|
70
|
|
|
*/ |
|
71
|
2 |
|
public function define($key, Closure $callback) |
|
72
|
|
|
{ |
|
73
|
2 |
|
$this->definitions[$key] = $callback; |
|
74
|
|
|
|
|
75
|
2 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Let the client fetch the given url as image. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $url |
|
82
|
|
|
* @return $this |
|
83
|
|
|
*/ |
|
84
|
18 |
|
public function fetch($url) |
|
85
|
|
|
{ |
|
86
|
18 |
|
$this->builder = new Builder(urlencode($url)); |
|
87
|
|
|
|
|
88
|
18 |
|
foreach ($this->defaults as $key => $value) { |
|
89
|
2 |
|
if (method_exists($this->builder, $key)) { |
|
90
|
2 |
|
call_user_func([$this->builder, $key], $value); |
|
91
|
2 |
|
} |
|
92
|
18 |
|
} |
|
93
|
|
|
|
|
94
|
18 |
|
return $this; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Set the default dpr. |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $key |
|
101
|
|
|
* @param mixed $value |
|
102
|
|
|
* @return $this |
|
103
|
|
|
*/ |
|
104
|
2 |
|
public function setDefault($key, $value) |
|
105
|
|
|
{ |
|
106
|
2 |
|
$this->defaults[$key] = $value; |
|
107
|
|
|
|
|
108
|
2 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Retrieve the imaginary url. |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
17 |
|
public function url() |
|
117
|
|
|
{ |
|
118
|
17 |
|
if (!isset($this->config['url'])) { |
|
119
|
2 |
|
throw InvalidConfigurationException::urlNotDefined(); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
15 |
|
if (!isset($this->config['client'])) { |
|
123
|
1 |
|
throw InvalidConfigurationException::clientNotDefined(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
14 |
|
return implode('/', array_filter([ |
|
127
|
14 |
|
$this->config['url'], |
|
128
|
14 |
|
$this->config['client'], |
|
129
|
14 |
|
$this->getResourceKey(), |
|
130
|
14 |
|
$this->getTypeKey(), |
|
131
|
14 |
|
$this->builder->getManipulations(), |
|
132
|
14 |
|
$this->builder->getKey(), |
|
133
|
14 |
|
])); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Get the resource key. |
|
138
|
|
|
* We currently only support image. |
|
139
|
|
|
* |
|
140
|
|
|
* @return string |
|
141
|
|
|
*/ |
|
142
|
14 |
|
protected function getResourceKey() |
|
143
|
|
|
{ |
|
144
|
14 |
|
return 'images'; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get the type key. |
|
149
|
|
|
* We only support fetch at the moment. |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
14 |
|
protected function getTypeKey() |
|
154
|
|
|
{ |
|
155
|
14 |
|
return 'fetch'; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Return the url for this image. |
|
160
|
|
|
* |
|
161
|
|
|
* @return string |
|
162
|
|
|
*/ |
|
163
|
1 |
|
public function __toString() |
|
164
|
|
|
{ |
|
165
|
1 |
|
return $this->url(); |
|
166
|
|
|
} |
|
167
|
|
|
} |