Completed
Push — master ( 13ee85...192638 )
by Ivannis Suárez
02:46
created

ComparatorTrait::accept()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 8
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Cubiche\Core\Comparable;
12
13
/**
14
 * Comparator Trait.
15
 *
16
 * @author Karel Osorio Ramírez <[email protected]>
17
 * @author Ivannis Suárez Jerez <[email protected]>
18
 */
19
trait ComparatorTrait
20
{
21
    /**
22
     * @param string $method
23
     * @param array  $arguments
24
     *
25
     * @throws \BadMethodCallException
26
     */
27
    public function __call($method, array $arguments)
28
    {
29
        if ($method === 'or') {
30
            return \call_user_func_array(array($this, 'orX'), $arguments);
31
        }
32
33
        throw new \BadMethodCallException(\sprintf('Call to undefined method %s::%s', \get_class($this), $method));
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function compare($a, $b)
40
    {
41
        if ($a instanceof ComparableInterface) {
42
            return $a->compareTo($b);
43
        }
44
45
        return $a < $b ? -1 : ($a == $b ? 0 : 1);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51
    public function reverse()
52
    {
53
        return new ReverseComparator($this);
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function orX(ComparatorInterface $other)
60
    {
61
        return new MultiComparator($this, $other);
62
    }
63
}
64