1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Graze\CiffRenderer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Filesystem\Filesystem; |
6
|
|
|
use Graze\CiffRenderer\Parser\ParserDocument; |
7
|
|
|
use Graze\CiffRenderer\Renderer\RendererDocument; |
8
|
|
|
|
9
|
|
|
class CiffRenderer |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var Filesystem |
13
|
|
|
*/ |
14
|
|
|
private $filesystem; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var ParserDocument |
18
|
|
|
*/ |
19
|
|
|
private $parserDocument; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var RendererDocument |
23
|
|
|
*/ |
24
|
|
|
private $rendererDocument; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var callable |
28
|
|
|
*/ |
29
|
|
|
private $fontResolver; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var callable |
33
|
|
|
*/ |
34
|
|
|
private $graphicResolver; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Filesystem $filesystem |
38
|
|
|
* @param ParserDocument $parserDocument |
39
|
|
|
* @param RendererDocument $rendererDocument |
40
|
|
|
*/ |
41
|
2 |
|
public function __construct( |
42
|
|
|
Filesystem $filesystem, |
43
|
|
|
ParserDocument $parserDocument, |
44
|
|
|
RendererDocument $rendererDocument |
45
|
|
|
) { |
46
|
2 |
|
$this->filesystem = $filesystem; |
47
|
2 |
|
$this->parserDocument = $parserDocument; |
48
|
2 |
|
$this->rendererDocument = $rendererDocument; |
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Render an image from a ciff file located on the filesystem. |
53
|
|
|
* |
54
|
|
|
* @param string $path |
55
|
|
|
* @return Intervention\Image\Image |
|
|
|
|
56
|
|
|
*/ |
57
|
1 |
|
public function renderFile($path) |
58
|
|
|
{ |
59
|
1 |
|
$string = $this->filesystem->get($path); |
60
|
1 |
|
return $this->renderString($string); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Render an image from ciff data contained within a string. |
65
|
|
|
* |
66
|
|
|
* @param string $string |
67
|
|
|
* @return Intervention\Image\Image |
|
|
|
|
68
|
|
|
*/ |
69
|
2 |
|
public function renderString($string) |
70
|
|
|
{ |
71
|
2 |
|
$this->parserDocument->parse($string); |
72
|
|
|
|
73
|
2 |
|
return $this->rendererDocument->render( |
74
|
2 |
|
$this->parserDocument, |
75
|
2 |
|
$this->fontResolver, |
76
|
2 |
|
$this->graphicResolver |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param callable $fontResolver |
82
|
|
|
*/ |
83
|
2 |
|
public function setFontResolver(callable $fontResolver) |
84
|
|
|
{ |
85
|
2 |
|
$this->fontResolver = $fontResolver; |
86
|
2 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param callable $graphicResolver |
90
|
|
|
*/ |
91
|
2 |
|
public function setGraphicResolver(callable $graphicResolver) |
92
|
|
|
{ |
93
|
2 |
|
$this->graphicResolver = $graphicResolver; |
94
|
2 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return CiffRenderer |
98
|
|
|
*/ |
99
|
|
|
public static function factory() |
100
|
|
|
{ |
101
|
|
|
return new static( |
102
|
|
|
new Filesystem(), |
103
|
|
|
ParserDocument::factory(), |
104
|
|
|
RendererDocument::factory() |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.