-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Atk4\Data\Model; | ||
|
||
use Atk4\Core\WarnDynamicPropertyTrait; | ||
use Atk4\Data\Field; | ||
use Atk4\Data\Model; | ||
use Atk4\Ui\Demos\WrappedId; | ||
|
||
/** | ||
* @template-covariant TModel of Model | ||
* @template-covariant TField of Field | ||
*/ | ||
class EntityFieldPair | ||
{ | ||
use WarnDynamicPropertyTrait; | ||
|
||
/** @var TModel */ | ||
private $entity; | ||
/** @var string */ | ||
private $fieldName; | ||
|
||
/** | ||
* @param ModelWithPrefixedFields $entity | ||
*/ | ||
public function __construct(Model $entity, string $fieldName) | ||
Check failure on line 28 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 28 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 28 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
|
||
{ | ||
$entity->assertIsEntity(); | ||
|
||
$this->entity = $entity; | ||
$this->fieldName = $fieldName; | ||
} | ||
|
||
/** | ||
* @return ModelWithPrefixedFields | ||
*/ | ||
public function getModel(): Model | ||
Check failure on line 39 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 39 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 39 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 39 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
|
||
{ | ||
return $this->entity->getModel(); | ||
} | ||
|
||
/** | ||
* @return ModelWithPrefixedFields | ||
*/ | ||
public function getEntity(): Model | ||
Check failure on line 47 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 47 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 47 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
Check failure on line 47 in demos/EntityFieldPair.php GitHub Actions / Smoke (latest, StaticAnalysis)
|
||
{ | ||
return $this->entity; | ||
} | ||
|
||
public function getFieldName(): string | ||
{ | ||
return $this->fieldName; | ||
} | ||
|
||
/** | ||
* @phpstan-return TField | ||
*/ | ||
public function getField(): Field | ||
{ | ||
$field = $this->getModel()->getField($this->getFieldName()); | ||
|
||
return $field; | ||
} | ||
|
||
/** | ||
* @return WrappedId|null | ||
*/ | ||
public function get() | ||
{ | ||
return $this->getEntity()->get($this->getFieldName()); | ||
} | ||
|
||
/** | ||
* @param WrappedId $value | ||
*/ | ||
public function set($value): void | ||
{ | ||
$this->getEntity()->set($this->getFieldName(), $value); | ||
} | ||
|
||
public function setNull(): void | ||
{ | ||
$this->getEntity()->setNull($this->getFieldName()); | ||
} | ||
} |