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

CurriculumVitae::getExperiences()   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 0
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\LibXmlDisplayErrors;
15
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
16
17
class CurriculumVitae extends Xml2arrayFunctions
18
{
19
    private $lang;
20
    private $curriculumVitae;
21
    private $pathToFile;
22
    private $interface;
23
    private $cvFile;
24
    private $xml2arrayFunctions;
25
26
    const IDENTITY_MYSELF = 'myself';
27
    const EXPERIENCES     = 'experiences';
28
29
    /**
30
     * @param string $pathToFile
31
     * @param string $lang
32
     */
33
    public function __construct($pathToFile, $lang = 'en') {
34
        $this->pathToFile = $pathToFile;
35
        $this->setFileName();
36
        $this->lang               = $lang;
37
        $this->curriculumVitae    = $this->getXmlCurriculumVitae();
38
        $this->xml2arrayFunctions = new Xml2arrayFunctions($this->curriculumVitae, $this->lang);
39
    }
40
41
    /**
42
     * @return null|array
43
     */
44
    public function getDropDownLanguages() {
45
        $this->interface = $this->curriculumVitae->{'langs'};
46
        $return          = $this->getXMLValue();
47
        if (!$return) {
48
            $return = [$this->lang => $this->lang];
49
        }
50
51
        return $return;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getAnchors() {
58
        $anchorsAttribute = $this->curriculumVitae->xpath('curriculumVitae/*[attribute::anchor]');
59
60
        $anchors = [];
61
        foreach ($anchorsAttribute as $anchorsValue) {
62
            $anchor = (string) $anchorsValue['anchor'];
63
            $title  = $anchorsValue->xpath("anchorTitle[@lang='" . $this->lang . "']");
64
            if (count($title) == 0) {
65
                $title = $anchorsValue->xpath('anchorTitle');
66
            }
67
            $anchors[$anchor] = [
68
                'href'  => $anchor,
69
                'title' => (string) $title[0],
70
            ];
71
        }
72
73
        return $anchors;
74
    }
75
76
    /**
77
     * @return string
78
     */
79
    public function getHumanFileName() {
80
        $myName = $this->getMyName();
81
        if (empty($myName)) {
82
            return $this->cvFile;
83
        }
84
85
        $myCurrentJob = $this->getMyCurrentJob();
86
        if (empty($myCurrentJob)) {
87
            return $myName;
88
        }
89
90
        return $myName.' - '.$myCurrentJob;
91
    }
92
93
    /**
94
     * @return array<string,null|array<string,array>>
95
     */
96
    public function getCurriculumVitaeArray() {
97
        return [
98
            'identity'          => $this->getIdentity(),
99
            'followMe'          => $this->getFollowMe(),
100
            'lookingFor'        => $this->getLookingFor(),
101
            self::EXPERIENCES   => $this->getExperiences(),
102
            'skills'            => $this->getSkills(),
103
            'educations'        => $this->getEducations(),
104
            'languageSkills'    => $this->getLanguageSkills(),
105
            'miscellaneous'     => $this->getMiscellaneous()
106
        ];
107
    }
108
109
    /**
110
     * @return null|array<string,array>
111
     */
112
    public function getIdentity() {
113
        $this->interface = $this->curriculumVitae->curriculumVitae->identity->items;
114
        return $this->getXMLValue();
115
    }
116
117
    /**
118
     * @return null|array<string,array>
119
     */
120
    public function getFollowMe() {
121
        $this->interface = $this->curriculumVitae->curriculumVitae->followMe->items;
122
        return $this->getXMLValue();
123
    }
124
125
    /**
126
     * @return null|array<string,array>
127
     */
128
    public function getLookingFor() {
129
        $this->interface = $this->curriculumVitae->curriculumVitae->lookingFor;
130
        return $this->getXMLValue();
131
    }
132
133
    /**
134
     * @return null|array<string,array>
135
     */
136
    public function getExperiences() {
137
        $this->interface = $this->curriculumVitae->curriculumVitae->experiences->items;
138
        return $this->getXMLValue();
139
    }
140
141
    /**
142
     * @return null|array<string,array>
143
     */
144
    public function getSkills() {
145
        $this->interface = $this->curriculumVitae->curriculumVitae->skills->items;
146
        return $this->getXMLValue();
147
    }
148
149
    /**
150
     * @return null|array<string,array>
151
     */
152
    public function getEducations() {
153
        $this->interface = $this->curriculumVitae->curriculumVitae->educations->items;
154
        return $this->getXMLValue();
155
    }
156
157
    /**
158
     * @return null|array<string,array>
159
     */
160
    public function getLanguageSkills() {
161
        $this->interface = $this->curriculumVitae->curriculumVitae->languageSkills->items;
162
        return $this->getXMLValue();
163
    }
164
165
    /**
166
     * @return null|array<string,array>
167
     */
168
    public function getMiscellaneous() {
169
        $this->interface = $this->curriculumVitae->curriculumVitae->miscellaneous->items;
170
        return $this->getXMLValue();
171
    }
172
173
    private function setFileName() {
174
        $data = explode('/', $this->pathToFile);
175
        $data = $data[count($data) - 1];
176
        $data = explode('.', $data);
177
178
        $this->cvFile = $data[0];
179
    }
180
181
    /**
182
     * @return null|string
183
     */
184
    private function getMyName() {
185
        $identity = $this->getIdentity();
186
187
        if (isset($identity[self::IDENTITY_MYSELF]['name'])) {
188
            return $identity[self::IDENTITY_MYSELF]['name'];
189
        }
190
191
        return NULL;
192
    }
193
194
    /**
195
     * @return null|string
196
     */
197
    private function getMyCurrentJob() {
198
        $lookingFor = $this->getLookingFor();
199
        $experience = 'experience';
200
        if (isset($lookingFor[$experience]['job'])) {
201
            return (string) $lookingFor[$experience]['job'];
202
        } elseif (isset($lookingFor[$experience])) {
203
            return (string) $lookingFor[$experience];
204
        }
205
206
        return NULL;
207
    }
208
209
    /**
210
     * @return \SimpleXMLElement
211
     */
212
    private function getXmlCurriculumVitae() {
213
        if (is_null($this->pathToFile) || !is_file($this->pathToFile)) {
214
            throw new InvalidArgumentException('The path ' . $this->pathToFile . ' is not a valid path to file.');
215
        }
216
        $this->isValidXmlCurriculumVitae();
217
218
        return simplexml_load_file($this->pathToFile);
219
    }
220
221
    /**
222
     * @return boolean
223
     */
224
    private function isValidXmlCurriculumVitae() {
225
        // Active "user error handling"
226
        libxml_use_internal_errors(TRUE);
227
228
        // Instanciate of a DOMDocument
229
        $dom = new \DOMDocument('1.0');
230
231
        // Load the XML from the file
232
        $dom->load($this->pathToFile);
233
234
        // Validation duof the XML document
235
        $reflClass = new \ReflectionClass(get_class($this));
236
        $xsdFile   = dirname($reflClass->getFileName()).'/validator.xsd';
237
        $validate  = $dom->schemaValidate($xsdFile);
238
        if (!$validate) {
239
            $libxmlDisplayErrors = new LibXmlDisplayErrors;
240
            throw new InvalidArgumentException($libxmlDisplayErrors->displayErrors());
241
        }
242
243
        return $validate;
244
    }
245
246
    /**
247
     * @return null|array<string,array>
248
     */
249
    private function getXMLValue() {
250
        if (!$this->interface) {
251
            return NULL;
252
        }
253
254
        return $this->xml2arrayFunctions->xml2array($this->interface);
255
    }
256
}
257