<?php
namespace App\Controller\AppBundle\Connector\Awacs;
class ResponseField implements \Serializable
{
private $entityType;
private $number;
private $name;
private $value;
private $type;
private $display;
private $fieldName;
/**
* @param mixed $display Set display value
*/
public function setDisplay($display)
{
$this->display = $display;
}
/**
* @param string $name Field name
*/
public function setFieldName($name)
{
$this->fieldName = $name;
}
/**
* @param array $config Configuration array. Can contain index for 'prefix'
* @param bool $yesNo If true, yes and no will be displayed as text
* @return mixed Viewable representation of field
* @throws \Exception When type is unknown
*/
public function view($config = [], $yesNo = false)
{
if (isset($config['prefix'])) {
$prefix = $config['prefix'];
unset($config['prefix']);
return $prefix . $this->view($config);
}
if (isset($config['suffix'])) {
$suffix = $config['suffix'];
unset($config['suffix']);
return $this->view($config) . $suffix;
}
if ($this->type === FIELD_TYPE_RAW) {
return $this->value;
} elseif ($this->type === FIELD_TYPE_RAWTEXTAREA) {
return $this->value;
} elseif ($this->type === FIELD_TYPE_DISPLAY) {
return $this->display;
} elseif ($this->type === FIELD_TYPE_DATE) {
if (empty($this->value)) return '';
$months = ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'];
try {
$date = \DateTime::createFromFormat('Ymd', $this->value);
if ($date === false) return '';
$dateString = '';
$dateString .= $date->format('j') . ' ';
$dateString .= $months[$date->format('n') - 1] . ' ';
$dateString .= $date->format('Y');
return $dateString;
} catch (\Exception $e) {
return '';
}
} elseif ($this->type === FIELD_TYPE_BOOLEAN) {
if ($yesNo) {
if ($this->value == 'J') {
return 'Ja';
} elseif ($this->value() == 'N') {
return 'Nee';
} else {
return '';
}
} else {
if ($this->value == 'J') {
return true;
} else {
return false;
}
}
} elseif ($this->type === FIELD_TYPE_MONTH) {
if (empty($this->value) || intval($this->value()) < 1) return '';
$months = ['januari', 'februari', 'maart', 'april', 'mei', 'juni', 'juli', 'augustus', 'september', 'oktober', 'november', 'december'];
return ucfirst($months[intval($this->value) - 1]);
} elseif ($this->type === FIELD_TYPE_TIME) {
if (empty($this->value) || strlen($this->value) !== 6) return '';
return implode(':', str_split($this->value, 2));
} else {
throw new \Exception('Unknown type ' . $this->type);
}
}
/**
* @return mixed Value of field
*/
public function value()
{
return $this->value;
}
/**
* @return \DateTime DateTime value of field
*/
public function dateTimeValue()
{
if (!empty($this->value)) {
try {
$date = \DateTime::createFromFormat('Ymd', $this->value);
if ($date === false) $date = null;
} catch (\Exception $e) {
$date = null;
}
return $date;
}
return null;
}
/**
* @return string Date string in dd-mm-yyyy notation
*/
public function dateString()
{
if (!empty($this->value)) {
try {
$date = \DateTime::createFromFormat('Ymd', $this->value);
} catch (\Exception $e) {
$date = '';
}
return $date->format('d-m-Y');
}
return '';
}
/**
* @return string Name of field.
*/
public function fieldName()
{
return $this->fieldName;
}
/**
* ResponseField constructor.
* @param string $number Field number
* @param mixed $value Type definition
* @param string $entityType Type of entity (singular)
*/
function __construct($number, $value, $entityType, $slotvragen = null)
{
$this->number = $number;
$this->value = $value;
$this->entityType = $entityType;
$this->name = FieldNumberMap::getName($entityType, $number, $slotvragen);
$this->type = FieldNumberMap::getFieldType($entityType, $this->name, $slotvragen);
}
public function toArray()
{
return [
'entityType' => $this->entityType,
'number' => $this->number,
'name' => $this->name,
'value' => $this->value,
'type' => $this->type,
'display' => $this->display,
'fieldName' => $this->fieldName
];
}
public function serialize()
{
return serialize(array_values($this->toArray()));
}
public function unserialize($serialized)
{
[
$this->entityType,
$this->number,
$this->name,
$this->value,
$this->type,
$this->display,
$this->fieldName
] = unserialize($serialized);
}
/**
* Get display value
* @return mixed
*/
public function display()
{
return $this->display;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
/**
* @return string
*/
public function getNumber(): string
{
return $this->number;
}
}