Completed
Push — master ( 77a338...555e8a )
by Marco
09:51
created

ORMInvalidArgumentException::invalidEntityName()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license. For more information, see
17
 * <http://www.doctrine-project.org>.
18
 */
19
20
namespace Doctrine\ORM;
21
use Doctrine\ORM\Mapping\ClassMetadata;
22
23
/**
24
 * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
25
 *
26
 * @author Benjamin Eberlei <[email protected]>
27
 */
28
class ORMInvalidArgumentException extends \InvalidArgumentException
29
{
30
    /**
31
     * @param object $entity
32
     *
33
     * @return ORMInvalidArgumentException
34
     */
35 1
    static public function scheduleInsertForManagedEntity($entity)
36
    {
37 1
        return new self("A managed+dirty entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
38
    }
39
40
    /**
41
     * @param object $entity
42
     *
43
     * @return ORMInvalidArgumentException
44
     */
45 1
    static public function scheduleInsertForRemovedEntity($entity)
46
    {
47 1
        return new self("Removed entity " . self::objToStr($entity) . " can not be scheduled for insertion.");
48
    }
49
50
    /**
51
     * @param object $entity
52
     *
53
     * @return ORMInvalidArgumentException
54
     */
55 1
    static public function scheduleInsertTwice($entity)
56
    {
57 1
        return new self("Entity " . self::objToStr($entity) . " can not be scheduled for insertion twice.");
58
    }
59
60
    /**
61
     * @param string $className
62
     * @param object $entity
63
     *
64
     * @return ORMInvalidArgumentException
65
     */
66 6
    static public function entityWithoutIdentity($className, $entity)
67
    {
68 6
        return new self(
69 6
            "The given entity of type '" . $className . "' (".self::objToStr($entity).") has no identity/no " .
70 6
            "id values set. It cannot be added to the identity map."
71
        );
72
    }
73
74
    /**
75
     * @param object $entity
76
     *
77
     * @return ORMInvalidArgumentException
78
     */
79 1
    static public function readOnlyRequiresManagedEntity($entity)
80
    {
81 1
        return new self("Only managed entities can be marked or checked as read only. But " . self::objToStr($entity) . " is not");
82
    }
83
84
    /**
85
     * @param array  $assoc
86
     * @param object $entry
87
     *
88
     * @return ORMInvalidArgumentException
89
     */
90 4
    static public function newEntityFoundThroughRelationship(array $assoc, $entry)
91
    {
92 4
        return new self("A new entity was found through the relationship '"
93 4
            . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' that was not"
94 4
            . " configured to cascade persist operations for entity: " . self::objToStr($entry) . "."
95 4
            . " To solve this issue: Either explicitly call EntityManager#persist()"
96 4
            . " on this unknown entity or configure cascade persist "
97 4
            . " this association in the mapping for example @ManyToOne(..,cascade={\"persist\"})."
98 4
            . (method_exists($entry, '__toString') ? "": " If you cannot find out which entity causes the problem"
99 4
            . " implement '" . $assoc['targetEntity'] . "#__toString()' to get a clue."));
100
    }
101
102
    /**
103
     * @param array  $assoc
104
     * @param object $entry
105
     *
106
     * @return ORMInvalidArgumentException
107
     */
108
    static public function detachedEntityFoundThroughRelationship(array $assoc, $entry)
109
    {
110
        return new self("A detached entity of type " . $assoc['targetEntity'] . " (" . self::objToStr($entry) . ") "
111
            . " was found through the relationship '" . $assoc['sourceEntity'] . "#" . $assoc['fieldName'] . "' "
112
            . "during cascading a persist operation.");
113
    }
114
115
    /**
116
     * @param object $entity
117
     *
118
     * @return ORMInvalidArgumentException
119
     */
120 1
    static public function entityNotManaged($entity)
121
    {
122 1
        return new self("Entity " . self::objToStr($entity) . " is not managed. An entity is managed if its fetched " .
123 1
            "from the database or registered as new through EntityManager#persist");
124
    }
125
126
    /**
127
     * @param object $entity
128
     * @param string $operation
129
     *
130
     * @return ORMInvalidArgumentException
131
     */
132
    static public function entityHasNoIdentity($entity, $operation)
133
    {
134
        return new self("Entity has no identity, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
135
    }
136
137
    /**
138
     * @param object $entity
139
     * @param string $operation
140
     *
141
     * @return ORMInvalidArgumentException
142
     */
143
    static public function entityIsRemoved($entity, $operation)
144
    {
145
        return new self("Entity is removed, therefore " . $operation ." cannot be performed. " . self::objToStr($entity));
146
    }
147
148
    /**
149
     * @param object $entity
150
     * @param string $operation
151
     *
152
     * @return ORMInvalidArgumentException
153
     */
154
    static public function detachedEntityCannot($entity, $operation)
155
    {
156
        return new self("Detached entity " . self::objToStr($entity) . " cannot be " . $operation);
157
    }
158
159
    /**
160
     * @param string $context
161
     * @param mixed  $given
162
     * @param int    $parameterIndex
163
     *
164
     * @return ORMInvalidArgumentException
165
     */
166 5
    public static function invalidObject($context, $given, $parameterIndex = 1)
167
    {
168 5
        return new self($context . ' expects parameter ' . $parameterIndex .
169 5
            ' to be an entity object, '. gettype($given) . ' given.');
170
    }
171
172
    /**
173
     * @return ORMInvalidArgumentException
174
     */
175
    public static function invalidCompositeIdentifier()
176
    {
177
        return new self("Binding an entity with a composite primary key to a query is not supported. " .
178
            "You should split the parameter into the explicit fields and bind them separately.");
179
    }
180
181
    /**
182
     * @return ORMInvalidArgumentException
183
     */
184 1
    public static function invalidIdentifierBindingEntity()
185
    {
186 1
        return new self("Binding entities to query parameters only allowed for entities that have an identifier.");
187
    }
188
189
    /**
190
     * @param ClassMetadata $targetClass
191
     * @param array         $assoc
192
     * @param mixed         $actualValue
193
     *
194
     * @return self
195
     */
196 13
    public static function invalidAssociation(ClassMetadata $targetClass, $assoc, $actualValue)
197
    {
198 13
        $expectedType = 'Doctrine\Common\Collections\Collection|array';
199
200 13
        if (($assoc['type'] & ClassMetadata::TO_ONE) > 0) {
201 13
            $expectedType = $targetClass->getName();
202
        }
203
204 13
        return new self(sprintf(
205 13
            'Expected value of type "%s" for association field "%s#$%s", got "%s" instead.',
206
            $expectedType,
207 13
            $assoc['sourceEntity'],
208 13
            $assoc['fieldName'],
209 13
            is_object($actualValue) ? get_class($actualValue) : gettype($actualValue)
210
        ));
211
    }
212
213
    /**
214
     * Used when a given entityName hasn't the good type
215
     *
216
     * @param mixed $entityName The given entity (which shouldn't be a string)
217
     *
218
     * @return self
219
     */
220 6
    public static function invalidEntityName($entityName)
221
    {
222 6
        return new self(sprintf('Entity name must be a string, %s given', gettype($entityName)));
223
    }
224
225
    /**
226
     * Helper method to show an object as string.
227
     *
228
     * @param object $obj
229
     *
230
     * @return string
231
     */
232 15
    private static function objToStr($obj)
233
    {
234 15
        return method_exists($obj, '__toString') ? (string) $obj : get_class($obj).'@'.spl_object_hash($obj);
235
    }
236
}
237