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