-
Notifications
You must be signed in to change notification settings - Fork 0
/
xpression.php
66 lines (57 loc) · 1.4 KB
/
xpression.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
class xpression
{
function __construct($expression = '', $name = '', $params = array())
{
$this->mangled = self::mangle($name, $params);
$this->params = $params;
$this->expression = $expression;
}
function validate($args)
{
foreach($args as $name => $value)
{
validate::assert($this->params[$name], $value);
}
}
function mangled()
{
return $this->mangled;
}
function get($args)
{
$this->validate($args);
return vars::apply_assoc($this->expression, $args, true);
}
static function evaluate($xpath)
{
if(!self::$xml)
{
self::$xml = new DOMDocument();
self::$xpath = new DOMXPath(self::$xml);
}
return self::$xpath->evaluate($xpath);
}
static function mangle($xpression, $args)
{
foreach($args as $name => $vt)
{
$xpression .= "[$name]";
}
return $xpression;
}
static function mangle_values($xpression, $args)
{
foreach($args as $name => $value)
{
$xpression .= "[$name -> $value]";
}
return $xpression;
}
static private $xml = null;
static private $xpath = null;
private $mangled;
private $params;
private $expression;
}
?>