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