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\Graph\Node; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A variable with a certain property. |
17
|
|
|
*/ |
18
|
|
|
class WithProperty extends Variable { |
19
|
|
|
/** |
20
|
|
|
* @var Variable |
21
|
|
|
*/ |
22
|
|
|
private $other; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Property |
26
|
|
|
*/ |
27
|
|
|
private $property; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $arguments; |
33
|
|
|
|
34
|
28 |
|
public function __construct(Variable $other, Property $property, array $arguments) { |
35
|
28 |
|
parent::__construct(); |
36
|
28 |
|
assert('$property->arguments_are_valid($arguments)'); |
37
|
28 |
|
$this->other = $other; |
38
|
28 |
|
$this->property = $property; |
39
|
28 |
|
$this->arguments = $arguments; |
40
|
28 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Variable |
44
|
|
|
*/ |
45
|
9 |
|
public function variable() { |
46
|
9 |
|
return $this->other; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @inheritdocs |
51
|
|
|
*/ |
52
|
9 |
|
public function meaning() { |
53
|
|
|
// TODO: maybe i should use the name here? |
54
|
9 |
|
return $this->variable()->meaning()." with ".$this->property->name().": ".$this->argument_list(); |
55
|
|
|
} |
56
|
|
|
|
57
|
9 |
|
protected function argument_list() { |
58
|
9 |
|
$args = array(); |
59
|
9 |
|
foreach ($this->arguments as $argument) { |
60
|
9 |
|
if (is_string($argument)) { |
61
|
9 |
|
$args[] = "\"$argument\""; |
62
|
9 |
|
} |
63
|
|
|
else { |
64
|
|
|
throw new \LogicException("Unknown arg type: ".gettype($argument)); |
65
|
|
|
} |
66
|
9 |
|
} |
67
|
9 |
|
return implode(", ", $args); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @inheritdocs |
72
|
|
|
*/ |
73
|
23 |
|
public function compile() { |
74
|
23 |
|
$left_condition = $this->other->compile(); |
75
|
23 |
|
$property_condition = $this->property->compile($this->arguments); |
76
|
|
|
|
77
|
23 |
|
return function(Node $n) use ($left_condition, $property_condition) { |
78
|
23 |
|
return $left_condition($n) |
79
|
23 |
|
&& $property_condition($n); |
80
|
23 |
|
}; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|