Issues (115)

src/Resources/ResourceObjectCollection.php (2 issues)

Labels
1
<?php
2
3
namespace VGirol\JsonApi\Resources;
4
5
use Illuminate\Http\Request;
6
use VGirol\JsonApiConstant\Members;
7
8
class ResourceObjectCollection extends BaseResourceCollection
9
{
10
    use IsData;
11
12
    public function __construct($resource, $includes = null)
13
    {
14
        parent::__construct($resource);
15
16
        $this->wrap(Members::DATA);
17
        $this->initIncludes($includes);
18
    }
19
20
    /**
21
     * Undocumented function
22
     *
23
     * @param \Illuminate\Support\Collection $includes
24
     *
25
     * @return void
26
     */
27
    public function initIncludes($includes)
28
    {
29
        if (is_null($includes)) {
30
            $service = jsonapiInclude();
31
            $includes = $service->parameters();
32
        }
33
34
        $this->collection->each(function ($item) use ($includes) {
35
            $item->initIncludes($includes);
36
        });
37
    }
38
39
    /**
40
     * Get any additional data that should be returned with the resource array.
41
     *
42
     * @param  Request  $request
43
     *
44
     * @return array
45
     */
46
    public function with($request)
47
    {
48
        // Add document (top-level) "self" link
49
        $this->addDocumentLink(Members::LINK_SELF, $this->getDocumentSelfLink($request));
0 ignored issues
show
$request of type Illuminate\Http\Request is incompatible with the type Illuminate\Support\Facades\Request expected by parameter $request of VGirol\JsonApi\Resources...::getDocumentSelfLink(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
        $this->addDocumentLink(Members::LINK_SELF, $this->getDocumentSelfLink(/** @scrutinizer ignore-type */ $request));
Loading history...
50
51
        // Add pagination meta and links
52
        if (jsonapiPagination()->allowed($request)) {
53
            $this->addDocumentMeta(Members::META_PAGINATION, jsonapiPagination()->getPaginationMeta());
54
55
            foreach (jsonapiPagination()->getPaginationLinks() as $name => $url) {
56
                $this->addDocumentLink($name, $url);
57
            }
58
        }
59
60
        // Add top-level included member
61
        $this->setIncluded($request);
62
63
        return $this->with;
64
    }
65
66
    /**
67
     * Transform the resource collection into an array used as included resource.
68
     *
69
     * @param Request $request
70
     * @param Collection $includes
0 ignored issues
show
The type VGirol\JsonApi\Resources\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
71
     *
72
     * @return array
73
     */
74
    public function asIncluded($request): array
75
    {
76
        $data = $this->collection->map->asIncluded($request)->all();
77
78
        return array_merge_recursive($data, $this->additional);
79
    }
80
81
    /**
82
     * Undocumented function
83
     * Could be overloaded, but it is not recommended.
84
     *
85
     * @param Request $request
86
     */
87
    protected function setIncluded($request)
88
    {
89
        /** @var Collection $included A collection of ResourceObject objects */
90
        $included = $this->collection->flatMap(function ($item) {
91
            return $item->getAllIncludedResources();
92
        })->unique(function ($item) {
93
            return $this->aliases->getResourceType(\get_class($item->resource)) . '_' . $item->getId();
94
        })->values()->filter();
95
96
        if ($included->isEmpty()) {
97
            return;
98
        }
99
100
        $this->addIncludedResources(
101
            $included->map->asIncluded($request)->toArray()
102
        );
103
    }
104
}
105