1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the cfdi-xslt project. |
5
|
|
|
* |
6
|
|
|
* (c) Kinedu |
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 Kinedu\CfdiXslt; |
13
|
|
|
|
14
|
|
|
use SimpleXMLElement; |
15
|
|
|
use DOMDocument; |
16
|
|
|
use DOMXpath; |
17
|
|
|
|
18
|
|
|
class Retrieve |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* SAT Endpoint |
22
|
|
|
* |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
const SAT_ENDPOINT = 'http://www.sat.gob.mx/sitio_internet/cfd/3/cadenaoriginal_3_3/cadenaoriginal_3_3.xslt'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
protected function getIncludeFiles(): array |
31
|
|
|
{ |
32
|
|
|
$files[] = static::SAT_ENDPOINT; |
|
|
|
|
33
|
|
|
|
34
|
|
|
$xml = new SimpleXMLElement( |
35
|
|
|
file_get_contents(static::SAT_ENDPOINT) |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
foreach ($xml->xpath('//xsl:include') as $node) { |
39
|
|
|
$files[] = reset($node['href']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $files; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $directory |
47
|
|
|
* |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function changeNodeReference(string $directory): bool |
51
|
|
|
{ |
52
|
|
|
$file = $directory.$this->getFileName(static::SAT_ENDPOINT); |
53
|
|
|
|
54
|
|
|
$dom = new DOMDocument(); |
55
|
|
|
$dom->load($file); |
56
|
|
|
|
57
|
|
|
$xpath = new DOMXpath($dom); |
58
|
|
|
$query = '//xsl:include'; |
59
|
|
|
|
60
|
|
|
foreach ($xpath->query($query) as $node) { |
61
|
|
|
$href = $node->getAttribute('href'); |
62
|
|
|
|
63
|
|
|
$node->setAttribute( |
64
|
|
|
'href', |
65
|
|
|
$this->getFileName($href) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $dom->save($file); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param string $directory |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
|
|
public function download(string $directory): bool |
78
|
|
|
{ |
79
|
|
|
foreach ($this->getIncludeFiles() as $url) { |
80
|
|
|
$file = file_get_contents($url); |
81
|
|
|
$fileName = $this->getFileName($url); |
82
|
|
|
|
83
|
|
|
file_put_contents("{$directory}{$fileName}", $file); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->changeNodeReference($directory); |
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $file |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
protected function getFileName(string $file): string |
97
|
|
|
{ |
98
|
|
|
return basename($file); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.