1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco; |
4
|
|
|
|
5
|
|
|
use Interop\Container\Definition\DefinitionInterface; |
6
|
|
|
use Interop\Container\Definition\FactoryCallDefinitionInterface; |
7
|
|
|
use Interop\Container\Definition\ObjectDefinitionInterface; |
8
|
|
|
use Interop\Container\Definition\ParameterDefinitionInterface; |
9
|
|
|
use Interop\Container\Definition\ReferenceDefinitionInterface; |
10
|
|
|
use TheCodingMachine\Yaco\Definition\AliasDefinition; |
11
|
|
|
use TheCodingMachine\Yaco\Definition\FactoryCallDefinition; |
12
|
|
|
use TheCodingMachine\Yaco\Definition\ObjectDefinition; |
13
|
|
|
use TheCodingMachine\Yaco\Definition\ParameterDefinition; |
14
|
|
|
use TheCodingMachine\Yaco\Definition\Reference; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This class is in charge of converting definitions from the Interop\Container\DefinitionInterface to the |
18
|
|
|
* internal DumpableInterface format. |
19
|
|
|
*/ |
20
|
|
|
class DefinitionConverter implements DefinitionConverterInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param string $identifier |
24
|
|
|
* @param DefinitionInterface|mixed $definition |
25
|
|
|
* |
26
|
|
|
* @return AliasDefinition|FactoryCallDefinition|ObjectDefinition|ParameterDefinition |
27
|
|
|
*/ |
28
|
|
|
public function convert($identifier, $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, $this->convertValue($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
|
|
|
} elseif (is_scalar($definition) || is_array($definition)) { |
58
|
|
|
return new ParameterDefinition($identifier, $this->convertValue($definition)); |
59
|
|
|
} else { |
60
|
|
|
throw new \RuntimeException(sprintf('Cannot convert object of type "%s"', get_class($definition))); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $arguments |
66
|
|
|
* |
67
|
|
|
* @return array |
68
|
|
|
*/ |
69
|
|
|
private function convertArguments(array $arguments) |
70
|
|
|
{ |
71
|
|
|
$yacoArguments = []; |
72
|
|
|
foreach ($arguments as $argument) { |
73
|
|
|
$yacoArguments[] = $this->convertValue($argument); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $yacoArguments; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
private function convertValue($value) |
80
|
|
|
{ |
81
|
|
|
if (is_array($value)) { |
82
|
|
|
return array_map([$this, 'convertValue'], $value); |
83
|
|
|
} elseif ($value instanceof DefinitionInterface) { |
84
|
|
|
return $this->convert(null, $value); |
85
|
|
|
} elseif (is_object($value) || is_resource($value)) { |
86
|
|
|
throw new \RuntimeException('Unable to convert a definition. Parameters cannot be an object or a resource.'); |
87
|
|
|
} else { |
88
|
|
|
return $value; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.