1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace NFePHP\MDFe\Common; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Class to Read and preprocess WS parameters from xml storage |
7
|
|
|
* file to json encode or stdClass |
8
|
|
|
* |
9
|
|
|
* @author Cleiton Perin <cperin20 at gmail dot com> |
10
|
|
|
* @package NFePHP\MDFe\Common\Webservices |
11
|
|
|
* @copyright NFePHP Copyright (c) 2008-2019 |
12
|
|
|
* @license http://www.gnu.org/licenses/lgpl.txt LGPLv3+ |
13
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
14
|
|
|
* @license http://www.gnu.org/licenses/gpl.txt GPLv3+ |
15
|
|
|
* @category NFePHP |
16
|
|
|
* @link http://github.com/nfephp-org/sped-mdfe for the canonical source repository |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use SimpleXMLElement; |
20
|
|
|
|
21
|
|
|
class Webservices |
22
|
|
|
{ |
23
|
|
|
public $json; |
24
|
|
|
public $std; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Constructor |
28
|
|
|
* @param string $xml path or xml content from |
29
|
|
|
* nfe_ws3_mod55 |
30
|
|
|
*/ |
31
|
1 |
|
public function __construct($xml) |
32
|
|
|
{ |
33
|
1 |
|
$this->toStd($xml); |
34
|
1 |
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get webservices parameters for specific conditions |
38
|
|
|
* the parameters with the authorizers are in a json file in |
39
|
|
|
* the storage folder |
40
|
|
|
* @param string $sigla |
41
|
|
|
* @param string $ambiente "homologacao" ou "producao" |
42
|
|
|
* @param string $modelo "58" |
43
|
|
|
* @return boolean | \stdClass |
44
|
|
|
*/ |
45
|
1 |
|
public function get($sigla, $ambiente, $modelo) |
46
|
|
|
{ |
47
|
1 |
|
$autfile = realpath(__DIR__ . '/../../storage/autorizadores.json'); |
48
|
1 |
|
$autorizadores = json_decode(file_get_contents($autfile), true); |
49
|
1 |
|
if (!key_exists($sigla, $autorizadores[$modelo])) { |
50
|
|
|
throw new \RuntimeException( |
51
|
|
|
"Não existe o autorizador [$sigla] para os " |
52
|
|
|
. "webservices" |
53
|
|
|
); |
54
|
|
|
} |
55
|
1 |
|
$auto = $autorizadores[$modelo][$sigla]; |
56
|
1 |
|
if (empty($auto) || empty($this->std)) { |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
1 |
|
if (empty($this->std->$auto)) { |
60
|
|
|
throw new \RuntimeException( |
61
|
|
|
"Não existem webservices cadastrados para [$sigla]" |
62
|
|
|
); |
63
|
|
|
} |
64
|
1 |
|
$svw = $this->std->$auto->$ambiente; |
65
|
1 |
|
return $svw; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return WS parameters in a stdClass |
70
|
|
|
* @param string $xml |
71
|
|
|
* @return \stdClass |
72
|
|
|
*/ |
73
|
1 |
|
public function toStd($xml = '') |
74
|
|
|
{ |
75
|
1 |
|
if (!empty($xml)) { |
76
|
1 |
|
$this->convert($xml); |
77
|
|
|
} |
78
|
1 |
|
return $this->std; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Return WS parameters in json format |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function __toString() |
86
|
|
|
{ |
87
|
|
|
return (string)$this->json; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Read WS xml and convert to json and stdClass |
92
|
|
|
* @param string $xml |
93
|
|
|
*/ |
94
|
1 |
|
protected function convert($xml) |
95
|
|
|
{ |
96
|
1 |
|
$resp = simplexml_load_string($xml, null, LIBXML_NOCDATA); |
97
|
1 |
|
$aWS = []; |
98
|
1 |
|
foreach ($resp->children() as $element) { |
99
|
1 |
|
$sigla = (string)$element->sigla; |
100
|
1 |
|
$aWS[$sigla] = []; |
101
|
1 |
|
if (isset($element->homologacao)) { |
102
|
1 |
|
$aWS[$sigla] += $this->extract($element->homologacao, 'homologacao'); |
103
|
|
|
} |
104
|
1 |
|
if (isset($element->producao)) { |
105
|
1 |
|
$aWS[$sigla] += $this->extract($element->producao, 'producao'); |
106
|
|
|
} |
107
|
|
|
} |
108
|
1 |
|
$this->json = json_encode($aWS); |
109
|
1 |
|
$this->std = json_decode(json_encode($aWS)); |
110
|
1 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Extract data from wbservices XML strorage to a array |
114
|
|
|
* @param SimpleXMLElement $node |
115
|
|
|
* @param string $environment |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
1 |
|
protected function extract(SimpleXMLElement $node, $environment) |
119
|
|
|
{ |
120
|
1 |
|
$amb[$environment] = []; |
|
|
|
|
121
|
1 |
|
foreach ($node->children() as $children) { |
122
|
1 |
|
$name = (string)$children->getName(); |
123
|
1 |
|
$method = (string)$children['method']; |
124
|
1 |
|
$operation = (string)$children['operation']; |
125
|
1 |
|
$version = (string)$children['version']; |
126
|
1 |
|
$url = (string)$children[0]; |
127
|
|
|
$operations = [ |
128
|
1 |
|
'method' => $method, |
129
|
1 |
|
'operation' => $operation, |
130
|
1 |
|
'version' => $version, |
131
|
1 |
|
'url' => $url |
132
|
|
|
]; |
133
|
1 |
|
$amb[$environment][$name] = $operations; |
134
|
|
|
} |
135
|
1 |
|
return $amb; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.