src/Controller/AppBundle/Connector/Awacs/EntityANVA.php line 7

Open in your IDE?
  1. <?php
  2. namespace App\Controller\AppBundle\Connector\Awacs;
  3. use App\Controller\AppBundle\Connector\Entity;
  4. class EntityANVA extends Entity {
  5.     /**
  6.      * @param string $name name of field
  7.      * @return ResponseField Response Field
  8.      */
  9.     public function field($name) {
  10.         return $this->fieldByNumber(FieldNumberMap::getNumber($this->type$name));
  11.     }
  12.     /**
  13.      * @param string $name name of field
  14.      * @return bool
  15.      */
  16.     public function fieldExists($name) {
  17.         if (!FieldNumberMap::numberExists($this->type$name)) return false;
  18.         return $this->fieldByNumberExists(FieldNumberMap::getNumber($this->type$name));
  19.     }
  20.     /**
  21.      * @param string $number Field number
  22.      * @return ResponseField Response Field
  23.      * @throws \Exception when field does not exist
  24.      */
  25.     public function fieldByNumber($number) {
  26.         if (!$this->fieldByNumberExists($number)) return new ResponseField($number''$this->type);
  27.         return $this->fields[$number];
  28.     }
  29.     /**
  30.      * @param string $number Field number
  31.      * @return bool
  32.      */
  33.     public function fieldByNumberExists($number) {
  34.         return (isset($this->fields[$number]));
  35.     }
  36.     /**
  37.      * Updates field to be changed on persist.
  38.      * @param $name string Name of field to update
  39.      * @param $value mixed New value of field
  40.      */
  41.     public function updateField($name$value) {
  42.         $this->updatedFields[FieldNumberMap::getNumber($this->type$name)] = $value;
  43.     }
  44.     /**
  45.      * Updates field to be changed on persist if the value is different than the current value.
  46.      * @param $name string Name of field to update
  47.      * @param $value mixed New value of field
  48.      */
  49.     public function updateIfChanged($name$value) {
  50.         if ($this->field($name)->value() != $value) {
  51.             $this->updateField($name$value);
  52.         }
  53.     }
  54.     /**
  55.      * Updates field to be changed on persist.
  56.      * @param $number string Number of field to update
  57.      * @param $value mixed New value of field
  58.      */
  59.     public function updateFieldByNumber($number$value) {
  60.         $this->updatedFields[$number] = $value;
  61.     }
  62.     /**
  63.      * @return array Array of updated fields. The key is the number of the field, the value is the new value.
  64.      */
  65.     public function getUpdatedFields() {
  66.         return $this->updatedFields;
  67.     }
  68.     /**
  69.      * @return bool True iff there are fields changed
  70.      */
  71.     public function isUpdated() {
  72.         return !empty($this->updatedFields);
  73.     }
  74.     /**
  75.      * @return string Primary key of object
  76.      */
  77.     public function getKey()
  78.     {
  79.         return $this->key;
  80.     }
  81.     /**
  82.      * @return string Type of entity (singular)
  83.      */
  84.     public function getType()
  85.     {
  86.         return $this->type;
  87.     }
  88.     /**
  89.      * @return array of fields
  90.      */
  91.     public function fieldsAsArray() {
  92.         return $this->fields;
  93.     }
  94.     /**
  95.      * @return string Key of entity
  96.      */
  97.     public function key() {
  98.         return $this->key;
  99.     }
  100.     /**
  101.      * @param string $name Name of index
  102.      * @return mixed Content of index
  103.      * @throws \Exception When index does not exist
  104.      */
  105.     public function index($name) {
  106.         if (!$this->indexExists($name)) throw new \Exception('Unknown index ' $name);
  107.         return $this->indices[$name];
  108.     }
  109.     /**
  110.      * @param string $name Name of index
  111.      * @return bool
  112.      */
  113.     public function indexExists($name) {
  114.         return isset($this->indices[$name]);
  115.     }
  116.     /**
  117.      * @param string $type Collection type
  118.      * @return EntityANVA[] List of collections of type $type
  119.      */
  120.     public function collection($type) {
  121.         if (empty($this->collections[$type])) {
  122.             return [];
  123.         } else {
  124.             return $this->collections[$type];
  125.         }
  126.     }
  127.     /**
  128.      * Fetches field.
  129.      * @param string $field Name of field to fetch.
  130.      * @return string Displayable string, empty if it does not exist
  131.      */
  132.     public function view($field) {
  133.         if ($this->fieldExists($field)) {
  134.             return $this->field($field)->view();
  135.         }
  136.         return '';
  137.     }
  138.     /**
  139.      * Check if entity is wachtpolis
  140.      * @return bool true iff entity is wachtpolis
  141.      */
  142.     public function isWachtpolis()
  143.     {
  144.         return $this->isWachtpolis;
  145.     }
  146.     /**
  147.      * Check if entity is a business entity
  148.      * @return bool true iff entity is business
  149.      */
  150.     public function isBusiness() {
  151.         if (empty($this->view('soort_relatie'))) {
  152.             return false;
  153.         }
  154.         return $this->view('soort_relatie') == 50;
  155.     }
  156.     public function searchByPKInCollections($pk$collectionsText = [])
  157.     {
  158.         if ($collectionsText === []) {
  159.             $collections $this->collections;
  160.         } else {
  161.             $res = [];
  162.             foreach ($collectionsText as $collection) {
  163.                 $res[] = $this->collection($collection);
  164.             }
  165.             $collections $res;
  166.         }
  167.         foreach ($collections as $collection) {
  168.             foreach ($collection as $entity) {
  169.                 /** @var $entity EntityANVA */
  170.                 if ($entity->key() === $pk) {
  171.                     return $entity;
  172.                 }
  173.             }
  174.         }
  175.         $pakketten $this->collection('pakketten');
  176.         foreach ($pakketten as $pakket) {
  177.             $inPakket $pakket->searchByPKInCollections($pk$collectionsText);
  178.             if ($inPakket !== null) {
  179.                 return $inPakket;
  180.             }
  181.         }
  182.         return null;
  183.     }
  184.     public function getDekkingByID($anvaId) {
  185.         foreach ($this->collection('dekkingen') as $dekking) {
  186.             if ((string)$dekking->field('dekking')->value() === $anvaId) {
  187.                 return $dekking;
  188.             }
  189.         }
  190.         return false;
  191.     }
  192.     public function serialize()
  193.     {
  194.         return serialize([
  195.             $this->type,
  196.             $this->key,
  197.             $this->indices,
  198.             $this->fields,
  199.             $this->collections,
  200.             $this->isWachtpolis
  201.         ]);
  202.     }
  203.     public function unserialize($serialized)
  204.     {
  205.         list(
  206.             $this->type,
  207.             $this->key,
  208.             $this->indices,
  209.             $this->fields,
  210.             $this->collections,
  211.             $this->isWachtpolis
  212.         ) = unserialize($serialized);
  213.     }
  214. }