link vo new methods

This commit is contained in:
Yurii
2026-03-03 11:16:35 +02:00
parent 10ccbe4709
commit 7537fd25fd
3 changed files with 61 additions and 1 deletions

View File

@@ -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());
}
}

View File

@@ -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);

View File

@@ -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());
}
}