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
|
|
|
/** |
25
|
|
|
* Client constructor. |
26
|
|
|
* |
27
|
|
|
* @param $config |
28
|
|
|
*/ |
29
|
15 |
|
public function __construct($config) |
30
|
|
|
{ |
31
|
15 |
|
$this->config = $config; |
32
|
15 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Check if we can call a method on the manipulations builder. |
36
|
|
|
* Else check if a predefined manipulation set need to be called. |
37
|
|
|
* |
38
|
|
|
* @param string $method |
39
|
|
|
* @param array $arguments |
40
|
|
|
* @return $this |
41
|
|
|
* @throws UndefinedDefinitionException |
42
|
|
|
*/ |
43
|
11 |
|
public function __call($method, $arguments) |
44
|
|
|
{ |
45
|
11 |
|
if (method_exists($this->builder, $method)) { |
46
|
8 |
|
call_user_func_array([$this->builder, $method], $arguments); |
47
|
|
|
|
48
|
8 |
|
return $this; |
49
|
3 |
|
} elseif (isset($this->definitions[$method]) && is_callable($this->definitions[$method])) { |
50
|
2 |
|
array_unshift($arguments, $this->builder); |
51
|
|
|
|
52
|
2 |
|
call_user_func_array($this->definitions[$method], $arguments); |
53
|
|
|
|
54
|
2 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
throw UndefinedDefinitionException::definitionNotDefined($method); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Define a predefined set of manipulations. |
62
|
|
|
* |
63
|
|
|
* @param string $key |
64
|
|
|
* @param Closure $callback |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
2 |
|
public function define($key, Closure $callback) |
68
|
|
|
{ |
69
|
2 |
|
$this->definitions[$key] = $callback; |
70
|
|
|
|
71
|
2 |
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Let the client fetch the given url as image. |
76
|
|
|
* |
77
|
|
|
* @param string $url |
78
|
|
|
* @return $this |
79
|
|
|
*/ |
80
|
15 |
|
public function fetch($url) |
81
|
|
|
{ |
82
|
15 |
|
$this->builder = new Builder($url); |
83
|
|
|
|
84
|
15 |
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Retrieve the imaginary url. |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
* @throws InvalidConfigurationException |
92
|
|
|
*/ |
93
|
14 |
|
public function url() |
94
|
|
|
{ |
95
|
14 |
|
if (!isset($this->config['url'])) { |
96
|
2 |
|
throw InvalidConfigurationException::urlNotDefined(); |
97
|
|
|
} |
98
|
|
|
|
99
|
12 |
|
if (!isset($this->config['client'])) { |
100
|
1 |
|
throw InvalidConfigurationException::clientNotDefined(); |
101
|
|
|
} |
102
|
|
|
|
103
|
11 |
|
return implode('/', array_filter([ |
104
|
11 |
|
$this->config['url'], |
105
|
11 |
|
$this->config['client'], |
106
|
11 |
|
$this->getResourceKey(), |
107
|
11 |
|
$this->getTypeKey(), |
108
|
11 |
|
$this->builder->getManipulations(), |
109
|
11 |
|
$this->builder->getKey(), |
110
|
11 |
|
])); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the resource key. |
115
|
|
|
* We currently only support image. |
116
|
|
|
* |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
11 |
|
protected function getResourceKey() |
120
|
|
|
{ |
121
|
11 |
|
return 'images'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get the type key. |
126
|
|
|
* We only support fetch at the moment. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
11 |
|
protected function getTypeKey() |
131
|
|
|
{ |
132
|
11 |
|
return 'fetch'; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Return the url for this image. |
137
|
|
|
* |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
1 |
|
public function __toString() |
141
|
|
|
{ |
142
|
1 |
|
return $this->url(); |
143
|
|
|
} |
144
|
|
|
} |