PageRenderTime 60ms CodeModel.GetById 37ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Doctrine/ORM/ORMInvalidArgumentException.php

http://github.com/doctrine/doctrine2
PHP | 265 lines | 133 code | 27 blank | 105 comment | 1 complexity | 6c8ed8e43fe587f6067007f5a2646836 MD5 | raw file
Possible License(s): Unlicense
  1. <?php
  2. declare(strict_types=1);
  3. namespace Doctrine\ORM;
  4. use Doctrine\ORM\Mapping\ClassMetadata;
  5. use InvalidArgumentException;
  6. use function array_map;
  7. use function count;
  8. use function get_class;
  9. use function gettype;
  10. use function implode;
  11. use function is_object;
  12. use function method_exists;
  13. use function reset;
  14. use function spl_object_id;
  15. use function sprintf;
  16. /**
  17. * Contains exception messages for all invalid lifecycle state exceptions inside UnitOfWork
  18. */
  19. class ORMInvalidArgumentException extends InvalidArgumentException
  20. {
  21. /**
  22. * @param object $entity
  23. *
  24. * @return ORMInvalidArgumentException
  25. */
  26. public static function scheduleInsertForManagedEntity($entity)
  27. {
  28. return new self('A managed+dirty entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.');
  29. }
  30. /**
  31. * @param object $entity
  32. *
  33. * @return ORMInvalidArgumentException
  34. */
  35. public static function scheduleInsertForRemovedEntity($entity)
  36. {
  37. return new self('Removed entity ' . self::objToStr($entity) . ' can not be scheduled for insertion.');
  38. }
  39. /**
  40. * @param object $entity
  41. *
  42. * @return ORMInvalidArgumentException
  43. */
  44. public static function scheduleInsertTwice($entity)
  45. {
  46. return new self('Entity ' . self::objToStr($entity) . ' can not be scheduled for insertion twice.');
  47. }
  48. /**
  49. * @param string $className
  50. * @param object $entity
  51. *
  52. * @return ORMInvalidArgumentException
  53. */
  54. public static function entityWithoutIdentity($className, $entity)
  55. {
  56. return new self(
  57. "The given entity of type '" . $className . "' (" . self::objToStr($entity) . ') has no identity/no ' .
  58. 'id values set. It cannot be added to the identity map.'
  59. );
  60. }
  61. /**
  62. * @param object $entity
  63. *
  64. * @return ORMInvalidArgumentException
  65. */
  66. public static function readOnlyRequiresManagedEntity($entity)
  67. {
  68. return new self('Only managed entities can be marked or checked as read only. But ' . self::objToStr($entity) . ' is not');
  69. }
  70. /**
  71. * @param array[][]|object[][] $newEntitiesWithAssociations non-empty an array
  72. * of [array $associationMapping, object $entity] pairs
  73. *
  74. * @return ORMInvalidArgumentException
  75. */
  76. public static function newEntitiesFoundThroughRelationships($newEntitiesWithAssociations)
  77. {
  78. $errorMessages = array_map(
  79. static function (array $newEntityWithAssociation): string {
  80. [$associationMapping, $entity] = $newEntityWithAssociation;
  81. return self::newEntityFoundThroughRelationshipMessage($associationMapping, $entity);
  82. },
  83. $newEntitiesWithAssociations
  84. );
  85. if (count($errorMessages) === 1) {
  86. return new self(reset($errorMessages));
  87. }
  88. return new self(
  89. 'Multiple non-persisted new entities were found through the given association graph:'
  90. . "\n\n * "
  91. . implode("\n * ", $errorMessages)
  92. );
  93. }
  94. /**
  95. * @param object $entry
  96. * @psalm-param array<string, string> $associationMapping
  97. *
  98. * @return ORMInvalidArgumentException
  99. */
  100. public static function newEntityFoundThroughRelationship(array $associationMapping, $entry)
  101. {
  102. return new self(self::newEntityFoundThroughRelationshipMessage($associationMapping, $entry));
  103. }
  104. /**
  105. * @param object $entry
  106. * @psalm-param array<string, string> $assoc
  107. *
  108. * @return ORMInvalidArgumentException
  109. */
  110. public static function detachedEntityFoundThroughRelationship(array $assoc, $entry)
  111. {
  112. return new self('A detached entity of type ' . $assoc['targetEntity'] . ' (' . self::objToStr($entry) . ') '
  113. . " was found through the relationship '" . $assoc['sourceEntity'] . '#' . $assoc['fieldName'] . "' "
  114. . 'during cascading a persist operation.');
  115. }
  116. /**
  117. * @param object $entity
  118. *
  119. * @return ORMInvalidArgumentException
  120. */
  121. public static function entityNotManaged($entity)
  122. {
  123. return new self('Entity ' . self::objToStr($entity) . ' is not managed. An entity is managed if its fetched ' .
  124. 'from the database or registered as new through EntityManager#persist');
  125. }
  126. /**
  127. * @param object $entity
  128. * @param string $operation
  129. *
  130. * @return ORMInvalidArgumentException
  131. */
  132. public static function entityHasNoIdentity($entity, $operation)
  133. {
  134. return new self('Entity has no identity, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity));
  135. }
  136. /**
  137. * @param object $entity
  138. * @param string $operation
  139. *
  140. * @return ORMInvalidArgumentException
  141. */
  142. public static function entityIsRemoved($entity, $operation)
  143. {
  144. return new self('Entity is removed, therefore ' . $operation . ' cannot be performed. ' . self::objToStr($entity));
  145. }
  146. /**
  147. * @param object $entity
  148. * @param string $operation
  149. *
  150. * @return ORMInvalidArgumentException
  151. */
  152. public static function detachedEntityCannot($entity, $operation)
  153. {
  154. return new self('Detached entity ' . self::objToStr($entity) . ' cannot be ' . $operation);
  155. }
  156. /**
  157. * @param string $context
  158. * @param mixed $given
  159. * @param int $parameterIndex
  160. *
  161. * @return ORMInvalidArgumentException
  162. */
  163. public static function invalidObject($context, $given, $parameterIndex = 1)
  164. {
  165. return new self($context . ' expects parameter ' . $parameterIndex .
  166. ' to be an entity object, ' . gettype($given) . ' given.');
  167. }
  168. /**
  169. * @return ORMInvalidArgumentException
  170. */
  171. public static function invalidCompositeIdentifier()
  172. {
  173. return new self('Binding an entity with a composite primary key to a query is not supported. ' .
  174. 'You should split the parameter into the explicit fields and bind them separately.');
  175. }
  176. /**
  177. * @return ORMInvalidArgumentException
  178. */
  179. public static function invalidIdentifierBindingEntity()
  180. {
  181. return new self('Binding entities to query parameters only allowed for entities that have an identifier.');
  182. }
  183. /**
  184. * @param mixed[] $assoc
  185. * @param mixed $actualValue
  186. *
  187. * @return self
  188. */
  189. public static function invalidAssociation(ClassMetadata $targetClass, $assoc, $actualValue)
  190. {
  191. $expectedType = $targetClass->getName();
  192. return new self(sprintf(
  193. 'Expected value of type "%s" for association field "%s#$%s", got "%s" instead.',
  194. $expectedType,
  195. $assoc['sourceEntity'],
  196. $assoc['fieldName'],
  197. is_object($actualValue) ? get_class($actualValue) : gettype($actualValue)
  198. ));
  199. }
  200. /**
  201. * Used when a given entityName hasn't the good type
  202. *
  203. * @param mixed $entityName The given entity (which shouldn't be a string)
  204. *
  205. * @return self
  206. */
  207. public static function invalidEntityName($entityName)
  208. {
  209. return new self(sprintf('Entity name must be a string, %s given', gettype($entityName)));
  210. }
  211. /**
  212. * Helper method to show an object as string.
  213. *
  214. * @param object $obj
  215. */
  216. private static function objToStr($obj): string
  217. {
  218. return method_exists($obj, '__toString') ? (string) $obj : get_class($obj) . '@' . spl_object_id($obj);
  219. }
  220. /**
  221. * @param object $entity
  222. * @psalm-param array<string,string> $associationMapping
  223. */
  224. private static function newEntityFoundThroughRelationshipMessage(array $associationMapping, $entity): string
  225. {
  226. return 'A new entity was found through the relationship \''
  227. . $associationMapping['sourceEntity'] . '#' . $associationMapping['fieldName'] . '\' that was not'
  228. . ' configured to cascade persist operations for entity: ' . self::objToStr($entity) . '.'
  229. . ' To solve this issue: Either explicitly call EntityManager#persist()'
  230. . ' on this unknown entity or configure cascade persist'
  231. . ' this association in the mapping for example @ManyToOne(..,cascade={"persist"}).'
  232. . (method_exists($entity, '__toString')
  233. ? ''
  234. : ' If you cannot find out which entity causes the problem implement \''
  235. . $associationMapping['targetEntity'] . '#__toString()\' to get a clue.'
  236. );
  237. }
  238. }