Purchase::cabbage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php declare(strict_types=1);
2
3
namespace Stratadox\PuzzleSolver\Puzzle\WolfGoatCabbage;
4
5
use function assert;
6
use function in_array;
7
8
final class Purchase
9
{
10
    private const WOLF = 'wolf';
11
    private const GOAT = 'goat';
12
    private const CABBAGE = 'cabbage';
13
    private const ALLOWED = [self::WOLF, self::GOAT, self::CABBAGE];
14
    private const EATS = [
15
        self::WOLF => self::GOAT,
16
        self::GOAT => self::CABBAGE,
17
        self::CABBAGE => null,
18
    ];
19
20
    /** @var string */
21
    private $type;
22
23
    private function __construct(string $type)
24
    {
25
        assert(in_array($type, self::ALLOWED));
26
        $this->type = $type;
27
    }
28
29
    public static function wolf(): self
30
    {
31
        return new self(self::WOLF);
32
    }
33
34
    public static function goat(): self
35
    {
36
        return new self(self::GOAT);
37
    }
38
39
    public static function cabbage(): self
40
    {
41
        return new self(self::CABBAGE);
42
    }
43
44
    public static function fromString(string $purchase): self
45
    {
46
        return new self($purchase);
47
    }
48
49
    public function eats(Purchase $other): bool
50
    {
51
        return self::EATS[$this->type] === $other->type;
52
    }
53
54
    public function __toString(): string
55
    {
56
        return $this->type;
57
    }
58
}
59