vendor/doctrine/orm/lib/Doctrine/ORM/ORMInvalidArgumentException.php line 26

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