1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Framework; |
4
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\API\ConverterInterface; |
6
|
|
|
use AmaTeam\Image\Projection\API\SpecificationInterface; |
7
|
|
|
use AmaTeam\Image\Projection\API\Type\ReaderInterface; |
8
|
|
|
use AmaTeam\Image\Projection\API\Conversion\FilterInterface; |
9
|
|
|
use AmaTeam\Image\Projection\Type\Registry; |
10
|
|
|
|
11
|
|
|
class Converter implements ConverterInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Registry |
15
|
|
|
*/ |
16
|
|
|
private $registry; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @param Registry $registry |
20
|
|
|
*/ |
21
|
|
|
public function __construct(Registry $registry) |
22
|
|
|
{ |
23
|
|
|
$this->registry = $registry; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param SpecificationInterface $source |
28
|
|
|
* @param SpecificationInterface[] $targets |
29
|
|
|
* @param FilterInterface[] $filters |
30
|
|
|
* @return Conversion[] |
31
|
|
|
*/ |
32
|
|
|
public function createConversions( |
33
|
|
|
SpecificationInterface $source, |
34
|
|
|
array $targets, |
35
|
|
|
...$filters |
36
|
|
|
) { |
37
|
|
|
$reader = $this->getReader($source); |
38
|
|
|
$pipelines = []; |
39
|
|
|
foreach ($targets as $target) { |
40
|
|
|
$pipelines[] = $this |
41
|
|
|
->instantiateConversion($reader, $target, $filters); |
42
|
|
|
} |
43
|
|
|
return $pipelines; |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Creates conversion pipeline for single target. |
48
|
|
|
* |
49
|
|
|
* @param SpecificationInterface $source |
50
|
|
|
* @param SpecificationInterface $target |
51
|
|
|
* @param FilterInterface[] $filters |
52
|
|
|
* @return Conversion |
53
|
|
|
*/ |
54
|
|
|
public function createConversion( |
55
|
|
|
SpecificationInterface $source, |
56
|
|
|
SpecificationInterface $target, |
57
|
|
|
...$filters |
58
|
|
|
) { |
59
|
|
|
$reader = $this->getReader($source); |
60
|
|
|
return $this->instantiateConversion($reader, $target, $filters); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param SpecificationInterface $source |
65
|
|
|
* @return ReaderInterface |
66
|
|
|
*/ |
67
|
|
|
private function getReader(SpecificationInterface $source) |
68
|
|
|
{ |
69
|
|
|
return $this |
70
|
|
|
->registry |
71
|
|
|
->getHandler($source->getType()) |
72
|
|
|
->read($source); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param ReaderInterface $source |
77
|
|
|
* @param SpecificationInterface $target |
78
|
|
|
* @param FilterInterface[] $filters |
79
|
|
|
* @return Conversion |
80
|
|
|
*/ |
81
|
|
|
private function instantiateConversion( |
82
|
|
|
ReaderInterface $source, |
83
|
|
|
SpecificationInterface $target, |
84
|
|
|
array $filters |
85
|
|
|
) { |
86
|
|
|
$generator = $this |
87
|
|
|
->registry |
88
|
|
|
->getHandler($target->getType()) |
89
|
|
|
->createGenerator($source, $target, $filters); |
90
|
|
|
return new Conversion($target, $generator); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: