From f28515fcf185c951ad079f9a4f257503ce1dc29e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 8 Feb 2020 22:31:48 +0100 Subject: [PATCH] fixes #237 --- Account/Account.php | 4 +- ApplicationAbstract.php | 1 + DataStorage/Database/DataMapperAbstract.php | 40 ++-- Localization/Defaults/Definitions/de_DE.json | 27 +-- Localization/Defaults/Definitions/en_US.json | 21 ++- Localization/Defaults/Definitions/it_IT.json | 21 ++- Localization/Localization.php | 172 +++++++++++++++++- Utils/Converter/LengthType.php | 1 + Utils/Converter/Measurement.php | 6 + Utils/Converter/VolumeType.php | 4 +- .../Database/Schema/BuilderTest.php | 4 +- tests/Utils/Converter/LengthTypeTest.php | 3 +- tests/Utils/Converter/VolumeTypeTest.php | 4 +- 13 files changed, 249 insertions(+), 59 deletions(-) diff --git a/Account/Account.php b/Account/Account.php index a58f19cc9..27606dad1 100644 --- a/Account/Account.php +++ b/Account/Account.php @@ -144,7 +144,7 @@ class Account implements ArrayableInterface, \JsonSerializable * @var Localization * @since 1.0.0 */ - protected Localization $localization; + protected $localization; use PermissionHandlingTrait; @@ -186,7 +186,7 @@ class Account implements ArrayableInterface, \JsonSerializable * * @since 1.0.0 */ - public function getL11n() : Localization + public function getL11n() { return $this->localization; } diff --git a/ApplicationAbstract.php b/ApplicationAbstract.php index 846dcb5ee..9e08fae54 100644 --- a/ApplicationAbstract.php +++ b/ApplicationAbstract.php @@ -39,6 +39,7 @@ use phpOMS\Router\RouterInterface; * @property int $orgId * @property \phpOMS\DataStorage\Database\DatabasePool $dbPool * @property \phpOMS\Localization\L11nManager $l11nManager + * @property \phpOMS\Localization\Localization $l11nServer * @property \phpOMS\Router\RouterInterface $router * @property \phpOMS\DataStorage\Session\SessionInterface $sessionManager * @property \phpOMS\DataStorage\Cookie\CookieJar $cookieJar diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index def5d0ea3..fc232258c 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -549,8 +549,9 @@ class DataMapperAbstract implements DataMapperInterface $query->insert($column['name'])->value($value); } elseif ($column['name'] !== static::$primaryField || !empty($property->getValue($obj))) { $tValue = $property->getValue($obj); - if (\stripos($column['internal'], '/') === 0) { - $tValue = ArrayUtils::getArray($column['internal'], $tValue, '/'); + if (\stripos($column['internal'], '/') !== false) { + $path = \substr($column['internal'], \stripos($column['internal'], '/') + 1); + $tValue = ArrayUtils::getArray($path, $tValue, '/'); } $value = self::parseValue($column['type'], $tValue); @@ -1280,7 +1281,7 @@ class DataMapperAbstract implements DataMapperInterface * The reference is stored in the main model * * @param string $propertyName Property name to initialize - * @param object $obj Object to update + * @param mixed $obj Object to update * @param int $relations Create all relations as well * @param int $depth Depth of relations to update (default = 1 = none) * @@ -1288,8 +1289,12 @@ class DataMapperAbstract implements DataMapperInterface * * @since 1.0.0 */ - private static function updateOwnsOne(string $propertyName, object $obj, int $relations = RelationType::ALL, int $depth = 1) + private static function updateOwnsOne(string $propertyName, $obj, int $relations = RelationType::ALL, int $depth = 1) { + if (!\is_object($obj)) { + return $obj; + } + /** @var string $mapper */ $mapper = static::$ownsOne[$propertyName]['mapper']; @@ -1336,14 +1341,14 @@ class DataMapperAbstract implements DataMapperInterface */ private static function updateBelongsTo(string $propertyName, $obj, int $relations = RelationType::ALL, int $depth = 1) { - if (\is_object($obj)) { - /** @var string $mapper */ - $mapper = static::$belongsTo[$propertyName]['mapper']; - - return $mapper::update($obj, $relations, $depth); + if (!\is_object($obj)) { + return $obj; } - return $obj; + /** @var string $mapper */ + $mapper = static::$belongsTo[$propertyName]['mapper']; + + return $mapper::update($obj, $relations, $depth); } /** @@ -1431,8 +1436,9 @@ class DataMapperAbstract implements DataMapperInterface $query->set([static::$table . '.' . $column['name'] => $value]); } elseif ($column['name'] !== static::$primaryField) { $tValue = $property->getValue($obj); - if (\stripos($column['internal'], '/') === 0) { - $tValue = ArrayUtils::getArray($column['internal'], $tValue, '/'); + if (\stripos($column['internal'], '/') !== false) { + $path = \substr($column['internal'], \stripos($column['internal'], '/') + 1); + $tValue = ArrayUtils::getArray($path, $tValue, '/'); } $value = self::parseValue($column['type'], $tValue); @@ -1481,8 +1487,9 @@ class DataMapperAbstract implements DataMapperInterface if ($column['name'] !== $conditional['external']) { $tValue = $property->getValue($obj); - if (\stripos($column['internal'], '/') === 0) { - $tValue = ArrayUtils::getArray($column['internal'], $tValue, '/'); + if (\stripos($column['internal'], '/') !== false) { + $path = \substr($column['internal'], \stripos($column['internal'], '/') + 1); + $tValue = ArrayUtils::getArray($path, $tValue, '/'); } $value = self::parseValue($column['type'], $tValue); @@ -1524,8 +1531,9 @@ class DataMapperAbstract implements DataMapperInterface } $path = $column['internal']; - if (\stripos($column['internal'], '/') === 0) { - $path = \ltrim($column['internal'], '/'); + if (\stripos($column['internal'], '/') !== false) { + $path = \substr($column['internal'], \stripos($column['internal'], '/') + 1); + //$path = \ltrim($column['internal'], '/'); } $property = ArrayUtils::getArray($column['internal'], $obj, '/'); diff --git a/Localization/Defaults/Definitions/de_DE.json b/Localization/Defaults/Definitions/de_DE.json index bdc130be3..848814e66 100644 --- a/Localization/Defaults/Definitions/de_DE.json +++ b/Localization/Defaults/Definitions/de_DE.json @@ -3,7 +3,8 @@ "country": "DE", "currency": { "code": "EUR", - "position": 0 + "position": 0, + "format": "%s1 %s2" }, "thousand": ".", "decimal": ",", @@ -17,7 +18,7 @@ "very_heavy": "t" }, "speed": { - "very_slow": "ns", + "very_slow": "mps", "slow": "ms", "medium": "kph", "fast": "kph", @@ -25,7 +26,7 @@ "sea": "knot" }, "length": { - "very_short": "nm", + "very_short": "micron", "short": "mm", "medium": "cm", "long": "m", @@ -33,29 +34,29 @@ "sea": "mile" }, "area": { - "very_small": "nm", + "very_small": "micron", "small": "mm", "medium": "cm", "large": "m", "very_large": "km" }, "volume": { - "very_small": "ml", + "very_small": "mul", "small": "ml", "medium": "l", - "large": "m", - "very_large": "km", - "teaspoon": "metric teaspoon", - "tablespoon": "metric tablespoon", - "glass": "metric glass" + "large": "cm", + "very_large": "m", + "teaspoon": "Metric tsp", + "tablespoon": "Metric tblsp", + "glass": "Metric cup" }, "datetime": { "delim_date": ".", "delim_time": ":", "very_short": "d.m", "short": "m.y", - "medium": "d.m.Y", - "long": "d.m.Y h:i", - "very_long": "d.m.Y h:i:s" + "medium": "Y.m.d", + "long": "Y.m.d h:i", + "very_long": "Y.m.d h:i:s" } } \ No newline at end of file diff --git a/Localization/Defaults/Definitions/en_US.json b/Localization/Defaults/Definitions/en_US.json index f5fb7b0b5..aa87072ea 100644 --- a/Localization/Defaults/Definitions/en_US.json +++ b/Localization/Defaults/Definitions/en_US.json @@ -3,7 +3,8 @@ "country": "US", "currency": { "code": "USD", - "position": 0 + "position": 0, + "format": "%s1 %s2" }, "thousand": ",", "decimal": ".", @@ -17,7 +18,7 @@ "very_heavy": "t" }, "speed": { - "very_slow": "ns", + "very_slow": "mps", "slow": "ms", "medium": "kph", "fast": "kph", @@ -25,7 +26,7 @@ "sea": "knot" }, "length": { - "very_short": "nm", + "very_short": "micron", "short": "mm", "medium": "cm", "long": "m", @@ -33,21 +34,21 @@ "sea": "mile" }, "area": { - "very_small": "nm", + "very_small": "micron", "small": "mm", "medium": "cm", "large": "m", "very_large": "km" }, "volume": { - "very_small": "ml", + "very_small": "mul", "small": "ml", "medium": "l", - "large": "m", - "very_large": "km", - "teaspoon": "metric teaspoon", - "tablespoon": "metric tablespoon", - "glass": "metric glass" + "large": "cm", + "very_large": "m", + "teaspoon": "Metric tsp", + "tablespoon": "Metric tblsp", + "glass": "Metric cup" }, "datetime": { "delim_date": ".", diff --git a/Localization/Defaults/Definitions/it_IT.json b/Localization/Defaults/Definitions/it_IT.json index f5fb7b0b5..aa87072ea 100644 --- a/Localization/Defaults/Definitions/it_IT.json +++ b/Localization/Defaults/Definitions/it_IT.json @@ -3,7 +3,8 @@ "country": "US", "currency": { "code": "USD", - "position": 0 + "position": 0, + "format": "%s1 %s2" }, "thousand": ",", "decimal": ".", @@ -17,7 +18,7 @@ "very_heavy": "t" }, "speed": { - "very_slow": "ns", + "very_slow": "mps", "slow": "ms", "medium": "kph", "fast": "kph", @@ -25,7 +26,7 @@ "sea": "knot" }, "length": { - "very_short": "nm", + "very_short": "micron", "short": "mm", "medium": "cm", "long": "m", @@ -33,21 +34,21 @@ "sea": "mile" }, "area": { - "very_small": "nm", + "very_small": "micron", "small": "mm", "medium": "cm", "large": "m", "very_large": "km" }, "volume": { - "very_small": "ml", + "very_small": "mul", "small": "ml", "medium": "l", - "large": "m", - "very_large": "km", - "teaspoon": "metric teaspoon", - "tablespoon": "metric tablespoon", - "glass": "metric glass" + "large": "cm", + "very_large": "m", + "teaspoon": "Metric tsp", + "tablespoon": "Metric tblsp", + "glass": "Metric cup" }, "datetime": { "delim_date": ".", diff --git a/Localization/Localization.php b/Localization/Localization.php index 5031dde7a..1e8235011 100644 --- a/Localization/Localization.php +++ b/Localization/Localization.php @@ -26,7 +26,7 @@ use phpOMS\Utils\Converter\TemperatureType; * @link https://orange-management.org * @since 1.0.0 */ -class Localization +class Localization implements \JsonSerializable { /** * Country ID. @@ -60,6 +60,14 @@ class Localization */ protected string $currency = ISO4217CharEnum::_USD; + /** + * Currency format. + * + * @var string + * @since 1.0.0 + */ + protected string $currencyFormat = '%s1 %s2'; + /** * Number format. * @@ -100,6 +108,22 @@ class Localization */ protected array $datetime = []; + /** + * Datetime delim. + * + * @var string + * @since 1.0.0 + */ + protected string $dateDelim = '.'; + + /** + * Datetime delim. + * + * @var string + * @since 1.0.0 + */ + protected string $timeDelim = ':'; + /** * Weight. * @@ -178,6 +202,39 @@ class Localization return $l11n; } + /** + * Create localization from json + * + * @param array $json Json serialization + * + * @return Localization + * + * @since 1.0.0 + */ + public static function fromJson(array $json) : self + { + $l11n = new self(); + $l11n->setCountry($json['country']); + $l11n->setTimezone($json['timezone']); + $l11n->setLanguage($json['language']); + $l11n->setCurrency($json['currency']); + $l11n->setCurrencyFormat($json['currencyformat']); + $l11n->setDecimal($json['decimal']); + $l11n->setThousands($json['thousands']); + $l11n->setAngle($json['angle']); + $l11n->setTemperature($json['temperature']); + $l11n->setDatetime($json['datetime']); + $l11n->setDateDelim($json['datedelim']); + $l11n->setTimeDelim($json['timedelim']); + $l11n->setWeight($json['weight']); + $l11n->setSpeed($json['speed']); + $l11n->setLength($json['length']); + $l11n->setArea($json['area']); + $l11n->setVolume($json['volume']); + + return $l11n; + } + /** * Load localization from language code * @@ -388,6 +445,32 @@ class Localization $this->currency = $currency; } + /** + * Get currency format + * + * @return string + * + * @since 1.0.0 + */ + public function getCurrencyFormat() : string + { + return $this->currencyFormat; + } + + /** + * Set currency format + * + * @param string $format Currency format + * + * @return void + * + * @since 1.0.0 + */ + public function setCurrencyFormat(string $format) : void + { + $this->currencyFormat = $format; + } + /** * get datetime format * @@ -414,6 +497,58 @@ class Localization $this->datetime = $datetime; } + /** + * Set dateDelim char + * + * @return string + * + * @since 1.0.0 + */ + public function getDateDelim() : string + { + return $this->dateDelim; + } + + /** + * Get dateDelim char + * + * @param string $dateDelim Date delim char + * + * @return void + * + * @since 1.0.0 + */ + public function setDateDelim(string $dateDelim) : void + { + $this->dateDelim = $dateDelim; + } + + /** + * Get timeDelim char + * + * @return string + * + * @since 1.0.0 + */ + public function getTimeDelim() : string + { + return $this->timeDelim; + } + + /** + * Set timeDelim char + * + * @param string $timeDelim Time delim char + * + * @return void + * + * @since 1.0.0 + */ + public function setTimeDelim(string $timeDelim) : void + { + $this->timeDelim = $timeDelim; + } + /** * Set decimal char * @@ -657,4 +792,39 @@ class Localization { $this->volume = $volume; } + + /** + * {@inheritdoc} + */ + public function toArray() : array + { + return [ + 'id' => $this->id, + 'country' => $this->country, + 'timezone' => $this->timezone, + 'language' => $this->language, + 'currency' => $this->currency, + 'currencyformat' => $this->currencyFormat, + 'decimal' => $this->decimal, + 'thousands' => $this->thousands, + 'angle' => $this->angle, + 'temperature' => $this->temperature, + 'datetime' => $this->datetime, + 'datedelim' => $this->dateDelim, + 'timedelim' => $this->timeDelim, + 'weight' => $this->weight, + 'speed' => $this->speed, + 'length' => $this->length, + 'area' => $this->area, + 'volume' => $this->volume, + ]; + } + + /** + * {@inheritdoc} + */ + public function jsonSerialize() + { + return $this->toArray(); + } } diff --git a/Utils/Converter/LengthType.php b/Utils/Converter/LengthType.php index 26a84811c..0621af6db 100644 --- a/Utils/Converter/LengthType.php +++ b/Utils/Converter/LengthType.php @@ -28,6 +28,7 @@ abstract class LengthType extends Enum { public const MILES = 'mi'; public const METERS = 'm'; + public const NANOMETER = 'nm'; public const MICROMETER = 'micron'; public const CENTIMETERS = 'cm'; public const MILLIMETERS = 'mm'; diff --git a/Utils/Converter/Measurement.php b/Utils/Converter/Measurement.php index 9fea1ab83..7242ed518 100644 --- a/Utils/Converter/Measurement.php +++ b/Utils/Converter/Measurement.php @@ -296,6 +296,9 @@ final class Measurement case LengthType::MICROINCH: $value *= 39370000; break; + case LengthType::NANOMETER: + $value *= 1000000000; + break; case LengthType::INCHES: $value *= 39.370; break; @@ -357,6 +360,9 @@ final class Measurement case LengthType::MICROMETER: $value /= 1000000; break; + case LengthType::NANOMETER: + $value /= 1000000000; + break; case LengthType::CENTIMETERS: $value /= 100; break; diff --git a/Utils/Converter/VolumeType.php b/Utils/Converter/VolumeType.php index 7a0dc8f5c..4a46733fa 100644 --- a/Utils/Converter/VolumeType.php +++ b/Utils/Converter/VolumeType.php @@ -37,9 +37,9 @@ abstract class VolumeType extends Enum public const UK_QUARTS = 'UK qt dry'; public const US_GILL = 'US gi'; public const UK_GILL = 'UK gi'; - public const LITER = 'L'; + public const LITER = 'l'; public const MICROLITER = 'mul'; - public const MILLILITER = 'mL'; + public const MILLILITER = 'ml'; public const CENTILITER = 'cl'; public const KILOLITER = 'kl'; public const UK_BARREL = 'UK bbl'; diff --git a/tests/DataStorage/Database/Schema/BuilderTest.php b/tests/DataStorage/Database/Schema/BuilderTest.php index 9a9997f3c..2f746495e 100644 --- a/tests/DataStorage/Database/Schema/BuilderTest.php +++ b/tests/DataStorage/Database/Schema/BuilderTest.php @@ -75,8 +75,8 @@ class BuilderTest extends \PHPUnit\Framework\TestCase self::assertEquals( $sql, $query->createTable('user_roles') - ->field('user_id', 'INT', null, false, true, true, 'users', 'ext1_id') - ->field('role_id', 'VARCHAR(10)', '1', true, false, false, 'roles', 'ext2_id') + ->field('user_id', 'INT', null, false, true, false, true, 'users', 'ext1_id') + ->field('role_id', 'VARCHAR(10)', '1', true, false, false, false, 'roles', 'ext2_id') ->toSql() ); } diff --git a/tests/Utils/Converter/LengthTypeTest.php b/tests/Utils/Converter/LengthTypeTest.php index 133faf502..d1521a235 100644 --- a/tests/Utils/Converter/LengthTypeTest.php +++ b/tests/Utils/Converter/LengthTypeTest.php @@ -27,7 +27,7 @@ class LengthTypeTest extends \PHPUnit\Framework\TestCase */ public function testEnumCount() : void { - self::assertCount(21, LengthType::getConstants()); + self::assertCount(22, LengthType::getConstants()); } /** @@ -48,6 +48,7 @@ class LengthTypeTest extends \PHPUnit\Framework\TestCase self::assertEquals('mi', LengthType::MILES); self::assertEquals('m', LengthType::METERS); self::assertEquals('micron', LengthType::MICROMETER); + self::assertEquals('nm', LengthType::NANOMETER); self::assertEquals('cm', LengthType::CENTIMETERS); self::assertEquals('mm', LengthType::MILLIMETERS); self::assertEquals('km', LengthType::KILOMETERS); diff --git a/tests/Utils/Converter/VolumeTypeTest.php b/tests/Utils/Converter/VolumeTypeTest.php index 48fc9c4de..214894c88 100644 --- a/tests/Utils/Converter/VolumeTypeTest.php +++ b/tests/Utils/Converter/VolumeTypeTest.php @@ -56,9 +56,9 @@ class VolumeTypeTest extends \PHPUnit\Framework\TestCase self::assertEquals('UK qt dry', VolumeType::UK_QUARTS); self::assertEquals('US gi', VolumeType::US_GILL); self::assertEquals('UK gi', VolumeType::UK_GILL); - self::assertEquals('L', VolumeType::LITER); + self::assertEquals('l', VolumeType::LITER); self::assertEquals('mul', VolumeType::MICROLITER); - self::assertEquals('mL', VolumeType::MILLILITER); + self::assertEquals('ml', VolumeType::MILLILITER); self::assertEquals('cl', VolumeType::CENTILITER); self::assertEquals('kl', VolumeType::KILOLITER); self::assertEquals('UK bbl', VolumeType::UK_BARREL);