src/Controller/AppBundle/Connector/Awacs/ResponseField.php line 5

Open in your IDE?
  1. <?php
  2. namespace App\Controller\AppBundle\Connector\Awacs;
  3. class ResponseField implements \Serializable
  4. {
  5.     private $entityType;
  6.     private $number;
  7.     private $name;
  8.     private $value;
  9.     private $type;
  10.     private $display;
  11.     private $fieldName;
  12.     /**
  13.      * @param mixed $display Set display value
  14.      */
  15.     public function setDisplay($display)
  16.     {
  17.         $this->display $display;
  18.     }
  19.     /**
  20.      * @param string $name Field name
  21.      */
  22.     public function setFieldName($name)
  23.     {
  24.         $this->fieldName $name;
  25.     }
  26.     /**
  27.      * @param array $config Configuration array. Can contain index for 'prefix'
  28.      * @param bool $yesNo If true, yes and no will be displayed as text
  29.      * @return mixed Viewable representation of field
  30.      * @throws \Exception When type is unknown
  31.      */
  32.     public function view($config = [], $yesNo false)
  33.     {
  34.         if (isset($config['prefix'])) {
  35.             $prefix $config['prefix'];
  36.             unset($config['prefix']);
  37.             return $prefix $this->view($config);
  38.         }
  39.         if (isset($config['suffix'])) {
  40.             $suffix $config['suffix'];
  41.             unset($config['suffix']);
  42.             return $this->view($config) . $suffix;
  43.         }
  44.         if ($this->type === FIELD_TYPE_RAW) {
  45.             return $this->value;
  46.         } elseif ($this->type === FIELD_TYPE_RAWTEXTAREA) {
  47.             return $this->value;
  48.         } elseif ($this->type === FIELD_TYPE_DISPLAY) {
  49.             return $this->display;
  50.         } elseif ($this->type === FIELD_TYPE_DATE) {
  51.             if (empty($this->value)) return '';
  52.             $months = ['januari''februari''maart''april''mei''juni''juli''augustus''september''oktober''november''december'];
  53.             try {
  54.                 $date \DateTime::createFromFormat('Ymd'$this->value);
  55.                 if ($date === false) return '';
  56.                 $dateString '';
  57.                 $dateString .= $date->format('j') . ' ';
  58.                 $dateString .= $months[$date->format('n') - 1] . ' ';
  59.                 $dateString .= $date->format('Y');
  60.                 return $dateString;
  61.             } catch (\Exception $e) {
  62.                 return '';
  63.             }
  64.         } elseif ($this->type === FIELD_TYPE_BOOLEAN) {
  65.             if ($yesNo) {
  66.                 if ($this->value == 'J') {
  67.                     return 'Ja';
  68.                 } elseif ($this->value() == 'N') {
  69.                     return 'Nee';
  70.                 } else {
  71.                     return '';
  72.                 }
  73.             } else {
  74.                 if ($this->value == 'J') {
  75.                     return true;
  76.                 } else {
  77.                     return false;
  78.                 }
  79.             }
  80.         } elseif ($this->type === FIELD_TYPE_MONTH) {
  81.             if (empty($this->value) || intval($this->value()) < 1) return '';
  82.             $months = ['januari''februari''maart''april''mei''juni''juli''augustus''september''oktober''november''december'];
  83.             return ucfirst($months[intval($this->value) - 1]);
  84.         } elseif ($this->type === FIELD_TYPE_TIME) {
  85.             if (empty($this->value) || strlen($this->value) !== 6) return '';
  86.             return implode(':'str_split($this->value2));
  87.         } else {
  88.             throw new \Exception('Unknown type ' $this->type);
  89.         }
  90.     }
  91.     /**
  92.      * @return mixed Value of field
  93.      */
  94.     public function value()
  95.     {
  96.         return $this->value;
  97.     }
  98.     /**
  99.      * @return \DateTime DateTime value of field
  100.      */
  101.     public function dateTimeValue()
  102.     {
  103.         if (!empty($this->value)) {
  104.             try {
  105.                 $date \DateTime::createFromFormat('Ymd'$this->value);
  106.                 if ($date === false$date null;
  107.             } catch (\Exception $e) {
  108.                 $date null;
  109.             }
  110.             return $date;
  111.         }
  112.         return null;
  113.     }
  114.     /**
  115.      * @return string Date string in dd-mm-yyyy notation
  116.      */
  117.     public function dateString()
  118.     {
  119.         if (!empty($this->value)) {
  120.             try {
  121.                 $date \DateTime::createFromFormat('Ymd'$this->value);
  122.             } catch (\Exception $e) {
  123.                 $date '';
  124.             }
  125.             return $date->format('d-m-Y');
  126.         }
  127.         return '';
  128.     }
  129.     /**
  130.      * @return string Name of field.
  131.      */
  132.     public function fieldName()
  133.     {
  134.         return $this->fieldName;
  135.     }
  136.     /**
  137.      * ResponseField constructor.
  138.      * @param string $number Field number
  139.      * @param mixed $value Type definition
  140.      * @param string $entityType Type of entity (singular)
  141.      */
  142.     function __construct($number$value$entityType$slotvragen null)
  143.     {
  144.         $this->number $number;
  145.         $this->value $value;
  146.         $this->entityType $entityType;
  147.         $this->name FieldNumberMap::getName($entityType$number$slotvragen);
  148.         $this->type FieldNumberMap::getFieldType($entityType$this->name$slotvragen);
  149.     }
  150.     public function toArray()
  151.     {
  152.         return [
  153.             'entityType' => $this->entityType,
  154.             'number' => $this->number,
  155.             'name' => $this->name,
  156.             'value' => $this->value,
  157.             'type' => $this->type,
  158.             'display' => $this->display,
  159.             'fieldName' => $this->fieldName
  160.         ];
  161.     }
  162.     public function serialize()
  163.     {
  164.         return serialize(array_values($this->toArray()));
  165.     }
  166.     public function unserialize($serialized)
  167.     {
  168.         [
  169.             $this->entityType,
  170.             $this->number,
  171.             $this->name,
  172.             $this->value,
  173.             $this->type,
  174.             $this->display,
  175.             $this->fieldName
  176.         ] = unserialize($serialized);
  177.     }
  178.     /**
  179.      * Get display value
  180.      * @return mixed
  181.      */
  182.     public function display()
  183.     {
  184.         return $this->display;
  185.     }
  186.     /**
  187.      * @return string
  188.      */
  189.     public function getName(): string
  190.     {
  191.         return $this->name;
  192.     }
  193.     /**
  194.      * @return string
  195.      */
  196.     public function getNumber(): string
  197.     {
  198.         return $this->number;
  199.     }
  200. }