|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Knp\FriendlyContexts\Record; |
|
4
|
|
|
|
|
5
|
|
|
use Knp\FriendlyContexts\Reflection\ObjectReflector; |
|
6
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
7
|
|
|
|
|
8
|
|
|
class Collection |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
protected $reflector; |
|
12
|
|
|
protected $referencial; |
|
13
|
|
|
protected $headers; |
|
14
|
|
|
protected $records; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(ObjectReflector $reflector) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->reflector = $reflector; |
|
19
|
|
|
$this->headers = []; |
|
20
|
|
|
$this->records = []; |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public function support($entity) |
|
24
|
|
|
{ |
|
25
|
|
|
if (null !== $this->referencial) { |
|
26
|
|
|
$name = $this->referencial; |
|
27
|
|
|
|
|
28
|
|
|
return $this->reflector->isInstanceOf($entity, $name); |
|
29
|
|
|
} |
|
30
|
|
|
$this->setReferencial($entity); |
|
31
|
|
|
|
|
32
|
|
|
return true; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getReferencial() |
|
|
|
|
|
|
36
|
|
|
{ |
|
37
|
|
|
return $this->referencial; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function setReferencial($entity) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->referencial = is_object($entity) ? $this->reflector->getClassLongName($entity) : $entity; |
|
43
|
|
|
|
|
44
|
|
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function attach($entity, array $values = null) |
|
48
|
|
|
{ |
|
49
|
|
|
$values = $values ?: $this->buildValues($entity); |
|
50
|
|
|
$this->mergeHeaders(array_keys($values)); |
|
51
|
|
|
|
|
52
|
|
|
$record = new Record($this->reflector, $this); |
|
53
|
|
|
$record->attach($entity, $values); |
|
54
|
|
|
|
|
55
|
|
|
$this->records[] = $record; |
|
56
|
|
|
|
|
57
|
|
|
return $record; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function search($value) |
|
|
|
|
|
|
61
|
|
|
{ |
|
62
|
|
|
foreach ($this->records as $record) { |
|
63
|
|
|
$entity = $record->getEntity(); |
|
64
|
|
|
if (method_exists($entity, '__toString') && (string) $entity === $value) { |
|
65
|
|
|
return $record; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
foreach ($this->headers as $header) { |
|
70
|
|
|
foreach ($this->records as $record) { |
|
71
|
|
|
if (null !== $record->get($header) && $value === $record->get($header)) { |
|
72
|
|
|
return $record; |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
public function all() |
|
79
|
|
|
{ |
|
80
|
|
|
return array_values($this->records); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function count() |
|
84
|
|
|
{ |
|
85
|
|
|
return count($this->records); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
protected function mergeHeaders($headers) |
|
89
|
|
|
{ |
|
90
|
|
|
foreach ($headers as $header) { |
|
91
|
|
|
if (!in_array($header, $this->headers)) { |
|
92
|
|
|
$this->headers[] = $header; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected function buildValues($entity) |
|
98
|
|
|
{ |
|
99
|
|
|
$result = []; |
|
|
|
|
|
|
100
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
|
101
|
|
|
|
|
102
|
|
|
foreach ($this->headers as $header) { |
|
103
|
|
|
try { |
|
104
|
|
|
$value = $accessor->getValue($entity, $header); |
|
105
|
|
|
if (is_scalar($value)) { |
|
106
|
|
|
$result[$header] = $value; |
|
107
|
|
|
} |
|
108
|
|
|
} catch (\Exception $ex) { |
|
109
|
|
|
unset($ex); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $result; |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.