-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRawData.php
More file actions
77 lines (62 loc) · 1.33 KB
/
RawData.php
File metadata and controls
77 lines (62 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<?php
namespace G4\DataMapper\Common;
use G4\DataMapper\Exception\EmptyDataException;
class RawData implements \Countable
{
/**
* @var int
*/
private $count;
/**
* @var array
*/
private $data;
/**
* @var int
*/
private $total;
/**
* @param array $data
* @param int $total
*/
public function __construct(array $data, $total)
{
$this->data = $data;
$this->total = $total;
}
public function count(): int
{
if ($this->count === null) {
$this->count = count($this->data);
}
return $this->count;
}
public function getAll()
{
return $this->data;
}
public function getOne()
{
return $this->count() > 0
? current($this->data)
: null;
}
public function getTotal()
{
return $this->total;
}
public function getAllDataWithIdIdentifier($identifier)
{
$rawData = ($this->data === null || empty($this->data)) ? [] : $this->data;
$data = [];
foreach ($rawData as $item) {
if (isset($item[$identifier])) {
$data[$item[$identifier]] = $item;
}
}
if (empty($data)) {
throw new EmptyDataException();
}
return $data;
}
}