Completed
Push — master ( 3785c8...e3dbba )
by Konstantinos
03:58
created

ModelEqualityOperator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace BZIon\Twig;
4
5
/**
6
 * A twig operator to test if two models are the same (they are of the same type
7
 * and have the same ID)
8
 */
9
class ModelEqualityOperator extends \Twig_Node_Expression
10
{
11
    /**
12
     * {@inheritdoc}
13
     */
14
    public function __construct(\Twig_NodeInterface $left, \Twig_NodeInterface $right, $lineno)
15
    {
16
        parent::__construct(array('left' => $left, 'right' => $right), array(), $lineno);
17
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22
    public function compile(\Twig_Compiler $compiler)
23
    {
24
        $compiler
25
            ->raw('( \BZIon\Twig\ModelEqualityOperator::equalModels((')
26
            ->subcompile($this->getNode('left'))
27
            ->raw('),(')
28
            ->subcompile($this->getNode('right'))
29
            ->raw(')) )')
30
        ;
31
    }
32
33
    /**
34
     * Checks if $a and $b represent the same object
35
     *
36
     * Used as a helper function, since there might be syntax errors when older
37
     * PHP versions try to parse this: (new Model())->isSameAs($b)
38
     *
39
     * @param \Model $a
40
     * @param \Model $b
41
     *
42
     * @return bool
43
     */
44
    public static function equalModels(\Model $a, \Model $b) {
45
        return $a->isSameAs($b);
46
    }
47
}
48