<?php
namespace App\Controller\AppBundle\Connector\Awacs;
use App\Controller\AppBundle\Connector\Entity;
class EntityANVA extends Entity {
/**
* @param string $name name of field
* @return ResponseField Response Field
*/
public function field($name) {
return $this->fieldByNumber(FieldNumberMap::getNumber($this->type, $name));
}
/**
* @param string $name name of field
* @return bool
*/
public function fieldExists($name) {
if (!FieldNumberMap::numberExists($this->type, $name)) return false;
return $this->fieldByNumberExists(FieldNumberMap::getNumber($this->type, $name));
}
/**
* @param string $number Field number
* @return ResponseField Response Field
* @throws \Exception when field does not exist
*/
public function fieldByNumber($number) {
if (!$this->fieldByNumberExists($number)) return new ResponseField($number, '', $this->type);
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[FieldNumberMap::getNumber($this->type, $name)] = $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 EntityANVA[] List of collections of type $type
*/
public function collection($type) {
if (empty($this->collections[$type])) {
return [];
} else {
return $this->collections[$type];
}
}
/**
* Fetches field.
* @param string $field Name of field to fetch.
* @return string Displayable string, empty if it does not exist
*/
public function view($field) {
if ($this->fieldExists($field)) {
return $this->field($field)->view();
}
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') == 50;
}
public function searchByPKInCollections($pk, $collectionsText = [])
{
if ($collectionsText === []) {
$collections = $this->collections;
} else {
$res = [];
foreach ($collectionsText as $collection) {
$res[] = $this->collection($collection);
}
$collections = $res;
}
foreach ($collections as $collection) {
foreach ($collection as $entity) {
/** @var $entity EntityANVA */
if ($entity->key() === $pk) {
return $entity;
}
}
}
$pakketten = $this->collection('pakketten');
foreach ($pakketten as $pakket) {
$inPakket = $pakket->searchByPKInCollections($pk, $collectionsText);
if ($inPakket !== null) {
return $inPakket;
}
}
return null;
}
public function getDekkingByID($anvaId) {
foreach ($this->collection('dekkingen') as $dekking) {
if ((string)$dekking->field('dekking')->value() === $anvaId) {
return $dekking;
}
}
return false;
}
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);
}
}