Implement getBy

This commit is contained in:
Dennis Eichhorn 2020-02-07 20:37:26 +01:00
parent 4107551a09
commit d20a6b9e8d
15 changed files with 264 additions and 40 deletions

View File

@ -144,7 +144,7 @@ class Account implements ArrayableInterface, \JsonSerializable
* @var Localization
* @since 1.0.0
*/
protected Localization $l11n;
protected Localization $localization;
use PermissionHandlingTrait;
@ -159,10 +159,10 @@ class Account implements ArrayableInterface, \JsonSerializable
*/
public function __construct(int $id = 0)
{
$this->createdAt = new \DateTime('now');
$this->lastActive = new \DateTime('now');
$this->id = $id;
$this->l11n = new Localization();
$this->createdAt = new \DateTime('now');
$this->lastActive = new \DateTime('now');
$this->id = $id;
$this->localization = new Localization();
}
/**
@ -188,7 +188,7 @@ class Account implements ArrayableInterface, \JsonSerializable
*/
public function getL11n() : Localization
{
return $this->l11n;
return $this->localization;
}
/**
@ -231,7 +231,7 @@ class Account implements ArrayableInterface, \JsonSerializable
*/
public function setL11n(Localization $l11n) : void
{
$this->l11n = $l11n;
$this->localization = $l11n;
}
/**

View File

@ -2066,7 +2066,12 @@ class DataMapperAbstract implements DataMapperInterface
$refProp->setAccessible(true);
}
$objects = $mapper::get($values, RelationType::ALL, null, $depth);
if (!isset(static::$hasMany[$member]['by'])) {
$objects = $mapper::get($values, RelationType::ALL, null, $depth);
} else {
$objects = $mapper::getBy($values, static::$hasMany[$member]['by'], RelationType::ALL, null, $depth);
}
$refProp->setValue($obj, !\is_array($objects) ? [$objects->getId() => $objects] : $objects);
if (!$accessible) {
@ -2129,8 +2134,13 @@ class DataMapperAbstract implements DataMapperInterface
continue;
}
$id = \is_object($id) ? self::getObjectId($id) : $id;
$value = self::getInitialized($mapper, $id) ?? $mapper::get($id, RelationType::ALL, null, $depth);
$id = \is_object($id) ? self::getObjectId($id) : $id;
if (!isset(static::$ownsOne[$member]['by'])) {
$value = self::getInitialized($mapper, $id) ?? $mapper::get($id, RelationType::ALL, null, $depth);
} else {
$value = $mapper::getBy($id, static::$ownsOne[$member]['by'], RelationType::ALL, null, $depth);
}
$refProp->setValue($obj, $value);
@ -2507,8 +2517,8 @@ class DataMapperAbstract implements DataMapperInterface
/**
* Get object.
*
* @param mixed $refKey Key
* @param string $ref The field that defines the for
* @param mixed $forKey Key
* @param string $for The field that defines the for
* @param int $relations Load relations
* @param mixed $fill Object to fill
* @param int $depth Relation depth
@ -2517,10 +2527,10 @@ class DataMapperAbstract implements DataMapperInterface
*
* @since 1.0.0
*/
public static function getFor($refKey, string $ref, int $relations = RelationType::ALL, $fill = null, int $depth = 3)
public static function getFor($forKey, string $for, int $relations = RelationType::ALL, $fill = null, int $depth = 3)
{
if ($depth < 1) {
return $refKey;
return $forKey;
}
if (!isset(self::$parentMapper)) {
@ -2529,16 +2539,68 @@ class DataMapperAbstract implements DataMapperInterface
self::extend(__CLASS__);
$refKey = (array) $refKey;
$forKey = (array) $forKey;
$obj = [];
foreach ($refKey as $key => $value) {
foreach ($forKey as $key => $value) {
$toLoad = [];
if (isset(static::$hasMany[$ref]) && static::$hasMany[$ref]['src'] !== null) {
$toLoad = self::getHasManyPrimaryKeys($value, $ref);
if (isset(static::$hasMany[$for]) && static::$hasMany[$for]['src'] !== null) {
$toLoad = self::getHasManyPrimaryKeys($value, $for);
} else {
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($ref));
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($for));
}
$obj[$value] = self::get($toLoad, $relations, $fill, $depth);
}
$countResulsts = \count($obj);
if ($countResulsts === 0) {
return self::getNullModelObj();
} elseif ($countResulsts === 1) {
return \reset($obj);
}
return $obj;
}
/**
* Get object.
*
* @param mixed $byKey Key
* @param string $by The field that defines the for
* @param int $relations Load relations
* @param mixed $fill Object to fill
* @param int $depth Relation depth
*
* @return mixed
*
* @since 1.0.0
*/
public static function getBy($byKey, string $by, int $relations = RelationType::ALL, $fill = null, int $depth = 3)
{
if ($depth < 1) {
return $byKey;
}
if (!isset(self::$parentMapper)) {
self::$parentMapper = static::class;
}
self::extend(__CLASS__);
$byKey = (array) $byKey;
$obj = [];
foreach ($byKey as $key => $value) {
$toLoad = [];
if (isset(static::$hasMany[$by]) && static::$hasMany[$by]['src'] !== null) {
// todo: maybe wrong?!
$toLoad = self::getHasManyPrimaryKeys($value, $by);
} elseif (isset(static::$ownsOne[$by])) {
$toLoad = self::getPrimaryKeysBy($value, self::getColumnByMember($by));
}
$obj[$value] = self::get($toLoad, $relations, $fill, $depth);

View File

@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults;
* @link https://orange-management.org
* @since 1.0.0
*/
final class City
class City
{
/**
* City id.

View File

@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults;
* @link https://orange-management.org
* @since 1.0.0
*/
final class Country
class Country
{
/**
* Country id.

View File

@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults;
* @link https://orange-management.org
* @since 1.0.0
*/
final class Currency
class Currency
{
/**
* Currency id.

View File

@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults;
* @link https://orange-management.org
* @since 1.0.0
*/
final class Iban
class Iban
{
/**
* Iban id.

View File

@ -22,7 +22,7 @@ namespace phpOMS\Localization\Defaults;
* @link https://orange-management.org
* @since 1.0.0
*/
final class Language
class Language
{
/**
* Language id.

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Localization\Defaults
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Localization\Defaults;
/**
* City.
*
* @package phpOMS\Localization\Defaults
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class NullCity extends City
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Localization\Defaults
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Localization\Defaults;
/**
* Country.
*
* @package phpOMS\Localization\Defaults
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class NullCountry extends Country
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Localization\Defaults
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Localization\Defaults;
/**
* Currency.
*
* @package phpOMS\Localization\Defaults
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class NullCurrency extends Currency
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Localization\Defaults
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Localization\Defaults;
/**
* Iban.
*
* @package phpOMS\Localization\Defaults
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class NullIban extends Iban
{
}

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Localization\Defaults
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Localization\Defaults;
/**
* Language.
*
* @package phpOMS\Localization\Defaults
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class NullLanguage extends Language
{
}

View File

@ -34,7 +34,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $country = ISO3166TwoEnum::_USA;
protected string $country = ISO3166TwoEnum::_USA;
/**
* Timezone.
@ -42,7 +42,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $timezone = 'America/New_York';
protected string $timezone = 'America/New_York';
/**
* Language ISO code.
@ -50,7 +50,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $language = ISO639x1Enum::_EN;
protected string $language = ISO639x1Enum::_EN;
/**
* Currency.
@ -58,7 +58,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $currency = ISO4217CharEnum::_USD;
protected string $currency = ISO4217CharEnum::_USD;
/**
* Number format.
@ -66,7 +66,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $decimal = '.';
protected string $decimal = '.';
/**
* Number format.
@ -74,7 +74,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $thousands = ',';
protected string $thousands = ',';
/**
* Angle type.
@ -82,7 +82,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $angle = AngleType::DEGREE;
protected string $angle = AngleType::DEGREE;
/**
* Temperature type.
@ -90,7 +90,7 @@ class Localization
* @var string
* @since 1.0.0
*/
private string $temperature = TemperatureType::CELSIUS;
protected string $temperature = TemperatureType::CELSIUS;
/**
* Time format.
@ -98,7 +98,7 @@ class Localization
* @var array<string, string>
* @since 1.0.0
*/
private array $datetime = [];
protected array $datetime = [];
/**
* Weight.
@ -106,7 +106,7 @@ class Localization
* @var array<string, string>
* @since 1.0.0
*/
private array $weight = [];
protected array $weight = [];
/**
* Speed.
@ -114,7 +114,7 @@ class Localization
* @var array<string, string>
* @since 1.0.0
*/
private array $speed = [];
protected array $speed = [];
/**
* Length.
@ -122,7 +122,7 @@ class Localization
* @var array<string, string>
* @since 1.0.0
*/
private array $length = [];
protected array $length = [];
/**
* Area.
@ -130,7 +130,7 @@ class Localization
* @var array<string, string>
* @since 1.0.0
*/
private array $area = [];
protected array $area = [];
/**
* Volume.
@ -138,7 +138,7 @@ class Localization
* @var array<string, string>
* @since 1.0.0
*/
private array $volume = [];
protected array $volume = [];
/**
* Country id.
@ -146,7 +146,7 @@ class Localization
* @var int
* @since 1.0.0
*/
private $id = 0;
protected int $id = 0;
/**
* Get id

View File

@ -0,0 +1,27 @@
<?php
/**
* Orange Management
*
* PHP Version 7.4
*
* @package phpOMS\Localization
* @copyright Dennis Eichhorn
* @license OMS License 1.0
* @version 1.0.0
* @link https://orange-management.org
*/
declare(strict_types=1);
namespace phpOMS\Localization;
/**
* Localization.
*
* @package phpOMS\Localization
* @license OMS License 1.0
* @link https://orange-management.org
* @since 1.0.0
*/
class NullLocalization extends Localization
{
}

View File

@ -63,7 +63,7 @@ class AccountTest extends \PHPUnit\Framework\TestCase
self::assertObjectHasAttribute('groups', $account);
self::assertObjectHasAttribute('type', $account);
self::assertObjectHasAttribute('status', $account);
self::assertObjectHasAttribute('l11n', $account);
self::assertObjectHasAttribute('localization', $account);
}
/**