1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2019 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Utils; |
9
|
|
|
|
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
abstract class XPath |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Export a literal as an XPath expression |
16
|
|
|
* |
17
|
|
|
* @param mixed $value Literal, e.g. "foo" |
18
|
|
|
* @return string XPath expression, e.g. "'foo'" |
19
|
|
|
*/ |
20
|
8 |
|
public static function export($value) |
21
|
|
|
{ |
22
|
8 |
|
if (!is_scalar($value)) |
23
|
|
|
{ |
24
|
1 |
|
throw new InvalidArgumentException(__METHOD__ . '() cannot export non-scalar values'); |
25
|
|
|
} |
26
|
7 |
|
if (is_int($value)) |
27
|
|
|
{ |
28
|
1 |
|
return (string) $value; |
29
|
|
|
} |
30
|
6 |
|
if (is_float($value)) |
31
|
|
|
{ |
32
|
|
|
// Avoid locale issues by using sprintf() |
33
|
2 |
|
return preg_replace('(\\.?0+$)', '', sprintf('%F', $value)); |
34
|
|
|
} |
35
|
|
|
|
36
|
4 |
|
return self::exportString((string) $value); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Export a string as an XPath expression |
41
|
|
|
* |
42
|
|
|
* @param string $str Literal, e.g. "foo" |
43
|
|
|
* @return string XPath expression, e.g. "'foo'" |
44
|
|
|
*/ |
45
|
4 |
|
protected static function exportString($str) |
46
|
|
|
{ |
47
|
|
|
// foo becomes 'foo' |
48
|
4 |
|
if (strpos($str, "'") === false) |
49
|
|
|
{ |
50
|
1 |
|
return "'" . $str . "'"; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// d'oh becomes "d'oh" |
54
|
3 |
|
if (strpos($str, '"') === false) |
55
|
|
|
{ |
56
|
1 |
|
return '"' . $str . '"'; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// This string contains both ' and ". XPath 1.0 doesn't have a mechanism to escape quotes, |
60
|
|
|
// so we have to get creative and use concat() to join chunks in single quotes and chunks |
61
|
|
|
// in double quotes |
62
|
2 |
|
$toks = []; |
63
|
2 |
|
$c = '"'; |
64
|
2 |
|
$pos = 0; |
65
|
2 |
|
while ($pos < strlen($str)) |
66
|
|
|
{ |
67
|
2 |
|
$spn = strcspn($str, $c, $pos); |
68
|
2 |
|
if ($spn) |
69
|
|
|
{ |
70
|
2 |
|
$toks[] = $c . substr($str, $pos, $spn) . $c; |
71
|
2 |
|
$pos += $spn; |
72
|
|
|
} |
73
|
2 |
|
$c = ($c === '"') ? "'" : '"'; |
74
|
|
|
} |
75
|
|
|
|
76
|
2 |
|
return 'concat(' . implode(',', $toks) . ')'; |
77
|
|
|
} |
78
|
|
|
} |