Order::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
/**
4
 * Author: Nil Portugués Calderó <[email protected]>
5
 * Date: 6/15/15
6
 * Time: 11:13 PM.
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 NilPortugues\Foundation\Domain\Model\Repository;
12
13
use InvalidArgumentException;
14
use NilPortugues\Foundation\Domain\Model\Repository\Contracts\Order as OrderInterface;
15
16
class Order implements OrderInterface
17
{
18
    /**
19
     * @var string
20
     */
21
    protected $direction;
22
23
    /**
24
     * @param string $direction
25
     */
26
    public function __construct($direction)
27
    {
28
        $direction = (string) strtoupper($direction);
29
        $this->assert($direction);
30
        $this->direction = $direction;
31
    }
32
33
    /**
34
     * @param string $direction
35
     *
36
     * @throws \InvalidArgumentException
37
     */
38
    protected function assert($direction)
39
    {
40
        if (false === in_array($direction, [self::ASCENDING, self::DESCENDING], true)) {
41
            throw new InvalidArgumentException('Only accepted values for direction must be either ASC or DESC');
42
        }
43
    }
44
45
    /**
46
     * @return bool
47
     */
48
    public function isDescending()
49
    {
50
        return !$this->isAscending();
51
    }
52
53
    /**
54
     * @return bool
55
     */
56
    public function isAscending()
57
    {
58
        return $this->direction == self::ASCENDING;
59
    }
60
61
    /**
62
     * @return string
63
     */
64
    public function __toString()
65
    {
66
        return (string) $this->direction;
67
    }
68
69
    /**
70
     * @param self $object
71
     *
72
     * @return bool
73
     */
74
    public function equals($object)
75
    {
76
        return get_class($this) === get_class($object)
77
        && $this->direction() === $object->direction();
78
    }
79
80
    /**
81
     * @return string
82
     */
83
    public function direction()
84
    {
85
        return (!empty($this->direction)) ? $this->direction : self::ASCENDING;
86
    }
87
}
88