| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\FieldsAPI; |
| 4 |
|
| 5 |
use Give\Framework\FieldsAPI\Contracts\Node; |
| 6 |
use Give\Framework\FieldsAPI\Exceptions\EmptyNameException; |
| 7 |
use Give\Framework\FieldsAPI\ValueObjects\PersistenceScope; |
| 8 |
use Give\Vendors\StellarWP\Validation\Concerns\HasValidationRules; |
| 9 |
|
| 10 |
/** |
| 11 |
* @since 2.27.3 add ShowInAdmin, ShowInReceipt, StoreAsMeta |
| 12 |
* @since 2.17.0 allow fields to be macroable |
| 13 |
* @since 2.12.0 |
| 14 |
* @since 2.13.0 Support visibility conditions |
| 15 |
* @since 2.22.0 Add TapNode trait |
| 16 |
*/ |
| 17 |
abstract class Field implements Node |
| 18 |
{ |
| 19 |
use Concerns\HasDefaultValue; |
| 20 |
use Concerns\HasName; |
| 21 |
use Concerns\HasType; |
| 22 |
use Concerns\HasPersistence; |
| 23 |
use Concerns\IsReadOnly; |
| 24 |
use Concerns\IsRequired; |
| 25 |
use Concerns\Macroable; |
| 26 |
use Concerns\SerializeAsJson; |
| 27 |
use Concerns\TapNode; |
| 28 |
use Concerns\ShowInAdmin; |
| 29 |
use Concerns\ShowInReceipt; |
| 30 |
use Concerns\HasVisibilityConditions { |
| 31 |
Concerns\HasVisibilityConditions::__construct as private __visibilityConditionsConstruct; |
| 32 |
} |
| 33 |
use HasValidationRules { |
| 34 |
HasValidationRules::__construct as private __validationRulesConstruct; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* @since 2.32.0 sets the default scope to donation |
| 39 |
* @since 2.23.1 make constructor final to avoid unsafe usage of `new static()`. |
| 40 |
* @since 2.12.0 |
| 41 |
* |
| 42 |
* @throws EmptyNameException |
| 43 |
*/ |
| 44 |
final public function __construct(string $name) |
| 45 |
{ |
| 46 |
if (!$name) { |
| 47 |
throw new EmptyNameException(); |
| 48 |
} |
| 49 |
|
| 50 |
$this->name = $name; |
| 51 |
$this->scope = PersistenceScope::donation(); |
| 52 |
$this->__validationRulesConstruct(); |
| 53 |
$this->__visibilityConditionsConstruct(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* @inheritDoc |
| 58 |
*/ |
| 59 |
public function getNodeType(): string |
| 60 |
{ |
| 61 |
return 'field'; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Create a named field. |
| 66 |
* |
| 67 |
* @since 2.12.0 |
| 68 |
* |
| 69 |
* @return static |
| 70 |
* @throws EmptyNameException |
| 71 |
*/ |
| 72 |
public static function make(string $name): self |
| 73 |
{ |
| 74 |
if (!$name) { |
| 75 |
throw new EmptyNameException(); |
| 76 |
} |
| 77 |
|
| 78 |
return new static($name); |
| 79 |
} |
| 80 |
} |
| 81 |
|