Initial commit
This commit is contained in:
115
application/Espo/ORM/Defs/AttributeDefs.php
Normal file
115
application/Espo/ORM/Defs/AttributeDefs.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs;
|
||||
|
||||
use Espo\ORM\Defs\Params\AttributeParam;
|
||||
|
||||
/**
|
||||
* Attribute definitions.
|
||||
*/
|
||||
class AttributeDefs
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
private array $data;
|
||||
private string $name;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $raw
|
||||
*/
|
||||
public static function fromRaw(array $raw, string $name): self
|
||||
{
|
||||
$obj = new self();
|
||||
$obj->data = $raw;
|
||||
$obj->name = $name;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a name.
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a type.
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
return $this->data[AttributeParam::TYPE];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a length.
|
||||
*/
|
||||
public function getLength(): ?int
|
||||
{
|
||||
return $this->data[AttributeParam::LEN] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is not-storable. Not-storable attributes are not stored in DB.
|
||||
*/
|
||||
public function isNotStorable(): bool
|
||||
{
|
||||
return $this->data[AttributeParam::NOT_STORABLE] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is auto-increment.
|
||||
*/
|
||||
public function isAutoincrement(): bool
|
||||
{
|
||||
return $this->data[AttributeParam::AUTOINCREMENT] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a parameter is set.
|
||||
*/
|
||||
public function hasParam(string $name): bool
|
||||
{
|
||||
return array_key_exists($name, $this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a parameter value by a name.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getParam(string $name)
|
||||
{
|
||||
return $this->data[$name] ?? null;
|
||||
}
|
||||
}
|
||||
99
application/Espo/ORM/Defs/Defs.php
Normal file
99
application/Espo/ORM/Defs/Defs.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Definitions.
|
||||
*/
|
||||
class Defs
|
||||
{
|
||||
public function __construct(private DefsData $data)
|
||||
{}
|
||||
|
||||
/**
|
||||
* Get an entity type list.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getEntityTypeList(): array
|
||||
{
|
||||
return $this->data->getEntityTypeList();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an entity definitions list.
|
||||
*
|
||||
* @return EntityDefs[]
|
||||
*/
|
||||
public function getEntityList(): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($this->getEntityTypeList() as $name) {
|
||||
$list[] = $this->getEntity($name);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has an entity type.
|
||||
*/
|
||||
public function hasEntity(string $entityType): bool
|
||||
{
|
||||
return $this->data->hasEntity($entityType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get entity definitions.
|
||||
*/
|
||||
public function getEntity(string $entityType): EntityDefs
|
||||
{
|
||||
if (!$this->hasEntity($entityType)) {
|
||||
throw new RuntimeException("Entity type '{$entityType}' does not exist.");
|
||||
}
|
||||
|
||||
return $this->data->getEntity($entityType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get entity definitions, if an entity type does not exist, then return null.
|
||||
*/
|
||||
public function tryGetEntity(string $entityType): ?EntityDefs
|
||||
{
|
||||
if (!$this->hasEntity($entityType)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getEntity($entityType);
|
||||
}
|
||||
}
|
||||
95
application/Espo/ORM/Defs/DefsData.php
Normal file
95
application/Espo/ORM/Defs/DefsData.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs;
|
||||
|
||||
use Espo\ORM\Metadata;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class DefsData
|
||||
{
|
||||
/** @var array<string, ?EntityDefs> */
|
||||
private array $cache = [];
|
||||
|
||||
public function __construct(private Metadata $metadata)
|
||||
{}
|
||||
|
||||
public function clearCache(): void
|
||||
{
|
||||
$this->cache = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getEntityTypeList(): array
|
||||
{
|
||||
return $this->metadata->getEntityTypeList();
|
||||
}
|
||||
|
||||
public function hasEntity(string $name): bool
|
||||
{
|
||||
$this->cacheEntity($name);
|
||||
|
||||
return !is_null($this->cache[$name]);
|
||||
}
|
||||
|
||||
public function getEntity(string $name): EntityDefs
|
||||
{
|
||||
$this->cacheEntity($name);
|
||||
|
||||
if (!$this->hasEntity($name)) {
|
||||
throw new RuntimeException("Entity type '{$name}' does not exist.");
|
||||
}
|
||||
|
||||
/** @var EntityDefs */
|
||||
return $this->cache[$name];
|
||||
}
|
||||
|
||||
private function cacheEntity(string $name): void
|
||||
{
|
||||
if (array_key_exists($name, $this->cache)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->cache[$name] = $this->loadEntity($name);
|
||||
}
|
||||
|
||||
private function loadEntity(string $name): ?EntityDefs
|
||||
{
|
||||
$raw = $this->metadata->get($name) ?? null;
|
||||
|
||||
if (!$raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return EntityDefs::fromRaw($raw, $name);
|
||||
}
|
||||
}
|
||||
431
application/Espo/ORM/Defs/EntityDefs.php
Normal file
431
application/Espo/ORM/Defs/EntityDefs.php
Normal file
@@ -0,0 +1,431 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs;
|
||||
|
||||
use Espo\ORM\Defs\Params\EntityParam;
|
||||
use RuntimeException;
|
||||
|
||||
class EntityDefs
|
||||
{
|
||||
/** @var array<string, array<string, mixed>|mixed> */
|
||||
private array $data;
|
||||
private string $name;
|
||||
/** @var array<string, ?AttributeDefs> */
|
||||
private $attributeCache = [];
|
||||
/** @var array<string, ?RelationDefs> */
|
||||
private $relationCache = [];
|
||||
/** @var array<string, ?IndexDefs> */
|
||||
private $indexCache = [];
|
||||
/** @var array<string, ?FieldDefs> */
|
||||
private $fieldCache = [];
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $raw
|
||||
*/
|
||||
public static function fromRaw(array $raw, string $name): self
|
||||
{
|
||||
$obj = new self();
|
||||
$obj->data = $raw;
|
||||
$obj->name = $name;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an entity name (entity type).
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an attribute name list.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAttributeNameList(): array
|
||||
{
|
||||
/** @var string[] */
|
||||
return array_keys($this->data[EntityParam::ATTRIBUTES] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a relation name list.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getRelationNameList(): array
|
||||
{
|
||||
/** @var string[] */
|
||||
return array_keys($this->data[EntityParam::RELATIONS] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an index name list.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getIndexNameList(): array
|
||||
{
|
||||
/** @var string[] */
|
||||
return array_keys($this->data[EntityParam::INDEXES] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a field name list.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getFieldNameList(): array
|
||||
{
|
||||
/** @var string[] */
|
||||
return array_keys($this->data[EntityParam::FIELDS] ?? []);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an attribute definitions list.
|
||||
*
|
||||
* @return AttributeDefs[]
|
||||
*/
|
||||
public function getAttributeList(): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($this->getAttributeNameList() as $name) {
|
||||
$list[] = $this->getAttribute($name);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a relation definitions list.
|
||||
*
|
||||
* @return RelationDefs[]
|
||||
*/
|
||||
public function getRelationList(): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($this->getRelationNameList() as $name) {
|
||||
$list[] = $this->getRelation($name);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an index definitions list.
|
||||
*
|
||||
* @return IndexDefs[]
|
||||
*/
|
||||
public function getIndexList(): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($this->getIndexNameList() as $name) {
|
||||
$list[] = $this->getIndex($name);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a field definitions list.
|
||||
*
|
||||
* @return FieldDefs[]
|
||||
*/
|
||||
public function getFieldList(): array
|
||||
{
|
||||
$list = [];
|
||||
|
||||
foreach ($this->getFieldNameList() as $name) {
|
||||
$list[] = $this->getField($name);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has an attribute.
|
||||
*/
|
||||
public function hasAttribute(string $name): bool
|
||||
{
|
||||
$this->cacheAttribute($name);
|
||||
|
||||
return !is_null($this->attributeCache[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Has a relation.
|
||||
*/
|
||||
public function hasRelation(string $name): bool
|
||||
{
|
||||
$this->cacheRelation($name);
|
||||
|
||||
return !is_null($this->relationCache[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Has an index.
|
||||
*/
|
||||
public function hasIndex(string $name): bool
|
||||
{
|
||||
$this->cacheIndex($name);
|
||||
|
||||
return !is_null($this->indexCache[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Has a field.
|
||||
*/
|
||||
public function hasField(string $name): bool
|
||||
{
|
||||
$this->cacheField($name);
|
||||
|
||||
return !is_null($this->fieldCache[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get attribute definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getAttribute(string $name): AttributeDefs
|
||||
{
|
||||
$this->cacheAttribute($name);
|
||||
|
||||
if (!$this->hasAttribute($name)) {
|
||||
throw new RuntimeException("Attribute '{$name}' does not exist.");
|
||||
}
|
||||
|
||||
/** @var AttributeDefs */
|
||||
return $this->attributeCache[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get relation definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getRelation(string $name): RelationDefs
|
||||
{
|
||||
$this->cacheRelation($name);
|
||||
|
||||
if (!$this->hasRelation($name)) {
|
||||
throw new RuntimeException("Relation '{$name}' does not exist.");
|
||||
}
|
||||
|
||||
/** @var RelationDefs */
|
||||
return $this->relationCache[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get index definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getIndex(string $name): IndexDefs
|
||||
{
|
||||
$this->cacheIndex($name);
|
||||
|
||||
if (!$this->hasIndex($name)) {
|
||||
throw new RuntimeException("Index '{$name}' does not exist.");
|
||||
}
|
||||
|
||||
/** @var IndexDefs */
|
||||
return $this->indexCache[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get field definitions.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getField(string $name): FieldDefs
|
||||
{
|
||||
$this->cacheField($name);
|
||||
|
||||
if (!$this->hasField($name)) {
|
||||
throw new RuntimeException("Field '{$name}' does not exist.");
|
||||
}
|
||||
|
||||
/** @var FieldDefs */
|
||||
return $this->fieldCache[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get attribute definitions.
|
||||
*/
|
||||
public function tryGetAttribute(string $name): ?AttributeDefs
|
||||
{
|
||||
if (!$this->hasAttribute($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getAttribute($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get field definitions.
|
||||
*/
|
||||
public function tryGetField(string $name): ?FieldDefs
|
||||
{
|
||||
if (!$this->hasField($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getField($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get relation definitions.
|
||||
*/
|
||||
public function tryGetRelation(string $name): ?RelationDefs
|
||||
{
|
||||
if (!$this->hasRelation($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getRelation($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get index definitions.
|
||||
*/
|
||||
public function tryGetIndex(string $name): ?IndexDefs
|
||||
{
|
||||
if (!$this->hasIndex($name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getIndex($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a parameter is set.
|
||||
*/
|
||||
public function hasParam(string $name): bool
|
||||
{
|
||||
return array_key_exists($name, $this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a parameter value by a name.
|
||||
*/
|
||||
public function getParam(string $name): mixed
|
||||
{
|
||||
return $this->data[$name] ?? null;
|
||||
}
|
||||
|
||||
private function cacheAttribute(string $name): void
|
||||
{
|
||||
if (array_key_exists($name, $this->attributeCache)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->attributeCache[$name] = $this->loadAttribute($name);
|
||||
}
|
||||
|
||||
private function loadAttribute(string $name): ?AttributeDefs
|
||||
{
|
||||
$raw = $this->data[EntityParam::ATTRIBUTES][$name] ?? null;
|
||||
|
||||
if (!$raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return AttributeDefs::fromRaw($raw, $name);
|
||||
}
|
||||
|
||||
private function cacheRelation(string $name): void
|
||||
{
|
||||
if (array_key_exists($name, $this->relationCache)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->relationCache[$name] = $this->loadRelation($name);
|
||||
}
|
||||
|
||||
private function loadRelation(string $name): ?RelationDefs
|
||||
{
|
||||
$raw = $this->data[EntityParam::RELATIONS][$name] ?? null;
|
||||
|
||||
if (!$raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return RelationDefs::fromRaw($raw, $name);
|
||||
}
|
||||
|
||||
private function cacheIndex(string $name): void
|
||||
{
|
||||
if (array_key_exists($name, $this->indexCache)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->indexCache[$name] = $this->loadIndex($name);
|
||||
}
|
||||
|
||||
private function loadIndex(string $name): ?IndexDefs
|
||||
{
|
||||
$raw = $this->data[EntityParam::INDEXES][$name] ?? null;
|
||||
|
||||
if (!$raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return IndexDefs::fromRaw($raw, $name);
|
||||
}
|
||||
|
||||
private function cacheField(string $name): void
|
||||
{
|
||||
if (array_key_exists($name, $this->fieldCache)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->fieldCache[$name] = $this->loadField($name);
|
||||
}
|
||||
|
||||
private function loadField(string $name): ?FieldDefs
|
||||
{
|
||||
$raw = $this->data[EntityParam::FIELDS][$name] ?? null;
|
||||
|
||||
if (!$raw) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return FieldDefs::fromRaw($raw, $name);
|
||||
}
|
||||
}
|
||||
104
application/Espo/ORM/Defs/FieldDefs.php
Normal file
104
application/Espo/ORM/Defs/FieldDefs.php
Normal file
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs;
|
||||
|
||||
use Espo\ORM\Defs\Params\FieldParam;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Field definitions.
|
||||
*/
|
||||
class FieldDefs
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
private array $data;
|
||||
private string $name;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $raw
|
||||
*/
|
||||
public static function fromRaw(array $raw, string $name): self
|
||||
{
|
||||
$obj = new self();
|
||||
$obj->data = $raw;
|
||||
$obj->name = $name;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a name.
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a type.
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
$type = $this->data[FieldParam::TYPE] ?? null;
|
||||
|
||||
if ($type === null) {
|
||||
throw new RuntimeException("Field '$this->name' has no type.");
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is not-storable.
|
||||
*/
|
||||
public function isNotStorable(): bool
|
||||
{
|
||||
return $this->data[FieldParam::NOT_STORABLE] ?? false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a parameter value by a name.
|
||||
*/
|
||||
public function getParam(string $name): mixed
|
||||
{
|
||||
return $this->data[$name] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Has a parameter value.
|
||||
*/
|
||||
public function hasParam(string $name): bool
|
||||
{
|
||||
return array_key_exists($name, $this->data);
|
||||
}
|
||||
}
|
||||
108
application/Espo/ORM/Defs/IndexDefs.php
Normal file
108
application/Espo/ORM/Defs/IndexDefs.php
Normal file
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs;
|
||||
|
||||
use Espo\ORM\Defs\Params\IndexParam;
|
||||
|
||||
/**
|
||||
* Index definitions.
|
||||
*/
|
||||
class IndexDefs
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
private $data;
|
||||
private string $name;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $raw
|
||||
*/
|
||||
public static function fromRaw(array $raw, string $name): self
|
||||
{
|
||||
$obj = new self();
|
||||
$obj->data = $raw;
|
||||
$obj->name = $name;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a name.
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a key.
|
||||
*/
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->data[IndexParam::KEY] ?? '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is unique.
|
||||
*/
|
||||
public function isUnique(): bool
|
||||
{
|
||||
// For bc.
|
||||
if (($this->data['unique'] ?? false)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$type = $this->data[IndexParam::TYPE] ?? null;
|
||||
|
||||
return $type === 'unique';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a column list.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getColumnList(): array
|
||||
{
|
||||
return $this->data[IndexParam::COLUMNS] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a flag list.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getFlagList(): array
|
||||
{
|
||||
return $this->data[IndexParam::FLAGS] ?? [];
|
||||
}
|
||||
}
|
||||
96
application/Espo/ORM/Defs/Params/AttributeParam.php
Normal file
96
application/Espo/ORM/Defs/Params/AttributeParam.php
Normal file
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs\Params;
|
||||
|
||||
/**
|
||||
* An attribute parameter.
|
||||
*/
|
||||
class AttributeParam
|
||||
{
|
||||
/**
|
||||
* A type.
|
||||
*/
|
||||
public const TYPE = 'type';
|
||||
|
||||
/**
|
||||
* Not stored in database.
|
||||
*/
|
||||
public const NOT_STORABLE = 'notStorable';
|
||||
|
||||
/**
|
||||
* A database type.
|
||||
*/
|
||||
public const DB_TYPE = 'dbType';
|
||||
|
||||
/**
|
||||
* A length.
|
||||
*/
|
||||
public const LEN = 'len';
|
||||
|
||||
/**
|
||||
* Not null.
|
||||
*/
|
||||
public const NOT_NULL = 'notNull';
|
||||
|
||||
/**
|
||||
* Autoincrement.
|
||||
*/
|
||||
public const AUTOINCREMENT = 'autoincrement';
|
||||
|
||||
/**
|
||||
* A default value.
|
||||
*/
|
||||
public const DEFAULT = 'default';
|
||||
|
||||
/**
|
||||
* A relation. For foreign attributes.
|
||||
*/
|
||||
public const RELATION = 'relation';
|
||||
|
||||
/**
|
||||
* A foreign attribute name. For foreign attributes.
|
||||
*/
|
||||
public const FOREIGN = 'foreign';
|
||||
|
||||
/**
|
||||
* Precision.
|
||||
*/
|
||||
public const PRECISION = 'precision';
|
||||
|
||||
/**
|
||||
* Scale.
|
||||
*/
|
||||
public const SCALE = 'scale';
|
||||
|
||||
/**
|
||||
* Dependee attributes.
|
||||
*/
|
||||
public const DEPENDEE_ATTRIBUTE_LIST = 'dependeeAttributeList';
|
||||
}
|
||||
56
application/Espo/ORM/Defs/Params/EntityParam.php
Normal file
56
application/Espo/ORM/Defs/Params/EntityParam.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs\Params;
|
||||
|
||||
/**
|
||||
* An entity parameter.
|
||||
*/
|
||||
class EntityParam
|
||||
{
|
||||
/**
|
||||
* Fields.
|
||||
*/
|
||||
public const FIELDS = 'fields';
|
||||
|
||||
/**
|
||||
* Attributes.
|
||||
*/
|
||||
public const ATTRIBUTES = 'attributes';
|
||||
|
||||
/**
|
||||
* Relations.
|
||||
*/
|
||||
public const RELATIONS = 'relations';
|
||||
|
||||
/**
|
||||
* Indexes.
|
||||
*/
|
||||
public const INDEXES = 'indexes';
|
||||
}
|
||||
106
application/Espo/ORM/Defs/Params/FieldParam.php
Normal file
106
application/Espo/ORM/Defs/Params/FieldParam.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs\Params;
|
||||
|
||||
/**
|
||||
* A field parameter.
|
||||
*/
|
||||
class FieldParam
|
||||
{
|
||||
/**
|
||||
* A type.
|
||||
*/
|
||||
public const TYPE = 'type';
|
||||
|
||||
/**
|
||||
* Not stored in database.
|
||||
*/
|
||||
public const NOT_STORABLE = 'notStorable';
|
||||
|
||||
/**
|
||||
* A database type.
|
||||
*/
|
||||
public const DB_TYPE = 'dbType';
|
||||
|
||||
/**
|
||||
* Autoincrement.
|
||||
*/
|
||||
public const AUTOINCREMENT = 'autoincrement';
|
||||
|
||||
/**
|
||||
* A max length.
|
||||
*/
|
||||
public const MAX_LENGTH = 'maxLength';
|
||||
|
||||
/**
|
||||
* Not null.
|
||||
*/
|
||||
public const NOT_NULL = 'notNull';
|
||||
|
||||
/**
|
||||
* A default value.
|
||||
*/
|
||||
public const DEFAULT = 'default';
|
||||
|
||||
/**
|
||||
* Read-only.
|
||||
*/
|
||||
public const READ_ONLY = 'readOnly';
|
||||
|
||||
/**
|
||||
* Decimal.
|
||||
*/
|
||||
public const DECIMAL = 'decimal';
|
||||
|
||||
/**
|
||||
* Precision.
|
||||
*/
|
||||
public const PRECISION = 'precision';
|
||||
|
||||
/**
|
||||
* Scale.
|
||||
*/
|
||||
public const SCALE = 'scale';
|
||||
|
||||
/**
|
||||
* Dependee attributes.
|
||||
*/
|
||||
public const DEPENDEE_ATTRIBUTE_LIST = 'dependeeAttributeList';
|
||||
|
||||
/**
|
||||
* Foreign link.
|
||||
*/
|
||||
public const LINK = 'link';
|
||||
|
||||
/**
|
||||
* Foreign field.
|
||||
*/
|
||||
public const FIELD = 'field';
|
||||
}
|
||||
56
application/Espo/ORM/Defs/Params/IndexParam.php
Normal file
56
application/Espo/ORM/Defs/Params/IndexParam.php
Normal file
@@ -0,0 +1,56 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs\Params;
|
||||
|
||||
/**
|
||||
* An index parameter.
|
||||
*/
|
||||
class IndexParam
|
||||
{
|
||||
/**
|
||||
* A type.
|
||||
*/
|
||||
public const TYPE = 'type';
|
||||
|
||||
/**
|
||||
* A key.
|
||||
*/
|
||||
public const KEY = 'key';
|
||||
|
||||
/**
|
||||
* Columns.
|
||||
*/
|
||||
public const COLUMNS = 'columns';
|
||||
|
||||
/**
|
||||
* Flags.
|
||||
*/
|
||||
public const FLAGS = 'flags';
|
||||
}
|
||||
110
application/Espo/ORM/Defs/Params/RelationParam.php
Normal file
110
application/Espo/ORM/Defs/Params/RelationParam.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs\Params;
|
||||
|
||||
/**
|
||||
* A relation parameter.
|
||||
*/
|
||||
class RelationParam
|
||||
{
|
||||
/**
|
||||
* A type.
|
||||
*/
|
||||
public const TYPE = 'type';
|
||||
|
||||
/**
|
||||
* Indexes.
|
||||
*/
|
||||
public const INDEXES = 'indexes';
|
||||
|
||||
/**
|
||||
* A relation name.
|
||||
*/
|
||||
public const RELATION_NAME = 'relationName';
|
||||
|
||||
/**
|
||||
* A foreign entity type.
|
||||
*/
|
||||
public const ENTITY = 'entity';
|
||||
|
||||
/**
|
||||
* A foreign relation name.
|
||||
*/
|
||||
public const FOREIGN = 'foreign';
|
||||
|
||||
/**
|
||||
* Conditions.
|
||||
*/
|
||||
public const CONDITIONS = 'conditions';
|
||||
|
||||
/**
|
||||
* Additional columns.
|
||||
*/
|
||||
public const ADDITIONAL_COLUMNS = 'additionalColumns';
|
||||
|
||||
/**
|
||||
* A key.
|
||||
*/
|
||||
public const KEY = 'key';
|
||||
|
||||
/**
|
||||
* A foreign key.
|
||||
*/
|
||||
public const FOREIGN_KEY = 'foreignKey';
|
||||
|
||||
/**
|
||||
* Middle keys.
|
||||
*/
|
||||
public const MID_KEYS = 'midKeys';
|
||||
|
||||
/**
|
||||
* No join.
|
||||
*/
|
||||
public const NO_JOIN = 'noJoin';
|
||||
|
||||
/**
|
||||
* Deferred load.
|
||||
*/
|
||||
public const DEFERRED_LOAD = 'deferredLoad';
|
||||
|
||||
/**
|
||||
* Default order by. Applied on the entity level.
|
||||
*
|
||||
* @since 9.2.5
|
||||
*/
|
||||
public const ORDER_BY = 'orderBy';
|
||||
|
||||
/**
|
||||
* Default order. Applied on the entity level.
|
||||
*
|
||||
* @since 9.2.5
|
||||
*/
|
||||
public const ORDER = 'order';
|
||||
}
|
||||
362
application/Espo/ORM/Defs/RelationDefs.php
Normal file
362
application/Espo/ORM/Defs/RelationDefs.php
Normal file
@@ -0,0 +1,362 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\ORM\Defs;
|
||||
|
||||
use Espo\ORM\Defs\Params\RelationParam;
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Relation definitions.
|
||||
*/
|
||||
class RelationDefs
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
private array $data;
|
||||
private string $name;
|
||||
|
||||
private function __construct()
|
||||
{}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $raw
|
||||
*/
|
||||
public static function fromRaw(array $raw, string $name): self
|
||||
{
|
||||
$obj = new self();
|
||||
$obj->data = $raw;
|
||||
$obj->name = $name;
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a name.
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a type.
|
||||
*/
|
||||
public function getType(): string
|
||||
{
|
||||
$type = $this->data[RelationParam::TYPE] ?? null;
|
||||
|
||||
if ($type === null) {
|
||||
throw new RuntimeException("Relation '{$this->name}' has no type.");
|
||||
}
|
||||
|
||||
return $type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is Many-to-Many.
|
||||
*/
|
||||
public function isManyToMany(): bool
|
||||
{
|
||||
return $this->getType() === Entity::MANY_MANY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is Has-Many (One-to-Many).
|
||||
*/
|
||||
public function isHasMany(): bool
|
||||
{
|
||||
return $this->getType() === Entity::HAS_MANY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is Has-One (Many-to-One or One-to-One).
|
||||
*/
|
||||
public function isHasOne(): bool
|
||||
{
|
||||
return $this->getType() === Entity::HAS_ONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is Has-Children (Parent-to-Children).
|
||||
*/
|
||||
public function isHasChildren(): bool
|
||||
{
|
||||
return $this->getType() === Entity::HAS_CHILDREN;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is Belongs-to (Many-to-One).
|
||||
*/
|
||||
public function isBelongsTo(): bool
|
||||
{
|
||||
return $this->getType() === Entity::BELONGS_TO;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether is Belongs-to-Parent (Children-to-Parent).
|
||||
*/
|
||||
public function isBelongsToParent(): bool
|
||||
{
|
||||
return $this->getType() === Entity::BELONGS_TO_PARENT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether it has a foreign entity type is defined.
|
||||
*/
|
||||
public function hasForeignEntityType(): bool
|
||||
{
|
||||
return isset($this->data[RelationParam::ENTITY]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a foreign entity type.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getForeignEntityType(): string
|
||||
{
|
||||
if (!$this->hasForeignEntityType()) {
|
||||
throw new RuntimeException("No 'entity' parameter defined in the relation '{$this->name}'.");
|
||||
}
|
||||
|
||||
return $this->data[RelationParam::ENTITY];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a foreign entity type.
|
||||
*/
|
||||
public function tryGetForeignEntityType(): ?string
|
||||
{
|
||||
if (!$this->hasForeignEntityType()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getForeignEntityType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether it has a foreign relation name.
|
||||
*/
|
||||
public function hasForeignRelationName(): bool
|
||||
{
|
||||
return isset($this->data[RelationParam::FOREIGN]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get a foreign relation name.
|
||||
*
|
||||
* @since 8.3.0
|
||||
*/
|
||||
public function tryGetForeignRelationName(): ?string
|
||||
{
|
||||
if (!$this->hasForeignRelationName()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $this->getForeignRelationName();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a foreign relation name.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getForeignRelationName(): string
|
||||
{
|
||||
if (!$this->hasForeignRelationName()) {
|
||||
throw new RuntimeException("No 'foreign' parameter defined in the relation '{$this->name}'.");
|
||||
}
|
||||
|
||||
return $this->data[RelationParam::FOREIGN];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a foreign key is defined.
|
||||
*/
|
||||
public function hasForeignKey(): bool
|
||||
{
|
||||
return isset($this->data[RelationParam::FOREIGN_KEY]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a foreign key.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getForeignKey(): string
|
||||
{
|
||||
if (!$this->hasForeignKey()) {
|
||||
throw new RuntimeException("No 'foreignKey' parameter defined in the relation '{$this->name}'.");
|
||||
}
|
||||
|
||||
return $this->data[RelationParam::FOREIGN_KEY];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a key is defined.
|
||||
*/
|
||||
public function hasKey(): bool
|
||||
{
|
||||
return isset($this->data[RelationParam::KEY]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a key.
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getKey(): string
|
||||
{
|
||||
if (!$this->hasKey()) {
|
||||
throw new RuntimeException("No 'key' parameter defined in the relation '{$this->name}'.");
|
||||
}
|
||||
|
||||
return $this->data[RelationParam::KEY];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a mid-key is defined. For Many-to-Many relationships only.
|
||||
*/
|
||||
public function hasMidKey(): bool
|
||||
{
|
||||
return !is_null($this->data[RelationParam::MID_KEYS][0] ?? null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a mid-key. For Many-to-Many relationships only.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getMidKey(): string
|
||||
{
|
||||
if (!$this->hasMidKey()) {
|
||||
throw new RuntimeException("No 'midKey' parameter defined in the relation '{$this->name}'.");
|
||||
}
|
||||
|
||||
return $this->data[RelationParam::MID_KEYS][0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a foreign mid-key is defined. For Many-to-Many relationships only.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function hasForeignMidKey(): bool
|
||||
{
|
||||
return !is_null($this->data[RelationParam::MID_KEYS][1] ?? null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a foreign mid-key. For Many-to-Many relationships only.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getForeignMidKey(): string
|
||||
{
|
||||
if (!$this->hasForeignMidKey()) {
|
||||
throw new RuntimeException("No 'foreignMidKey' parameter defined in the relation '{$this->name}'.");
|
||||
}
|
||||
|
||||
return $this->data[RelationParam::MID_KEYS][1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a relationship name is defined.
|
||||
*/
|
||||
public function hasRelationshipName(): bool
|
||||
{
|
||||
return isset($this->data[RelationParam::RELATION_NAME]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a relationship name.
|
||||
*
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getRelationshipName(): string
|
||||
{
|
||||
if (!$this->hasRelationshipName()) {
|
||||
throw new RuntimeException("No 'relationName' parameter defined in the relation '{$this->name}'.");
|
||||
}
|
||||
|
||||
return $this->data[RelationParam::RELATION_NAME];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get indexes.
|
||||
*
|
||||
* @return IndexDefs[]
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
public function getIndexList(): array
|
||||
{
|
||||
if ($this->getType() !== Entity::MANY_MANY) {
|
||||
throw new RuntimeException("Can't get indexes.");
|
||||
}
|
||||
|
||||
$list = [];
|
||||
|
||||
foreach (($this->data[RelationParam::INDEXES] ?? []) as $name => $item) {
|
||||
$list[] = IndexDefs::fromRaw($item, $name);
|
||||
}
|
||||
|
||||
return $list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get additional middle table conditions.
|
||||
*
|
||||
* @return array<string, ?scalar>
|
||||
*/
|
||||
public function getConditions(): array
|
||||
{
|
||||
if ($this->getType() !== Entity::MANY_MANY) {
|
||||
throw new RuntimeException("Can't get conditions for non many-many relationship.");
|
||||
}
|
||||
|
||||
return $this->getParam(RelationParam::CONDITIONS) ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether a parameter is set.
|
||||
*/
|
||||
public function hasParam(string $name): bool
|
||||
{
|
||||
return array_key_exists($name, $this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a parameter value by a name.
|
||||
*/
|
||||
public function getParam(string $name): mixed
|
||||
{
|
||||
return $this->data[$name] ?? null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user