| 1 |
<?php |
| 2 |
|
| 3 |
namespace ipl\Orm; |
| 4 |
|
| 5 |
use IteratorAggregate; |
| 6 |
use Traversable; |
| 7 |
|
| 8 |
class Defaults implements IteratorAggregate |
| 9 |
{ |
| 10 |
/** @var array<string, mixed> Registered defaults */ |
| 11 |
protected array $defaults = []; |
| 12 |
|
| 13 |
/** |
| 14 |
* Iterate over the defaults |
| 15 |
* |
| 16 |
* @return Traversable |
| 17 |
*/ |
| 18 |
public function getIterator(): Traversable |
| 19 |
{ |
| 20 |
foreach ($this->defaults as $column => $default) { |
| 21 |
yield $column => $default; |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Add a default for the given property |
| 27 |
* |
| 28 |
* @param string $property |
| 29 |
* @param mixed $default If it's a closure, its interface is assumed to be |
| 30 |
* ({@see Model} $subject, string $propertyName) |
| 31 |
* |
| 32 |
* @return $this |
| 33 |
*/ |
| 34 |
public function add(string $property, mixed $default): static |
| 35 |
{ |
| 36 |
$this->defaults[$property] = $default; |
| 37 |
|
| 38 |
return $this; |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Get whether a default for the given property exists |
| 43 |
* |
| 44 |
* @param string $property |
| 45 |
* |
| 46 |
* @return bool |
| 47 |
*/ |
| 48 |
public function has(string $property): bool |
| 49 |
{ |
| 50 |
return array_key_exists($property, $this->defaults); |
| 51 |
} |
| 52 |
} |