| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Orm; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
|
| 7 |
class ColumnDefinition |
| 8 |
{ |
| 9 |
/** @var string The name of the column */ |
| 10 |
protected string $name; |
| 11 |
|
| 12 |
/** @var ?string The label of the column */ |
| 13 |
protected ?string $label = null; |
| 14 |
|
| 15 |
/** |
| 16 |
* Create a new column definition |
| 17 |
* |
| 18 |
* @param string $name |
| 19 |
*/ |
| 20 |
public function __construct(string $name) |
| 21 |
{ |
| 22 |
$this->name = $name; |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Get the column name |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public function getName(): string |
| 31 |
{ |
| 32 |
return $this->name; |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Get the column label |
| 37 |
* |
| 38 |
* @return ?string |
| 39 |
*/ |
| 40 |
public function getLabel(): ?string |
| 41 |
{ |
| 42 |
return $this->label; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Set the column label |
| 47 |
* |
| 48 |
* @param ?string $label |
| 49 |
* |
| 50 |
* @return $this |
| 51 |
*/ |
| 52 |
public function setLabel(?string $label): static |
| 53 |
{ |
| 54 |
$this->label = $label; |
| 55 |
|
| 56 |
return $this; |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Create a new column definition based on the given options |
| 61 |
* |
| 62 |
* @param array $options |
| 63 |
* |
| 64 |
* @return static |
| 65 |
* |
| 66 |
* @throws InvalidArgumentException If the given options do not provide a name |
| 67 |
*/ |
| 68 |
public static function fromArray(array $options): static |
| 69 |
{ |
| 70 |
if (! isset($options['name'])) { |
| 71 |
throw new InvalidArgumentException('$options must provide a name'); |
| 72 |
} |
| 73 |
|
| 74 |
$self = new static($options['name']); |
| 75 |
if (isset($options['label'])) { |
| 76 |
$self->setLabel($options['label']); |
| 77 |
} |
| 78 |
|
| 79 |
return $self; |
| 80 |
} |
| 81 |
} |