1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* An implementation of dicto (scg.unibe.ch/dicto) in and for PHP. |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2016, 2015 Richard Klees <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This software is licensed under The MIT License. You should have received |
8
|
|
|
* a copy of the license along with the code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Lechimp\Dicto\Variables; |
12
|
|
|
|
13
|
|
|
use Lechimp\Dicto\Regexp; |
14
|
|
|
use Lechimp\Dicto\Graph\Node; |
15
|
|
|
use Lechimp\Dicto\Graph\PredicateFactory; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* A variable with a certain property. |
19
|
|
|
*/ |
20
|
|
|
class WithProperty extends Variable { |
21
|
|
|
/** |
22
|
|
|
* @var Variable |
23
|
|
|
*/ |
24
|
|
|
private $other; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var Property |
28
|
|
|
*/ |
29
|
|
|
private $property; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $arguments; |
35
|
|
|
|
36
|
35 |
|
public function __construct(Variable $other, Property $property, array $arguments) { |
37
|
35 |
|
parent::__construct(); |
38
|
35 |
|
assert('$property->arguments_are_valid($arguments)'); |
39
|
35 |
|
$this->other = $other; |
40
|
35 |
|
$this->property = $property; |
41
|
35 |
|
$this->arguments = $arguments; |
42
|
35 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return Variable |
46
|
|
|
*/ |
47
|
14 |
|
public function variable() { |
48
|
14 |
|
return $this->other; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @inheritdocs |
53
|
|
|
*/ |
54
|
14 |
|
public function meaning() { |
55
|
14 |
|
return $this->variable()->meaning()." ".$this->property->parse_as().": ".$this->argument_list(); |
56
|
|
|
} |
57
|
|
|
|
58
|
14 |
|
protected function argument_list() { |
59
|
14 |
|
$args = array(); |
60
|
14 |
|
foreach ($this->arguments as $argument) { |
61
|
14 |
|
if (is_string($argument)) { |
62
|
|
|
$args[] = "\"$argument\""; |
63
|
|
|
} |
64
|
14 |
|
else if ($argument instanceof Regexp) { |
65
|
12 |
|
$args[] = "\"".$argument->raw()."\""; |
66
|
12 |
|
} |
67
|
2 |
|
else if ($argument instanceof Variable) { |
68
|
2 |
|
$name = $argument->name(); |
69
|
2 |
|
if ($name !== null) { |
70
|
2 |
|
$args[] = $name; |
71
|
2 |
|
} |
72
|
|
|
else { |
73
|
|
|
$args[] = "(".$argument->meaning().")"; |
74
|
|
|
} |
75
|
2 |
|
} |
76
|
|
|
else { |
77
|
|
|
throw new \LogicException("Unknown arg type: ".gettype($argument)); |
78
|
|
|
} |
79
|
14 |
|
} |
80
|
14 |
|
return implode(", ", $args); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @inheritdocs |
85
|
|
|
*/ |
86
|
27 |
|
public function compile(PredicateFactory $f) { |
87
|
27 |
|
$l = $this->other->compile($f); |
88
|
27 |
|
$p = $this->property->compile($f, $this->arguments); |
89
|
|
|
|
90
|
27 |
|
return $f->_and([$l,$p]); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|