This commit is contained in:
Yuri Kuznetsov
2021-02-19 14:28:57 +02:00
parent fdb887e43a
commit 82a9779b09
21 changed files with 75 additions and 275 deletions
@@ -31,12 +31,12 @@ namespace Espo\Classes\AddressFormatters;
use Espo\Core\FieldUtils\Address\{
AddressFormatter,
AddressValue,
Address,
};
class Formatter1 implements AddressFormatter
{
public function format(AddressValue $address) : string
public function format(Address $address) : string
{
$result = '';
@@ -31,12 +31,12 @@ namespace Espo\Classes\AddressFormatters;
use Espo\Core\FieldUtils\Address\{
AddressFormatter,
AddressValue,
Address,
};
class Formatter2 implements AddressFormatter
{
public function format(AddressValue $address) : string
public function format(Address $address) : string
{
$result = '';
@@ -31,12 +31,12 @@ namespace Espo\Classes\AddressFormatters;
use Espo\Core\FieldUtils\Address\{
AddressFormatter,
AddressValue,
Address,
};
class Formatter3 implements AddressFormatter
{
public function format(AddressValue $address) : string
public function format(Address $address) : string
{
$result = '';
@@ -31,12 +31,12 @@ namespace Espo\Classes\AddressFormatters;
use Espo\Core\FieldUtils\Address\{
AddressFormatter,
AddressValue,
Address,
};
class Formatter4 implements AddressFormatter
{
public function format(AddressValue $address) : string
public function format(Address $address) : string
{
$result = '';
@@ -42,7 +42,7 @@ use Espo\Core\{
Utils\Metadata,
FieldUtils\Currency\CurrencyConfigDataProvider,
FieldUtils\Currency\CurrencyConverter,
FieldUtils\Currency\CurrencyValue,
FieldUtils\Currency\Currency,
FieldUtils\Currency\CurrencyRates,
};
@@ -139,7 +139,7 @@ class ConvertCurrency implements Action
continue;
}
$value = new CurrencyValue($amount, $code);
$value = new Currency($amount, $code);
$convertedValue = $this->currencyConverter->convertWithRates($value, $targetCurrency, $rates);
@@ -32,7 +32,7 @@ namespace Espo\Core\FieldUtils\Address;
/**
* An address value.
*/
class AddressValue
class Address
{
protected $street = null;
@@ -157,8 +157,8 @@ class AddressValue
return $obj;
}
public static function createBuilder() : AddressValueBuilder
public static function createBuilder() : AddressBuilder
{
return new AddressValueBuilder();
return new AddressBuilder();
}
}
@@ -31,11 +31,11 @@ namespace Espo\Core\FieldUtils\Address;
use StdClass;
class AddressValueAttributeExtractor // implements \Espo\ORM\Value\AttributeExtractable
class AddressAttributeExtractor // implements \Espo\ORM\Value\AttributeExtractable
{
private $value;
public function __construct(AddressValue $value)
public function __construct(Address $value)
{
$this->value = $value;
}
@@ -32,7 +32,7 @@ namespace Espo\Core\FieldUtils\Address;
/**
* An address value builder.
*/
class AddressValueBuilder
class AddressBuilder
{
protected $street;
@@ -44,7 +44,7 @@ class AddressValueBuilder
protected $postalCode;
public function clone(AddressValue $address) : self
public function clone(Address $address) : self
{
$this->setStreet($address->getStreet());
$this->setCity($address->getCity());
@@ -90,9 +90,9 @@ class AddressValueBuilder
return $this;
}
public function build() : AddressValue
public function build() : Address
{
return AddressValue::fromRaw([
return Address::fromRaw([
'street' => $this->street,
'city' => $this->city,
'country' => $this->country,
@@ -33,17 +33,16 @@ use Espo\{
ORM\Entity,
};
class AddressValueFactory // implements \Espo\ORM\Value\ValueFactory
class AddressFactory // implements \Espo\ORM\Value\ValueFactory
{
public function isCreatableFromEntity(Entity $entity, string $field) : bool
{
return true;
}
public function createFromEntity(Entity $entity, string $field) : AddressValue
public function createFromEntity(Entity $entity, string $field) : Address
{
return (new AddressValueBuilder())
return (new AddressBuilder())
->setStreet($entity->get($field . 'Street'))
->setCity($entity->get($field . 'City'))
->setCountry($entity->get($field . 'Country'))
@@ -34,5 +34,5 @@ namespace Espo\Core\FieldUtils\Address;
*/
interface AddressFormatter
{
public function format(AddressValue $address) : string;
public function format(Address $address) : string;
}
@@ -1,199 +0,0 @@
<?php
/************************************************************************
* This file is part of EspoCRM.
*
* EspoCRM - Open Source CRM application.
* Copyright (C) 2014-2021 Yurii Kuznietsov, Taras Machyshyn, Oleksii Avramenko
* Website: https://www.espocrm.com
*
* EspoCRM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* EspoCRM 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with EspoCRM. If not, see http://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 General Public License version 3.
*
* In accordance with Section 7(b) of the GNU General Public License version 3,
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
************************************************************************/
namespace Espo\Core\FieldUtils\Address;
use Espo\{
ORM\Entity,
};
use StdClass;
/**
* An address value.
*/
class AddressValue
{
protected $street = null;
protected $city = null;
protected $country = null;
protected $state = null;
protected $postalCode = null;
public function hasStreet() : bool
{
return $this->street !== null;
}
public function hasCity() : bool
{
return $this->city !== null;
}
public function hasCountry() : bool
{
return $this->country !== null;
}
public function hasState() : bool
{
return $this->state !== null;
}
public function hasPostalCode() : bool
{
return $this->postalCode !== null;
}
public function getStreet() : ?string
{
return $this->street;
}
public function getCity() : ?string
{
return $this->city;
}
public function getCountry() : ?string
{
return $this->country;
}
public function getState() : ?string
{
return $this->state;
}
public function getPostalCode() : ?string
{
return $this->postalCode;
}
public function withStreet(?string $street) : self
{
$newAddress = self::createBuilder()
->clone($this)
->setStreet($street)
->build();
return $newAddress;
}
public function withCity(?string $city) : self
{
$newAddress = self::createBuilder()
->clone($this)
->setCity($city)
->build();
return $newAddress;
}
public function withCountry(?string $country) : self
{
$newAddress = self::createBuilder()
->clone($this)
->setCountry($country)
->build();
return $newAddress;
}
public function withState(?string $state) : self
{
$newAddress = self::createBuilder()
->clone($this)
->setState($state)
->build();
return $newAddress;
}
public function withPostalCode(?string $postalCode) : self
{
$newAddress = self::createBuilder()
->clone($this)
->setPostalCode($postalCode)
->build();
return $newAddress;
}
public function getValueMap(string $field) : StdClass
{
return (object) [
$field . 'Street' => $this->street,
$field . 'City' => $this->city,
$field . 'Country' => $this->country,
$field . 'State' => $this->state,
$field . 'PostalCode' => $this->postalCode,
];
}
public static function isSetInEntity(Entity $entity, string $field) : bool
{
return true;
}
public static function fromEntity(Entity $entity, string $field) : self
{
$obj = new self();
$obj->street = $entity->get($field . 'Street');
$obj->city = $entity->get($field . 'City');
$obj->country = $entity->get($field . 'Country');
$obj->state = $entity->get($field . 'State');
$obj->postalCode = $entity->get($field . 'PostalCode');
return $obj;
}
public static function fromRaw(array $raw) : self
{
$obj = new self();
$obj->street = $raw['street'] ?? null;
$obj->city = $raw['city'] ?? null;
$obj->country = $raw['country'] ?? null;
$obj->state = $raw['state'] ?? null;
$obj->postalCode = $raw['postalCode'] ?? null;
return $obj;
}
public static function createBuilder() : AddressBuilder
{
return new AddressBuilder();
}
}
@@ -34,7 +34,7 @@ use RuntimeException;
/**
* A currency value.
*/
class CurrencyValue
class Currency
{
private $amount;
@@ -31,11 +31,11 @@ namespace Espo\Core\FieldUtils\Currency;
use StdClass;
class CurrencyValueAttributeExtractor // implements \Espo\ORM\Value\AttributeExtractable
class CurrencyAttributeExtractor // implements \Espo\ORM\Value\AttributeExtractable
{
private $value;
public function __construct(CurrencyValue $value)
public function __construct(Currency $value)
{
$this->value = $value;
}
@@ -48,7 +48,7 @@ class CurrencyConverter
*
* @throws RuntimeException
*/
public function convert(CurrencyValue $value, string $targetCurrencyCode) : CurrencyValue
public function convert(Currency $value, string $targetCurrencyCode) : Currency
{
$amount = $value->getAmount();
@@ -64,13 +64,13 @@ class CurrencyConverter
$amount /= $targetRate;
return new CurrencyValue($amount, $targetCurrencyCode);
return new Currency($amount, $targetCurrencyCode);
}
/**
* Convert a currency value to the system default currency.
*/
public function convertToDefault(CurrencyValue $value) : CurrencyValue
public function convertToDefault(Currency $value) : Currency
{
$targetCurrencyCode = $this->configDataProvider->getDefaultCurrency();
@@ -84,8 +84,8 @@ class CurrencyConverter
* @throws RuntimeException
*/
public function convertWithRates(
CurrencyValue $value, string $targetCurrencyCode, CurrencyRates $rates
) : CurrencyValue {
Currency $value, string $targetCurrencyCode, CurrencyRates $rates
) : Currency {
$amount = $value->getAmount();
@@ -107,6 +107,6 @@ class CurrencyConverter
$amount /= $targetRate;
return new CurrencyValue($amount, $targetCurrencyCode);
return new Currency($amount, $targetCurrencyCode);
}
}
@@ -35,20 +35,20 @@ use Espo\{
use RuntimeException;
class CurrencyValueFactory // implements \Espo\ORM\Value\ValueFactory
class CurrencyFactory // implements \Espo\ORM\Value\ValueFactory
{
public function isCreatableFromEntity(Entity $entity, string $field) : bool
{
return $entity->has($field) && $entity->has($field . 'Currency');
}
public function createFromEntity(Entity $entity, string $field) : CurrencyValue
public function createFromEntity(Entity $entity, string $field) : Currency
{
if (!$this->isCreatableFromEntity($entity, $field)) {
throw new RuntimeException();
}
return new CurrencyValue(
return new Currency(
$entity->get($field),
$entity->get($field . 'Currency')
);
@@ -43,7 +43,7 @@ use Espo\Core\{
Utils\Metadata,
FieldUtils\Currency\CurrencyConfigDataProvider,
FieldUtils\Currency\CurrencyConverter,
FieldUtils\Currency\CurrencyValue,
FieldUtils\Currency\Currency,
FieldUtils\Currency\CurrencyRates,
};
@@ -172,7 +172,7 @@ class MassConvertCurrency implements MassAction
continue;
}
$value = new CurrencyValue($amount, $code);
$value = new Currency($amount, $code);
$convertedValue = $this->currencyConverter->convertWithRates($value, $targetCurrency, $rates);
@@ -40,7 +40,7 @@ use Espo\Core\{
FileStorage\Manager as FileStorageManager,
Utils\File\Manager as FileManager,
ORM\EntityManager,
FieldUtils\Address\AddressValue,
FieldUtils\Address\Address,
FieldUtils\Address\AddressFormatterFactory,
};
@@ -523,7 +523,7 @@ class Xlsx
}
}
else if ($type == 'address') {
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->setStreet($row[$name . 'Street'] ?? null)
->setCity($row[$name . 'City'] ?? null)
->setState($row[$name . 'State'] ?? null)
@@ -30,7 +30,7 @@
namespace tests\unit\Espo\Core\FieldUtils\Address;
use Espo\Core\{
FieldUtils\Address\AddressValue,
FieldUtils\Address\Address,
};
use Espo\Classes\{
@@ -49,7 +49,7 @@ class AddressFormattersTest extends \PHPUnit\Framework\TestCase
public function testFormat1All()
{
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -71,7 +71,7 @@ class AddressFormattersTest extends \PHPUnit\Framework\TestCase
public function testFormat1NoState()
{
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -93,7 +93,7 @@ class AddressFormattersTest extends \PHPUnit\Framework\TestCase
public function testFormat2All()
{
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -115,7 +115,7 @@ class AddressFormattersTest extends \PHPUnit\Framework\TestCase
public function testFormat3All()
{
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -137,7 +137,7 @@ class AddressFormattersTest extends \PHPUnit\Framework\TestCase
public function testFormat4All()
{
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -30,13 +30,13 @@
namespace tests\unit\Espo\Core\FieldUtils\Address;
use Espo\Core\{
FieldUtils\Address\AddressValue,
FieldUtils\Address\AddressValueFactory,
FieldUtils\Address\Address,
FieldUtils\Address\AddressFactory,
};
use Espo\ORM\Entity;
class AddressValueTest extends \PHPUnit\Framework\TestCase
class AddressTest extends \PHPUnit\Framework\TestCase
{
protected function setUp() : void
{
@@ -45,7 +45,7 @@ class AddressValueTest extends \PHPUnit\Framework\TestCase
public function testAddress1()
{
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -62,7 +62,7 @@ class AddressValueTest extends \PHPUnit\Framework\TestCase
public function testBuilderClone()
{
$addressOriginal = AddressValue::createBuilder()
$addressOriginal = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -70,7 +70,7 @@ class AddressValueTest extends \PHPUnit\Framework\TestCase
->setPostalCode('postalCode')
->build();
$address = AddressValue::createBuilder()
$address = Address::createBuilder()
->clone($addressOriginal)
->build();
@@ -83,7 +83,7 @@ class AddressValueTest extends \PHPUnit\Framework\TestCase
public function testAddressWith()
{
$addressOriginal = AddressValue::createBuilder()
$addressOriginal = Address::createBuilder()
->setStreet('street')
->setCity('city')
->setCountry('country')
@@ -112,7 +112,7 @@ class AddressValueTest extends \PHPUnit\Framework\TestCase
['addressPostalCode', null],
]);
$factory = new AddressValueFactory();
$factory = new AddressFactory();
$address = $factory->createFromEntity($entity, 'address');
@@ -30,7 +30,7 @@
namespace tests\unit\Espo\Core\FieldUtils\Currency;
use Espo\Core\{
FieldUtils\Currency\CurrencyValue,
FieldUtils\Currency\Currency,
FieldUtils\Currency\CurrencyConverter,
FieldUtils\Currency\CurrencyConfigDataProvider,
FieldUtils\Currency\CurrencyRates,
@@ -61,7 +61,7 @@ class CurrencyConverterTest extends \PHPUnit\Framework\TestCase
['EUR', 1.2],
]);
$value = new CurrencyValue(2.0, 'USD');
$value = new Currency(2.0, 'USD');
$converer = new CurrencyConverter($currencyConfigDataProvider);
@@ -91,7 +91,7 @@ class CurrencyConverterTest extends \PHPUnit\Framework\TestCase
['UAH', 0.035],
]);
$value = new CurrencyValue(2.0, 'UAH');
$value = new Currency(2.0, 'UAH');
$converer = new CurrencyConverter($currencyConfigDataProvider);
@@ -125,7 +125,7 @@ class CurrencyConverterTest extends \PHPUnit\Framework\TestCase
['EUR', 1.2],
]);
$value = new CurrencyValue(2.0, 'EUR');
$value = new Currency(2.0, 'EUR');
$converer = new CurrencyConverter($currencyConfigDataProvider);
@@ -146,7 +146,7 @@ class CurrencyConverterTest extends \PHPUnit\Framework\TestCase
'UAH' => 0.035,
]);
$value = new CurrencyValue(2.0, 'UAH');
$value = new Currency(2.0, 'UAH');
$converer = new CurrencyConverter($currencyConfigDataProvider);
@@ -30,8 +30,8 @@
namespace tests\unit\Espo\Core\FieldUtils\Currency;
use Espo\Core\{
FieldUtils\Currency\CurrencyValue,
FieldUtils\Currency\CurrencyValueFactory,
FieldUtils\Currency\Currency,
FieldUtils\Currency\CurrencyFactory,
};
use Espo\{
@@ -40,7 +40,7 @@ use Espo\{
use RuntimeException;
class CurrencyValueTest extends \PHPUnit\Framework\TestCase
class CurrencyTest extends \PHPUnit\Framework\TestCase
{
protected function setUp() : void
{
@@ -48,7 +48,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
public function testValue()
{
$value = new CurrencyValue(2.0, 'USD');
$value = new Currency(2.0, 'USD');
$this->assertEquals(2.0, $value->getAmount());
@@ -57,8 +57,8 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
public function testAdd()
{
$value = (new CurrencyValue(2.0, 'USD'))->add(
new CurrencyValue(1.0, 'USD')
$value = (new Currency(2.0, 'USD'))->add(
new Currency(1.0, 'USD')
);
$this->assertEquals(3.0, $value->getAmount());
@@ -68,8 +68,8 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
public function testSubtract()
{
$value = (new CurrencyValue(2.0, 'USD'))->subtract(
new CurrencyValue(3.0, 'USD')
$value = (new Currency(2.0, 'USD'))->subtract(
new Currency(3.0, 'USD')
);
$this->assertEquals(-1.0, $value->getAmount());
@@ -79,7 +79,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
public function testMultiply()
{
$value = (new CurrencyValue(2.0, 'USD'))->multiply(3.0);
$value = (new Currency(2.0, 'USD'))->multiply(3.0);
$this->assertEquals(6.0, $value->getAmount());
@@ -88,7 +88,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
public function testDivide()
{
$value = (new CurrencyValue(6.0, 'USD'))->divide(3.0);
$value = (new Currency(6.0, 'USD'))->divide(3.0);
$this->assertEquals(2.0, $value->getAmount());
@@ -97,7 +97,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
public function testRound()
{
$value = (new CurrencyValue(2.306, 'USD'))->round(2);
$value = (new Currency(2.306, 'USD'))->round(2);
$this->assertEquals(2.31, $value->getAmount());
@@ -108,8 +108,8 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
{
$this->expectException(RuntimeException::class);
(new CurrencyValue(2.0, 'USD'))->add(
new CurrencyValue(1.0, 'EUR')
(new Currency(2.0, 'USD'))->add(
new Currency(1.0, 'EUR')
);
}
@@ -117,7 +117,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
{
$this->expectException(RuntimeException::class);
new CurrencyValue(2.0, '');
new Currency(2.0, '');
}
public function testCreateFromEntity()
@@ -140,7 +140,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
['testCurrency', true],
]);
$factory = new CurrencyValueFactory();
$factory = new CurrencyFactory();
$value = $factory->createFromEntity($entity, 'test');
@@ -160,7 +160,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
['testCurrency', true],
]);
$factory = new CurrencyValueFactory();
$factory = new CurrencyFactory();
$this->assertTrue(
$factory->isCreatableFromEntity($entity, 'test')
@@ -179,7 +179,7 @@ class CurrencyValueTest extends \PHPUnit\Framework\TestCase
['testCurrency', False],
]);
$factory = new CurrencyValueFactory();
$factory = new CurrencyFactory();
$this->assertFalse(
$factory->isCreatableFromEntity($entity, 'test')