1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Saxulum\Accessor\Accessors; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\Collection; |
6
|
|
|
use Saxulum\Accessor\CallbackBag; |
7
|
|
|
use Saxulum\Accessor\Collection\ArrayCollection; |
8
|
|
|
use Saxulum\Accessor\Collection\CollectionInterface; |
9
|
|
|
use Saxulum\Accessor\Collection\DoctrineArrayCollection; |
10
|
|
|
use Saxulum\Accessor\Prop; |
11
|
|
|
|
12
|
|
|
abstract class AbstractCollection extends AbstractWrite |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @param CallbackBag $callbackBag |
16
|
|
|
*/ |
17
|
|
|
protected function propertyDefault(CallbackBag $callbackBag) |
18
|
|
|
{ |
19
|
|
|
if (null === $callbackBag->getProperty()) { |
20
|
|
|
$callbackBag->setProperty(array()); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param CallbackBag $callbackBag |
26
|
|
|
* @return CollectionInterface |
27
|
|
|
*/ |
28
|
|
|
protected function getCollection(CallbackBag $callbackBag) |
29
|
|
|
{ |
30
|
|
|
if (is_array($callbackBag->getProperty())) { |
31
|
|
|
return new ArrayCollection($callbackBag->getProperty()); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if (interface_exists('Doctrine\Common\Collections\Collection') && $callbackBag->getProperty() instanceof Collection) { |
35
|
|
|
return new DoctrineArrayCollection($callbackBag->getProperty()); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
throw new \InvalidArgumentException("Property must be an array or a collection!"); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param CallbackBag $callbackBag |
43
|
|
|
* @param bool $nullValue |
44
|
|
|
* @throws \Exception |
45
|
|
|
*/ |
46
|
|
|
protected function handleMappedBy(CallbackBag $callbackBag, $nullValue = false) |
47
|
|
|
{ |
48
|
|
|
if (null === $mappedBy = $callbackBag->getMappedBy()) { |
49
|
|
|
throw new \Exception("Remote name needs to be set on '{$callbackBag->getName()}', if remote type is given!"); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$value = false === $nullValue ? $callbackBag->getObject() : null; |
53
|
|
|
|
54
|
|
|
if (false === $callbackBag->getArgument(1, false)) { |
55
|
|
|
$method = $this->getPrefixByProp($callbackBag->getProp()) . ucfirst($mappedBy); |
56
|
|
|
$callbackBag->getArgument(0)->$method($value, true); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param Prop $prop |
62
|
|
|
* @param string $namespace |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
protected static function getPhpDocHint(Prop $prop, $namespace) |
66
|
|
|
{ |
67
|
|
|
$hint = $prop->getPhpDocHint($namespace); |
68
|
|
|
|
69
|
|
|
if ('' === $hint) { |
70
|
|
|
return $hint; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if ('[]' === substr($hint, -2)) { |
74
|
|
|
return substr($hint, 0, -2) . ' '; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $hint . ' '; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.