|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Cubiche package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright (c) Cubiche |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
namespace Cubiche\Infrastructure\Collections\Doctrine\ODM\MongoDB\Types; |
|
12
|
|
|
|
|
13
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMap; |
|
14
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArrayHashMapInterface; |
|
15
|
|
|
use Doctrine\ODM\MongoDB\Types\HashType; |
|
16
|
|
|
use Doctrine\ODM\MongoDB\Types\Type; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Array HashMap Type Class. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
|
22
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class ArrayHashMapType extends HashType |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $innerType = ''; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function innerType() |
|
35
|
|
|
{ |
|
36
|
|
|
return $this->innerType; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $innerType |
|
41
|
|
|
*/ |
|
42
|
|
|
public function setInnerType($innerType) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->innerType = $innerType; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function convertToDatabaseValue($value) |
|
51
|
|
|
{ |
|
52
|
|
|
if ($value !== null && $value instanceof ArrayHashMapInterface) { |
|
53
|
|
|
$items = array(); |
|
54
|
|
|
$type = Type::getType($this->innerType); |
|
55
|
|
|
foreach ($value as $key => $item) { |
|
56
|
|
|
$items[$key] = $type->convertToDatabaseValue($item); |
|
57
|
|
|
} |
|
58
|
|
|
$value = $items; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return parent::convertToDatabaseValue($value); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
public function convertToPHPValue($value) |
|
68
|
|
|
{ |
|
69
|
|
|
if ($value === null) { |
|
70
|
|
|
return new ArrayHashMap(); |
|
|
|
|
|
|
71
|
|
|
} |
|
72
|
|
|
if (is_array($value) || $value instanceof \Traversable) { |
|
73
|
|
|
$items = array(); |
|
74
|
|
|
$type = Type::getType($this->innerType); |
|
75
|
|
|
foreach ($value as $key => $item) { |
|
76
|
|
|
$items[$key] = $type->convertToPHPValue($item); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return new ArrayHashMap($items); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return parent::convertToPHPValue($value); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
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.