Completed
Push — master ( e6c808...16a507 )
by Ruben
01:05
created

HasLinks::withCollectionLinks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\ResourceLinks;
4
5
/** @mixin \Illuminate\Http\Resources\Json\JsonResource */
6
trait HasLinks
7
{
8
    /** @var bool  */
9
    private $withCollectionLinks = false;
10
11
    /**
12
     * @param string|\Closure|null|array $controller
13
     * @param null $parameters
14
     *
15
     * @return \Spatie\ResourceLinks\LinkResource
16
     */
17
    public function links($controller = null, $parameters = null): LinkResource
18
    {
19
        $resource = LinkResource::create($this->resource, LinkResourceType::ITEM)->link($controller, $parameters);
0 ignored issues
show
Bug introduced by
The property resource does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
21
        if (property_exists($this, 'withCollectionLinks') && $this->withCollectionLinks) {
22
            $resource->withCollectionLinks();
23
        }
24
25
        return $resource;
26
    }
27
28
    /**
29
     * @param string|\Closure|null|array $controller
30
     * @param null $parameters
31
     *
32
     * @return \Spatie\ResourceLinks\LinkResource
33
     */
34
    public static function collectionLinks($controller = null, $parameters = null): LinkResource
35
    {
36
        return LinkResource::create(null, LinkResourceType::COLLECTION)->link($controller, $parameters);
37
    }
38
39
    public function withCollectionLinks()
40
    {
41
        $this->withCollectionLinks = true;
42
43
        return $this;
44
    }
45
}
46