Completed
Push — master ( 8268f7...e79b43 )
by Ruben
01:59
created

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