-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.php
88 lines (75 loc) · 2.74 KB
/
solution.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
class Person {
public $name;
public $age;
public $birthDate;
public $programmingLanguages;
public function __construct($name, $age, $birthDate, $programmingLanguages) {
$this->name = $name;
$this->age = $age;
$this->birthDate = $birthDate;
$this->programmingLanguages = $programmingLanguages;
}
public static function fromJSON($jsonFileName) {
$jsonData = json_decode(file_get_contents($jsonFileName), true);
return new Person(
$jsonData['name'],
$jsonData['age'],
$jsonData['birthDate'],
$jsonData['programmingLanguages']
);
}
public static function fromXML($xmlFileName) {
$xmlData = simplexml_load_file($xmlFileName);
$name = (string)$xmlData->name;
$age = (int)$xmlData->age;
$birthDate = (string)$xmlData->birthDate;
$programmingLanguages = [];
foreach ($xmlData->programmingLanguages->language as $language) {
$programmingLanguages[] = (string)$language;
}
return new Person($name, $age, $birthDate, $programmingLanguages);
}
public function display() {
echo "Nombre: {$this->name}\n";
echo "Edad: {$this->age}\n";
echo "Fecha de Nacimiento: {$this->birthDate}\n";
echo "Lenguajes de Programación: " . implode(', ', $this->programmingLanguages) . "\n";
}
}
// Datos a almacenar
$data = [
'name' => 'Marco Cruz',
'age' => date('Y') - 2001,
'birthDate' => '2001-08-24',
'programmingLanguages' => ['JavaScript', 'TypeScript', 'C#', 'PHP'],
];
// Crear archivo JSON
$jsonFileName = 'data.json';
file_put_contents($jsonFileName, json_encode($data, JSON_PRETTY_PRINT));
echo "Archivo JSON creado: {$jsonFileName}\n";
// Crear archivo XML
$xmlFileName = 'data.xml';
$xmlContent = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><person></person>');
$xmlContent->addChild('name', $data['name']);
$xmlContent->addChild('age', $data['age']);
$xmlContent->addChild('birthDate', $data['birthDate']);
$programmingLanguages = $xmlContent->addChild('programmingLanguages');
foreach ($data['programmingLanguages'] as $lang) {
$programmingLanguages->addChild('language', $lang);
}
$xmlContent->asXML($xmlFileName);
echo "Archivo XML creado: {$xmlFileName}\n";
echo "\nContenido del archivo JSON:\n";
echo file_get_contents($jsonFileName);
echo "\nContenido del archivo XML:\n";
echo file_get_contents($xmlFileName);
$personFromJson = Person::fromJSON($jsonFileName);
echo "\nDatos desde JSON:\n";
$personFromJson->display();
$personFromXml = Person::fromXML($xmlFileName);
echo "\nDatos desde XML:\n";
$personFromXml->display();
unlink($jsonFileName);
unlink($xmlFileName);
echo "\nArchivos borrados.\n";