JsonApi::createFromArray()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 4
eloc 3
c 1
b 0
f 1
nc 8
nop 1
dl 0
loc 6
rs 10
1
<?php
2
namespace Javis\JsonApi\Schema;
3
4
class JsonApi
5
{
6
    /**
7
     * @var string
8
     */
9
    protected $version;
10
11
    /**
12
     * @var array
13
     */
14
    protected $meta;
15
16
    /**
17
     * @param array $array
18
     * @return $this
19
     */
20
    public static function createFromArray($array)
21
    {
22
        $version = empty($array["version"]) ? "" : $array["version"];
23
        $meta = isset($array["meta"]) && is_array($array["meta"]) ? $array["meta"] : [];
24
25
        return new self($version, $meta);
26
    }
27
28
    /**
29
     * @param string $version
30
     * @param array $meta
31
     */
32
    public function __construct($version, array $meta)
33
    {
34
        $this->version = $version;
35
        $this->meta = $meta;
36
    }
37
38
    /**
39
     * @return array
40
     */
41
    public function toArray()
42
    {
43
        $result = [];
44
45
        if ($this->version) {
46
            $result["version"] = $this->version;
47
        }
48
49
        if (empty($this->meta) === false) {
50
            $result["meta"] = $this->meta;
51
        }
52
53
        return $result;
54
    }
55
56
    /**
57
     * @return bool
58
     */
59
    public function hasJsonApi()
60
    {
61
        return $this->hasVersion() || $this->hasMeta();
62
    }
63
64
    /**
65
     * @return bool
66
     */
67
    public function hasVersion()
68
    {
69
        return empty($this->version) === false;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function version()
76
    {
77
        return $this->version;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function hasMeta()
84
    {
85
        return empty($this->meta) === false;
86
    }
87
88
    /**
89
     * @return array
90
     */
91
    public function meta()
92
    {
93
        return $this->meta;
94
    }
95
}
96