<?php
/**
* Created by PhpStorm.
* User: waldemar.abich
* Date: 10.12.14
* Time: 09:29
*/
namespace Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main;
use Exception;
use SimpleXMLElement;
use XMLReader;
use DOMDocument;
class XmlModelThermalData {
private $elementCode;
private $tpc;
private $tpcKeys = array();
private $allTpcKeys = array('u1', 'u2', 'u3', 'c', 'q', 'y');
function __construct($elementCode, Array $tpc = array(), Array $tpcKeys = array())
{
$this->elementCode = $elementCode;
$this->tpcKeys = $tpcKeys;
}
/** loads XML file into array
* @param $dataDirectory
*
* @throws \Exception
*/
public function loadData($dataDirectory) {
$xmlData = $this->readXmlFile($dataDirectory);
if (!$xmlData) {
$this->setJson(json_encode($this->returnAsArray()));
return;
}
while ($xmlData->read() && $xmlData->name !== 'tpc');
ini_set('memory_limit', '1024M');
while ($xmlData->name === 'tpc')
{
$child = new SimpleXMLElement($xmlData->readOuterXML());
$key = $child->attributes()['key']->__toString();
foreach ($this->allTpcKeys as $tpcKey) {
if(isset($child->attributes()[$tpcKey])) {
$this->tpc[$key][$tpcKey] = $child->attributes()[$tpcKey]->__toString();
$this->addTpcKey($tpcKey);
}
}
unset($child);
$xmlData->next('tpc');
}
$this->setJson(json_encode($this->returnAsArray()));
}
/** loadData runs only if the json file doesn't exist, therefore it's necessary to load tpc keys only
* @param $dataDirectory
*
* @throws \Exception
*/
public function loadTpc($dataDirectory) {
if (!$xmlData = $this->readXmlFile($dataDirectory)) {
return;
}
//find first tpc element and read tpc keys
while ($xmlData->read() && $xmlData->name !== 'tpc');
$child = new SimpleXMLElement($xmlData->readOuterXML());
foreach ($this->allTpcKeys as $tpcKey) {
if(isset($child->attributes()[$tpcKey])) {
$this->addTpcKey($tpcKey);
}
}
}
/**
* @param $dataDirectory
*
* @return SimpleXMLElement
* @throws \Exception
*/
private function readXmlFile ($dataDirectory) {
$filePath = $dataDirectory.DIRECTORY_SEPARATOR.$this->getElementCode().'.xml';
if (!is_file($filePath) || !is_readable($filePath)) {
return null;
}
$xmlReader = new XMLReader();
$xmlReader->open($filePath);
return $xmlReader;
}
/**
* @param mixed $elementCode
*/
public function setElementCode($elementCode)
{
$this->elementCode = $elementCode;
}
/**
* @return mixed
*/
public function getElementCode()
{
return $this->elementCode;
}
/**
* @param array $tpc
*/
public function setTpc($tpc)
{
$this->tpc = $tpc;
}
/**
* @return array
*/
public function getTpc()
{
return $this->tpc;
}
public function returnAsArray() {
$data = array();
$data['key'] = $this->getElementCode();
$data['tpc'] = $this->getTpc();
return $data;
}
/**
* @param array $tpcKey
*/
public function addTpcKey($tpcKey)
{
$this->tpcKeys[$tpcKey] = $tpcKey;
}
/**
* @param array $tpcKeys
*/
public function setTpcKeys($tpcKeys)
{
$this->tpcKeys = $tpcKeys;
}
/**
* @return array
*/
public function getTpcKeys()
{
return $this->tpcKeys;
}
public function getJson()
{
return $this->json;
}
public function setJson($json)
{
$this->json = $json;
}
}