|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Loaders\GenericMemberNameLoader |
|
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 2021 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\Loaders; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Generic loader iplementation that loads the values of the member with |
|
25
|
|
|
* the given name of the entities laoded by the parent loader instance. |
|
26
|
|
|
* |
|
27
|
|
|
* @author Tim Wagner <[email protected]> |
|
28
|
|
|
* @copyright 2021 TechDivision GmbH <[email protected]> |
|
29
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
30
|
|
|
* @link https://github.com/techdivision/import |
|
31
|
|
|
* @link http://www.techdivision.com |
|
32
|
|
|
*/ |
|
33
|
|
|
class GenericMemberNameLoader implements LoaderInterface |
|
34
|
|
|
{ |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The array with the member values. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $values; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Construct that initializes the iterator with the parent iterator instance used to load the enities. |
|
45
|
|
|
* |
|
46
|
|
|
* @param \TechDivision\Import\Loaders\LoaderInterface $entityLoader The entity loader instance |
|
47
|
|
|
* @param string $memberName The name of the entity's member to load the values for |
|
48
|
|
|
* |
|
49
|
|
|
* @throws \InvalidArgumentException Is thrown, if one of the loaded entities doesn't contain the member with the given name |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct(LoaderInterface $entityLoader, string $memberName) |
|
52
|
|
|
{ |
|
53
|
|
|
|
|
54
|
|
|
// load the entities |
|
55
|
|
|
$entities = $entityLoader->load(); |
|
56
|
|
|
|
|
57
|
|
|
// load the member values |
|
58
|
|
|
foreach ($entities as $entity) { |
|
59
|
|
|
if (isset($entity[$memberName])) { |
|
60
|
|
|
$this->values[] = $entity[$memberName]; |
|
61
|
|
|
} else { |
|
62
|
|
|
throw new \InvalidArgumentException(sprintf('Mandatory member "%s" not available', $memberName)); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Loads and returns data. |
|
69
|
|
|
* |
|
70
|
|
|
* @return \ArrayAccess The array with the data |
|
71
|
|
|
*/ |
|
72
|
|
|
public function load() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->values; |
|
|
|
|
|
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
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.