1
0
Fork 0
mirror of synced 2025-04-20 01:21:01 +00:00

reuse logic from repositories for passing primary as constructor parameter for AbstractSerializableModel

This commit is contained in:
Pavel 2020-08-01 11:56:53 +03:00
parent bc2a54d855
commit 9eff510da4
5 changed files with 41 additions and 52 deletions

View file

@ -48,16 +48,18 @@ abstract class AbstractSerializableModel
abstract public function isDeleteStatic(): bool;
/**
* Should return data by provided primary key
* Should return filled entity by provided primary key
*
* @param mixed $primary
*
* @return array
* @return mixed
*/
abstract public static function getDataArrayByPrimary($primary): array;
abstract public static function getEntityByPrimary($primary);
/**
* AbstractSerializableModel constructor.
* Will fill model with data if primary key is passed.
* Better use repository getById method. It is faster, and passing primary by constructor uses it's under the hood.
*
* @param mixed $primary
*
@ -66,21 +68,17 @@ abstract class AbstractSerializableModel
public function __construct($primary = null)
{
if ($primary !== null) {
$data = static::getDataArrayByPrimary($primary);
$thisClassName = get_class($this);
$instance = static::getEntityByPrimary($primary);
if (!empty($data)) {
$thisClassName = get_class($this);
$instance = Deserializer::deserializeArray($data, $thisClassName);
if ($instance instanceof $thisClassName) {
$instanceReflection = new \ReflectionClass($instance);
if ($instance instanceof $thisClassName) {
$instanceReflection = new \ReflectionClass($instance);
foreach ($instanceReflection->getProperties() as $property) {
$thisProperty = new \ReflectionProperty($thisClassName, $property->getName());
$property->setAccessible(true);
$thisProperty->setAccessible(true);
$thisProperty->setValue($this, $property->getValue($instance));
}
foreach ($instanceReflection->getProperties() as $property) {
$thisProperty = new \ReflectionProperty($thisClassName, $property->getName());
$property->setAccessible(true);
$thisProperty->setAccessible(true);
$thisProperty->setValue($this, $property->getValue($instance));
}
}
}

View file

@ -12,6 +12,7 @@
namespace Intaro\RetailCrm\Model\Bitrix;
use Intaro\RetailCrm\Component\Json\Mapping;
use Intaro\RetailCrm\Repository\BuyerProfileRepository;
/**
* Class BuyerProfile
@ -155,24 +156,8 @@ class BuyerProfile extends AbstractSerializableModel
/**
* @inheritDoc
*/
public static function getDataArrayByPrimary($primary): array
public static function getEntityByPrimary($primary)
{
$result = \CSaleOrderUserProps::GetList([], ['ID' => $primary]);
if ($result) {
$data = $result->Fetch();
if (!$data) {
return [];
}
if (isset($data['ID']) && $data['ID'] != $primary) {
return [];
}
return $data;
}
return [];
return BuyerProfileRepository::getById((int) $primary);
}
}

View file

@ -13,6 +13,7 @@ namespace Intaro\RetailCrm\Model\Bitrix;
use Bitrix\Main\Type\DateTime;
use Intaro\RetailCrm\Component\Json\Mapping;
use Intaro\RetailCrm\Repository\UserRepository;
/**
* Class User
@ -1698,20 +1699,8 @@ class User extends AbstractSerializableModel
/**
* @inheritDoc
*/
public static function getDataArrayByPrimary($primary): array
public static function getEntityByPrimary($primary)
{
$result = \CUser::GetByID($primary);
if ($result) {
$data = $result->Fetch();
if (!$data) {
return [];
}
return $data;
}
return [];
return UserRepository::getById((int) $primary);
}
}

View file

@ -23,6 +23,24 @@ use Intaro\RetailCrm\Model\Bitrix\BuyerProfile;
*/
class BuyerProfileRepository extends AbstractRepository
{
/**
* Returns BuyerProfile by id
*
* @param int $id
*
* @return \Intaro\RetailCrm\Model\Bitrix\BuyerProfile|null
*/
public static function getById(int $id): ?BuyerProfile
{
$result = OrderUserProperties::getList(['filter' => ['ID' => $id]])->fetch();
if (!$result) {
return null;
}
return static::deserialize($result);
}
/**
* Returns true if provided BuyerProfile exists
*
@ -49,9 +67,9 @@ class BuyerProfileRepository extends AbstractRepository
$buyerProfileInstance = new \CSaleOrderUserProps();
if ($buyerProfileInstance->Add($profileData)) {
$profileData = OrderUserProperties::getList(array(
$profileData = OrderUserProperties::getList([
"filter" => $buyerProfile
))->fetch();
])->fetch();
}
return static::deserialize($profileData);

View file

@ -28,8 +28,7 @@ class UserRepository extends AbstractRepository
*/
public static function getById(int $id): ?User
{
$dbResult = \CUser::GetByID($id);
$fields = $dbResult->Fetch();
$fields = \CUser::GetByID($id)->Fetch();
if (!$fields) {
return null;