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 (!empty($this->timeout)) { |
97
|
|
|
$options['connect_timeout'] = $this->timeout; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $options; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return bool |
105
|
|
|
*/ |
106
|
|
|
public function isNoCache(): bool |
107
|
|
|
{ |
108
|
|
|
return $this->noCache; |
109
|
|
|
} |
110
|
|
|
} |
PHP provides two ways to mark string literals. Either with single quotes
'literal'
or with double quotes"literal"
. The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (
\'
) and the backslash (\\
). Every other character is displayed as is.Double quoted string literals may contain other variables or more complex escape sequences.
will print an indented:
Single is Value
If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.
For more information on PHP string literals and available escape sequences see the PHP core documentation.