src/Domain/Zones/Model/Town.php line 24

Open in your IDE?
  1. <?php
  2. namespace Whater\Domain\Zones\Model;
  3. use BornFree\TacticianDomainEvent\Recorder\ContainsRecordedEvents;
  4. use BornFree\TacticianDomainEvent\Recorder\EventRecorderCapabilities;
  5. use Doctrine\Common\Collections\ArrayCollection;
  6. use Doctrine\Common\Collections\Collection;
  7. use Location\Coordinate;
  8. use Location\Ellipsoid;
  9. use Location\Formatter\Polygon\GeoJSON;
  10. use Location\Polygon as LocationPolygon;
  11. use Ramsey\Uuid\Uuid;
  12. use Whater\Domain\Common\Exception\InvalidUUIDException;
  13. use LongitudeOne\Spatial\PHP\Types\Geometry\Polygon;
  14. use Whater\Domain\Whater\Model\WhaterOrganization;
  15. use Whater\Domain\Zones\Event\TownPolygonWasUpdate;
  16. /**
  17. * Class Town
  18. *
  19. * @package Whater\Domain\Zones\Model
  20. */
  21. class Town implements ContainsRecordedEvents
  22. {
  23. use EventRecorderCapabilities;
  24. const IDS_SINAC_MUNICIPIO = 'sinac_codMunicipio';
  25. /**
  26. * @var string
  27. */
  28. private $uuid;
  29. /**
  30. * @var string
  31. */
  32. private $slug;
  33. /**
  34. * @var string
  35. */
  36. private $name;
  37. /**
  38. * @var array
  39. */
  40. private $externalIds;
  41. /**
  42. * @var string
  43. */
  44. private $geoNameId;
  45. /**
  46. * @var int
  47. */
  48. private $population;
  49. /**
  50. * @var float
  51. */
  52. private $latitude;
  53. /**
  54. * @var float
  55. */
  56. private $longitude;
  57. /**
  58. * @var Polygon
  59. */
  60. private $geometryPolygon;
  61. /**
  62. * @var Country
  63. */
  64. private $country;
  65. /**
  66. * @var CountryArea
  67. */
  68. private $countryArea;
  69. /**
  70. * @var Collection
  71. */
  72. private $distributionNetworks;
  73. /**
  74. * @var Collection
  75. */
  76. private $whaterPoints;
  77. /**
  78. * @var Collection
  79. */
  80. private $districts;
  81. /**
  82. * @var \DateTime
  83. */
  84. private $createdAt;
  85. /**
  86. * @var \DateTime
  87. */
  88. private $updatedAt;
  89. /**
  90. * @var \DateTime
  91. */
  92. private $sinacSynchonizationAt;
  93. /**
  94. * @var Collection
  95. */
  96. private $ubications;
  97. /**
  98. * @var Collection
  99. */
  100. private $townLocations;
  101. /**
  102. * @var Collection
  103. */
  104. private $agrupations;
  105. /**
  106. * @var Collection
  107. */
  108. private $establishments;
  109. /**
  110. * @var Collection
  111. */
  112. private $organizationLicenses;
  113. /**
  114. * @var WhaterOrganization
  115. */
  116. private $whaterOrganization;
  117. /**
  118. * @var Collection
  119. */
  120. private $ownershipRequests;
  121. /**
  122. * @param string $townId
  123. * @param CountryArea $countryArea
  124. * @param string $name
  125. * @param string $slug
  126. */
  127. public function __construct(
  128. string $townId = null,
  129. Country $country,
  130. CountryArea $countryArea,
  131. string $name,
  132. float $latitude,
  133. float $longitude,
  134. string $slug = null,
  135. array $externalIds = []
  136. ) {
  137. try {
  138. $this->uuid = Uuid::fromString($townId ?: Uuid::uuid4())->toString();
  139. } catch (\InvalidArgumentException $e) {
  140. throw new InvalidUUIDException();
  141. }
  142. $this->country = $country;
  143. if ($countryArea != null) {
  144. $this->countryArea = $countryArea;
  145. $this->country = $countryArea->country();
  146. }
  147. $this->slug = $slug;
  148. $this->name = $name;
  149. $this->externalIds = $externalIds;
  150. $this->latitude = $latitude;
  151. $this->longitude = $longitude;
  152. $this->geometryPolygon = new Polygon([[
  153. [$this->longitude + 0.005, $this->latitude + 0.005],
  154. [$this->longitude + 0.005, $this->latitude - 0.005],
  155. [$this->longitude - 0.005, $this->latitude - 0.005],
  156. [$this->longitude - 0.005, $this->latitude + 0.005],
  157. [$this->longitude + 0.005, $this->latitude + 0.005]
  158. ]]);
  159. $this->distributionNetworks = new ArrayCollection();
  160. $this->whaterPoints = new ArrayCollection();
  161. $this->districts = new ArrayCollection();
  162. $this->ubications = new ArrayCollection();
  163. $this->townLocations = new ArrayCollection();
  164. $this->establishments = new ArrayCollection();
  165. $this->agrupations = new ArrayCollection();
  166. $this->organizationLicenses = new ArrayCollection();
  167. $this->createdAt = new \DateTime();
  168. $this->updatedAt = new \DateTime();
  169. $this->ownershipRequests = new ArrayCollection();
  170. }
  171. public function updateTownData(
  172. string $name,
  173. string $slug = null,
  174. int $population = null,
  175. string $geoNameId = null,
  176. WhaterOrganization $whaterOrganization = null
  177. ) {
  178. $this->name = $name;
  179. $this->slug = $slug;
  180. $this->population = $population;
  181. $this->geoNameId = $geoNameId;
  182. $this->whaterOrganization = $whaterOrganization;
  183. $this->updatedAt = new \DateTime();
  184. }
  185. public function addAgrupation(
  186. Agrupation $agrupation = null
  187. ) {
  188. if (!$this->agrupations()) {
  189. $this->agrupations = new ArrayCollection();
  190. }
  191. $this->agrupations->add($agrupation);
  192. $this->updatedAt = new \DateTime();
  193. }
  194. public function editPolygon(
  195. Polygon $geometryPolygon
  196. ) {
  197. $this->geometryPolygon = $geometryPolygon;
  198. $this->record(new TownPolygonWasUpdate($this));
  199. }
  200. public function editCenter(
  201. float $latitude = null,
  202. float $longitude = null
  203. ) {
  204. if ($latitude != null) {
  205. $this->latitude = $latitude;
  206. }
  207. if ($longitude != null) {
  208. $this->longitude = $longitude;
  209. }
  210. }
  211. public function updatesinacSynchonizationAt(\DateTime $sinacSynchonizationAt)
  212. {
  213. $this->sinacSynchonizationAt = $sinacSynchonizationAt;
  214. }
  215. /**
  216. * @return string
  217. */
  218. public function id(): string
  219. {
  220. return $this->uuid;
  221. }
  222. /**
  223. * @return string
  224. */
  225. public function name(): string
  226. {
  227. return $this->name;
  228. }
  229. /**
  230. * @return float|null
  231. */
  232. public function latitude(): ?float
  233. {
  234. return $this->latitude;
  235. }
  236. /**
  237. * @return float|null
  238. */
  239. public function longitude(): ?float
  240. {
  241. return $this->longitude;
  242. }
  243. /**
  244. * @return Polygon
  245. */
  246. public function geometryPolygon(): ?Polygon
  247. {
  248. return $this->geometryPolygon;
  249. }
  250. /**
  251. * @return LocationPolygon
  252. */
  253. public function locationPolygon(): ?LocationPolygon
  254. {
  255. if ($this->geometryPolygon() != null) {
  256. $pol = new LocationPolygon();
  257. $points = $this->geometryPolygon()->toArray();
  258. foreach ($points[0] as $point) {
  259. $lat = $point[1];
  260. $long = $point[0];
  261. $pol->addPoint(new Coordinate($lat, $long, Ellipsoid::createDefault()));
  262. }
  263. return $pol;
  264. }
  265. return null;
  266. }
  267. public function getGeoJsonPolygon(): ?string
  268. {
  269. if ($this->geometryPolygon() != null) {
  270. $geoJsonCoordinates = [];
  271. // Recorre los anillos (exterior + huecos si existen)
  272. foreach ($this->geometryPolygon()->getRings() as $ring) {
  273. $ringCoordinates = [];
  274. // Recorre los puntos del anillo
  275. foreach ($ring->getPoints() as $point) {
  276. $ringCoordinates[] = [$point->getLongitude(), $point->getLatitude()];
  277. }
  278. // Cierra el anillo si no está cerrado
  279. if ($ringCoordinates[0] !== $ringCoordinates[count($ringCoordinates) - 1]) {
  280. $ringCoordinates[] = $ringCoordinates[0];
  281. }
  282. // Añade el anillo a las coordenadas del GeoJSON
  283. $geoJsonCoordinates[] = $ringCoordinates;
  284. }
  285. // Construye el GeoJSON
  286. $geoJson = [
  287. "type" => "Polygon",
  288. "coordinates" => $geoJsonCoordinates
  289. ];
  290. return json_encode($geoJson, JSON_PRETTY_PRINT);
  291. }
  292. return null;
  293. }
  294. /**
  295. * @return string
  296. */
  297. public function slug(): ?string
  298. {
  299. return $this->slug;
  300. }
  301. /**
  302. * @return string
  303. */
  304. public function geoNameId(): ?string
  305. {
  306. return $this->geoNameId;
  307. }
  308. /**
  309. * @return integer|null
  310. */
  311. public function population(): ?int
  312. {
  313. return $this->population;
  314. }
  315. /**
  316. * @return array
  317. */
  318. public function externalIds(): array
  319. {
  320. return $this->externalIds;
  321. }
  322. /**
  323. * @return Country
  324. */
  325. public function country(): Country
  326. {
  327. return $this->country;
  328. }
  329. /**
  330. * @return CountryArea|null
  331. */
  332. public function countryArea(): ?CountryArea
  333. {
  334. return $this->countryArea;
  335. }
  336. /**
  337. * @return Collection
  338. */
  339. public function agrupations(): ?Collection
  340. {
  341. return $this->agrupations;
  342. }
  343. /**
  344. * @return Collection
  345. */
  346. public function distributionNetworks(): Collection
  347. {
  348. return $this->distributionNetworks;
  349. }
  350. /**
  351. * @return Collection
  352. */
  353. public function whaterPoints(): Collection
  354. {
  355. return $this->whaterPoints;
  356. }
  357. /**
  358. * @return Collection
  359. */
  360. public function districts(): Collection
  361. {
  362. return $this->districts;
  363. }
  364. /**
  365. * @return Collection
  366. */
  367. public function ubications(): Collection
  368. {
  369. return $this->ubications;
  370. }
  371. /**
  372. * @return Collection
  373. */
  374. public function townLocations(): Collection
  375. {
  376. return $this->townLocations;
  377. }
  378. /**
  379. * @return Collection
  380. */
  381. public function establishments(): Collection
  382. {
  383. return $this->establishments;
  384. }
  385. /**
  386. * @return Collection
  387. */
  388. public function organizationLicenses(): Collection
  389. {
  390. return $this->organizationLicenses;
  391. }
  392. /**
  393. * @return WhaterOrganization|null
  394. */
  395. public function whaterOrganization(): ?WhaterOrganization
  396. {
  397. return $this->whaterOrganization;
  398. }
  399. /**
  400. * @return \DateTime
  401. */
  402. public function createdAt(): \DateTime
  403. {
  404. return $this->createdAt;
  405. }
  406. /**
  407. * @return \DateTime
  408. */
  409. public function updatedAt(): \DateTime
  410. {
  411. return $this->updatedAt;
  412. }
  413. /**
  414. * @return Collection
  415. */
  416. public function ownershipRequests(): Collection
  417. {
  418. return $this->ownershipRequests;
  419. }
  420. /**
  421. * @return \DateTime
  422. */
  423. public function sinacSynchonizationAt(): ?\DateTime
  424. {
  425. return $this->sinacSynchonizationAt;
  426. }
  427. /**
  428. * @return bool
  429. */
  430. public function equals(Town $town)
  431. {
  432. return $this->id() === $town->id();
  433. }
  434. }