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