|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Graze\CiffRenderer\Renderer\FieldRenderer; |
|
4
|
|
|
|
|
5
|
|
|
use Graze\CiffRenderer\Renderer\FieldRenderer\GraphicPrimitive\GraphicPrimitiveFactory; |
|
6
|
|
|
use Graze\CiffRenderer\Parser\FieldParser\FieldParserInterface; |
|
7
|
|
|
use Graze\CiffRenderer\Parser\FieldParser\FieldParserBarcode; |
|
8
|
|
|
use Graze\CiffRenderer\Parser\FieldParser\FieldParserStaticGraphic; |
|
9
|
|
|
use Graze\CiffRenderer\Parser\FieldParser\FieldParserGraphicPrimitive; |
|
10
|
|
|
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererBarcode; |
|
11
|
|
|
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererFixedText; |
|
12
|
|
|
use Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererStaticGraphic; |
|
13
|
|
|
|
|
14
|
|
|
class FieldRendererFactory |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var GraphicPrimitiveFactory |
|
18
|
|
|
*/ |
|
19
|
|
|
private $graphicPrimitiveFactory; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param GraphicPrimitiveFactory $graphicPrimitiveFactory |
|
23
|
|
|
*/ |
|
24
|
4 |
|
public function __construct(GraphicPrimitiveFactory $graphicPrimitiveFactory) |
|
25
|
|
|
{ |
|
26
|
4 |
|
$this->graphicPrimitiveFactory = $graphicPrimitiveFactory; |
|
27
|
4 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @param FieldParserInterface $parser |
|
31
|
|
|
* @return Graze\CiffRenderer\Renderer\FieldRenderer\FieldRendererInterface |
|
32
|
|
|
*/ |
|
33
|
4 |
|
public function getFieldRenderer(FieldParserInterface $parser) |
|
34
|
|
|
{ |
|
35
|
4 |
|
switch (get_class($parser)) { |
|
36
|
4 |
|
case FieldParserBarcode::class: |
|
37
|
1 |
|
$renderer = FieldRendererBarcode::class; |
|
38
|
1 |
|
break; |
|
39
|
|
|
|
|
40
|
3 |
|
case FieldParserStaticGraphic::class: |
|
41
|
1 |
|
$renderer = FieldRendererStaticGraphic::class; |
|
42
|
1 |
|
break; |
|
43
|
|
|
|
|
44
|
2 |
|
case FieldParserGraphicPrimitive::class: |
|
45
|
1 |
|
return $this->graphicPrimitiveFactory->getFieldRenderer($parser->getShape()); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
1 |
|
default: |
|
48
|
1 |
|
$renderer = FieldRendererFixedText::class; |
|
49
|
1 |
|
break; |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
3 |
|
return $renderer::factory(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return FieldRendererFactory |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function factory() |
|
59
|
|
|
{ |
|
60
|
|
|
return new static( |
|
61
|
|
|
new GraphicPrimitiveFactory() |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.