give
/
vendor
/
vendor-prefixed
/
stellarwp
/
admin-notices
/
src
/
ValueObjects
/
NoticeLocation.php
NoticeLocation.php
1 year ago
NoticeUrgency.php
1 year ago
ScreenCondition.php
1 year ago
Script.php
1 year ago
Style.php
1 year ago
UserCapability.php
1 year ago
NoticeLocation.php
143 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | |
| 6 | namespace Give\Vendors\StellarWP\AdminNotices\ValueObjects; |
| 7 | |
| 8 | use InvalidArgumentException; |
| 9 | |
| 10 | class NoticeLocation |
| 11 | { |
| 12 | private const ABOVE_HEADER = 'above_header'; |
| 13 | private const BELOW_HEADER = 'below_header'; |
| 14 | private const INLINE = 'inline'; |
| 15 | |
| 16 | /** |
| 17 | * @var string |
| 18 | */ |
| 19 | private $location; |
| 20 | |
| 21 | /** |
| 22 | * @since 2.0.0 |
| 23 | */ |
| 24 | public static function aboveHeader(): self |
| 25 | { |
| 26 | return new self(self::ABOVE_HEADER); |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * An alias of aboveHeader(), as this is the standard WordPress location for admin notices. |
| 31 | * |
| 32 | * @since 2.0.0 |
| 33 | */ |
| 34 | public static function standard(): self |
| 35 | { |
| 36 | return self::belowHeader(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * @since 2.0.0 |
| 41 | */ |
| 42 | public static function belowHeader(): self |
| 43 | { |
| 44 | return new self(self::BELOW_HEADER); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * @since 2.0.0 |
| 49 | */ |
| 50 | public static function inline(): self |
| 51 | { |
| 52 | return new self(self::INLINE); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @since 2.0.0 |
| 57 | */ |
| 58 | public function __construct(string $location) |
| 59 | { |
| 60 | $this->validateLocation($location); |
| 61 | |
| 62 | $this->location = $location; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @since 2.0.0 |
| 67 | */ |
| 68 | public function __toString() |
| 69 | { |
| 70 | return $this->location; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * @since 2.0.0 |
| 75 | */ |
| 76 | public function isAboveHeader(): bool |
| 77 | { |
| 78 | return $this->location === self::ABOVE_HEADER; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @since 2.0.0 |
| 83 | */ |
| 84 | public function isBelowHeader(): bool |
| 85 | { |
| 86 | return $this->location === self::BELOW_HEADER; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * An alias of isAboveHeader(), as this is the standard WordPress location for admin notices. |
| 91 | * |
| 92 | * @since 2.0.0 |
| 93 | */ |
| 94 | public function isStandard(): bool |
| 95 | { |
| 96 | return $this->isBelowHeader(); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @since 2.0.0 |
| 101 | */ |
| 102 | public function isInline(): bool |
| 103 | { |
| 104 | return $this->location === self::INLINE; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * @since 2.0.0 |
| 109 | * |
| 110 | * @param string|self $location |
| 111 | */ |
| 112 | public function equals($location): bool |
| 113 | { |
| 114 | if (!(is_string($location) || $location instanceof self)) { |
| 115 | throw new InvalidArgumentException( |
| 116 | 'Invalid location: ' . gettype($location) . ' - must be a string or an instance of ' . self::class |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | return $location instanceof self |
| 121 | ? $this->location === $location->location |
| 122 | : $this->location === $location; |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * @since 2.0.0 |
| 127 | */ |
| 128 | private function validateLocation(string $location) |
| 129 | { |
| 130 | $validLocations = [ |
| 131 | self::ABOVE_HEADER, |
| 132 | self::BELOW_HEADER, |
| 133 | self::INLINE, |
| 134 | ]; |
| 135 | |
| 136 | if (!in_array($location, $validLocations, true)) { |
| 137 | throw new InvalidArgumentException( |
| 138 | "Invalid location: $location - must be one of " . implode(', ', $validLocations) |
| 139 | ); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 |