Completed
Push — master ( c59679...9e7f7b )
by Fabien
03:51
created

Xml2arrayFunctions::setAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
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 $arXML;
0 ignored issues
show
Unused Code introduced by
The property $arXML is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
18
    private $attr;
0 ignored issues
show
Unused Code introduced by
The property $attr is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
19
    private $language;
20
    private $CVFile;
21
22
    /**
23
     * @param \SimpleXMLElement $CVFile
24
     * @param string $language
25
     */
26 34
    public function __construct($CVFile, $language = 'en') {
27 34
        $this->language = $language;
28 34
        $this->CVFile = $CVFile;
29 34
    }
30
31
    /**
32
     * @param \SimpleXMLElement $xml
33
     * @param integer $depth
34
     * @param boolean $format
35
     *
36
     * @return null|array
37
     */
38 31
    public function xml2array(\SimpleXMLElement $xml, $depth = 0, $format = TRUE) {
39 31
        $depth = $depth + 1;
40 31
        $result = array();
41
42
        // Extraction of the node
43 31
        $key = trim($xml->getName());
44 31
        $value = trim((string) $xml);
45
46
        // Specific Attribute: do nothing when it is not the good language
47 31
        if ($xml->attributes()->lang) {
48 21
            if ($xml->attributes()->lang <> $this->language) {
49 18
                return NULL;
50
            }
51 19
            unset($xml->attributes()->lang);
52 19
        }
53
54
        // Specific Attributes changing the xml
55 31
        $key         = $this->setSpecificAttributeKeyWithGivenId($xml, $key);
56 31
        $value       = $this->setSpecificAttributeAge($xml, $depth, $value);
57 31
        $result      = $this->retrieveSpecificAttributeCrossRef($xml, $depth, $result, $key);
58
        // Standard Attributes
59 31
        $result = $this->setStandardAttributes($xml, $result, $key);
60
        // Specific Key
61 31
        $value = $this->setValueForSpecificKeys($key, $value, $format);
62
63 31
        $result = $this->setValue($result, $key, $value);
64 31
        $result = $this->setChildren($xml, $depth, $key, $result);
65
66 31
        return $result;
67
    }
68
69
    /**
70
     * @param array|string $value
71
     */
72 31
    private function setValue($result, $key, $value) {
73 31
        if ($value <> '') {
74 28
            $result = array_merge($result, array($key => $value));
75 28
        }
76 31
        return $result;
77
    }
78
79
    /**
80
     * @param \SimpleXMLElement $xml
81
     * @param integer $depth
82
     * @param string $key
83
     * @param array $arXML
84
     *
85
     * @return array
86
     */
87 31
    private function setChildren(\SimpleXMLElement $xml, $depth, $key, array $arXML) {
88 31
        if ($xml->children()->count() > 0) {
89 29
            foreach($xml->children() as $childKey => $childValue) {
90 29
                $child = $this->xml2array($childValue, $depth);
91 29
                if ($depth > 1 && ! empty($child)) {
92 21
                    $arXML = array_merge_recursive($arXML, array($key => $child));
93 29
                } elseif (! empty($child)) {
94 28
                    $arXML = array_merge_recursive($arXML, $child);
95 28
                }
96 29
            }
97 29
        }
98 31
        return $arXML;
99
    }
100
101
    /**
102
     * @param \SimpleXMLElement $xml
103
     */
104 31
    private function setStandardAttributes(\SimpleXMLElement $xml, $result, $key) {
105
        // Standard Attributes (without Specific thanks to unset())
106 31
        $attributes = array();
107 31
        foreach($xml->attributes() as $attributeKey => $attributeValue) {
108
            if ($attributeKey <> 'id'
109 25
            && $attributeKey <> 'ref'
110 25
            && $attributeKey <> 'lang'
111 25
            && $attributeKey <> 'crossref') {
112 16
                $attributes[$attributeKey] = trim($attributeValue);
113 16
            }
114 31
        }
115 31
        if (count($attributes) > 0) {
116 16
            $result = array_merge_recursive($result, array($key => $attributes));
117 16
        }
118 31
        return $result;
119
    }
120
121
    /**
122
     * @param \SimpleXMLElement $xml
123
     * @param string $key
124
     *
125
     * @return string
126
     */
127 31
    private function setSpecificAttributeKeyWithGivenId(\SimpleXMLElement $xml, $key) {
128
        // Specific Attribute: change the key with the given id
129 31
        if ($xml->attributes()->id) {
130 19
            $key = (string) $xml->attributes()->id;
131 19
        }
132 31
        return $key;
133
    }
134
135
    /**
136
     * @param \SimpleXMLElement $xml
137
     * @param string $value
138
     *
139
     * @return string
140
     */
141 31
    private function setSpecificAttributeAge(\SimpleXMLElement $xml, $depth, $value) {
142
        // Specific Attribute: Retreive the age
143 31
        if ($xml->attributes()->getAge) {
144 11
            $CVCrossRef = $this->CVFile->xpath(trim($xml->attributes()->getAge));
145 11
            $cr = $this->xml2array(clone $CVCrossRef[0], $depth, FALSE);
146 11
            $cr = implode("", $cr);
147 11
            $AgeCalculator = new AgeCalculator((string) $cr);
148 11
            $value = $AgeCalculator->age();
149 11
        }
150 31
        return $value;
151
    }
152
153
    /**
154
     * @param \SimpleXMLElement $xml
155
     * @param array $arXML
156
     * @param string $key
157
     *
158
     * @return array
159
     */
160 31
    private function retrieveSpecificAttributeCrossRef(\SimpleXMLElement $xml, $depth, array $arXML, $key) {
161
        // Specific Attribute: Retrieve the given crossref
162 31
        if ($xml->attributes()->crossref) {
163 14
            $CVCrossRef = $this->CVFile->xpath(trim($xml->attributes()->crossref));
164 14
            $temp = array();
165 14
            foreach ($CVCrossRef as $ref => $value) {
166 14
                $cr = $this->xml2array($value, $depth);
167 14
                if($cr) $temp = array_merge($temp, $cr);
168 14
            }
169 14
            $arXML = array_merge($arXML, array($key => $temp));
170 14
        }
171 31
        return $arXML;
172
    }
173
174
    /**
175
     * @param string $key
176
     * @param string $value
177
     * @param boolean $format
178
     *
179
     * @return array|string
180
     */
181 31
    private function setValueForSpecificKeys($key, $value, $format) {
182 31
        if ($key == 'birthday') {
183 12
            return $this->setValueForBirthdayKey($value, $format);
184
        }
185 31
        elseif ($key == "item") {
186 9
            return array($value); // convert to apply array_merge()
187
        }
188
        else {
189 31
            return $value;
190
        }
191
    }
192
193
    /**
194
     * Specific Key: Format to french date format
195
     *
196
     * @param string $value
197
     * @param boolean $format
198
     *
199
     * @return string
200
     */
201 12
    private function setValueForBirthdayKey($value, $format) {
202 12
        if ($format) {
203 12
            setlocale(LC_TIME, array('fra_fra', 'fr', 'fr_FR', 'fr_FR.UTF8'));
204 12
            $value = strftime('%d %B %Y', strtotime(date($value)));
205 12
        }
206
207 12
        return $value;
208
    }
209
}
210