Completed
Push — master ( 470c23...605cf4 )
by Richard
08:11
created

_PropertyEquals::_compile()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 7
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 1
nop 0
crap 6
1
<?php
2
/******************************************************************************
3
 * An implementation of dicto (scg.unibe.ch/dicto) in and for PHP.
4
 * 
5
 * Copyright (c) 2016 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\Graph\Predicate;
12
13
use Lechimp\Dicto\Regexp;
14
use Lechimp\Dicto\Graph\Predicate;
15
use Lechimp\Dicto\Graph\Entity;
16
17
/**
18
 * A predicate that is true if a property is equal to a given value.
19
 */
20
class _PropertyEquals extends Predicate {
21
    /**
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * @var string
28
     */
29
    protected $value;
30
31 6
    public function __construct($name, $value) {
32 6
        assert('is_string($name)');
33 6
        assert('is_string($value)');
34 6
        $this->name = $name;
35 6
        $this->value = $value;
36 6
    }
37
38
    /**
39
     * @inheritdocs
40
     */
41
    public function _compile() {
42
        $name = $this->name;
43
        return function(Entity $e) use ($name) {
44
            if (!$e->has_property($name)) {
45
                return false;
46
            }
47
            return $this->name === $e->property($name);
48
        };
49
    }
50
51
    /**
52
     * @inheritdocs
53
     */
54 6
    public function compile_to_source(array &$custom_closures) {
55 6
        $name = $this->name;
56 6
        $value = $this->value;
57
        return
58
            "   \$value = \n".
59 6
            "       \$e->has_property(\"$name\")\n".
60 6
            "       && (\$e->property(\"$name\") === \"$value\");\n";
61
    }
62
63
    /**
64
     * @inheritdocs
65
     */
66 1
    public function for_types(array $existing_types) {
67 1
        return $existing_types;
68
    }
69
}
70