|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CrazyGoat\Octophpus; |
|
4
|
|
|
|
|
5
|
|
|
use CrazyGoat\Octophpus\Exception\EsiTagParseException; |
|
6
|
|
|
use CrazyGoat\Octophpus\Validator\EsiAttributeValidator; |
|
7
|
|
|
use Nunzion\Expect; |
|
8
|
|
|
|
|
9
|
|
|
class EsiRequest |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string |
|
13
|
|
|
*/ |
|
14
|
|
|
private $src; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $esiTag; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var float|null |
|
23
|
|
|
*/ |
|
24
|
|
|
private $timeout = null; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
private $noCache = false; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(string $esiTag) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->esiTag = $esiTag; |
|
34
|
|
|
$this->parse($esiTag); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
private function parse(string $esiTag) : void |
|
38
|
|
|
{ |
|
39
|
|
|
$p = xml_parser_create(); |
|
40
|
|
|
$parseStatus = xml_parse_into_struct($p, $esiTag, $values); |
|
41
|
|
|
xml_parser_free($p); |
|
42
|
|
|
|
|
43
|
|
|
if ($parseStatus == 0) { |
|
44
|
|
|
throw new EsiTagParseException('Unable to parse xml: '.$esiTag); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$parsedTag = $this->getTag($values); |
|
48
|
|
|
|
|
49
|
|
|
$validator = new EsiAttributeValidator($parsedTag['attributes']); |
|
50
|
|
|
$validator->validate(); |
|
51
|
|
|
|
|
52
|
|
|
$this->src = $parsedTag['attributes']['SRC']; |
|
53
|
|
|
$this->timeout = isset($parsedTag['attributes']['TIMEOUT']) ? (float)$parsedTag['attributes']['TIMEOUT'] : null; |
|
54
|
|
|
$this->noCache = isset($parsedTag['attributes']['NOCACHE']); |
|
55
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
private function getTag(array $tags) : array |
|
59
|
|
|
{ |
|
60
|
|
|
Expect::that($tags)->itsArrayElement(0); |
|
61
|
|
|
$tag = $tags[0]; |
|
62
|
|
|
|
|
63
|
|
|
Expect::that($tag)->isArray(); |
|
64
|
|
|
Expect::that($tag)->itsArrayElement('tag')->isString()->equals('ESI:INCLUDE'); |
|
65
|
|
|
Expect::that($tag)->itsArrayElement('attributes')->isArray(); |
|
66
|
|
|
Expect::that($tag['attributes'])->itsArrayElement('SRC')->isString(); |
|
67
|
|
|
|
|
68
|
|
|
return $tag; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
public function getSrc(): string |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->src; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @return string |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getEsiTag(): string |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->esiTag; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function requestOptions() : array |
|
88
|
|
|
{ |
|
89
|
|
|
$options = []; |
|
90
|
|
|
|
|
91
|
|
|
if (!is_null($this->timeout)) { |
|
92
|
|
|
$options['connect_timeout'] = $this->timeout; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $options; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function noCache(): bool |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->noCache; |
|
104
|
|
|
} |
|
105
|
|
|
} |