type fixes

This commit is contained in:
Yuri Kuznetsov
2022-03-08 21:17:23 +02:00
parent 17728b76f1
commit 17c2204dd3
5 changed files with 91 additions and 68 deletions

View File

@@ -400,8 +400,7 @@ class Converter
if (isset($fieldTypeMetadata['linkDefs'])) {
$linkDefs = $this->getMetadataHelper()->getLinkDefsInFieldMeta(
$entityType,
$attributeParams,
$fieldTypeMetadata['linkDefs']
$attributeParams
);
if (isset($linkDefs)) {

View File

@@ -46,31 +46,40 @@ use stdClass;
*/
class Metadata
{
private $data = null;
/**
* @var ?array<string,mixed>
*/
private ?array $data = null;
private $objData = null;
private ?stdClass $objData = null;
private $useCache;
private bool $useCache;
private $cacheKey = 'metadata';
private string $cacheKey = 'metadata';
private $objCacheKey = 'objMetadata';
private string $objCacheKey = 'objMetadata';
private $customPath = 'custom/Espo/Custom/Resources/metadata';
private string $customPath = 'custom/Espo/Custom/Resources/metadata';
/**
* @var array<string,array<string,mixed>>
*/
private $deletedData = [];
/**
* @var array<string,array<string,mixed>>
*/
private $changedData = [];
private $metadataHelper;
private Helper $metadataHelper;
private $module;
private Module $module;
private $fileManager;
private FileManager $fileManager;
private $dataCache;
private DataCache $dataCache;
private $resourceReader;
private ResourceReader $resourceReader;
public function __construct(
FileManager $fileManager,
@@ -125,7 +134,7 @@ class Metadata
/**
* Get metadata array.
*
* @return array
* @return array<string,mixed>
*/
private function getData(): array
{
@@ -139,9 +148,8 @@ class Metadata
/**
* Get metadata by key.
*
* @param string|array $key
* @param string|string[] $key
* @param mixed $default
*
* @return mixed
*/
public function get($key = null, $default = null)
@@ -156,8 +164,7 @@ class Metadata
*
* @param bool $isJSON
* @param bool $reload
*
* @return array|string
* @return array<string,mixed>|string
*/
public function getAll(bool $isJSON = false, bool $reload = false)
{
@@ -205,9 +212,8 @@ class Metadata
/**
* Get metadata with stdClass items.
*
* @param string|array $key
* @param string|string[] $key
* @param mixed $default
*
* @return mixed
*/
public function getObjects($key = null, $default = null)
@@ -217,7 +223,10 @@ class Metadata
return Util::getValueByKey($objData, $key, $default);
}
public function getAllObjects($isJSON = false, $reload = false)
/**
* @return stdClass|string
*/
public function getAllObjects(bool $isJSON = false, bool $reload = false)
{
$objData = $this->getObjData($reload);
@@ -241,6 +250,11 @@ class Metadata
return $data;
}
/**
*
* @param string[] $row
* @param stdClass $data
*/
private function removeDataByPath($row, &$data): void
{
$p = &$data;
@@ -254,7 +268,8 @@ class Metadata
if ($item === '__ANY__') {
foreach (get_object_vars($p) as &$v) {
$this->removeDataByPath(
array_slice($row, $i + 1), $v
array_slice($row, $i + 1),
$v
);
}
@@ -355,13 +370,9 @@ class Metadata
/**
* Get metadata definition in custom directory.
*
* @param string $key1
* @param string $key2
* @param mixed $default
*
* @return stdClass
*/
public function getCustom($key1, $key2, $default = null)
public function getCustom(string $key1, string $key2, $default = null): stdClass
{
$filePath = $this->customPath . "/{$key1}/{$key2}.json";
@@ -378,15 +389,12 @@ class Metadata
* Set and save metadata in custom directory.
* The data is not merging with existing data. Use getCustom() to get existing data.
*
* @param string $key1
* @param string $key2
* @param array|stdClass $data
* @param array<string,mixed>|stdClass $data
*/
public function saveCustom(string $key1, string $key2, $data): void
{
if (is_object($data)) {
/** @phpstan-ignore-next-line */
foreach ($data as $key => $item) {
foreach (get_object_vars($data) as $key => $item) {
if ($item == new stdClass()) {
unset($data->$key);
}
@@ -404,6 +412,8 @@ class Metadata
/**
* Set Metadata data.
*
* @param array<mixed,mixed>|scalar|null $data
*/
public function set(string $key1, string $key2, $data): void
{
@@ -430,7 +440,7 @@ class Metadata
/**
* Unset some fields and other stuff in metadata.
*
* @param array|string $unsets Example: `fields.name`.
* @param string[]|string $unsets Example: `fields.name`.
*/
public function delete(string $key1, string $key2, $unsets = null): void
{
@@ -489,7 +499,10 @@ class Metadata
$this->data = Util::unsetInArray($this->getData(), $metadataUnsetData, true);
}
private function undelete($key1, $key2, $data): void
/**
* @param stdClass|array<string,mixed> $data
*/
private function undelete(string $key1, string $key2, $data): void
{
if (isset($this->deletedData[$key1][$key2])) {
foreach ($this->deletedData[$key1][$key2] as $unsetIndex => $unsetItem) {

View File

@@ -30,17 +30,20 @@
namespace Espo\Core\Utils\Metadata;
use Espo\Core\Utils\Util;
use Espo\Core\Utils\Metadata;
class Helper
{
private $metadata;
private Metadata $metadata;
protected $defaultNaming = 'postfix';
protected string $defaultNaming = 'postfix';
/**
* List of copied params for metadata -> 'fields' from parent items.
*
* @var string[]
*/
protected $copiedDefParams = array(
protected $copiedDefParams = [
'readOnly',
'disabled',
'notStorable',
@@ -53,37 +56,36 @@ class Helper
'customizationDisabled',
'importDisabled',
'exportDisabled',
);
];
public function __construct(\Espo\Core\Utils\Metadata $metadata)
public function __construct(Metadata $metadata)
{
$this->metadata = $metadata;
}
protected function getMetadata()
{
return $this->metadata;
}
/**
* Get field definition by type in metadata, "fields" key.
*
* @param array|string $fieldDef - It can be a string or field definition from entityDefs
* @return array|null
* @param array<string,mixed>|string $fieldDef It can be a string or field definition from entityDefs.
* @return ?array<string,mixed>
*/
public function getFieldDefsByType($fieldDef)
{
if (is_string($fieldDef)) {
$fieldDef = array('type' => $fieldDef);
$fieldDef = ['type' => $fieldDef];
}
if (isset($fieldDef['type'])) {
return $this->getMetadata()->get('fields.'.$fieldDef['type']);
return $this->metadata->get('fields.' . $fieldDef['type']);
}
return null;
}
/**
* @param array<string,mixed>|string $fieldDef
* @return ?array<string,mixed>
*/
public function getFieldDefsInFieldMeta($fieldDef)
{
$fieldDefsByType = $this->getFieldDefsByType($fieldDef);
@@ -101,11 +103,10 @@ class Helper
* Variables should be defined into fieldDefs (in 'entityDefs' metadata).
*
* @param string $entityName
* @param array $fieldDef
* @param array $linkFieldDefsByType
* @return array|null
* @param array<string,mixed>|string $fieldDef
* @return ?array<string,mixed>
*/
public function getLinkDefsInFieldMeta($entityName, $fieldDef, array $linkFieldDefsByType = null)
public function getLinkDefsInFieldMeta($entityName, $fieldDef)
{
$fieldDefsByType = $this->getFieldDefsByType($fieldDef);
@@ -115,11 +116,12 @@ class Helper
$linkFieldDefsByType = $fieldDefsByType['linkDefs'];
foreach ($linkFieldDefsByType as $paramName => &$paramValue) {
foreach ($linkFieldDefsByType as &$paramValue) {
if (preg_match('/{(.*?)}/', $paramValue, $matches)) {
if (in_array($matches[1], array_keys($fieldDef))) {
$value = $fieldDef[$matches[1]];
} else if (strtolower($matches[1]) == 'entity') {
}
else if (strtolower($matches[1]) == 'entity') {
$value = $entityName;
}
@@ -136,10 +138,9 @@ class Helper
* Get additional field list based on field definition in metadata 'fields'.
*
* @param string $fieldName
* @param array $fieldParams
* @param array $definitionList
*
* @return array|null
* @param array<string,mixed> $fieldParams
* @param array<string,mixed> $definitionList
* @return ?array<string,mixed>
*/
public function getAdditionalFieldList($fieldName, array $fieldParams, array $definitionList)
{

View File

@@ -40,21 +40,24 @@ use Espo\Core\{
class OrmMetadataData
{
/**
* @var ?array<string,array<string,mixed>>
*/
protected $data = null;
protected $cacheKey = 'ormMetadata';
protected string $cacheKey = 'ormMetadata';
protected $useCache;
protected bool $useCache;
protected $metadata;
protected Metadata $metadata;
protected $fileManager;
protected FileManager $fileManager;
protected $dataCache;
protected DataCache $dataCache;
protected $config;
protected Config $config;
private $converter;
private ?Converter $converter = null;
public function __construct(
Metadata $metadata,
@@ -67,7 +70,7 @@ class OrmMetadataData
$this->dataCache = $dataCache;
$this->config = $config;
$this->useCache = $this->config->get('useCache', false);
$this->useCache = (bool) $this->config->get('useCache', false);
}
protected function getConverter(): Converter
@@ -79,6 +82,9 @@ class OrmMetadataData
return $this->converter;
}
/**
* @return array<string,array<string,mixed>>
*/
public function getData(bool $reload = false): array
{
if (isset($this->data) && !$reload) {
@@ -100,6 +106,11 @@ class OrmMetadataData
return $this->data;
}
/**
* @param string|string[]|null $key
* @param mixed $default
* @return mixed
*/
public function get($key = null, $default = null)
{
return Util::getValueByKey($this->getData(), $key, $default);

View File

@@ -498,7 +498,7 @@ class Util
/**
* Return values of defined $key.
*
* @param mixed $data
* @param \stdClass|array<string,mixed> $data
* @param string[]|string $key Ex. of key is "entityDefs", "entityDefs.User".
* @param mixed $default
* @return mixed
@@ -535,7 +535,6 @@ class Util
return $default;
}
}
}
return $item;