• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10537652610

24 Aug 2024 10:04AM UTC coverage: 7.707%. Remained the same
10537652610

push

github

dunglas
cleanup

12490 of 162060 relevant lines covered (7.71%)

22.98 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/src/Doctrine/Orm/PropertyHelperTrait.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\Doctrine\Orm;
15

16
use ApiPlatform\Doctrine\Orm\Util\QueryBuilderHelper;
17
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface;
18
use ApiPlatform\Metadata\Exception\InvalidArgumentException;
19
use Doctrine\ORM\Mapping\ClassMetadata as ClassMetadataInfo;
20
use Doctrine\ORM\QueryBuilder;
21
use Doctrine\Persistence\ManagerRegistry;
22
use Doctrine\Persistence\Mapping\ClassMetadata;
23

24
/**
25
 * Helper trait regarding a property in an entity using the resource metadata.
26
 *
27
 * @author Kévin Dunglas <dunglas@gmail.com>
28
 * @author Théo FIDRY <theo.fidry@gmail.com>
29
 */
30
trait PropertyHelperTrait
31
{
32
    abstract protected function getManagerRegistry(): ManagerRegistry;
33

34
    /**
35
     * Splits the given property into parts.
36
     */
37
    abstract protected function splitPropertyParts(string $property, string $resourceClass): array;
38

39
    /**
40
     * Gets class metadata for the given resource.
41
     */
42
    protected function getClassMetadata(string $resourceClass): ClassMetadata
43
    {
44
        $manager = $this
565✔
45
            ->getManagerRegistry()
565✔
46
            ->getManagerForClass($resourceClass);
565✔
47

48
        if ($manager) {
565✔
49
            return $manager->getClassMetadata($resourceClass);
565✔
50
        }
51

52
        return new ClassMetadataInfo($resourceClass);
×
53
    }
54

55
    /**
56
     * Adds the necessary joins for a nested property.
57
     *
58
     * @throws InvalidArgumentException If property is not nested
59
     *
60
     * @return array An array where the first element is the join $alias of the leaf entity,
61
     *               the second element is the $field name
62
     *               the third element is the $associations array
63
     */
64
    protected function addJoinsForNestedProperty(string $property, string $rootAlias, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, string $joinType): array
65
    {
66
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
16✔
67
        $parentAlias = $rootAlias;
16✔
68
        $alias = null;
16✔
69

70
        foreach ($propertyParts['associations'] as $association) {
16✔
71
            $alias = QueryBuilderHelper::addJoinOnce($queryBuilder, $queryNameGenerator, $parentAlias, $association, $joinType);
16✔
72
            $parentAlias = $alias;
16✔
73
        }
74

75
        if (null === $alias) {
16✔
76
            throw new InvalidArgumentException(\sprintf('Cannot add joins for property "%s" - property is not nested.', $property));
×
77
        }
78

79
        return [$alias, $propertyParts['field'], $propertyParts['associations']];
16✔
80
    }
81
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc