1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LoteriaApi\Provider\Reader; |
4
|
|
|
|
5
|
|
|
use LoteriaApi\Config; |
6
|
|
|
|
7
|
|
|
abstract class AbstractXmlLoteria implements IFinder |
8
|
|
|
{ |
9
|
|
|
protected $configPath; |
10
|
|
|
protected $configDatasource; |
11
|
|
|
protected $filename; |
12
|
|
|
|
13
|
|
|
public function __construct(Config $configPath, Config $configDatasource) |
14
|
|
|
{ |
15
|
|
|
$this->configPath = $configPath; |
16
|
|
|
$this->configDatasource = $configDatasource; |
17
|
|
|
$this->putFileName(); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
abstract protected function putFileName(); |
21
|
|
|
|
22
|
|
|
private function formatResultXpathToConcursoArray($resultXpath) |
23
|
|
|
{ |
24
|
|
|
$arrayConcursos = []; |
25
|
|
|
|
26
|
|
|
foreach ($resultXpath as $key => $concurso) { |
27
|
|
|
$arrayConcursos[$key]['concurso'] = (string) $concurso->attributes()->numero; |
28
|
|
|
|
29
|
|
|
$children = $concurso[0]->children(); |
30
|
|
|
|
31
|
|
|
$arrayConcursos[$key]['data'] = (string) $children->data; |
32
|
|
|
$arrayConcursos[$key]['dezenas'] = (array) $children->dezenas->children()->dezena; |
33
|
|
|
$arrayConcursos[$key]['arrecadacao'] = (string) $children->arrecadacao; |
34
|
|
|
$arrayConcursos[$key]['total_ganhadores_primeiro_premio'] = (string) $children->total_ganhadores_primeiro_premio; |
35
|
|
|
$arrayConcursos[$key]['total_ganhadores_segundo_premio'] = (string) $children->total_ganhadores_segundo_premio; |
36
|
|
|
$arrayConcursos[$key]['total_ganhadores_terceiro_premio'] = (string) $children->total_ganhadores_terceiro_premio; |
37
|
|
|
$arrayConcursos[$key]['valor_ganhadores_primeiro_premio'] = (string) $children->valor_ganhadores_primeiro_premio; |
38
|
|
|
$arrayConcursos[$key]['valor_ganhadores_segundo_premio'] = (string) $children->valor_ganhadores_segundo_premio; |
39
|
|
|
$arrayConcursos[$key]['valor_ganhadores_terceiro_premio'] = (string) $children->valor_ganhadores_terceiro_premio; |
40
|
|
|
$arrayConcursos[$key]['acumulado'] = (string) $children->acumulado; |
41
|
|
|
$arrayConcursos[$key]['valor_acumulado'] = (string) $children->valor_acumulado; |
42
|
|
|
$arrayConcursos[$key]['valor_estimado_proximo_concurso'] = (string) $children->valor_estimado_proximo_concurso; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $arrayConcursos; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
private function getSimpleXml() |
49
|
|
|
{ |
50
|
|
|
return simplexml_load_file($this->filename); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function findByConcurso($nrconcurso) |
54
|
|
|
{ |
55
|
|
|
$concurso = $this->getSimpleXml() |
56
|
|
|
->xpath("/concursos/concurso[@numero='{$nrconcurso}']"); |
57
|
|
|
|
58
|
|
|
if (!isset($concurso[0])) { |
59
|
|
|
throw new \InvalidArgumentException("Concurso does not exist"); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$concurso = $this->formatResultXpathToConcursoArray($concurso); |
63
|
|
|
|
64
|
|
|
return $concurso[0]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function findLatestConcursos($nrconcursoRange) |
68
|
|
|
{ |
69
|
|
|
// Get last concurso |
70
|
|
|
$lastConcurso = $this->findLastConcurso(); |
71
|
|
|
|
72
|
|
|
// Range bounds |
73
|
|
|
$rangeIni = (($lastConcurso['concurso'] + 1) - $nrconcursoRange); |
74
|
|
|
$rangeEnd = $lastConcurso['concurso']; |
75
|
|
|
|
76
|
|
|
foreach (range($rangeIni, $rangeEnd) as $nrconcurso) { |
77
|
|
|
$concursos[] = $this->findByConcurso($nrconcurso); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $concursos; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function findLastConcurso() |
84
|
|
|
{ |
85
|
|
|
$concurso = $this->getSimpleXml() |
86
|
|
|
->xpath("(/concursos/concurso)[last()]"); |
87
|
|
|
|
88
|
|
|
$concurso = $this->formatResultXpathToConcursoArray($concurso); |
89
|
|
|
|
90
|
|
|
if (!isset($concurso[0])) { |
91
|
|
|
throw new \InvalidArgumentException("Last concurso not found"); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $concurso[0]; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
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.