|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of EC-CUBE |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright(c) 2000-2017 LOCKON CO.,LTD. All Rights Reserved. |
|
6
|
|
|
* |
|
7
|
|
|
* http://www.lockon.co.jp/ |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software; you can redistribute it and/or |
|
10
|
|
|
* modify it under the terms of the GNU General Public License |
|
11
|
|
|
* as published by the Free Software Foundation; either version 2 |
|
12
|
|
|
* of the License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU General Public License |
|
20
|
|
|
* along with this program; if not, write to the Free Software |
|
21
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace Eccube\Common\Collection; |
|
25
|
|
|
|
|
26
|
|
|
use Closure; |
|
27
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
28
|
|
|
|
|
29
|
|
|
abstract class StrictArrayCollection extends ArrayCollection |
|
|
|
|
|
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* {@inheritdoc} |
|
33
|
|
|
*/ |
|
34
|
37 |
|
public function __construct(array $array = array()) |
|
35
|
|
|
{ |
|
36
|
37 |
|
parent::__construct(); |
|
37
|
|
|
|
|
38
|
37 |
|
foreach ($array as $key => $value) { |
|
39
|
32 |
|
$this->set($key, $value); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param mixed $element |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
abstract public function checkValue($element); |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
* @throws \InvalidArgumentException |
|
52
|
|
|
*/ |
|
53
|
32 |
View Code Duplication |
final public function set($key, $value) |
|
|
|
|
|
|
54
|
|
|
{ |
|
55
|
32 |
|
if (!$this->checkValue($value)) { |
|
56
|
|
|
throw new \InvalidArgumentException(sprintf("%s does not accept the passed element.", get_class($this))); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
32 |
|
parent::set($key, $value); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
* @throws \InvalidArgumentException |
|
65
|
|
|
*/ |
|
66
|
4 |
View Code Duplication |
final public function add($value) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
4 |
|
if (!$this->checkValue($value)) { |
|
69
|
1 |
|
throw new \InvalidArgumentException(sprintf('%s does not accept the passed element.', get_class($this))); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
3 |
|
return parent::add($value); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* {@inheritdoc} |
|
77
|
|
|
* @throws \InvalidArgumentException |
|
78
|
|
|
*/ |
|
79
|
|
|
final public function map(Closure $func) |
|
80
|
|
|
{ |
|
81
|
|
|
$collection = $this; |
|
82
|
|
|
|
|
83
|
|
|
$newFunc = function ($element) use ($collection, $func) { |
|
84
|
|
|
|
|
85
|
|
|
$new = $func($element); |
|
86
|
|
|
|
|
87
|
|
|
if (!$collection->checkValue($new)) { |
|
88
|
|
|
throw new \InvalidArgumentException(sprintf('Element converted to unacceptable value. Use %s::looseMap(Closure) instead.', get_class($collection))); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $new; |
|
92
|
|
|
}; |
|
93
|
|
|
|
|
94
|
|
|
return parent::map($newFunc); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* 型を保証しないmap |
|
99
|
|
|
* |
|
100
|
|
|
* @param Closure $func |
|
101
|
|
|
* @return ArrayCollection |
|
102
|
|
|
*/ |
|
103
|
|
|
final public function looseMap(Closure $func) |
|
104
|
|
|
{ |
|
105
|
|
|
return new parent(array_map($func, $this->toArray())); |
|
106
|
|
|
} |
|
107
|
|
|
} |