Completed
Push — master ( e79b43...d0162c )
by Ruben
01:28
created

HasEndpoints::meta()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
namespace Spatie\LaravelEndpointResources;
5
6
use Illuminate\Support\Arr;
7
8
trait HasEndpoints
9
{
10
    /** @var bool */
11
    protected $mergeGlobalEndpoints = false;
12
13
    public function endpoints(string $controller = null, $parameters = null): EndpointResource
14
    {
15
        $endPointResourceType = $this->mergeGlobalEndpoints
16
            ? EndpointResourceType::MULTI
17
            : EndpointResourceType::LOCAL;
18
19
        $endPointResource = new EndpointResource($this->resource, $endPointResourceType);
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 ($controller !== null) {
22
            $endPointResource->addController($controller, Arr::wrap($parameters));
23
        }
24
25
        return $endPointResource;
26
    }
27
28
    public static function globalEndpoints(string $controller = null, $parameters = null): EndpointResource
29
    {
30
        $endPointResource = new EndpointResource(null, EndpointResourceType::GLOBAL);
31
32
        if ($controller !== null) {
33
            $endPointResource->addController($controller, Arr::wrap($parameters));
34
        }
35
36
        return $endPointResource;
37
    }
38
39
    public static function meta()
40
    {
41
        return [];
42
    }
43
44
    public function mergeGlobalEndpoints()
45
    {
46
        $this->mergeGlobalEndpoints = true;
47
48
        return $this;
49
    }
50
51
    public static function collection($resource)
52
    {
53
        return parent::collection($resource)->additional([
54
            'meta' => self::meta(),
55
        ]);
56
    }
57
58
    public static function make(...$parameters)
59
    {
60
        return parent::make(...$parameters)->mergeGlobalEndpoints();
61
    }
62
}
63