Initial xentral_oss_20.3.c9ffacf
This commit is contained in:
@@ -0,0 +1,126 @@
|
||||
<?php
|
||||
|
||||
namespace Xentral\Modules\Country\Gateway;
|
||||
|
||||
use Xentral\Components\Database\Database;
|
||||
use Xentral\Modules\Country\Data\CountryDataValidator;
|
||||
|
||||
final class CountryGateway
|
||||
{
|
||||
/** @var Database $db */
|
||||
private $db;
|
||||
|
||||
/** @var CountryDataValidator */
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* @param Database $database
|
||||
*/
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->db = $database;
|
||||
$this->validator = new CountryDataValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function findAll()
|
||||
{
|
||||
return $this->db->fetchAll('SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM laender AS l;');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function findByName($name)
|
||||
{
|
||||
$this->validator->ensureNameGerman($name);
|
||||
$sql = 'SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM laender AS l
|
||||
WHERE bezeichnung_de LIKE :name OR bezeichnung_en LIKE :name;';
|
||||
|
||||
return $this->db->fetchRow($sql, ['name' => $name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iso2Code
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function findByIso2Code($iso2Code)
|
||||
{
|
||||
$this->validator->ensureIso2($iso2Code);
|
||||
$result = $this->db->fetchRow(
|
||||
'SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM laender AS l
|
||||
WHERE iso = :iso2_code;',
|
||||
['iso2_code' => $iso2Code]
|
||||
);
|
||||
|
||||
// TODO throw Exception
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iso3Code
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function findByIso3Code($iso3Code)
|
||||
{
|
||||
$this->validator->ensureIso3($iso3Code);
|
||||
$result = $this->db->fetchRow(
|
||||
'SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM laender AS l
|
||||
WHERE iso3 = :iso3_code;',
|
||||
['iso3_code' => $iso3Code]
|
||||
);
|
||||
|
||||
// TODO throw Exception
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $numericCode
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function findByNumericCode($numericCode)
|
||||
{
|
||||
$this->validator->ensureIsoNumeric($numericCode);
|
||||
$result = $this->db->fetchRow(
|
||||
'SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM laender AS l
|
||||
WHERE num_code = :num_code;',
|
||||
['num_code' => $numericCode]
|
||||
);
|
||||
|
||||
// TODO throw Exception
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns sql-ready 'column AS arrayName' structure
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getColumnArraySQLMapping()
|
||||
{
|
||||
return
|
||||
'l.iso AS iso2_code,
|
||||
l.iso3 AS iso3_code,
|
||||
l.num_code AS num_code,
|
||||
l.bezeichnung_de AS name_de,
|
||||
l.bezeichnung_en AS name_en,
|
||||
l.eu AS is_eu ';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Xentral\Modules\Country\Gateway;
|
||||
|
||||
use Xentral\Components\Database\Database;
|
||||
use Xentral\Modules\Country\Data\CountryDataValidator;
|
||||
|
||||
final class StateGateway
|
||||
{
|
||||
/** @var Database $db */
|
||||
private $db;
|
||||
|
||||
/** @var CountryDataValidator */
|
||||
private $validator;
|
||||
|
||||
/**
|
||||
* @param Database $database
|
||||
*/
|
||||
public function __construct(Database $database)
|
||||
{
|
||||
$this->db = $database;
|
||||
$this->validator = new CountryDataValidator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name state name
|
||||
* @param string $iso2CountryCode iso2 country code
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function findByNameAndIso2CountryCode(string $name, string $iso2CountryCode): array
|
||||
{
|
||||
$this->validator->ensureNameGerman($name);
|
||||
$this->validator->ensureIso2($iso2CountryCode);
|
||||
|
||||
$sql = 'SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM `bundesstaaten` AS s
|
||||
WHERE s.bundesstaat = :name AND s.aktiv = 1 AND s.land = :country_code';
|
||||
|
||||
return $this->db->fetchRow($sql, ['name' => $name, 'country_code' => $iso2CountryCode]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iso2Code iso state code
|
||||
* @param string $iso2CountryCode iso2 country code
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function findByIso2CodeAndIso2CountryCode(string $iso2Code, string $iso2CountryCode): array
|
||||
{
|
||||
$this->validator->ensureIso2($iso2CountryCode);
|
||||
|
||||
return $this->db->fetchRow(
|
||||
'SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM `bundesstaaten` AS s
|
||||
WHERE s.iso = :iso2_code AND s.aktiv = 1 AND s.land = :country_code',
|
||||
['iso2_code' => $iso2Code, 'country_code' => $iso2CountryCode]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $iso2CountryCode
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function findAllByCountryCode(string $iso2CountryCode): array
|
||||
{
|
||||
$this->validator->ensureIso2($iso2CountryCode);
|
||||
|
||||
return $this->db->fetchAll(
|
||||
'SELECT ' .
|
||||
$this->getColumnArraySQLMapping() .
|
||||
'FROM `bundesstaaten` AS s
|
||||
WHERE s.land = :iso2_code AND s.aktiv = 1',
|
||||
['iso2_code' => $iso2CountryCode]
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns sql-ready 'column AS arrayName' structure
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function getColumnArraySQLMapping(): string
|
||||
{
|
||||
return
|
||||
's.iso AS iso2_code,
|
||||
s.land AS iso2_country_code,
|
||||
s.bundesstaat AS name_de ';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user