<?php
namespace App\Controller\AppBundle\Connector\Assu;
use App\Controller\AppBundle\Connector\Assu\AFD\AFDMapping;
use App\Controller\AppBundle\Connector\Awacs\ResponseField;
use App\Controller\AppBundle\Connector\Entity;
class EntityAssu extends Entity {
public function __construct($fields, $type) {
if ($type == 'relatie') {
$this->fields = $fields;
} else {
$this->fields = json_decode(json_encode((array)$fields), 1);
}
$this->type = $type;
if ($this->type == 'relatie') {
$this->key = $this->view('relatienummer');
} else {
$this->key = $this->view('polisnummer');
}
}
/**
* @param string $name name of field
*/
public function field($name) {
$field = new AssuField();
$field->value = $this->view($name, true);
$field->portaalName = $name;
$field->codelist = isset(AssuMapping::$adnMap[$name . ' ']['codelist']) ? AssuMapping::$adnMap[$name . ' ']['codelist'] : '';
return $field;
}
/**
* @param string $name name of field
* @return bool
*/
public function fieldExists($name) {
if (!AssuMapping::fieldExists($name, $this->type)) return false;
return $this->fieldByNumberExists(AssuMapping::getField($name, $this->type));
}
/**
* @param string $number Field number
* @return ResponseField Response Field
* @throws \Exception when field does not exist
*/
public function fieldByNumber($number) {
if (!$this->fieldByNumberExists($number)) throw new \Exception('Field does not exist ' . $number);
return $this->fields[$number];
}
/**
* @param string $number Field number
* @return bool
*/
public function fieldByNumberExists($number) {
return (isset($this->fields[$number]));
}
/**
* Updates field to be changed on persist.
* @param $name string Name of field to update
* @param $value mixed New value of field
*/
public function updateField($name, $value) {
$this->updatedFields[AssuMapping::getField($name, $this->type)] = $value;
}
/**
* Updates field to be changed on persist if the value is different than the current value.
* @param $name string Name of field to update
* @param $value mixed New value of field
*/
public function updateIfChanged($name, $value) {
if ($this->field($name)->value() != $value) {
$this->updateField($name, $value);
}
}
/**
* Updates field to be changed on persist.
* @param $number string Number of field to update
* @param $value mixed New value of field
*/
public function updateFieldByNumber($number, $value) {
$this->updatedFields[$number] = $value;
}
/**
* @return array Array of updated fields. The key is the number of the field, the value is the new value.
*/
public function getUpdatedFields() {
return $this->updatedFields;
}
/**
* @return bool True iff there are fields changed
*/
public function isUpdated() {
return !empty($this->updatedFields);
}
/**
* @return string Primary key of object
*/
public function getKey()
{
return $this->key;
}
/**
* @return string Type of entity (singular)
*/
public function getType()
{
return $this->type;
}
/**
* @return array of fields
*/
public function fieldsAsArray() {
return $this->fields;
}
/**
* @return string Key of entity
*/
public function key() {
return $this->key;
}
/**
* @param string $name Name of index
* @return mixed Content of index
* @throws \Exception When index does not exist
*/
public function index($name) {
if (!$this->indexExists($name)) throw new \Exception('Unknown index ' . $name);
return $this->indices[$name];
}
/**
* @param string $name Name of index
* @return bool
*/
public function indexExists($name) {
return isset($this->indices[$name]);
}
/**
* @param string $type Collection type
* @return Entity[] List of collections of type $type
*/
public function collection($type) {
if (empty($this->collections[$type])) {
return [];
} else {
return $this->collections[$type];
}
}
public function view($portaalNaam, $raw = false) {
if ($this->type == 'relatie') {
if (isset(AssuMapping::$map[$portaalNaam]) && isset($this->fields[AssuMapping::$map[$portaalNaam]])) {
$return = $this->fields[AssuMapping::$map[$portaalNaam]];
if ($portaalNaam == 'geboortedatum') {
$return = str_replace('-', '', $return);
}
return $return;
}
} else {
$portaalNaam = $portaalNaam . ' ';
if (isset(AssuMapping::$adnMap[$portaalNaam])) {
foreach (AssuMapping::$adnMap[$portaalNaam]['codes'] as $code) {
if ($code != '') {
$sh = $code[0] . $code[1];
if (isset($this->fields['MESSAGE'][$sh][$code])) {
if (
isset(AssuMapping::$adnMap[$portaalNaam]['codelist']) &&
AFDMapping::getView(AssuMapping::$adnMap[$portaalNaam]['codelist'], $this->fields['MESSAGE'][$sh][$code]) !== null &&
AFDMapping::getView(AssuMapping::$adnMap[$portaalNaam]['codelist'], $this->fields['MESSAGE'][$sh][$code]) != '' &&
!$raw
) {
$res = AFDMapping::getView(AssuMapping::$adnMap[$portaalNaam]['codelist'], $this->fields['MESSAGE'][$sh][$code]);
if (\DateTime::createFromFormat('Y-m-d', $res) != false) {
return \DateTime::createFromFormat('Y-m-d', $res)->format('Ymd');
}
return AFDMapping::getView(AssuMapping::$adnMap[$portaalNaam]['codelist'], $this->fields['MESSAGE'][$sh][$code]);
}
return $this->fields['MESSAGE'][$sh][$code];
}
}
}
}
}
return '';
}
/**
* Check if entity is wachtpolis
* @return bool true iff entity is wachtpolis
*/
public function isWachtpolis()
{
return $this->isWachtpolis;
}
/**
* Check if entity is a business entity
* @return bool true iff entity is business
*/
public function isBusiness() {
if (empty($this->view('soort_relatie'))) {
return false;
}
return $this->view('soort_relatie') != 'Prive';
}
public function serialize()
{
return serialize([
$this->type,
$this->key,
$this->indices,
$this->fields,
$this->collections,
$this->isWachtpolis
]);
}
public function unserialize($serialized)
{
list(
$this->type,
$this->key,
$this->indices,
$this->fields,
$this->collections,
$this->isWachtpolis
) = unserialize($serialized);
}
}