JsonLinkedDataTrait   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
c 0
b 0
f 0
lcom 1
cbo 0
dl 0
loc 153
ccs 54
cts 54
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A serialize() 0 20 2
B createSerializedArrayFromObject() 0 35 3
A includeGivenProperty() 0 5 2
A mergeWithArray() 0 12 3
getCoreSchemaTypes() 0 1 ?
A settingIsCoreSchema() 0 6 1
getLinkedDataSchemaTypes() 0 1 ?
A settingIsLinkedData() 0 6 1
1
<?php namespace JobApis\Jobs\Client;
2
3
use Closure;
4
use ReflectionClass;
5
use ReflectionProperty;
6
7
trait JsonLinkedDataTrait
8
{
9
    /**
10
     * Serialize object as array
11
     *
12
     * @param  string  $serializeSetting
13
     *
14
     * @return array
15
     */
16 8
    protected function serialize($serializeSetting)
17
    {
18 8
        $array = [];
19
20 8
        if ($this->settingIsLinkedData($serializeSetting)) {
21 4
            $array["@context"] = "http://schema.org";
22 4
            $array["@type"] = "JobPosting";
23 4
        }
24
25 8
        $serializedArray = $this->createSerializedArrayFromObject(
26 8
            $this,
27
            $serializeSetting
28 8
        );
29
30 8
        $array = array_merge($serializedArray, $array);
31
32 8
        ksort($array);
33
34 8
        return $array;
35
    }
36
37
    /**
38
     * Create serialized array of given object, including recursion
39
     *
40
     * @param  object $object
41
     * @param  string $serializeSetting
42
     *
43
     * @return array
44
     */
45 8
    private function createSerializedArrayFromObject($object, $serializeSetting)
46
    {
47 8
        $ref = new ReflectionClass($object);
48 8
        $properties = $ref->getProperties();
49
50 8
        $array = [];
51
52 8
        if ($this->settingIsLinkedData($serializeSetting)) {
53 4
            $array["@type"] = $ref->getShortName();
54 4
        }
55
56
        $process = function ($property) use (&$array, $object, $serializeSetting) {
57 8
            if ($this->includeGivenProperty($property, $serializeSetting)) {
58 8
                $property->setAccessible(true);
59 8
                $name = $property->getName();
60 8
                $value = $property->getValue($object);
61
62 8
                $recursion = function ($value) use ($serializeSetting) {
63 2
                    return call_user_func_array(
64 2
                        [$this, 'createSerializedArrayFromObject'],
65 2
                        [$value, $serializeSetting]
66 2
                    );
67 8
                };
68
69 8
                $array = call_user_func_array(
70 8
                    [$this, 'mergeWithArray'],
71 8
                    [$array, $name, $value, $recursion]
72 8
                );
73 8
            }
74 8
        };
75
76 8
        array_map($process, $properties);
77
78 8
        return $array;
79
    }
80
81
    /**
82
     * Check if given property should be included in serialization
83
     *
84
     * @param  ReflectionProperty  $property
85
     * @param  string              $serializeSetting
86
     *
87
     * @return boolean
88
     */
89 8
    private function includeGivenProperty(ReflectionProperty $property, $serializeSetting)
90
    {
91 8
        return $property->class != Job::class
92 8
            || !$this->settingIsCoreSchema($serializeSetting);
93
    }
94
95
    /**
96
     * Attempt to merge a value with an existing array
97
     *
98
     * @param  array   $array
99
     * @param  string  $name
100
     * @param  mixed   $value
101
     * @param  Closure $recursion
102
     *
103
     * @return array
104
     */
105 8
    private function mergeWithArray(array $array, $name, $value, Closure $recursion)
106
    {
107 8
        if (!is_object($value)) {
108 8
            $array[$name] = $value;
109 8
        } elseif ($value instanceof \DateTime) {
110 8
            $array[$name] = date_format($value, 'Y-m-d');
111 8
        } else {
112 2
            $array[$name] = $recursion($value);
113
        }
114
115 8
        return $array;
116
    }
117
118
    /**
119
     * Get core schema types
120
     *
121
     * @return array
122
     */
123
    abstract public function getCoreSchemaTypes();
124
125
    /**
126
     * Check if setting indicates if only core schema should be included
127
     *
128
     * @param  string $setting
129
     *
130
     * @return boolean
131
     */
132 8
    private function settingIsCoreSchema($setting)
133
    {
134 8
        $coreTypes = $this->getCoreSchemaTypes();
135
136 8
        return in_array($setting, $coreTypes);
137
    }
138
139
    /**
140
     * Get linked data schema types
141
     *
142
     * @return array
143
     */
144
    abstract public function getLinkedDataSchemaTypes();
145
146
    /**
147
     * Check if setting indicates if Linked Data support should be provided
148
     *
149
     * @param  string $setting
150
     *
151
     * @return boolean
152
     */
153 8
    private function settingIsLinkedData($setting)
154
    {
155 8
        $linkedDataTypes = $this->getLinkedDataSchemaTypes();
156
157 8
        return in_array($setting, $linkedDataTypes);
158
    }
159
}
160