| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\FieldsAPI\Concerns; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Give\Framework\Exceptions\Primitives\InvalidArgumentException; |
| 7 |
use Give\Framework\FieldsAPI\ValueObjects\PersistenceScope; |
| 8 |
|
| 9 |
/** |
| 10 |
* This provides the ability to set a scope and meta key for a field. The scope is used to determine if and where the |
| 11 |
* field should be stored. The meta key is used to store the field in the database. |
| 12 |
* |
| 13 |
* @since 2.32.0 |
| 14 |
*/ |
| 15 |
trait HasPersistence |
| 16 |
{ |
| 17 |
/** |
| 18 |
* @var Closure |
| 19 |
*/ |
| 20 |
private $scopeCallback; |
| 21 |
|
| 22 |
/** |
| 23 |
* @since 2.32.0 |
| 24 |
* |
| 25 |
* @var PersistenceScope|null |
| 26 |
*/ |
| 27 |
protected $scope; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 2.32.0 |
| 31 |
* |
| 32 |
* @var string|null |
| 33 |
*/ |
| 34 |
protected $metaKey = null; |
| 35 |
|
| 36 |
/** |
| 37 |
* @since 2.32.0 |
| 38 |
* |
| 39 |
* @param string|PersistenceScope|Closure $scope |
| 40 |
*/ |
| 41 |
public function scope($scope): self |
| 42 |
{ |
| 43 |
if ($scope instanceof Closure) { |
| 44 |
$this->scopeCallback = $scope; |
| 45 |
$scope = PersistenceScope::callback(); |
| 46 |
} elseif (is_string($scope)) { |
| 47 |
$scope = new PersistenceScope($scope); |
| 48 |
} elseif (!$scope instanceof PersistenceScope) { |
| 49 |
throw new InvalidArgumentException('Scope must be a string, Closure, or PersistenceScope'); |
| 50 |
} |
| 51 |
|
| 52 |
$this->scope = $scope; |
| 53 |
|
| 54 |
return $this; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* @since 2.32.0 |
| 59 |
*/ |
| 60 |
public function getScope(): PersistenceScope |
| 61 |
{ |
| 62 |
return $this->scope; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* @since 2.32.0 |
| 67 |
*/ |
| 68 |
public function getScopeValue(): string |
| 69 |
{ |
| 70 |
return (string)$this->scope; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* @return Closure|null |
| 75 |
*/ |
| 76 |
public function getScopeCallback() |
| 77 |
{ |
| 78 |
return $this->scopeCallback; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* @since 2.32.0 |
| 83 |
*/ |
| 84 |
public function metaKey(string $metaKey): self |
| 85 |
{ |
| 86 |
$this->metaKey = $metaKey; |
| 87 |
|
| 88 |
return $this; |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* @since 2.32.0 |
| 93 |
* |
| 94 |
* @return string|null |
| 95 |
*/ |
| 96 |
public function getMetaKey() |
| 97 |
{ |
| 98 |
return $this->metaKey; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* @since 2.32.0 updated to use scoping under the hood, no signature change |
| 103 |
* @since 2.28.0 added types |
| 104 |
* @since 2.10.2 |
| 105 |
*/ |
| 106 |
public function storeAsDonorMeta(bool $storeAsDonorMeta = true) |
| 107 |
{ |
| 108 |
$this->scope = $storeAsDonorMeta |
| 109 |
? PersistenceScope::donor() |
| 110 |
: PersistenceScope::donation(); |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @since 2.32.0 updated to use scoping under the hood, no signature change |
| 117 |
* @since 2.28.0 added types |
| 118 |
* @since 2.10.2 |
| 119 |
*/ |
| 120 |
public function shouldStoreAsDonorMeta(): bool |
| 121 |
{ |
| 122 |
return $this->scope->isDonor(); |
| 123 |
} |
| 124 |
} |
| 125 |
|