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
|
|
|
use Lechimp\Dicto\Graph\PredicateFactory; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* A variable with a certain property. |
18
|
|
|
*/ |
19
|
|
|
class WithProperty extends Variable { |
20
|
|
|
/** |
21
|
|
|
* @var Variable |
22
|
|
|
*/ |
23
|
|
|
private $other; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var Property |
27
|
|
|
*/ |
28
|
|
|
private $property; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $arguments; |
34
|
|
|
|
35
|
29 |
|
public function __construct(Variable $other, Property $property, array $arguments) { |
36
|
29 |
|
parent::__construct(); |
37
|
29 |
|
assert('$property->arguments_are_valid($arguments)'); |
38
|
29 |
|
$this->other = $other; |
39
|
29 |
|
$this->property = $property; |
40
|
29 |
|
$this->arguments = $arguments; |
41
|
29 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return Variable |
45
|
|
|
*/ |
46
|
14 |
|
public function variable() { |
47
|
14 |
|
return $this->other; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @inheritdocs |
52
|
|
|
*/ |
53
|
14 |
|
public function meaning() { |
54
|
14 |
|
return $this->variable()->meaning()." ".$this->property->parse_as().": ".$this->argument_list(); |
55
|
|
|
} |
56
|
|
|
|
57
|
14 |
|
protected function argument_list() { |
58
|
14 |
|
$args = array(); |
59
|
14 |
|
foreach ($this->arguments as $argument) { |
60
|
14 |
|
if (is_string($argument)) { |
61
|
12 |
|
$args[] = "\"$argument\""; |
62
|
12 |
|
} |
63
|
2 |
|
else if ($argument instanceof Variable) { |
64
|
2 |
|
$name = $argument->name(); |
65
|
2 |
|
if ($name !== null) { |
66
|
2 |
|
$args[] = $name; |
67
|
2 |
|
} |
68
|
|
|
else { |
69
|
|
|
$args[] = "(".$argument->meaning().")"; |
70
|
|
|
} |
71
|
2 |
|
} |
72
|
|
|
else { |
73
|
|
|
throw new \LogicException("Unknown arg type: ".gettype($argument)); |
74
|
|
|
} |
75
|
14 |
|
} |
76
|
14 |
|
return implode(", ", $args); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @inheritdocs |
81
|
|
|
*/ |
82
|
23 |
|
public function compile(PredicateFactory $f) { |
83
|
23 |
|
$l = $this->other->compile($f); |
84
|
23 |
|
$p = $this->property->compile($f, $this->arguments); |
85
|
|
|
|
86
|
23 |
|
return $f->_and([$l,$p]); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: