<?php namespace RosreestrGeomToWKT; use RosreestrGeomToWKT\WktUtilites; class EntitySpatial { /** * @var array */ private $spatialElements; /** * @var object */ private $ut; /** * @var array */ public $entitySpatialData; /** * @param array $spatialElements */ public function __construct(array $spatialElements) { $this->spatialElements = $spatialElements; $this->ut = new WktUtilites(); $this->entitySpatialData = $this->getEntitySpatialData(); } /** * @return string * @throws */ private function getEntitySpatialData(): array { if ($this->ut::validateArray($this->spatialElements)) { $diffTypes = []; $spatialCount = count($this->spatialElements); foreach ($this->spatialElements as $spaEl) { $diffTypes[] = $spaEl->spatialElementData['type']; } $uniqueTypes = array_unique($diffTypes); $esType = $uniqueTypes[0]; if (count($uniqueTypes) === 0) { throw new \Exception('Empty spatialElements'); } else { if (count($uniqueTypes) === 1) { if ($spatialCount === 1) { return ['type' => $esType, 'coordinates' => $this->spatialElements[0]->spatialElementData['coordinates']]; } else { if ($esType === 'POLYGON') { $response = '('; foreach ($this->spatialElements as $spaEl) { $coordinates = $spaEl->spatialElementData['coordinates']; $coordinates = str_replace('))', ')', str_replace('((', '(', $coordinates)); $response .= $coordinates . ', '; } return ['type' => $esType, 'coordinates' => rtrim($response, ', ') . ')']; } else { throw new \Exception('Illegal multiple spatialElement collection (allowed only POLYGON)'); } } } else { throw new \Exception('Illegal spatialElement collection (allowed only one simple element type)'); } } } else { throw new \Exception('Spatial elements are not found'); } } }