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

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