Completed
Push — master ( 977a48...a95e66 )
by Fabien
03:07
created

Xml2arrayFunctions::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the FabienCrassat\CurriculumVitaeBundle Symfony bundle.
5
 *
6
 * (c) Fabien Crassat <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FabienCrassat\CurriculumVitaeBundle\Entity;
13
14
use FabienCrassat\CurriculumVitaeBundle\Utility\AgeCalculator;
15
16
class Xml2arrayFunctions {
17
    private $language;
18
    private $file;
19
20
    /**
21
     * @param \SimpleXMLElement $file
22
     * @param string $language
23
     */
24
    public function __construct($file, $language = 'en') {
25
        $this->language = $language;
26
        $this->file     = $file;
27
    }
28
29
    /**
30
     * @param \SimpleXMLElement $xml
31
     * @param integer $recursiveDepth
32
     * @param boolean $format
33
     *
34
     * @return null|array<string,array>
35
     */
36
    public function xml2array(\SimpleXMLElement $xml, $recursiveDepth = 0, $format = TRUE) {
37
        $recursiveDepth++;
38
        $result = [];
39
40
        // Extraction of the node
41
        $key   = trim($xml->getName());
42
        $value = trim((string) $xml);
43
44
        // Specific Attribute: do nothing when it is not the good language
45
        if ($xml->attributes()->lang) {
46
            if ($xml->attributes()->lang <> $this->language) {
0 ignored issues
show
introduced by
The condition $xml->attributes()->lang != $this->language is always true.
Loading history...
47
                return NULL;
48
            }
49
            unset($xml->attributes()->lang);
50
        }
51
52
        // Specific Attributes changing the xml
53
        $key    = $this->setSpecificAttributeKeyWithGivenId($xml, $key);
54
        $value  = $this->setSpecificAttributeAge($xml, $recursiveDepth, $value);
55
        $result = $this->retrieveSpecificAttributeCrossRef($xml, $recursiveDepth, $result, $key);
56
        // Standard Attributes
57
        $result = $this->setStandardAttributes($xml, $result, $key);
58
        // Specific Key
59
        $value = $this->setValueForSpecificKeys($key, $value, $format);
60
61
        $result = $this->setValue($result, $key, $value);
62
        return $this->setChildren($xml, $recursiveDepth, $key, $result);
63
    }
64
65
    /**
66
     * @param array $arrayToSet
67
     * @param string $key
68
     * @param array|string $value
69
     *
70
     * @return array<string,string>
71
     */
72
    private function setValue(array $arrayToSet, $key, $value) {
73
        if ($value <> '') {
74
            return array_merge($arrayToSet, [$key => $value]);
75
        }
76
        return $arrayToSet;
77
    }
78
79
    /**
80
     * @param \SimpleXMLElement $xml
81
     * @param integer $depth
82
     * @param string $key
83
     * @param array $arrayXML
84
     *
85
     * @return array<string,array>
86
     */
87
    private function setChildren(\SimpleXMLElement $xml, $depth, $key, array $arrayXML) {
88
        $return = $arrayXML;
89
        if ($xml->children()->count() > 0) {
90
            foreach ($xml->children() as $childValue) {
91
                $child = $this->xml2array($childValue, $depth);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $child is correct as $this->xml2array($childValue, $depth) targeting FabienCrassat\Curriculum...yFunctions::xml2array() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
92
                if ($depth > 1 && ! empty($child)) {
93
                    $return = array_merge_recursive($return, [$key => $child]);
94
                } elseif (! empty($child)) {
95
                    $return = array_merge_recursive($return, $child);
96
                }
97
            }
98
        }
99
        return $return;
100
    }
101
102
    /**
103
     * @param \SimpleXMLElement $xml
104
     * @param array $arrayToMerge
105
     * @param string $key
106
     *
107
     * @return array<string,string>
108
     */
109
    private function setStandardAttributes(\SimpleXMLElement $xml, array $arrayToMerge, $key) {
110
        // Standard Attributes (without Specific thanks to unset())
111
        $attributes = [];
112
        foreach ($xml->attributes() as $attributeKey => $attributeValue) {
113
            if ($this->isStandardAttributes($attributeKey)) {
114
                $attributes[$attributeKey] = trim($attributeValue);
115
            }
116
        }
117
        if (count($attributes) > 0) {
118
            return array_merge_recursive($arrayToMerge, [$key => $attributes]);
119
        }
120
        return $arrayToMerge;
121
    }
122
123
    /**
124
     * @param string $attribute
125
     *
126
     * @return boolean
127
     */
128
    private function isStandardAttributes($attribute) {
129
        return $attribute <> 'id'
130
            && $attribute <> 'ref'
131
            && $attribute <> 'lang'
132
            && $attribute <> 'crossref';
133
    }
134
135
    /**
136
     * @param \SimpleXMLElement $xml
137
     * @param string $keyValue
138
     *
139
     * @return string
140
     */
141
    private function setSpecificAttributeKeyWithGivenId(\SimpleXMLElement $xml, $keyValue) {
142
        // Specific Attribute: change the key with the given id
143
        if ($xml->attributes()->id) {
144
            return (string) $xml->attributes()->id;
145
        }
146
        return $keyValue;
147
    }
148
149
    /**
150
     * @param \SimpleXMLElement $xml
151
     * @param integer $depth
152
     * @param string $age
153
     *
154
     * @return string
155
     */
156
    private function setSpecificAttributeAge(\SimpleXMLElement $xml, $depth, $age) {
157
        // Specific Attribute: Retreive the age
158
        if ($xml->attributes()->getAge) {
159
            $crossref = $this->file->xpath(trim($xml->attributes()->getAge));
160
            $birthday = $this->xml2array(clone $crossref[0], $depth, FALSE);
161
            $birthday = implode('', $birthday);
162
163
            $ageCalculator = new AgeCalculator((string) $birthday);
164
165
            return (string) $ageCalculator->age();
166
        }
167
        return $age;
168
    }
169
170
    /**
171
     * @param \SimpleXMLElement $xml
172
     * @param integer $depth
173
     * @param array $arrayXML
174
     * @param string $key
175
     *
176
     * @return array<string,array>
177
     */
178
    private function retrieveSpecificAttributeCrossRef(\SimpleXMLElement $xml, $depth, array $arrayXML, $key) {
179
        // Specific Attribute: Retrieve the given crossref
180
        if ($xml->attributes()->crossref) {
181
            $crossref = $this->file->xpath(trim($xml->attributes()->crossref));
182
            $temp     = [];
183
            foreach ($crossref as $value) {
184
                $resultArray = $this->xml2array($value, $depth);
185
186
                if (! empty($resultArray)) {
187
                    $temp = array_merge($temp, $resultArray);
188
                }
189
            }
190
            return array_merge($arrayXML, [$key => $temp]);
191
        }
192
        return $arrayXML;
193
    }
194
195
    /**
196
     * @param string $key
197
     * @param string $value
198
     * @param boolean $format
199
     *
200
     * @return string|string[]
201
     */
202
    private function setValueForSpecificKeys($key, $value, $format) {
203
        if ($key == 'birthday') {
204
            return $this->setValueForBirthdayKey($value, $format);
205
        } elseif ($key == 'item') {
206
            return [$value]; // convert to apply array_merge()
207
        }
208
209
        return $value;
210
    }
211
212
    /**
213
     * Specific Key: Format to french date format
214
     *
215
     * @param string $date
216
     * @param boolean $format
217
     *
218
     * @return string
219
     */
220
    private function setValueForBirthdayKey($date, $format) {
221
        if ($format) {
222
            setlocale(LC_TIME, ['fra_fra', 'fr', 'fr_FR', 'fr_FR.UTF8']);
223
            return strftime('%d %B %Y', strtotime(date($date)));
224
        }
225
        return $date;
226
    }
227
}
228