Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SortDirection
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 reverse
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 isAscending
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isDescending
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 factor
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 applyToComparisonResult
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Ergonomic utilities for PHP enums, including names, values, lookups, and option maps.
7 *
8 * This file is part of fast-forward/enum project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/enum
14 * @see      https://github.com/php-fast-forward/enum/issues
15 * @see      https://php-fast-forward.github.io/enum/
16 * @see      https://datatracker.ietf.org/doc/html/rfc2119
17 */
18
19namespace FastForward\Enum\Sort;
20
21use FastForward\Enum\ReversibleInterface;
22use FastForward\Enum\Trait\Comparable;
23use FastForward\Enum\Trait\HasNameLookup;
24use FastForward\Enum\Trait\HasNameMap;
25use FastForward\Enum\Trait\HasNames;
26
27enum SortDirection implements ReversibleInterface
28{
29    use Comparable;
30    use HasNameLookup;
31    use HasNameMap;
32    use HasNames;
33
34    case Ascending;
35    case Descending;
36
37    /**
38     * @return static
39     */
40    public function reverse(): static
41    {
42        return match ($this) {
43            self::Ascending => self::Descending,
44            self::Descending => self::Ascending,
45        };
46    }
47
48    /**
49     * @return bool
50     */
51    public function isAscending(): bool
52    {
53        return $this->is(self::Ascending);
54    }
55
56    /**
57     * @return bool
58     */
59    public function isDescending(): bool
60    {
61        return $this->is(self::Descending);
62    }
63
64    /**
65     * @return int
66     */
67    public function factor(): int
68    {
69        return match ($this) {
70            self::Ascending => 1,
71            self::Descending => -1,
72        };
73    }
74
75    /**
76     * @param int $result
77     *
78     * @return int
79     */
80    public function applyToComparisonResult(int $result): int
81    {
82        return $result * $this->factor();
83    }
84}