| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\Framework\FieldsAPI; |
| 4 |
|
| 5 |
use Give\Framework\FieldsAPI\Concerns\ValidationRules; |
| 6 |
use Give\Framework\FieldsAPI\Contracts\Node; |
| 7 |
use Give\Framework\FieldsAPI\Exceptions\EmptyNameException; |
| 8 |
|
| 9 |
/** |
| 10 |
* @since 2.17.0 allow fields to be macroable |
| 11 |
* @since 2.12.0 |
| 12 |
* @since 2.13.0 Support visibility conditions |
| 13 |
*/ |
| 14 |
abstract class Field implements Node |
| 15 |
{ |
| 16 |
|
| 17 |
use Concerns\HasDefaultValue; |
| 18 |
use Concerns\HasName; |
| 19 |
use Concerns\HasType; |
| 20 |
use Concerns\HasVisibilityConditions; |
| 21 |
use Concerns\IsReadOnly; |
| 22 |
use Concerns\IsRequired; |
| 23 |
use Concerns\Macroable; |
| 24 |
use Concerns\SerializeAsJson; |
| 25 |
|
| 26 |
/** @var ValidationRules */ |
| 27 |
protected $validationRules; |
| 28 |
|
| 29 |
/** |
| 30 |
* @since 2.12.0 |
| 31 |
* |
| 32 |
* @param string $name |
| 33 |
* |
| 34 |
* @throws EmptyNameException |
| 35 |
*/ |
| 36 |
public function __construct($name) |
| 37 |
{ |
| 38 |
if ( ! $name) { |
| 39 |
throw new EmptyNameException(); |
| 40 |
} |
| 41 |
|
| 42 |
$this->name = $name; |
| 43 |
$this->validationRules = new ValidationRules(); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Create a named field. |
| 48 |
* |
| 49 |
* @since 2.12.0 |
| 50 |
* |
| 51 |
* @param string $name |
| 52 |
* |
| 53 |
* @return static |
| 54 |
* @throws EmptyNameException |
| 55 |
*/ |
| 56 |
public static function make($name) |
| 57 |
{ |
| 58 |
if ( ! $name) { |
| 59 |
throw new EmptyNameException(); |
| 60 |
} |
| 61 |
|
| 62 |
return new static($name); |
| 63 |
} |
| 64 |
} |
| 65 |
|