Completed
Pull Request — master (#615)
by Filippo
14:02 queued 03:29
created

AbstractCollectionStrategy::getObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DoctrineModule\Stdlib\Hydrator\Strategy;
4
5
use InvalidArgumentException;
6
use Doctrine\Common\Collections\Collection;
7
use Doctrine\Common\Persistence\Mapping\ClassMetadata;
8
use Zend\Hydrator\Strategy\StrategyInterface;
9
10
/**
11
 * @license MIT
12
 * @link    http://www.doctrine-project.org/
13
 * @since   0.7.0
14
 * @author  Michael Gallego <[email protected]>
15
 */
16
abstract class AbstractCollectionStrategy implements StrategyInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $collectionName;
22
23
    /**
24
     * @var ClassMetadata
25
     */
26
    protected $metadata;
27
28
    /**
29
     * @var object
30
     */
31
    protected $object;
32
33
34
    /**
35
     * Set the name of the collection
36
     *
37
     * @param  string $collectionName
38
     * @return AbstractCollectionStrategy
39
     */
40 21
    public function setCollectionName($collectionName)
41
    {
42 21
        $this->collectionName = (string) $collectionName;
43 21
        return $this;
44
    }
45
46
    /**
47
     * Get the name of the collection
48
     *
49
     * @return string
50
     */
51 17
    public function getCollectionName()
52
    {
53 17
        return $this->collectionName;
54
    }
55
56
    /**
57
     * Set the class metadata
58
     *
59
     * @param  ClassMetadata $classMetadata
60
     * @return AbstractCollectionStrategy
61
     */
62 21
    public function setClassMetadata(ClassMetadata $classMetadata)
63
    {
64 21
        $this->metadata = $classMetadata;
65 21
        return $this;
66
    }
67
68
    /**
69
     * Get the class metadata
70
     *
71
     * @return ClassMetadata
72
     */
73 6
    public function getClassMetadata()
74
    {
75 6
        return $this->metadata;
76
    }
77
78
    /**
79
     * Set the object
80
     *
81
     * @param  object $object
82
     *
83
     * @throws \InvalidArgumentException
84
     *
85
     * @return AbstractCollectionStrategy
86
     */
87 17
    public function setObject($object)
88
    {
89 17
        if (! is_object($object)) {
90
            throw new InvalidArgumentException(
91
                sprintf('The parameter given to setObject method of %s class is not an object', get_called_class())
92
            );
93
        }
94
95 17
        $this->object = $object;
96 17
        return $this;
97
    }
98
99
    /**
100
     * Get the object
101
     *
102
     * @return object
103
     */
104 17
    public function getObject()
105
    {
106 17
        return $this->object;
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112 4
    public function extract($value)
113
    {
114 4
        return $value;
115
    }
116
117
    /**
118
     * Return the collection by value (using the public API)
119
     *
120
     * @throws \InvalidArgumentException
121
     *
122
     * @return Collection
123
     */
124 11
    protected function getCollectionFromObjectByValue()
125
    {
126 11
        $object = $this->getObject();
127 11
        $getter = 'get' . ucfirst($this->getCollectionName());
128
129 11
        if (! method_exists($object, $getter)) {
130
            throw new InvalidArgumentException(
131
                sprintf(
132
                    'The getter %s to access collection %s in object %s does not exist',
133
                    $getter,
134
                    $this->getCollectionName(),
135
                    get_class($object)
136
                )
137
            );
138
        }
139
140 11
        return $object->$getter();
141
    }
142
143
    /**
144
     * Return the collection by reference (not using the public API)
145
     *
146
     * @return Collection
147
     */
148 6
    protected function getCollectionFromObjectByReference()
149
    {
150 6
        $object       = $this->getObject();
151 6
        $refl         = $this->getClassMetadata()->getReflectionClass();
152 6
        $reflProperty = $refl->getProperty($this->getCollectionName());
153
154 6
        $reflProperty->setAccessible(true);
155
156 6
        return $reflProperty->getValue($object);
157
    }
158
159
160
    /**
161
     * This method is used internally by array_udiff to check if two objects are equal, according to their
162
     * SPL hash. This is needed because the native array_diff only compare strings
163
     *
164
     * @param object $a
165
     * @param object $b
166
     *
167
     * @return int
168
     */
169 16
    protected function compareObjects($a, $b)
170
    {
171 16
        return strcmp(spl_object_hash($a), spl_object_hash($b));
172
    }
173
}
174