diff options
| author | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
|---|---|---|
| committer | polo <ordipolo@gmx.fr> | 2024-08-13 23:45:21 +0200 |
| commit | bf6655a534a6775d30cafa67bd801276bda1d98d (patch) | |
| tree | c6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/doctrine/orm/src/EntityRepository.php | |
| parent | 94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff) | |
| download | AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.tar.gz AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.tar.bz2 AppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip | |
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/doctrine/orm/src/EntityRepository.php')
| -rw-r--r-- | vendor/doctrine/orm/src/EntityRepository.php | 236 |
1 files changed, 236 insertions, 0 deletions
diff --git a/vendor/doctrine/orm/src/EntityRepository.php b/vendor/doctrine/orm/src/EntityRepository.php new file mode 100644 index 0000000..a53c528 --- /dev/null +++ b/vendor/doctrine/orm/src/EntityRepository.php | |||
| @@ -0,0 +1,236 @@ | |||
| 1 | <?php | ||
| 2 | |||
| 3 | declare(strict_types=1); | ||
| 4 | |||
| 5 | namespace Doctrine\ORM; | ||
| 6 | |||
| 7 | use BadMethodCallException; | ||
| 8 | use Doctrine\Common\Collections\AbstractLazyCollection; | ||
| 9 | use Doctrine\Common\Collections\Criteria; | ||
| 10 | use Doctrine\Common\Collections\Selectable; | ||
| 11 | use Doctrine\DBAL\LockMode; | ||
| 12 | use Doctrine\Inflector\Inflector; | ||
| 13 | use Doctrine\Inflector\InflectorFactory; | ||
| 14 | use Doctrine\ORM\Mapping\ClassMetadata; | ||
| 15 | use Doctrine\ORM\Query\ResultSetMappingBuilder; | ||
| 16 | use Doctrine\ORM\Repository\Exception\InvalidMagicMethodCall; | ||
| 17 | use Doctrine\Persistence\ObjectRepository; | ||
| 18 | |||
| 19 | use function array_slice; | ||
| 20 | use function lcfirst; | ||
| 21 | use function sprintf; | ||
| 22 | use function str_starts_with; | ||
| 23 | use function substr; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * An EntityRepository serves as a repository for entities with generic as well as | ||
| 27 | * business specific methods for retrieving entities. | ||
| 28 | * | ||
| 29 | * This class is designed for inheritance and users can subclass this class to | ||
| 30 | * write their own repositories with business-specific methods to locate entities. | ||
| 31 | * | ||
| 32 | * @template T of object | ||
| 33 | * @template-implements Selectable<int,T> | ||
| 34 | * @template-implements ObjectRepository<T> | ||
| 35 | */ | ||
| 36 | class EntityRepository implements ObjectRepository, Selectable | ||
| 37 | { | ||
| 38 | /** @psalm-var class-string<T> */ | ||
| 39 | private readonly string $entityName; | ||
| 40 | private static Inflector|null $inflector = null; | ||
| 41 | |||
| 42 | /** @psalm-param ClassMetadata<T> $class */ | ||
| 43 | public function __construct( | ||
| 44 | private readonly EntityManagerInterface $em, | ||
| 45 | private readonly ClassMetadata $class, | ||
| 46 | ) { | ||
| 47 | $this->entityName = $class->name; | ||
| 48 | } | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Creates a new QueryBuilder instance that is prepopulated for this entity name. | ||
| 52 | */ | ||
| 53 | public function createQueryBuilder(string $alias, string|null $indexBy = null): QueryBuilder | ||
| 54 | { | ||
| 55 | return $this->em->createQueryBuilder() | ||
| 56 | ->select($alias) | ||
| 57 | ->from($this->entityName, $alias, $indexBy); | ||
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Creates a new result set mapping builder for this entity. | ||
| 62 | * | ||
| 63 | * The column naming strategy is "INCREMENT". | ||
| 64 | */ | ||
| 65 | public function createResultSetMappingBuilder(string $alias): ResultSetMappingBuilder | ||
| 66 | { | ||
| 67 | $rsm = new ResultSetMappingBuilder($this->em, ResultSetMappingBuilder::COLUMN_RENAMING_INCREMENT); | ||
| 68 | $rsm->addRootEntityFromClassMetadata($this->entityName, $alias); | ||
| 69 | |||
| 70 | return $rsm; | ||
| 71 | } | ||
| 72 | |||
| 73 | /** | ||
| 74 | * Finds an entity by its primary key / identifier. | ||
| 75 | * | ||
| 76 | * @param LockMode|int|null $lockMode One of the \Doctrine\DBAL\LockMode::* constants | ||
| 77 | * or NULL if no specific lock mode should be used | ||
| 78 | * during the search. | ||
| 79 | * @psalm-param LockMode::*|null $lockMode | ||
| 80 | * | ||
| 81 | * @return object|null The entity instance or NULL if the entity can not be found. | ||
| 82 | * @psalm-return ?T | ||
| 83 | */ | ||
| 84 | public function find(mixed $id, LockMode|int|null $lockMode = null, int|null $lockVersion = null): object|null | ||
| 85 | { | ||
| 86 | return $this->em->find($this->entityName, $id, $lockMode, $lockVersion); | ||
| 87 | } | ||
| 88 | |||
| 89 | /** | ||
| 90 | * Finds all entities in the repository. | ||
| 91 | * | ||
| 92 | * @psalm-return list<T> The entities. | ||
| 93 | */ | ||
| 94 | public function findAll(): array | ||
| 95 | { | ||
| 96 | return $this->findBy([]); | ||
| 97 | } | ||
| 98 | |||
| 99 | /** | ||
| 100 | * Finds entities by a set of criteria. | ||
| 101 | * | ||
| 102 | * {@inheritDoc} | ||
| 103 | * | ||
| 104 | * @psalm-return list<T> | ||
| 105 | */ | ||
| 106 | public function findBy(array $criteria, array|null $orderBy = null, int|null $limit = null, int|null $offset = null): array | ||
| 107 | { | ||
| 108 | $persister = $this->em->getUnitOfWork()->getEntityPersister($this->entityName); | ||
| 109 | |||
| 110 | return $persister->loadAll($criteria, $orderBy, $limit, $offset); | ||
| 111 | } | ||
| 112 | |||
| 113 | /** | ||
| 114 | * Finds a single entity by a set of criteria. | ||
| 115 | * | ||
| 116 | * @psalm-param array<string, mixed> $criteria | ||
| 117 | * @psalm-param array<string, string>|null $orderBy | ||
| 118 | * | ||
| 119 | * @psalm-return T|null | ||
| 120 | */ | ||
| 121 | public function findOneBy(array $criteria, array|null $orderBy = null): object|null | ||
| 122 | { | ||
| 123 | $persister = $this->em->getUnitOfWork()->getEntityPersister($this->entityName); | ||
| 124 | |||
| 125 | return $persister->load($criteria, null, null, [], null, 1, $orderBy); | ||
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Counts entities by a set of criteria. | ||
| 130 | * | ||
| 131 | * @psalm-param array<string, mixed> $criteria | ||
| 132 | * | ||
| 133 | * @return int The cardinality of the objects that match the given criteria. | ||
| 134 | * | ||
| 135 | * @todo Add this method to `ObjectRepository` interface in the next major release | ||
| 136 | */ | ||
| 137 | public function count(array $criteria = []): int | ||
| 138 | { | ||
| 139 | return $this->em->getUnitOfWork()->getEntityPersister($this->entityName)->count($criteria); | ||
| 140 | } | ||
| 141 | |||
| 142 | /** | ||
| 143 | * Adds support for magic method calls. | ||
| 144 | * | ||
| 145 | * @param mixed[] $arguments | ||
| 146 | * @psalm-param list<mixed> $arguments | ||
| 147 | * | ||
| 148 | * @throws BadMethodCallException If the method called is invalid. | ||
| 149 | */ | ||
| 150 | public function __call(string $method, array $arguments): mixed | ||
| 151 | { | ||
| 152 | if (str_starts_with($method, 'findBy')) { | ||
| 153 | return $this->resolveMagicCall('findBy', substr($method, 6), $arguments); | ||
| 154 | } | ||
| 155 | |||
| 156 | if (str_starts_with($method, 'findOneBy')) { | ||
| 157 | return $this->resolveMagicCall('findOneBy', substr($method, 9), $arguments); | ||
| 158 | } | ||
| 159 | |||
| 160 | if (str_starts_with($method, 'countBy')) { | ||
| 161 | return $this->resolveMagicCall('count', substr($method, 7), $arguments); | ||
| 162 | } | ||
| 163 | |||
| 164 | throw new BadMethodCallException(sprintf( | ||
| 165 | 'Undefined method "%s". The method name must start with ' . | ||
| 166 | 'either findBy, findOneBy or countBy!', | ||
| 167 | $method, | ||
| 168 | )); | ||
| 169 | } | ||
| 170 | |||
| 171 | /** @psalm-return class-string<T> */ | ||
| 172 | protected function getEntityName(): string | ||
| 173 | { | ||
| 174 | return $this->entityName; | ||
| 175 | } | ||
| 176 | |||
| 177 | public function getClassName(): string | ||
| 178 | { | ||
| 179 | return $this->getEntityName(); | ||
| 180 | } | ||
| 181 | |||
| 182 | protected function getEntityManager(): EntityManagerInterface | ||
| 183 | { | ||
| 184 | return $this->em; | ||
| 185 | } | ||
| 186 | |||
| 187 | /** @psalm-return ClassMetadata<T> */ | ||
| 188 | protected function getClassMetadata(): ClassMetadata | ||
| 189 | { | ||
| 190 | return $this->class; | ||
| 191 | } | ||
| 192 | |||
| 193 | /** | ||
| 194 | * Select all elements from a selectable that match the expression and | ||
| 195 | * return a new collection containing these elements. | ||
| 196 | * | ||
| 197 | * @psalm-return AbstractLazyCollection<int, T>&Selectable<int, T> | ||
| 198 | */ | ||
| 199 | public function matching(Criteria $criteria): AbstractLazyCollection&Selectable | ||
| 200 | { | ||
| 201 | $persister = $this->em->getUnitOfWork()->getEntityPersister($this->entityName); | ||
| 202 | |||
| 203 | return new LazyCriteriaCollection($persister, $criteria); | ||
| 204 | } | ||
| 205 | |||
| 206 | /** | ||
| 207 | * Resolves a magic method call to the proper existent method at `EntityRepository`. | ||
| 208 | * | ||
| 209 | * @param string $method The method to call | ||
| 210 | * @param string $by The property name used as condition | ||
| 211 | * @psalm-param list<mixed> $arguments The arguments to pass at method call | ||
| 212 | * | ||
| 213 | * @throws InvalidMagicMethodCall If the method called is invalid or the | ||
| 214 | * requested field/association does not exist. | ||
| 215 | */ | ||
| 216 | private function resolveMagicCall(string $method, string $by, array $arguments): mixed | ||
| 217 | { | ||
| 218 | if (! $arguments) { | ||
| 219 | throw InvalidMagicMethodCall::onMissingParameter($method . $by); | ||
| 220 | } | ||
| 221 | |||
| 222 | self::$inflector ??= InflectorFactory::create()->build(); | ||
| 223 | |||
| 224 | $fieldName = lcfirst(self::$inflector->classify($by)); | ||
| 225 | |||
| 226 | if (! ($this->class->hasField($fieldName) || $this->class->hasAssociation($fieldName))) { | ||
| 227 | throw InvalidMagicMethodCall::becauseFieldNotFoundIn( | ||
| 228 | $this->entityName, | ||
| 229 | $fieldName, | ||
| 230 | $method . $by, | ||
| 231 | ); | ||
| 232 | } | ||
| 233 | |||
| 234 | return $this->$method([$fieldName => $arguments[0]], ...array_slice($arguments, 1)); | ||
| 235 | } | ||
| 236 | } | ||
