PersistenceScope.php
118 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\Framework\FieldsAPI\ValueObjects; |
| 4 | |
| 5 | /** |
| 6 | * The scope of the field for use in persistence. The two built-in scopes are donation and donor, but the scope may be |
| 7 | * any custom string. Using a custom scope allows for an add-on to either not store the field, or store it in a custom |
| 8 | * location. |
| 9 | * |
| 10 | * @since 2.32.0 |
| 11 | */ |
| 12 | class PersistenceScope |
| 13 | { |
| 14 | public const DONATION = 'donation'; |
| 15 | public const SUBSCRIPTION = 'subscription'; |
| 16 | public const DONOR = 'donor'; |
| 17 | public const CALLBACK = 'callback'; |
| 18 | |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | private $scope; |
| 23 | |
| 24 | /** |
| 25 | * @since 2.32.0 |
| 26 | */ |
| 27 | public static function donation(): self |
| 28 | { |
| 29 | return new self(self::DONATION); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * @since 2.32.0 |
| 34 | */ |
| 35 | public static function donor(): self |
| 36 | { |
| 37 | return new self(self::DONOR); |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * @since 3.0.0 |
| 42 | */ |
| 43 | public static function subscription(): self |
| 44 | { |
| 45 | return new self(self::SUBSCRIPTION); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * @since 2.32.0 |
| 50 | */ |
| 51 | public static function callback(): self |
| 52 | { |
| 53 | return new self(self::CALLBACK); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @since 2.32.0 |
| 58 | */ |
| 59 | public function __construct(string $scope) |
| 60 | { |
| 61 | $this->scope = $scope; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @since 2.32.0 |
| 66 | */ |
| 67 | public function isDonation(): bool |
| 68 | { |
| 69 | return $this->scope === self::DONATION; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @since 3.0.0 |
| 74 | */ |
| 75 | public function isSubscription(): bool |
| 76 | { |
| 77 | return $this->scope === self::SUBSCRIPTION; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @since 2.32.0 |
| 82 | */ |
| 83 | public function isDonor(): bool |
| 84 | { |
| 85 | return $this->scope === self::DONOR; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * @since 2.32.0 |
| 90 | */ |
| 91 | public function isCallback(): bool |
| 92 | { |
| 93 | return $this->scope === self::CALLBACK; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @since 2.32.0 |
| 98 | * |
| 99 | * @param self|string $scope |
| 100 | */ |
| 101 | public function is($scope): bool |
| 102 | { |
| 103 | if ($scope instanceof self) { |
| 104 | $scope = $scope->scope; |
| 105 | } |
| 106 | |
| 107 | return $this->scope === $scope; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @since 2.32.0 |
| 112 | */ |
| 113 | public function __toString() |
| 114 | { |
| 115 | return $this->scope; |
| 116 | } |
| 117 | } |
| 118 |