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\ArraySet; |
14
|
|
|
use Cubiche\Core\Collections\ArrayCollection\ArraySetInterface; |
15
|
|
|
use Doctrine\ODM\MongoDB\Types\CollectionType; |
16
|
|
|
use Doctrine\ODM\MongoDB\Types\Type; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Array Set Type Class. |
20
|
|
|
* |
21
|
|
|
* @author Karel Osorio Ramírez <[email protected]> |
22
|
|
|
* @author Ivannis Suárez Jerez <[email protected]> |
23
|
|
|
*/ |
24
|
|
View Code Duplication |
class ArraySetType extends CollectionType |
|
|
|
|
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 ArraySetInterface) { |
53
|
|
|
$items = array(); |
54
|
|
|
$type = Type::getType($this->innerType); |
55
|
|
|
foreach ($value as $item) { |
56
|
|
|
$items[] = $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 ArraySet(); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
if (is_array($value) || $value instanceof \Traversable) { |
73
|
|
|
$items = array(); |
74
|
|
|
$type = Type::getType($this->innerType); |
75
|
|
|
foreach ($value as $item) { |
76
|
|
|
$items[] = $type->convertToPHPValue($item); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return new ArraySet($items); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return parent::convertToPHPValue($value); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.