Completed
Push — develop ( dfe314...df0194 )
by
unknown
07:26
created

AbstractStatus::is()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2017 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Core\Entity\Status;
12
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
15
/**
16
 * Abstract status class.
17
 *
18
 * To use this boilerplate, it must be extended and all
19
 * available states must be defined as class constants.
20
 *
21
 * <pre>
22
 * // Do not forget the ODM Annotations!
23
 *
24
 * class Status extends AbstractStatus
25
 * {
26
 *      const NEW = 'new';
27
 *      const MODIFIED = 'modified';
28
 * }
29
 *
30
 * $state = new Status(Status::NEW); // or new Status('new');
31
 * $state->is(Status::NEW) // => true
32
 * $state->is(new Status(Status::MODIFIED)) // => false
33
 * </pre>
34
 *
35
 * @ODM\MappedSuperclass
36
 * @author Mathias Gelhausen <[email protected]>
37
 * @since 0,29
38
 */
39
abstract class AbstractStatus implements StatusInterface
40
{
41
    /**
42
     * The state name.
43
     *
44
     * @ODM\Field(type="string")
45
     * @var string
46
     */
47
    protected $state;
48
49
    public static function getStates()
50
    {
51
        $reflection = new \ReflectionClass(static::class);
52
53
        return array_values($reflection->getConstants());
54
    }
55
56
    public function __construct($state)
57
    {
58
        $states = static::getStates();
59
60
        if (!in_array($state, $states)) {
61
            throw new \InvalidArgumentException('Invalid state name: ' . $state);
62
        }
63
64
        $this->state = $state;
65
    }
66
67
    public function __toString()
68
    {
69
        return $this->state;
70
    }
71
72
    public function is($state)
73
    {
74
        return $this->state == (string) $state;
75
    }
76
}