|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Actions\AbstractAction |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Actions; |
|
22
|
|
|
|
|
23
|
|
|
use TechDivision\Import\Actions\Processors\ProcessorInterface; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* An abstract action implementation. |
|
27
|
|
|
* |
|
28
|
|
|
* @author Tim Wagner <[email protected]> |
|
29
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
30
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
31
|
|
|
* @link https://github.com/techdivision/import |
|
32
|
|
|
* @link http://www.techdivision.com |
|
33
|
|
|
*/ |
|
34
|
|
|
abstract class AbstractAction implements ActionInterface |
|
35
|
|
|
{ |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The persist processor instance. |
|
39
|
|
|
* |
|
40
|
|
|
* @var \TechDivision\Import\Actions\Processors\ProcessorInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $persistProcessor; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* The remove processor instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @var \TechDivision\Import\Actions\Processors\ProcessorInterface |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $removeProcessor; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Set's the persist processor instance. |
|
53
|
|
|
* |
|
54
|
|
|
* @param \TechDivision\Import\Actions\Processors\ProcessorInterface $persistProcessor The persist processor instance to use |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function setPersistProcessor(ProcessorInterface $persistProcessor) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->persistProcessor = $persistProcessor; |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Return's the processor instance. |
|
65
|
|
|
* |
|
66
|
|
|
* @return \TechDivision\Import\Actions\Processors\ProcessorInterface The processor instance |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function getPersistProcessor() |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->persistProcessor; |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Set's the remove processor instance. |
|
75
|
|
|
* |
|
76
|
|
|
* @param \TechDivision\Import\Actions\Processors\ProcessorInterface $removeProcessor The remove processor instance to use |
|
77
|
|
|
* |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public function setRemoveProcessor(ProcessorInterface $removeProcessor) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->removeProcessor = $removeProcessor; |
|
83
|
1 |
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Return's the processor instance. |
|
87
|
|
|
* |
|
88
|
|
|
* @return \TechDivision\Import\Actions\Processors\ProcessorInterface The processor instance |
|
89
|
|
|
*/ |
|
90
|
1 |
|
public function getRemoveProcessor() |
|
91
|
|
|
{ |
|
92
|
1 |
|
return $this->removeProcessor; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Persist's the passed row. |
|
97
|
|
|
* |
|
98
|
|
|
* @param array $row The row to persist |
|
99
|
|
|
* |
|
100
|
|
|
* @return void |
|
101
|
|
|
*/ |
|
102
|
1 |
|
public function persist($row) |
|
103
|
|
|
{ |
|
104
|
1 |
|
$this->getPersistProcessor()->execute($row); |
|
105
|
1 |
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Remove's the entity with the passed attributes. |
|
109
|
|
|
* |
|
110
|
|
|
* @param array $row The attributes of the entity to remove |
|
111
|
|
|
* |
|
112
|
|
|
* @return void |
|
113
|
|
|
*/ |
|
114
|
1 |
|
public function remove($row) |
|
115
|
|
|
{ |
|
116
|
1 |
|
$this->getRemoveProcessor()->execute($row); |
|
117
|
1 |
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
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.