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
|
15 |
|
* Client constructor. |
30
|
|
|
* |
31
|
15 |
|
* @param $config |
32
|
15 |
|
*/ |
33
|
|
|
public function __construct($config) |
34
|
|
|
{ |
35
|
|
|
$this->config = $config; |
36
|
|
|
} |
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
|
11 |
|
* @param array $arguments |
44
|
|
|
* @return $this |
45
|
11 |
|
* @throws UndefinedDefinitionException |
46
|
8 |
|
*/ |
47
|
|
|
public function __call($method, $arguments) |
48
|
8 |
|
{ |
49
|
3 |
|
if (method_exists($this->builder, $method)) { |
50
|
2 |
|
call_user_func_array([$this->builder, $method], $arguments); |
51
|
|
|
|
52
|
2 |
|
return $this; |
53
|
|
|
} elseif (isset($this->definitions[$method]) && is_callable($this->definitions[$method])) { |
54
|
2 |
|
array_unshift($arguments, $this->builder); |
55
|
|
|
|
56
|
|
|
call_user_func_array($this->definitions[$method], $arguments); |
57
|
1 |
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
throw UndefinedDefinitionException::definitionNotDefined($method); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Define a predefined set of manipulations. |
66
|
|
|
* |
67
|
2 |
|
* @param string $key |
68
|
|
|
* @param Closure $callback |
69
|
2 |
|
* @return $this |
70
|
|
|
*/ |
71
|
2 |
|
public function define($key, Closure $callback) |
72
|
|
|
{ |
73
|
|
|
$this->definitions[$key] = $callback; |
74
|
|
|
|
75
|
|
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Let the client fetch the given url as image. |
80
|
15 |
|
* |
81
|
|
|
* @param string $url |
82
|
15 |
|
* @return $this |
83
|
|
|
*/ |
84
|
15 |
|
public function fetch($url) |
85
|
|
|
{ |
86
|
|
|
$this->builder = new Builder(urlencode($url)); |
87
|
|
|
|
88
|
|
|
foreach ($this->defaults as $key => $value) { |
89
|
|
|
if (method_exists($this->builder, $key)) { |
90
|
|
|
call_user_func([$this->builder, $key], $value); |
91
|
|
|
} |
92
|
|
|
} |
93
|
14 |
|
|
94
|
|
|
return $this; |
95
|
14 |
|
} |
96
|
2 |
|
|
97
|
|
|
/** |
98
|
|
|
* Set the default dpr. |
99
|
12 |
|
* |
100
|
1 |
|
* @param string $key |
101
|
|
|
* @param mixed $value |
102
|
|
|
* @return $this |
103
|
11 |
|
*/ |
104
|
11 |
|
public function default($key, $value) |
|
|
|
|
105
|
11 |
|
{ |
106
|
11 |
|
$this->defaults[$key] = $value; |
107
|
11 |
|
|
108
|
11 |
|
return $this; |
|
|
|
|
109
|
11 |
|
} |
110
|
11 |
|
|
111
|
|
|
/** |
112
|
|
|
* Retrieve the imaginary url. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
public function url() |
117
|
|
|
{ |
118
|
|
|
if (!isset($this->config['url'])) { |
119
|
11 |
|
throw InvalidConfigurationException::urlNotDefined(); |
120
|
|
|
} |
121
|
11 |
|
|
122
|
|
|
if (!isset($this->config['client'])) { |
123
|
|
|
throw InvalidConfigurationException::clientNotDefined(); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return implode('/', array_filter([ |
127
|
|
|
$this->config['url'], |
128
|
|
|
$this->config['client'], |
129
|
|
|
$this->getResourceKey(), |
130
|
11 |
|
$this->getTypeKey(), |
131
|
|
|
$this->builder->getManipulations(), |
132
|
11 |
|
$this->builder->getKey(), |
133
|
|
|
])); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the resource key. |
138
|
|
|
* We currently only support image. |
139
|
|
|
* |
140
|
1 |
|
* @return string |
141
|
|
|
*/ |
142
|
1 |
|
protected function getResourceKey() |
143
|
|
|
{ |
144
|
|
|
return 'images'; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Get the type key. |
149
|
|
|
* We only support fetch at the moment. |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function getTypeKey() |
154
|
|
|
{ |
155
|
|
|
return 'fetch'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Return the url for this image. |
160
|
|
|
* |
161
|
|
|
* @return string |
162
|
|
|
*/ |
163
|
|
|
public function __toString() |
164
|
|
|
{ |
165
|
|
|
return $this->url(); |
166
|
|
|
} |
167
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.