1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hiapi\commands; |
4
|
|
|
|
5
|
|
|
use ArrayAccess; |
6
|
|
|
use Countable; |
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use hiapi\Core\commands\CommandFactory; |
9
|
|
|
use IteratorAggregate; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class BulkCommand |
13
|
|
|
* |
14
|
|
|
* @author Dmytro Naumenko <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
final class BulkCommand extends BaseCommand implements Countable, IteratorAggregate, ArrayAccess |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var ArrayCollection |
20
|
|
|
*/ |
21
|
|
|
private $collection; |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $commandClassName; |
26
|
|
|
|
27
|
|
|
private CommandFactory $factory; |
|
|
|
|
28
|
|
|
|
29
|
|
|
public function __construct(string $className, CommandFactory $factory, $config = []) |
30
|
|
|
{ |
31
|
|
|
$this->commandClassName = $className; |
32
|
|
|
$this->factory = $factory; |
33
|
|
|
parent::__construct($config); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function init(): void |
37
|
|
|
{ |
38
|
|
|
parent::init(); |
39
|
|
|
|
40
|
|
|
$this->collection = new ArrayCollection(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public static function of(string $className, CommandFactory $factory): self |
44
|
|
|
{ |
45
|
|
|
return new self($className, $factory); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function load($data, $formName = null): bool |
49
|
|
|
{ |
50
|
|
|
$data = array_filter($data, 'is_array'); |
51
|
|
|
|
52
|
|
|
for ($i = 0, $iMax = count($data); $i < $iMax; $i++) { |
53
|
|
|
$this->collection->add($this->factory->createByClass($this->commandClassName)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return self::loadMultiple($this->collection, $data, $formName); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function validate($attributeNames = null, $clearErrors = true) |
60
|
|
|
{ |
61
|
|
|
return array_reduce($this->collection->toArray(), static function (bool $isValid, BaseCommand $command): bool { |
62
|
|
|
return $command->validate() && $isValid; |
63
|
|
|
}, true); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function add($command): self |
67
|
|
|
{ |
68
|
|
|
$this->collection->add($command); |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
*/ |
76
|
|
|
public function count() |
77
|
|
|
{ |
78
|
|
|
return $this->collection->count(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @inheritDoc |
83
|
|
|
*/ |
84
|
|
|
public function getIterator() |
85
|
|
|
{ |
86
|
|
|
return $this->collection->getIterator(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @inheritDoc |
91
|
|
|
*/ |
92
|
|
|
public function offsetExists($offset) |
93
|
|
|
{ |
94
|
|
|
return $this->collection->offsetExists($offset); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @inheritDoc |
99
|
|
|
*/ |
100
|
|
|
public function offsetGet($offset) |
101
|
|
|
{ |
102
|
|
|
return $this->collection->offsetGet($offset); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @inheritDoc |
107
|
|
|
*/ |
108
|
|
|
public function offsetSet($offset, $value) |
109
|
|
|
{ |
110
|
|
|
return $this->collection->offsetSet($offset, $value); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @inheritDoc |
115
|
|
|
*/ |
116
|
|
|
public function offsetUnset($offset) |
117
|
|
|
{ |
118
|
|
|
return $this->collection->offsetUnset($offset); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|