| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace Atk4\Data\Model; |
| 6: | |
| 7: | use Atk4\Core\WarnDynamicPropertyTrait; |
| 8: | use Atk4\Data\Field; |
| 9: | use Atk4\Data\Model; |
| 10: | |
| 11: | /** |
| 12: | * @template-covariant TModel of Model |
| 13: | * @template-covariant TField of Field |
| 14: | */ |
| 15: | class EntityFieldPair |
| 16: | { |
| 17: | use WarnDynamicPropertyTrait; |
| 18: | |
| 19: | /** @var TModel */ |
| 20: | private $entity; |
| 21: | /** @var string */ |
| 22: | private $fieldName; |
| 23: | |
| 24: | /** |
| 25: | * @param TModel $entity |
| 26: | */ |
| 27: | public function __construct(Model $entity, string $fieldName) |
| 28: | { |
| 29: | $entity->assertIsEntity(); |
| 30: | |
| 31: | $this->entity = $entity; |
| 32: | $this->fieldName = $fieldName; |
| 33: | } |
| 34: | |
| 35: | /** |
| 36: | * @return TModel |
| 37: | */ |
| 38: | public function getModel(): Model |
| 39: | { |
| 40: | return $this->entity->getModel(); |
| 41: | } |
| 42: | |
| 43: | /** |
| 44: | * @return TModel |
| 45: | */ |
| 46: | public function getEntity(): Model |
| 47: | { |
| 48: | return $this->entity; |
| 49: | } |
| 50: | |
| 51: | public function getFieldName(): string |
| 52: | { |
| 53: | return $this->fieldName; |
| 54: | } |
| 55: | |
| 56: | /** |
| 57: | * @phpstan-return TField |
| 58: | */ |
| 59: | public function getField(): Field |
| 60: | { |
| 61: | $field = $this->getModel()->getField($this->getFieldName()); |
| 62: | |
| 63: | return $field; |
| 64: | } |
| 65: | |
| 66: | /** |
| 67: | * @return mixed |
| 68: | */ |
| 69: | public function get() |
| 70: | { |
| 71: | return $this->getEntity()->get($this->getFieldName()); |
| 72: | } |
| 73: | |
| 74: | /** |
| 75: | * @param mixed $value |
| 76: | */ |
| 77: | public function set($value): void |
| 78: | { |
| 79: | $this->getEntity()->set($this->getFieldName(), $value); |
| 80: | } |
| 81: | |
| 82: | public function setNull(): void |
| 83: | { |
| 84: | $this->getEntity()->setNull($this->getFieldName()); |
| 85: | } |
| 86: | } |
| 87: |