<?php
namespace App\Controller\AppBundle\Connector\FasterForward;
use App\Controller\AppBundle\Connector\Assu\AFD\AFDMapping;
use App\Controller\AppBundle\Connector\Awacs\ResponseField;
use App\Controller\AppBundle\Connector\Entity;
class EntityFasterForward extends Entity
{
public function __construct($fields, $type)
{
$this->fields = json_decode(json_encode($fields), true);
$this->type = $type;
$this->processDates();
}
public function hasAnvaPoliciesEntity()
{
$polissen = $this->collection('polissen');
foreach ($polissen as $polis) {
if (substr($polis->view('polisnummer'), 0, 5) == "8056-") {
return substr($polis->view('polisnummer'), 0, strpos($polis->view('polisnummer'), "-", 5));
}
}
return false;
}
private function processDates() {
foreach ($this->fields as $key => $field) {
if (is_string($field)) {
$r = \DateTime::createFromFormat(\DateTime::ATOM, $field);
if ($r) {
$this->fields[$key] = $r->format('Ymd');
}
}
}
}
/**
* @param string $name name of field
*/
public function field($name)
{
$field = new FasterForwardField();
$field->portaalName = $name;
if (FasterForwardMapping::fieldExists($name, $this->type)) {
$f = FasterForwardMapping::getField($name, $this->type);
if (isset($f['level']) && $f['level'] == 'bezitting') {
$field->value = $this->collections['bezittingen'][0]->fields[$f['name']];
} else {
$field->value = $this->fields[$f['name']];
}
} else {
$field->value = $this->fields[$name] ?? '';
}
$field->type = $this->type;
//$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));
return true;
}
/**
* @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[FasterForwardMapping::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)
{
return $this->field($portaalNaam)->view();
// if ($this->type == 'relatie') {
// if (isset(FasterForwardMapping::$map[$this->type][$portaalNaam]) && isset($this->fields[FasterForwardMapping::$map[$this->type][$portaalNaam]['name']])) {
// $block = FasterForwardMapping::$map[$this->type][$portaalNaam];
// $return = $this->fields[$block['name']];
// if (isset($block['function'])) {
// return FasterForwardMapping::executeFunction($block['function'], $return);
// }
// if (isset($block['type'])) {
// switch ($block['type']) {
// case 'date':
// $dt = new \DateTime($return);
// default:
// break;
// }
// }
// return $return;
// } elseif (isset($this->fields[$portaalNaam])) {
// return $this->fields[$portaalNaam];
// }
// } 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);
}
}