Passed
Push — master ( 6ba492...107a58 )
by Arthur
04:07
created

JsonAbstract::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace SoliDry\Helpers;
4
5
6
use SoliDry\Types\ApiInterface;
7
8
abstract class JsonAbstract
9
{
10
    /**
11
     * @param array $jsonApiArr
12
     *
13
     * @return array
14
     */
15
    public static function getAttributes(array $jsonApiArr) : array
16
    {
17
        return empty($jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_ATTRS]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_ATTRS];
18
    }
19
20
    /**
21
     * Returns an array of bulk attributes for each element
22
     *
23
     * @param array $jsonApiArr
24
     * @return array
25
     */
26
    public static function getBulkAttributes(array $jsonApiArr) : array
27
    {
28
        return empty($jsonApiArr[ApiInterface::RAML_DATA]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA];
29
    }
30
31
    /**
32
     * @param array $jsonApiArr
33
     *
34
     * @return array
35
     */
36
    public static function getRelationships(array $jsonApiArr) : array
37
    {
38
        return empty($jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_RELATIONSHIPS]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA][ApiInterface::RAML_RELATIONSHIPS];
39
    }
40
41
    /**
42
     * @param array $jsonApiArr
43
     *
44
     * @return array
45
     */
46
    public static function getData(array $jsonApiArr) : array
47
    {
48
        return empty($jsonApiArr[ApiInterface::RAML_DATA]) ? [] : $jsonApiArr[ApiInterface::RAML_DATA];
49
    }
50
51
    /**
52
     * Encoder array -> json
53
     *
54
     * @param array $array
55
     * @param int $opts
56
     * @return string
57
     */
58
    public static function encode(array $array, int $opts = 0): string
59
    {
60
        return json_encode($array, $opts);
61
    }
62
63
    /**
64
     * Decoder json -> array
65
     *
66
     * @param mixed $json
67
     * @return mixed
68
     */
69
    public static function decode(string $json): array
70
    {
71
        return json_decode($json, true);
72
    }
73
74
    /**
75
     * This method is wrapper over decode to let polymorphism between raml/json parsers
76
     *
77
     * @param string $json
78
     * @return array
79
     */
80
    public static function parse(string $json): array
81
    {
82
        return self::decode($json);
83
    }
84
}