diff --git a/application/Espo/Core/Field/Link.php b/application/Espo/Core/Field/Link.php index c2200ef1e8..b5ef502624 100644 --- a/application/Espo/Core/Field/Link.php +++ b/application/Espo/Core/Field/Link.php @@ -29,6 +29,8 @@ namespace Espo\Core\Field; +use Espo\Core\Name\Field; +use Espo\ORM\Entity; use InvalidArgumentException; /** @@ -39,6 +41,9 @@ class Link private string $id; private ?string $name = null; + /** + * @throws InvalidArgumentException + */ public function __construct(string $id) { if (!$id) { @@ -78,9 +83,23 @@ class Link /** * Create from an ID. + * + * @throws InvalidArgumentException */ public static function create(string $id, ?string $name = null): self { return (new self($id))->withName($name); } + + /** + * Create from an entity. + * + * @throws InvalidArgumentException + * + * @since 9.4.0 + */ + public static function fromEntity(Entity $entity): self + { + return self::create($entity->getId()); + } } diff --git a/application/Espo/Core/Field/LinkMultipleItem.php b/application/Espo/Core/Field/LinkMultipleItem.php index 92cdc267b6..a559f42993 100644 --- a/application/Espo/Core/Field/LinkMultipleItem.php +++ b/application/Espo/Core/Field/LinkMultipleItem.php @@ -29,6 +29,8 @@ namespace Espo\Core\Field; +use Espo\Core\Name\Field; +use Espo\ORM\Entity; use InvalidArgumentException; /** @@ -99,8 +101,10 @@ class LinkMultipleItem /** * Clone with a name. + * + * @param ?string $name Is nullable since 9.4.0. */ - public function withName(string $name): self + public function withName(?string $name): self { $obj = $this->clone(); $obj->name = $name; @@ -134,6 +138,30 @@ class LinkMultipleItem return $obj; } + /** + * Create from a link. + * + * @throws InvalidArgumentException + * + * @since 9.4.0 + */ + public static function fromLink(Link $link): self + { + return self::create($link->getId(), $link->getName()); + } + + /** + * Create from an entity. + * + * @throws InvalidArgumentException + * + * @since 9.4.0 + */ + public static function fromEntity(Entity $entity): self + { + return self::create($entity->getId()); + } + private function clone(): self { $obj = new self($this->id); diff --git a/application/Espo/Core/Field/LinkParent.php b/application/Espo/Core/Field/LinkParent.php index 4f58122335..6d8974925c 100644 --- a/application/Espo/Core/Field/LinkParent.php +++ b/application/Espo/Core/Field/LinkParent.php @@ -103,9 +103,22 @@ class LinkParent /** * Create from an entity. + * + * @deprecated Since 9.4.0. Use `fromEntity`. + * @todo Remove in v12.0. */ public static function createFromEntity(Entity $entity): self { return new self($entity->getEntityType(), $entity->getId()); } + + /** + * Create from an entity. + * + * @since 9.4.0 + */ + public static function fromEntity(Entity $entity): self + { + return new self($entity->getEntityType(), $entity->getId()); + } }