Skip to content

Commit

Permalink
Merge pull request #12 from jkuchar/feature-11-make-ares-record-immut…
Browse files Browse the repository at this point in the history
…able

Make data objects immutable
  • Loading branch information
dfridrich committed Mar 24, 2016
2 parents d085b4e + 54705ce commit 8770d92
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 97 deletions.
56 changes: 25 additions & 31 deletions src/Ares.php
Expand Up @@ -105,27 +105,17 @@ public function findByIdentificationNumber($id)
throw new AresException('IČ firmy nebylo nalezeno.');
}

$record = new AresRecord();
$record = new AresRecord(
$id,
strval($elements->DIC), // taxId
strval($elements->OF), // companyName
strval($elements->AA->NU), // street
strval($elements->AA->CD), // house number
strval($elements->AA->CO) ?: null, // house orientation number
strval($elements->AA->NCO) ? strval($elements->AA->N.' - '.strval($elements->AA->NCO)) : strval($elements->AA->N), // town
strval($elements->AA->PSC) // ZIP
);

$record->setCompanyId($id);
$record->setTaxId(strval($elements->DIC));
$record->setCompanyName(strval($elements->OF));
$record->setStreet(strval($elements->AA->NU));

if (strval($elements->AA->CO)) {
$record->setStreetHouseNumber(strval($elements->AA->CD));
$record->setStreetOrientationNumber(strval($elements->AA->CO));
} else {
$record->setStreetHouseNumber(strval($elements->AA->CD));
}

if (strval($elements->AA->NCO)) {
$record->setTown(strval($elements->AA->N.' - '.strval($elements->AA->NCO)));
} else {
$record->setTown(strval($elements->AA->N));
}

$record->setZip(strval($elements->AA->PSC));
} else {
throw new AresException('Databáze ARES není dostupná.');
}
Expand Down Expand Up @@ -175,15 +165,16 @@ public function findInResById($id)
$elements = $data->children($ns['D'])->Vypis_RES;

if (strval($elements->ZAU->ICO) === $id) {
$record = new AresRecord();
$record->setCompanyId(strval($id));
$record->setTaxId($this->findVatById($id));
$record->setCompanyName(strval($elements->ZAU->OF));
$record->setStreet(strval($elements->SI->NU));
$record->setStreetHouseNumber(strval($elements->SI->CD));
$record->setStreetOrientationNumber(strval($elements->SI->CO));
$record->setTown(strval($elements->SI->N));
$record->setZip(strval($elements->SI->PSC));
$record = new AresRecord(
strval($id),
$this->findVatById($id),
strval($elements->ZAU->OF),
strval($elements->SI->NU),
strval($elements->SI->CD),
strval($elements->SI->CO),
strval($elements->SI->N),
strval($elements->SI->PSC)
);
} else {
throw new AresException('IČ firmy nebylo nalezeno.');
}
Expand Down Expand Up @@ -231,13 +222,15 @@ public function findVatById($id)
$vatResponse = simplexml_load_string($vatRequest);

if ($vatResponse) {
$record = new TaxRecord();

$ns = $vatResponse->getDocNamespaces();
$data = $vatResponse->children($ns['are']);
$elements = $data->children($ns['dtt'])->V->S;

if (strval($elements->ico) === $id) {
$record->setTaxId(str_replace('dic=', 'CZ', strval($elements->p_dph)));
$record = new TaxRecord(
str_replace('dic=', 'CZ', strval($elements->p_dph))
);
} else {
throw new AresException('DIČ firmy nebylo nalezeno.');
}
Expand Down Expand Up @@ -300,6 +293,7 @@ public function findByName($name, $city = null)

$records = new AresRecords();
foreach ($elements as $element) {
// TODO: What is this?
$record = new AresRecord();
$record->setCompanyId(strval($element->ico));
$record->setTaxId(
Expand Down
83 changes: 21 additions & 62 deletions src/Ares/AresRecord.php
Expand Up @@ -15,89 +15,64 @@ class AresRecord
/**
* @var int
*/
public $companyId;
private $companyId;

/**
* @var string
*/
public $taxId;
private $taxId;

/**
* @var string
*/
public $companyName;
private $companyName;

/**
* @var string
*/
public $street;
private $street;

/**
* @var string
*/
public $streetHouseNumber;
private $streetHouseNumber;

/**
* @var string
*/
public $streetOrientationNumber;
private $streetOrientationNumber;

/**
* @var string
*/
public $town;
private $town;

/**
* @var string
*/
public $zip;
private $zip;

/**
* @param $companyId
* AresRecord constructor.
* @param int $companyId
* @param string $taxId
* @param string $companyName
* @param string $street
* @param string $streetHouseNumber
* @param string $streetOrientationNumber
* @param string $town
* @param string $zip
*/
public function setCompanyId($companyId)
public function __construct($companyId, $taxId, $companyName, $street, $streetHouseNumber, $streetOrientationNumber, $town, $zip)
{
$this->companyId = $companyId;
}

/**
* @param $companyName
*/
public function setCompanyName($companyName)
{
$this->companyName = $companyName;
}

/**
* @param $taxId
*/
public function setTaxId($taxId)
{
$this->taxId = !empty($taxId) ? $taxId : null;
}

/**
* @param $street
*/
public function setStreet($street)
{
$this->companyName = $companyName;
$this->street = $street;
}

/**
* @param $streetHouseNumber
*/
public function setStreetHouseNumber($streetHouseNumber)
{
$this->streetHouseNumber = !empty($streetHouseNumber) ? $streetHouseNumber : null;
}

/**
* @param $streetOrientationNumber
*/
public function setStreetOrientationNumber($streetOrientationNumber)
{
$this->streetOrientationNumber = !empty($streetOrientationNumber) ? $streetOrientationNumber : null;
$this->town = $town;
$this->zip = $zip;
}

/**
Expand All @@ -113,22 +88,6 @@ public function getStreetWithNumbers()
$this->streetHouseNumber);
}

/**
* @param $town
*/
public function setTown($town)
{
$this->town = $town;
}

/**
* @param $zip
*/
public function setZip($zip)
{
$this->zip = $zip;
}

/**
* @return mixed
*/
Expand Down
12 changes: 10 additions & 2 deletions src/Ares/AresRecords.php
Expand Up @@ -9,12 +9,12 @@
*
* @author Dennis Fridrich <fridrich.dennis@gmail.com>
*/
final class AresRecords implements \ArrayAccess, \IteratorAggregate
final class AresRecords implements \ArrayAccess, \IteratorAggregate, \Countable
{
/**
* @var AresRecord[]
*/
public $array = [];
private $array = [];

/**
* @param mixed $offset
Expand Down Expand Up @@ -72,4 +72,12 @@ public function getIterator()
{
return new ArrayIterator($this->array);
}

/**
* {@inheritdoc}
*/
public function count()
{
return count($this->array);
}
}
13 changes: 11 additions & 2 deletions src/Ares/TaxRecord.php
Expand Up @@ -12,16 +12,25 @@ class TaxRecord
/**
* @var string
*/
public $taxId = null;
private $taxId = null;

/**
* TaxRecord constructor.
* @param string $taxId
*/
public function setTaxId($taxId)
public function __construct($taxId)
{
$this->taxId = !empty($taxId) ? $taxId : null;
}

/**
* @return string
*/
public function getTaxId()
{
return $this->taxId;
}

/**
* @return string
*/
Expand Down

0 comments on commit 8770d92

Please sign in to comment.