Completed
Push — master ( c239db...b86bbe )
by wen
02:42
created

Field::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
4
namespace Sco\Admin\Fields;
5
6
use JsonSerializable;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Illuminate\Contracts\Support\Jsonable;
9
use Sco\Admin\Contracts\Field as FieldContract;
10
11
abstract class Field implements FieldContract, Arrayable, Jsonable, JsonSerializable
12
{
13
    protected $type;
14
15
    protected $name;
16
    protected $title;
17
18
    public function __construct($name, $title)
19
    {
20
        $this->name  = $name;
21
        $this->title = $title;
22
    }
23
24
    public function isRelationship()
25
    {
26
        return !empty($this->relationship);
0 ignored issues
show
Bug introduced by
The property relationship 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...
27
    }
28
29
    public function toArray()
30
    {
31
        return [
32
            'key'   => $this->name,
33
            'title' => $this->title,
34
            'type'  => $this->type,
35
        ];
36
    }
37
38
    public function jsonSerialize()
39
    {
40
        return $this->toArray();
41
    }
42
43
    public function toJson($options = 0)
44
    {
45
        return json_encode($this->jsonSerialize(), $options);
46
    }
47
48
    public function __toString()
49
    {
50
        return $this->toJson();
51
    }
52
}
53