src/Mm/Beton/Planungsatlas/AtlasBundle/Entity/Main/XmlModelThermalData.php line 173

Open in your IDE?
  1. <?php
  2. /**
  3.  * Created by PhpStorm.
  4.  * User: waldemar.abich
  5.  * Date: 10.12.14
  6.  * Time: 09:29
  7.  */
  8. namespace Mm\Beton\Planungsatlas\AtlasBundle\Entity\Main;
  9. use Exception;
  10. use SimpleXMLElement;
  11. use XMLReader;
  12. use DOMDocument;
  13. class XmlModelThermalData {
  14.     private $elementCode;
  15.     private $tpc;
  16.     private $tpcKeys = array();
  17.     private $allTpcKeys = array('u1''u2''u3''c''q''y');
  18.     function __construct($elementCode, Array $tpc = array(), Array $tpcKeys = array())
  19.     {
  20.         $this->elementCode $elementCode;
  21.         $this->tpcKeys $tpcKeys;
  22.     }
  23.     /** loads XML file into array
  24.      * @param $dataDirectory
  25.      *
  26.      * @throws \Exception
  27.      */
  28.     public function loadData($dataDirectory) {
  29.         $xmlData $this->readXmlFile($dataDirectory);
  30.         if (!$xmlData) {
  31.             $this->setJson(json_encode($this->returnAsArray()));
  32.             return;
  33.         }
  34.         while ($xmlData->read() && $xmlData->name !== 'tpc');
  35.         ini_set('memory_limit''1024M');
  36.         while ($xmlData->name === 'tpc')
  37.         {
  38.             $child = new SimpleXMLElement($xmlData->readOuterXML());
  39.             $key $child->attributes()['key']->__toString();
  40.             foreach ($this->allTpcKeys as $tpcKey) {
  41.                 if(isset($child->attributes()[$tpcKey])) {
  42.                     $this->tpc[$key][$tpcKey] = $child->attributes()[$tpcKey]->__toString();
  43.                     $this->addTpcKey($tpcKey);
  44.                 }
  45.             }
  46.             unset($child);
  47.             $xmlData->next('tpc');
  48.         }
  49.         $this->setJson(json_encode($this->returnAsArray()));
  50.     }
  51.     /** loadData runs only if the json file doesn't exist, therefore it's necessary to load tpc keys only
  52.      * @param $dataDirectory
  53.      *
  54.      * @throws \Exception
  55.      */
  56.     public function loadTpc($dataDirectory) {
  57.         if (!$xmlData $this->readXmlFile($dataDirectory)) {
  58.             return;
  59.         }
  60.         //find first tpc element and read tpc keys
  61.         while ($xmlData->read() && $xmlData->name !== 'tpc');
  62.         $child = new SimpleXMLElement($xmlData->readOuterXML());
  63.         foreach ($this->allTpcKeys as $tpcKey) {
  64.             if(isset($child->attributes()[$tpcKey])) {
  65.                 $this->addTpcKey($tpcKey);
  66.             }
  67.         }
  68.     }
  69.     /**
  70.      * @param $dataDirectory
  71.      *
  72.      * @return SimpleXMLElement
  73.      * @throws \Exception
  74.      */
  75.     private function readXmlFile ($dataDirectory) {
  76.         $filePath $dataDirectory.DIRECTORY_SEPARATOR.$this->getElementCode().'.xml';
  77.         if (!is_file($filePath) || !is_readable($filePath)) {
  78.             return null;
  79.         }
  80.         $xmlReader = new XMLReader();
  81.         $xmlReader->open($filePath);
  82.         return $xmlReader;
  83.     }
  84.     /**
  85.      * @param mixed $elementCode
  86.      */
  87.     public function setElementCode($elementCode)
  88.     {
  89.         $this->elementCode $elementCode;
  90.     }
  91.     /**
  92.      * @return mixed
  93.      */
  94.     public function getElementCode()
  95.     {
  96.         return $this->elementCode;
  97.     }
  98.     /**
  99.      * @param array $tpc
  100.      */
  101.     public function setTpc($tpc)
  102.     {
  103.         $this->tpc $tpc;
  104.     }
  105.     /**
  106.      * @return array
  107.      */
  108.     public function getTpc()
  109.     {
  110.         return $this->tpc;
  111.     }
  112.     public function returnAsArray() {
  113.         $data = array();
  114.         $data['key'] = $this->getElementCode();
  115.         $data['tpc'] = $this->getTpc();
  116.         return $data;
  117.     }
  118.     /**
  119.      * @param array $tpcKey
  120.      */
  121.     public function addTpcKey($tpcKey)
  122.     {
  123.         $this->tpcKeys[$tpcKey] = $tpcKey;
  124.     }
  125.     /**
  126.      * @param array $tpcKeys
  127.      */
  128.     public function setTpcKeys($tpcKeys)
  129.     {
  130.         $this->tpcKeys $tpcKeys;
  131.     }
  132.     /**
  133.      * @return array
  134.      */
  135.     public function getTpcKeys()
  136.     {
  137.         return $this->tpcKeys;
  138.     }
  139.     public function getJson()
  140.     {
  141.         return $this->json;
  142.     }
  143.     public function setJson($json)
  144.     {
  145.         $this->json $json;
  146.     }
  147. }