mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
link vo new methods
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user