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

api-platform / core / 14726133838

29 Apr 2025 07:51AM UTC coverage: 0.0% (-23.4%) from 23.443%
14726133838

Pull #7114

github

web-flow
Merge b5ddc44e4 into 4b1b94cad
Pull Request #7114: Merge 4.1

0 of 88 new or added lines in 20 files covered. (0.0%)

10373 existing lines in 330 files now uncovered.

0 of 49280 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/Laravel/Eloquent/State/CollectionProvider.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\Laravel\Eloquent\State;
15

16
use ApiPlatform\Laravel\Eloquent\Extension\QueryExtensionInterface;
17
use ApiPlatform\Laravel\Eloquent\Paginator;
18
use ApiPlatform\Laravel\Eloquent\PartialPaginator;
19
use ApiPlatform\Metadata\Exception\RuntimeException;
20
use ApiPlatform\Metadata\Operation;
21
use ApiPlatform\State\Pagination\Pagination;
22
use ApiPlatform\State\ProviderInterface;
23
use ApiPlatform\State\Util\StateOptionsTrait;
24
use Illuminate\Database\Eloquent\Collection;
25
use Illuminate\Database\Eloquent\Model;
26
use Psr\Container\ContainerInterface;
27

28
/**
29
 * @implements ProviderInterface<Paginator|Collection<int, Model>|PartialPaginator>
30
 */
31
final class CollectionProvider implements ProviderInterface
32
{
33
    use LinksHandlerLocatorTrait;
34
    use StateOptionsTrait;
35

36
    /**
37
     * @param LinksHandlerInterface<Model>      $linksHandler
38
     * @param iterable<QueryExtensionInterface> $queryExtensions
39
     */
40
    public function __construct(
41
        private readonly Pagination $pagination,
42
        private readonly LinksHandlerInterface $linksHandler,
43
        private iterable $queryExtensions = [],
44
        ?ContainerInterface $handleLinksLocator = null,
45
    ) {
46
        $this->handleLinksLocator = $handleLinksLocator;
×
47
    }
48

49
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
50
    {
NEW
51
        $resourceClass = $this->getStateOptionsClass($operation, $operation->getClass(), Options::class);
×
52
        $model = new $resourceClass();
×
53

54
        if (!$model instanceof Model) {
×
55
            throw new RuntimeException(\sprintf('The class "%s" is not an Eloquent model.', $resourceClass));
×
56
        }
57

58
        if ($handleLinks = $this->getLinksHandler($operation)) {
×
59
            $query = $handleLinks($model->query(), $uriVariables, ['operation' => $operation, 'modelClass' => $operation->getClass()] + $context);
×
60
        } else {
61
            $query = $this->linksHandler->handleLinks($model->query(), $uriVariables, ['operation' => $operation, 'modelClass' => $operation->getClass()] + $context);
×
62
        }
63

64
        foreach ($this->queryExtensions as $extension) {
×
65
            $query = $extension->apply($query, $uriVariables, $operation, $context);
×
66
        }
67

68
        if ($order = $operation->getOrder()) {
×
69
            $isList = array_is_list($order);
×
70
            foreach ($order as $property => $direction) {
×
71
                if ($isList) {
×
72
                    $property = $direction;
×
73
                    $direction = 'ASC';
×
74
                }
75

76
                if (str_contains($property, '.')) {
×
77
                    [$table, $property] = explode('.', $property);
×
78

79
                    // Relation Order by, we need to do laravel eager loading
80
                    $query->with([
×
81
                        $table => fn ($query) => $query->orderBy($property, $direction),
×
82
                    ]);
×
83

84
                    continue;
×
85
                }
86

87
                $query->orderBy($property, $direction);
×
88
            }
89
        }
90

91
        if (false === $this->pagination->isEnabled($operation, $context)) {
×
92
            return $query->get();
×
93
        }
94

95
        $isPartial = $operation->getPaginationPartial();
×
96
        $collection = $query
×
97
            ->{$isPartial ? 'simplePaginate' : 'paginate'}(
×
98
                perPage: $this->pagination->getLimit($operation, $context),
×
99
                page: $this->pagination->getPage($context),
×
100
            );
×
101

102
        if ($isPartial) {
×
103
            return new PartialPaginator($collection);
×
104
        }
105

106
        return new Paginator($collection);
×
107
    }
108
}
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

© 2025 Coveralls, Inc