Address formatter
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\Classes\AddressFormatters;
|
||||
|
||||
use Espo\Core\FieldUtils\Address\{
|
||||
AddressFormatter,
|
||||
Address,
|
||||
};
|
||||
|
||||
class Formatter1 implements AddressFormatter
|
||||
{
|
||||
public function format(Address $address) : string
|
||||
{
|
||||
$result = '';
|
||||
|
||||
$street = $address->getStreet();
|
||||
$city = $address->getCity();
|
||||
$country = $address->getCountry();
|
||||
$state = $address->getState();
|
||||
$postalCode = $address->getPostalCode();
|
||||
|
||||
if ($street) {
|
||||
$result .= $street;
|
||||
}
|
||||
|
||||
if ($city || $state || $postalCode) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
if ($city) {
|
||||
$result .= $city;
|
||||
}
|
||||
|
||||
if ($state && $city) {
|
||||
$result .= ', ';
|
||||
}
|
||||
|
||||
if ($state) {
|
||||
$result .= $state;
|
||||
}
|
||||
|
||||
if ($postalCode && ($state || $city)) {
|
||||
$result .= ' ';
|
||||
}
|
||||
|
||||
if ($postalCode) {
|
||||
$result .= $postalCode;
|
||||
}
|
||||
}
|
||||
|
||||
if ($country) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
$result .= $country;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\Classes\AddressFormatters;
|
||||
|
||||
use Espo\Core\FieldUtils\Address\{
|
||||
AddressFormatter,
|
||||
Address,
|
||||
};
|
||||
|
||||
class Formatter2 implements AddressFormatter
|
||||
{
|
||||
public function format(Address $address) : string
|
||||
{
|
||||
$result = '';
|
||||
|
||||
$street = $address->getStreet();
|
||||
$city = $address->getCity();
|
||||
$country = $address->getCountry();
|
||||
$state = $address->getState();
|
||||
$postalCode = $address->getPostalCode();
|
||||
|
||||
if ($street) {
|
||||
$result .= $street;
|
||||
}
|
||||
|
||||
if ($city || $postalCode) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
if ($postalCode) {
|
||||
$result .= $postalCode;
|
||||
}
|
||||
|
||||
if ($postalCode && $city) {
|
||||
$result .= ' ';
|
||||
}
|
||||
|
||||
if ($city) {
|
||||
$result .= $city;
|
||||
}
|
||||
}
|
||||
|
||||
if ($state || $country) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
if ($state) {
|
||||
$result .= $state;
|
||||
}
|
||||
|
||||
if ($state && $country) {
|
||||
$result .= ' ';
|
||||
}
|
||||
|
||||
if ($country) {
|
||||
$result .= $country;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\Classes\AddressFormatters;
|
||||
|
||||
use Espo\Core\FieldUtils\Address\{
|
||||
AddressFormatter,
|
||||
Address,
|
||||
};
|
||||
|
||||
class Formatter3 implements AddressFormatter
|
||||
{
|
||||
public function format(Address $address) : string
|
||||
{
|
||||
$result = '';
|
||||
|
||||
$street = $address->getStreet();
|
||||
$city = $address->getCity();
|
||||
$country = $address->getCountry();
|
||||
$state = $address->getState();
|
||||
$postalCode = $address->getPostalCode();
|
||||
|
||||
if ($country) {
|
||||
$result .= $country;
|
||||
}
|
||||
|
||||
if ($city || $state || $postalCode) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
if ($state) {
|
||||
$result .= $state;
|
||||
}
|
||||
|
||||
if ($state && $postalCode) {
|
||||
$result .= ' ';
|
||||
}
|
||||
|
||||
if ($postalCode) {
|
||||
$result .= $postalCode;
|
||||
}
|
||||
|
||||
if ($city && ($state || $postalCode)) {
|
||||
$result .= ' ';
|
||||
}
|
||||
|
||||
if ($city) {
|
||||
$result .= $city;
|
||||
}
|
||||
}
|
||||
|
||||
if ($street) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
$result .= $street;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\Classes\AddressFormatters;
|
||||
|
||||
use Espo\Core\FieldUtils\Address\{
|
||||
AddressFormatter,
|
||||
Address,
|
||||
};
|
||||
|
||||
class Formatter4 implements AddressFormatter
|
||||
{
|
||||
public function format(Address $address) : string
|
||||
{
|
||||
$result = '';
|
||||
|
||||
$street = $address->getStreet();
|
||||
$city = $address->getCity();
|
||||
$country = $address->getCountry();
|
||||
$state = $address->getState();
|
||||
$postalCode = $address->getPostalCode();
|
||||
|
||||
if ($street) {
|
||||
$result .= $street;
|
||||
}
|
||||
|
||||
if ($city) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
$result .= $city;
|
||||
}
|
||||
|
||||
if ($country || $state || $postalCode) {
|
||||
if ($result) {
|
||||
$result .= "\n";
|
||||
}
|
||||
|
||||
if ($country) {
|
||||
$result .= $country;
|
||||
}
|
||||
|
||||
if ($state && $country) {
|
||||
$result .= ' - ';
|
||||
}
|
||||
|
||||
if ($state) {
|
||||
$result .= $state;
|
||||
}
|
||||
|
||||
if ($postalCode && ($state || $country)) {
|
||||
$result .= ' ';
|
||||
}
|
||||
|
||||
if ($postalCode) {
|
||||
$result .= $postalCode;
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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,
|
||||
};
|
||||
|
||||
/**
|
||||
* An address.
|
||||
*/
|
||||
class Address
|
||||
{
|
||||
protected $street;
|
||||
|
||||
protected $city;
|
||||
|
||||
protected $country;
|
||||
|
||||
protected $state;
|
||||
|
||||
protected $portalCode;
|
||||
|
||||
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 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'];
|
||||
$obj->city = $raw['city'];
|
||||
$obj->country = $raw['country'];
|
||||
$obj->state = $raw['state'];
|
||||
$obj->postalCode = $raw['postalCode'];
|
||||
|
||||
return $obj;
|
||||
}
|
||||
|
||||
public static function createBuilder() : AddressBuilder
|
||||
{
|
||||
return new AddressBuilder();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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;
|
||||
|
||||
/**
|
||||
* An address builder.
|
||||
*/
|
||||
class AddressBuilder
|
||||
{
|
||||
protected $street;
|
||||
|
||||
protected $city;
|
||||
|
||||
protected $country;
|
||||
|
||||
protected $state;
|
||||
|
||||
protected $portalCode;
|
||||
|
||||
public function clone(Address $address) : self
|
||||
{
|
||||
$this->setStreet($address->getStreet());
|
||||
$this->setCity($address->getCity());
|
||||
$this->setCountry($address->getCountry());
|
||||
$this->setState($address->getState());
|
||||
$this->setPostalCode($address->getPostalCode());
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setStreet(?string $street) : self
|
||||
{
|
||||
$this->street = $street;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCity(?string $city) : self
|
||||
{
|
||||
$this->city = $city;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setCountry(?string $country) : self
|
||||
{
|
||||
$this->country = $country;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setState(?string $state) : self
|
||||
{
|
||||
$this->state = $state;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setPostalCode(?string $postalCode) : self
|
||||
{
|
||||
$this->postalCode = $postalCode;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function build() : Address
|
||||
{
|
||||
return Address::fromRaw([
|
||||
'street' => $this->street,
|
||||
'city' => $this->city,
|
||||
'country' => $this->country,
|
||||
'state' => $this->state,
|
||||
'postalCode' => $this->postalCode,
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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;
|
||||
|
||||
/**
|
||||
* An address formatter.
|
||||
*/
|
||||
interface AddressFormatter
|
||||
{
|
||||
public function format(Address $address) : string;
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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 RuntimeException;
|
||||
|
||||
use Espo\Core\{
|
||||
InjectableFactory,
|
||||
Utils\Config,
|
||||
};
|
||||
|
||||
class AddressFormatterFactory
|
||||
{
|
||||
private $metadataProvider;
|
||||
|
||||
private $injectableFactory;
|
||||
|
||||
private $config;
|
||||
|
||||
public function __construct(
|
||||
AddressFormatterMetadataProvider $metadataProvider,
|
||||
InjectableFactory $injectableFactory,
|
||||
Config $config
|
||||
) {
|
||||
$this->metadataProvider = $metadataProvider;
|
||||
$this->injectableFactory = $injectableFactory;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function create(int $format) : AddressFormatter
|
||||
{
|
||||
$className = $this->metadataProvider->getFormatterClassName($format);
|
||||
|
||||
if (!$className) {
|
||||
throw new RuntimeException("Unknown address format '{$format}'.");
|
||||
}
|
||||
|
||||
return $this->injectableFactory->create($className);
|
||||
}
|
||||
|
||||
public function createDefault() : AddressFormatter
|
||||
{
|
||||
$format = $this->config->get('addressFormat') ?? 1;
|
||||
|
||||
return $this->create($format);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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\Core\Utils\Metadata;
|
||||
|
||||
class AddressFormatterMetadataProvider
|
||||
{
|
||||
private $metadata;
|
||||
|
||||
public function __construct(Metadata $metadata)
|
||||
{
|
||||
$this->metadata = $metadata;
|
||||
}
|
||||
|
||||
public function getFormatterClassName(int $format) : ?string
|
||||
{
|
||||
return $this->metadata->get([
|
||||
'app', 'addressFormats', strval($format), 'formatterClassName',
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"1": {
|
||||
"formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter1"
|
||||
},
|
||||
"2": {
|
||||
"formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter2"
|
||||
},
|
||||
"3": {
|
||||
"formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter3"
|
||||
},
|
||||
"4": {
|
||||
"formatterClassName": "Espo\\Classes\\AddressFormatters\\Formatter4"
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@
|
||||
["app", "appParams"],
|
||||
["app", "cleanup"],
|
||||
["app", "pdfEngines", "__ANY__", "implementationClassNameMap"],
|
||||
["app", "addressFormats", "__ANY__", "formatterClassName"],
|
||||
["app", "auth2FAMethods", "__ANY__", "implementationClassName"],
|
||||
["app", "auth2FAMethods", "__ANY__", "implementationUserClassName"],
|
||||
["authenticationMethods", "__ANY__", "implementationClassName"]
|
||||
|
||||
@@ -40,6 +40,8 @@ use Espo\Core\{
|
||||
FileStorage\Manager as FileStorageManager,
|
||||
Utils\File\Manager as FileManager,
|
||||
ORM\EntityManager,
|
||||
FieldUtils\Address\Address,
|
||||
FieldUtils\Address\AddressFormatterFactory,
|
||||
};
|
||||
|
||||
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
||||
@@ -62,6 +64,7 @@ class Xlsx
|
||||
protected $entityManager;
|
||||
protected $fileStorageManager;
|
||||
protected $fileManager;
|
||||
protected $addressFormatterFactory;
|
||||
|
||||
public function __construct(
|
||||
Config $config,
|
||||
@@ -70,7 +73,8 @@ class Xlsx
|
||||
DateTimeUtil $dateTime,
|
||||
EntityManager $entityManager,
|
||||
FileStorageManager $fileStorageManager,
|
||||
FileManager $fileManager
|
||||
FileManager $fileManager,
|
||||
AddressFormatterFactory $addressFormatterFactory
|
||||
) {
|
||||
$this->config = $config;
|
||||
$this->metadata = $metadata;
|
||||
@@ -79,21 +83,7 @@ class Xlsx
|
||||
$this->entityManager = $entityManager;
|
||||
$this->fileStorageManager = $fileStorageManager;
|
||||
$this->fileManager = $fileManager;
|
||||
}
|
||||
|
||||
protected function getConfig()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
protected function getMetadata()
|
||||
{
|
||||
return $this->metadata;
|
||||
}
|
||||
|
||||
protected function getEntityManager()
|
||||
{
|
||||
return $this->entityManager;
|
||||
$this->addressFormatterFactory = $addressFormatterFactory;
|
||||
}
|
||||
|
||||
public function loadAdditionalFields(Entity $entity, $fieldList)
|
||||
@@ -124,7 +114,7 @@ class Xlsx
|
||||
}
|
||||
}
|
||||
foreach ($fieldList as $field) {
|
||||
$fieldType = $this->getMetadata()->get(['entityDefs', $entity->getEntityType(), 'fields', $field, 'type']);
|
||||
$fieldType = $this->metadata->get(['entityDefs', $entity->getEntityType(), 'fields', $field, 'type']);
|
||||
|
||||
if ($fieldType === 'linkMultiple' || $fieldType === 'attachmentMultiple') {
|
||||
if (!$entity->has($field . 'Ids')) {
|
||||
@@ -138,7 +128,7 @@ class Xlsx
|
||||
{
|
||||
if ($exportAllFields) {
|
||||
foreach ($fieldList as $i => $field) {
|
||||
$type = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $field, 'type']);
|
||||
$type = $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']);
|
||||
if (in_array($type, ['linkMultiple', 'attachmentMultiple'])) {
|
||||
unset($fieldList[$i]);
|
||||
}
|
||||
@@ -156,7 +146,7 @@ class Xlsx
|
||||
$attributeList[] = 'id';
|
||||
}
|
||||
|
||||
$linkDefs = $this->getMetadata()->get(['entityDefs', $entityType, 'links']);
|
||||
$linkDefs = $this->metadata->get(['entityDefs', $entityType, 'links']);
|
||||
|
||||
if (is_array($linkDefs)) {
|
||||
foreach ($linkDefs as $link => $defs) {
|
||||
@@ -168,7 +158,7 @@ class Xlsx
|
||||
$linkList[] = $link;
|
||||
}
|
||||
else if ($defs['type'] === 'belongsTo' && !empty($defs['noJoin'])) {
|
||||
if ($this->getMetadata()->get(['entityDefs', $entityType, 'fields', $link])) {
|
||||
if ($this->metadata->get(['entityDefs', $entityType, 'fields', $link])) {
|
||||
$linkList[] = $link;
|
||||
}
|
||||
}
|
||||
@@ -182,7 +172,7 @@ class Xlsx
|
||||
}
|
||||
|
||||
foreach ($fieldList as $field) {
|
||||
$type = $this->getMetadata()->get(['entityDefs', $entityType, 'fields', $field, 'type']);
|
||||
$type = $this->metadata->get(['entityDefs', $entityType, 'fields', $field, 'type']);
|
||||
|
||||
if ($type === 'currencyConverted') {
|
||||
if (!in_array($field, $attributeList)) {
|
||||
@@ -402,7 +392,7 @@ class Xlsx
|
||||
if (array_key_exists($name.'Currency', $row) && array_key_exists($name, $row)) {
|
||||
$sheet->setCellValue("$col$rowNumber", $row[$name] ? $row[$name] : '');
|
||||
|
||||
$currency = $row[$name . 'Currency'] ?? $this->getConfig()->get('defaultCurrency');
|
||||
$currency = $row[$name . 'Currency'] ?? $this->config->get('defaultCurrency');
|
||||
|
||||
$sheet->getStyle("$col$rowNumber")
|
||||
->getNumberFormat()
|
||||
@@ -413,7 +403,7 @@ class Xlsx
|
||||
}
|
||||
else if ($type == 'currencyConverted') {
|
||||
if (array_key_exists($name, $row)) {
|
||||
$currency = $this->getConfig()->get('defaultCurrency');
|
||||
$currency = $this->config->get('defaultCurrency');
|
||||
|
||||
$sheet->getStyle("$col$rowNumber")
|
||||
->getNumberFormat()
|
||||
@@ -481,7 +471,7 @@ class Xlsx
|
||||
}
|
||||
else if ($type == 'image') {
|
||||
if (isset($row[$name . 'Id']) && $row[$name . 'Id']) {
|
||||
$attachment = $this->getEntityManager()->getEntity('Attachment', $row[$name . 'Id']);
|
||||
$attachment = $this->entityManager->getEntity('Attachment', $row[$name . 'Id']);
|
||||
|
||||
if ($attachment) {
|
||||
$objDrawing = new Drawing();
|
||||
@@ -533,47 +523,17 @@ class Xlsx
|
||||
}
|
||||
}
|
||||
else if ($type == 'address') {
|
||||
$value = '';
|
||||
$address = Address::createBuilder()
|
||||
->setStreet($row[$name . 'Street'] ?? null)
|
||||
->setCity($row[$name . 'City'] ?? null)
|
||||
->setState($row[$name . 'State'] ?? null)
|
||||
->setCountry($row[$name . 'Country'] ?? null)
|
||||
->setPostalCode($row[$name . 'PostalCode'] ?? null)
|
||||
->build();
|
||||
|
||||
if (!empty($row[$name . 'Street'])) {
|
||||
$value = $value .= $row[$name.'Street'];
|
||||
}
|
||||
$formatter = $this->addressFormatterFactory->createDefault();
|
||||
|
||||
if (!empty($row[$name.'City']) || !empty($row[$name.'State']) || !empty($row[$name.'PostalCode'])) {
|
||||
if ($value) {
|
||||
$value .= "\n";
|
||||
}
|
||||
|
||||
if (!empty($row[$name.'City'])) {
|
||||
$value .= $row[$name.'City'];
|
||||
|
||||
if (
|
||||
!empty($row[$name.'State']) || !empty($row[$name.'PostalCode'])
|
||||
) {
|
||||
$value .= ', ';
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($row[$name.'State'])) {
|
||||
$value .= $row[$name.'State'];
|
||||
|
||||
if (!empty($row[$name.'PostalCode'])) {
|
||||
$value .= ' ';
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($row[$name.'PostalCode'])) {
|
||||
$value .= $row[$name.'PostalCode'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($row[$name.'Country'])) {
|
||||
if ($value) {
|
||||
$value .= "\n";
|
||||
}
|
||||
|
||||
$value .= $row[$name.'Country'];
|
||||
}
|
||||
$value = $formatter->format($address);
|
||||
|
||||
$sheet->setCellValue("$col$rowNumber", $value);
|
||||
}
|
||||
@@ -656,7 +616,7 @@ class Xlsx
|
||||
|
||||
if ($name == 'name') {
|
||||
if (array_key_exists('id', $row)) {
|
||||
$link = $this->getConfig()->getSiteUrl() . "/#".$entityType . "/view/" . $row['id'];
|
||||
$link = $this->config->getSiteUrl() . "/#".$entityType . "/view/" . $row['id'];
|
||||
}
|
||||
}
|
||||
else if ($type == 'url') {
|
||||
@@ -669,33 +629,33 @@ class Xlsx
|
||||
$foreignEntity = null;
|
||||
|
||||
if (!$isForeign) {
|
||||
$foreignEntity = $this->getMetadata()->get(
|
||||
$foreignEntity = $this->metadata->get(
|
||||
['entityDefs', $entityType, 'links', $name, 'entity']
|
||||
);
|
||||
}
|
||||
else {
|
||||
$foreignEntity1 = $this->getMetadata()->get(
|
||||
$foreignEntity1 = $this->metadata->get(
|
||||
['entityDefs', $entityType, 'links', $foreignLink, 'entity']
|
||||
);
|
||||
|
||||
$foreignEntity = $this->getMetadata()->get(
|
||||
$foreignEntity = $this->metadata->get(
|
||||
['entityDefs', $foreignEntity1, 'links', $foreignField, 'entity']
|
||||
);
|
||||
}
|
||||
|
||||
if ($foreignEntity) {
|
||||
$link = $this->getConfig()->getSiteUrl() . "/#" . $foreignEntity. "/view/". $row[$name.'Id'];
|
||||
$link = $this->config->getSiteUrl() . "/#" . $foreignEntity. "/view/". $row[$name.'Id'];
|
||||
}
|
||||
}
|
||||
}
|
||||
else if ($type == 'file') {
|
||||
if (array_key_exists($name.'Id', $row)) {
|
||||
$link = $this->getConfig()->getSiteUrl() . "/?entryPoint=download&id=" . $row[$name.'Id'];
|
||||
$link = $this->config->getSiteUrl() . "/?entryPoint=download&id=" . $row[$name.'Id'];
|
||||
}
|
||||
}
|
||||
else if ($type == 'linkParent') {
|
||||
if (array_key_exists($name.'Id', $row) && array_key_exists($name.'Type', $row)) {
|
||||
$link = $this->getConfig()->getSiteUrl() . "/#".$row[$name.'Type']."/view/". $row[$name.'Id'];
|
||||
$link = $this->config->getSiteUrl() . "/#".$row[$name.'Type']."/view/". $row[$name.'Id'];
|
||||
}
|
||||
}
|
||||
else if ($type == 'phone') {
|
||||
@@ -809,9 +769,9 @@ class Xlsx
|
||||
|
||||
protected function getCurrencyFormatCode(string $currency) : string
|
||||
{
|
||||
$currencySymbol = $this->getMetadata()->get(['app', 'currency', 'symbolMap', $currency], '');
|
||||
$currencySymbol = $this->metadata->get(['app', 'currency', 'symbolMap', $currency], '');
|
||||
|
||||
$currencyFormat = $this->getConfig()->get('currencyFormat') ?? 2;
|
||||
$currencyFormat = $this->config->get('currencyFormat') ?? 2;
|
||||
|
||||
if ($currencyFormat == 3) {
|
||||
return '#,##0.00_-"' . $currencySymbol . '"';
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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 tests\integration\Espo\Core\FieldUtils\Address;
|
||||
|
||||
use Espo\Core\FieldUtils\Address\{
|
||||
AddressFormatterFactory,
|
||||
Address,
|
||||
};
|
||||
|
||||
class AddressFormatterTest extends \tests\integration\Core\BaseTestCase
|
||||
{
|
||||
public function testFormatter1()
|
||||
{
|
||||
$formatterFactory = $this->getContainer()->get('injectableFactory')->create(AddressFormatterFactory::class);
|
||||
|
||||
$formatter = $formatterFactory->create(1);
|
||||
|
||||
$address = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$expected =
|
||||
"street\n" .
|
||||
"city, state postalCode\n" .
|
||||
"country";
|
||||
|
||||
$result = $formatter->format($address);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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 tests\unit\Espo\Core\FieldUtils\Address;
|
||||
|
||||
use Espo\Core\{
|
||||
FieldUtils\Address\Address,
|
||||
};
|
||||
|
||||
use Espo\Classes\{
|
||||
AddressFormatters\Formatter1,
|
||||
AddressFormatters\Formatter2,
|
||||
AddressFormatters\Formatter3,
|
||||
AddressFormatters\Formatter4,
|
||||
};
|
||||
|
||||
class AddressFormattersTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected function setUp() : void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function testFormat1All()
|
||||
{
|
||||
$address = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$formatter = new Formatter1();
|
||||
|
||||
$expected =
|
||||
"street\n" .
|
||||
"city, state postalCode\n" .
|
||||
"country";
|
||||
|
||||
$result = $formatter->format($address);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testFormat1NoState()
|
||||
{
|
||||
$address = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState(null)
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$formatter = new Formatter1();
|
||||
|
||||
$expected =
|
||||
"street\n" .
|
||||
"city postalCode\n" .
|
||||
"country";
|
||||
|
||||
$result = $formatter->format($address);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testFormat2All()
|
||||
{
|
||||
$address = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$formatter = new Formatter2();
|
||||
|
||||
$expected =
|
||||
"street\n" .
|
||||
"postalCode city\n" .
|
||||
"state country";
|
||||
|
||||
$result = $formatter->format($address);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testFormat3All()
|
||||
{
|
||||
$address = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$formatter = new Formatter3();
|
||||
|
||||
$expected =
|
||||
"country\n" .
|
||||
"state postalCode city\n" .
|
||||
"street";
|
||||
|
||||
$result = $formatter->format($address);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
|
||||
public function testFormat4All()
|
||||
{
|
||||
$address = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$formatter = new Formatter4();
|
||||
|
||||
$expected =
|
||||
"street\n" .
|
||||
"city\n" .
|
||||
"country - state postalCode";
|
||||
|
||||
$result = $formatter->format($address);
|
||||
|
||||
$this->assertEquals($expected, $result);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM - Open Source CRM application.
|
||||
* Copyright (C) 2014-2020 Yuri Kuznetsov, Taras Machyshyn, Oleksiy 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 tests\unit\Espo\Core\FieldUtils\Address;
|
||||
|
||||
use Espo\Core\{
|
||||
FieldUtils\Address\Address,
|
||||
};
|
||||
|
||||
use Espo\ORM\Entity;
|
||||
|
||||
class AddressTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
protected function setUp() : void
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public function testAddress1()
|
||||
{
|
||||
$address = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$this->assertEquals('street', $address->getStreet());
|
||||
$this->assertEquals('city', $address->getCity());
|
||||
$this->assertEquals('country', $address->getCountry());
|
||||
$this->assertEquals('state', $address->getState());
|
||||
$this->assertEquals('postalCode', $address->getPostalCode());
|
||||
}
|
||||
|
||||
public function testBuilderClone()
|
||||
{
|
||||
$addressOriginal = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$address = Address::createBuilder()
|
||||
->clone($addressOriginal)
|
||||
->build();
|
||||
|
||||
$this->assertEquals('street', $address->getStreet());
|
||||
$this->assertEquals('city', $address->getCity());
|
||||
$this->assertEquals('country', $address->getCountry());
|
||||
$this->assertEquals('state', $address->getState());
|
||||
$this->assertEquals('postalCode', $address->getPostalCode());
|
||||
}
|
||||
|
||||
public function testAddressWith()
|
||||
{
|
||||
$addressOriginal = Address::createBuilder()
|
||||
->setStreet('street')
|
||||
->setCity('city')
|
||||
->setCountry('country')
|
||||
->setState('state')
|
||||
->setPostalCode('postalCode')
|
||||
->build();
|
||||
|
||||
$address = $addressOriginal->withStreet('new street');
|
||||
|
||||
$this->assertEquals('new street', $address->getStreet());
|
||||
$this->assertEquals('city', $address->getCity());
|
||||
}
|
||||
|
||||
public function testFromEntity()
|
||||
{
|
||||
$entity = $this->createMock(Entity::class);
|
||||
|
||||
$entity
|
||||
->expects($this->any())
|
||||
->method('get')
|
||||
->willReturnMap([
|
||||
['addressStreet', 'street'],
|
||||
['addressCity', 'city'],
|
||||
['addressCountry', 'country'],
|
||||
['addressState', null],
|
||||
['addressPostalCode', null],
|
||||
]);
|
||||
|
||||
$address = Address::fromEntity($entity, 'address');
|
||||
|
||||
$this->assertEquals('street', $address->getStreet());
|
||||
$this->assertEquals('city', $address->getCity());
|
||||
$this->assertEquals('country', $address->getCountry());
|
||||
$this->assertEquals(null, $address->getState());
|
||||
$this->assertEquals(null, $address->getPostalCode());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user