|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\Definition\AliasDefinitionInterface; |
|
6
|
|
|
use Interop\Container\Definition\DefinitionInterface; |
|
7
|
|
|
use Interop\Container\Definition\FactoryCallDefinitionInterface; |
|
8
|
|
|
use Interop\Container\Definition\ObjectDefinitionInterface; |
|
9
|
|
|
use Interop\Container\Definition\ParameterDefinitionInterface; |
|
10
|
|
|
use Interop\Container\Definition\ReferenceDefinitionInterface; |
|
11
|
|
|
use TheCodingMachine\Yaco\Definition\AliasDefinition; |
|
12
|
|
|
use TheCodingMachine\Yaco\Definition\FactoryCallDefinition; |
|
13
|
|
|
use TheCodingMachine\Yaco\Definition\ObjectDefinition; |
|
14
|
|
|
use TheCodingMachine\Yaco\Definition\ParameterDefinition; |
|
15
|
|
|
use TheCodingMachine\Yaco\Definition\Reference; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This class is in charge of converting definitions from the Interop\Container\DefinitionInterface to the |
|
19
|
|
|
* internal DumpableInterface format. |
|
20
|
|
|
*/ |
|
21
|
|
|
class DefinitionConverter implements DefinitionConverterInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $identifier |
|
25
|
|
|
* @param DefinitionInterface $definition |
|
26
|
|
|
* @return AliasDefinition|FactoryCallDefinition|ObjectDefinition|ParameterDefinition |
|
27
|
|
|
*/ |
|
28
|
|
|
public function convert($identifier, DefinitionInterface $definition) |
|
29
|
|
|
{ |
|
30
|
|
|
if ($definition instanceof ObjectDefinitionInterface) { |
|
31
|
|
|
$yacoObjectDefinition = new ObjectDefinition($identifier, |
|
32
|
|
|
$definition->getClassName(), |
|
33
|
|
|
$this->convertArguments($definition->getConstructorArguments())); |
|
34
|
|
|
|
|
35
|
|
|
foreach ($definition->getPropertyAssignments() as $assignment) { |
|
36
|
|
|
$yacoObjectDefinition->setProperty($assignment->getPropertyName(), $this->convertValue($assignment->getValue())); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
foreach ($definition->getMethodCalls() as $methodCall) { |
|
40
|
|
|
$yacoObjectDefinition->addMethodCall($methodCall->getMethodName(), $this->convertArguments($methodCall->getArguments())); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $yacoObjectDefinition; |
|
44
|
|
|
} elseif ($definition instanceof FactoryCallDefinitionInterface) { |
|
45
|
|
|
return new FactoryCallDefinition($identifier, |
|
46
|
|
|
$this->convertValue($definition->getFactory()), |
|
47
|
|
|
$definition->getMethodName(), |
|
48
|
|
|
$this->convertArguments($definition->getArguments())); |
|
49
|
|
|
} elseif ($definition instanceof ParameterDefinitionInterface) { |
|
50
|
|
|
return new ParameterDefinition($identifier, $definition->getValue()); |
|
51
|
|
|
} elseif ($definition instanceof ReferenceDefinitionInterface) { |
|
52
|
|
|
if ($identifier !== null) { |
|
53
|
|
|
return new AliasDefinition($identifier, $definition->getTarget()); |
|
54
|
|
|
} else { |
|
55
|
|
|
return new Reference($definition->getTarget()); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
} else { |
|
58
|
|
|
throw new \RuntimeException(sprintf('Cannot convert object of type "%s"', get_class($definition))); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param array $arguments |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
private function convertArguments(array $arguments) |
|
68
|
|
|
{ |
|
69
|
|
|
$yacoArguments = []; |
|
70
|
|
|
foreach ($arguments as $argument) { |
|
71
|
|
|
$yacoArguments[] = $this->convertValue($argument); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $yacoArguments; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
private function convertValue($value) |
|
78
|
|
|
{ |
|
79
|
|
|
if (is_array($value)) { |
|
80
|
|
|
return array_map([$this, 'convertValue'], $value); |
|
81
|
|
|
} elseif ($value instanceof DefinitionInterface) { |
|
82
|
|
|
return $this->convert(null, $value); |
|
83
|
|
|
} elseif (is_object($value) || is_resource($value)) { |
|
84
|
|
|
throw new \RuntimeException('Unable to convert a definition. Parameters cannot be an object or a resource.'); |
|
85
|
|
|
} else { |
|
86
|
|
|
return $value; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
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.