cerbero90 /
dto
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Cerbero\Dto\Traits; |
||||
| 4 | |||||
| 5 | use Cerbero\Dto\Dto; |
||||
| 6 | |||||
| 7 | use const Cerbero\Dto\MUTABLE; |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 8 | use const Cerbero\Dto\NONE; |
||||
|
0 ignored issues
–
show
|
|||||
| 9 | |||||
| 10 | /** |
||||
| 11 | * Trait to interact with flags. |
||||
| 12 | * |
||||
| 13 | */ |
||||
| 14 | trait HasFlags |
||||
| 15 | { |
||||
| 16 | /** |
||||
| 17 | * The default flags. |
||||
| 18 | * |
||||
| 19 | * @var int |
||||
| 20 | */ |
||||
| 21 | protected static $defaultFlags = NONE; |
||||
| 22 | |||||
| 23 | /** |
||||
| 24 | * The actual flags. |
||||
| 25 | * |
||||
| 26 | * @var int |
||||
| 27 | */ |
||||
| 28 | protected $flags; |
||||
| 29 | |||||
| 30 | /** |
||||
| 31 | * Retrieve the default flags |
||||
| 32 | * |
||||
| 33 | * @return int |
||||
| 34 | */ |
||||
| 35 | 82 | public static function getDefaultFlags(): int |
|||
| 36 | { |
||||
| 37 | 82 | return static::$defaultFlags; |
|||
| 38 | } |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * Retrieve the DTO flags |
||||
| 42 | * |
||||
| 43 | * @return int |
||||
| 44 | */ |
||||
| 45 | 81 | public function getFlags(): int |
|||
| 46 | { |
||||
| 47 | 81 | return $this->flags; |
|||
| 48 | } |
||||
| 49 | |||||
| 50 | /** |
||||
| 51 | * Determine whether the DTO flags include the given flags |
||||
| 52 | * |
||||
| 53 | * @param int $flags |
||||
| 54 | * @return bool |
||||
| 55 | */ |
||||
| 56 | 1 | public function hasFlags(int $flags): bool |
|||
| 57 | { |
||||
| 58 | 1 | return ($this->getFlags() & $flags) === $flags; |
|||
| 59 | } |
||||
| 60 | |||||
| 61 | /** |
||||
| 62 | * Set the DTO flags |
||||
| 63 | * |
||||
| 64 | * @param int $flags |
||||
| 65 | * @return Dto |
||||
| 66 | */ |
||||
| 67 | 6 | public function setFlags(int $flags): Dto |
|||
| 68 | { |
||||
| 69 | 6 | if (!($this->getFlags() & MUTABLE)) { |
|||
| 70 | 3 | return static::make($this->toArray(), $flags); |
|||
|
0 ignored issues
–
show
It seems like
toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 71 | } |
||||
| 72 | |||||
| 73 | 3 | $this->flags = $flags; |
|||
| 74 | |||||
| 75 | 3 | return $this; |
|||
| 76 | } |
||||
| 77 | |||||
| 78 | /** |
||||
| 79 | * Add the given flags to the DTO flags |
||||
| 80 | * |
||||
| 81 | * @param int $flags |
||||
| 82 | * @return Dto |
||||
| 83 | */ |
||||
| 84 | 2 | public function addFlags(int $flags): Dto |
|||
| 85 | { |
||||
| 86 | 2 | $mergedFlags = $this->getFlags() | $flags; |
|||
| 87 | |||||
| 88 | 2 | return $this->setFlags($mergedFlags); |
|||
| 89 | } |
||||
| 90 | |||||
| 91 | /** |
||||
| 92 | * Remove the given flags from the DTO flags |
||||
| 93 | * |
||||
| 94 | * @param int $flags |
||||
| 95 | * @return Dto |
||||
| 96 | */ |
||||
| 97 | 2 | public function removeFlags(int $flags): Dto |
|||
| 98 | { |
||||
| 99 | 2 | $currentFlags = $this->getFlags(); |
|||
| 100 | 2 | $remainingFlags = $currentFlags ^ ($currentFlags & $flags); |
|||
| 101 | |||||
| 102 | 2 | return $this->setFlags($remainingFlags); |
|||
| 103 | } |
||||
| 104 | } |
||||
| 105 |