src/BackendBundle/Helper/AddressHelper.php line 23

Open in your IDE?
  1. <?php
  2. namespace App\BackendBundle\Helper;
  3. use Doctrine\Common\Collections\Collection;
  4. use Doctrine\DBAL\Connection;
  5. use Doctrine\ORM\EntityManagerInterface;
  6. use App\BackendBundle\Interfaces\LocationInterface;
  7. use App\BackendBundle\Helper\GoogleApiHelper;
  8. use App\BackendBundle\Model\Address\AddressParameter;
  9. use App\Entity\Address;
  10. use App\Entity\AddressCity;
  11. use App\Entity\AddressDistrict;
  12. use App\Entity\AddressType;
  13. use App\Entity\AddressLatLon;
  14. class AddressHelper {
  15.     private EntityManagerInterface $em;
  16.     private Connection $connection;
  17.     private GoogleApiHelper $googleApiHelper;
  18.     public function __construct(EntityManagerInterface $emGoogleApiHelper $googleApiHelper) {
  19.         $this->em $em;
  20.         $this->connection $this->em->getConnection();
  21.         $this->googleApiHelper $googleApiHelper;
  22.     }
  23.     public function createAddress($addressData$withLatLon false) {
  24.         $address = new Address();
  25.         if (array_key_exists('street'$addressData)) {
  26.             $address->setStreet($addressData['street']);
  27.         }
  28.         if (array_key_exists('number'$addressData)) {
  29.             $address->setNumber($addressData['number']);
  30.         }
  31.         if (array_key_exists('floor'$addressData)) {
  32.             $address->setFloor($addressData['floor']);
  33.         }
  34.         if (array_key_exists('stair'$addressData)) {
  35.             $address->setStair($addressData['stair']);
  36.         }
  37.         if (array_key_exists('type'$addressData)) {
  38.             $address->setAddressType($addressData['type']);
  39.         }
  40.         if (array_key_exists('city'$addressData)) {
  41.             $address->setAddressCity($addressData['city']);
  42.         }
  43.         $this->em->persist($address);
  44.         $this->em->flush();
  45.         return $address;
  46.     }
  47.     public function createAddressLatLon(Address $address$lat$lon) {
  48.         $addressLatLon = new AddressLatLon();
  49.         $addressLatLon->setAddress($address);
  50.         $addressLatLon->setLat($lat);
  51.         $addressLatLon->setLon($lon);
  52.         $this->em->persist($addressLatLon);
  53.         $this->em->flush();
  54.         return $addressLatLon;
  55.     }
  56.     private function updateAddressLatLon(Address $addressAddressParameter $addressParameter) {
  57.         if ($addressParameter->hasLatLon()) {
  58.             $this->updateAddressLatLonValues($address$addressParameter->getLat(), $addressParameter->getLon());
  59.             return;
  60.         }
  61.         if ($addressParameter->getUseGoogleApi()) {
  62.             $this->updateAddressLatLonGoogle($address);
  63.             return;
  64.         }
  65.     }
  66.     private function updateAddressLatLonValues(Address $address$lat$lon) {
  67.         $addressLatLon $address->getAddressLatLon();
  68.         if (empty($addressLatLon)) {
  69.             $this->createAddressLatLon($address$lat$lon);
  70.             return;
  71.         }
  72.         $addressLatLon->setLat($lat);
  73.         $addressLatLon->setLon($lon);
  74.         $this->em->persist($addressLatLon);
  75.         $this->em->flush();
  76.     }
  77.     private function updateAddressLatLonGoogle(Address $address) {
  78.         [$lat$lon] = $this->googleApiHelper->getLatLon($address);
  79.         if (empty($lat) || empty($lon)) {
  80.             return;
  81.         }
  82.         $addressLatLon $address->getAddressLatLon();
  83.         if (empty($addressLatLon)) {
  84.             $this->createAddressLatLon($address$lat$lon);
  85.             return;
  86.         }
  87.         $addressLatLon->setLat($lat);
  88.         $addressLatLon->setLon($lon);
  89.         $this->em->persist($addressLatLon);
  90.         $this->em->flush();
  91.     }
  92.     public function deleteAddressLatLon(Address $address) {
  93.         $addressLatLon $address->getAddressLatLon();
  94.         if (!empty($addressLatLon)) {
  95.             $this->em->remove($addressLatLon);
  96.             $this->em->flush();
  97.         }
  98.     }
  99.     public function createAddressCity($zip$name) {
  100.         $city = new AddressCity();
  101.         $city->setZip($zip);
  102.         $city->setName($name);
  103.         $this->em->persist($city);
  104.         return $city;
  105.     }
  106.     public function updateAddress(Address $address$locationData$useGoogleAPI false) {
  107.         $cityString $locationData['city'];
  108.         $zipString $locationData['zip'];
  109.         // First check if the city already exists in our DB
  110.         $city $this->getAddressCityByName($cityString);
  111.         if (is_null($city)) {
  112.             // if not then create a new entry
  113.             $city $this->createAddressCity($zipString$cityString);
  114.         }
  115.         $streetParts $this->getStreetParts($locationData['street']);
  116.         if (!empty($streetParts['street'] && !empty($streetParts['number']))) {
  117.             $address->setStreet($streetParts['street']);
  118.             $address->setNumber($streetParts['number']);
  119.         } else {
  120.             $address->setStreet($locationData['street']);
  121.         }
  122.         $address->setAddressCity($city);
  123.         $this->em->persist($address);
  124.         $this->em->flush();
  125.         $lat $locationData['lat'];
  126.         $lon $locationData['lon'];
  127.         $this->updateAddressLatLon($address$lat$lon$useGoogleAPI);
  128.         return $address;
  129.     }
  130.     public function updateAddressAdmin($addressData) {
  131.         $address $addressData['address'];
  132.         if (array_key_exists('street'$addressData)) {
  133.             $address->setStreet($addressData['street']);
  134.         }
  135.         if (array_key_exists('number'$addressData)) {
  136.             $address->setNumber($addressData['number']);
  137.         }
  138.         if (array_key_exists('floor'$addressData)) {
  139.             $address->setFloor($addressData['floor']);
  140.         }
  141.         if (array_key_exists('stair'$addressData)) {
  142.             $address->setStair($addressData['stair']);
  143.         }
  144.         if (array_key_exists('latLon'$addressData)) {
  145.             $address->setAddressLatLon($addressData['latLon']);
  146.         }
  147.         if (array_key_exists('type'$addressData)) {
  148.             $address->setAddressType($addressData['type']);
  149.         }
  150.         if (array_key_exists('city'$addressData)) {
  151.             $address->setAddressCity($addressData['city']);
  152.         }
  153.         if (array_key_exists('latLon'$addressData)) {
  154.             $addressData['latLon']->setAddress($address);
  155.         }
  156.         $this->em->persist($address);
  157.         if (array_key_exists('latLon'$addressData)) {
  158.             $this->em->persist($addressData['latLon']);
  159.         }
  160.         return $address;
  161.     }
  162.     public function createAddressByData(AddressParameter $addressParameter) {
  163.         $city $this->getAddressCity($addressParameter->getCity(), $addressParameter->getZip());
  164.         if (empty($addressParameter->getAddressType())) {
  165.             $addressParameter->setAddressType($this->getAddressTypeByShortName('STD'));
  166.         }
  167.         // our street string contains both name and number, so we split it here
  168.         // and store it as an array in result
  169.         $streetParts $this->getStreetParts($addressParameter->getStreet());
  170.         //preg_match('/^([^\d]*[^\d\s]) *(\d.*)$/', $street, $streetAndNumber);
  171.         $addressData = array();
  172.         $addressData['type'] = $addressParameter->getAddressType();
  173.         $addressData['city'] = $city;
  174.         $address null;
  175.         if (!empty($streetParts['street'] && !empty($streetParts['number']))) {
  176.             // create a new address
  177.             $addressData['street'] = $streetParts['street'];
  178.             $addressData['number'] = $streetParts['number'];
  179.             $address $this->createAddress($addressData);
  180.         } else {
  181.             // create a new address
  182.             $addressData['street'] = $addressParameter->getStreet();
  183.             $address $this->createAddress($addressData);
  184.         }
  185.         if (!empty($address)) {
  186.             $this->updateAddressLatLon($address$addressParameter);
  187.         }
  188.         return $address;
  189.     }
  190.     public function getAddressCity($cityString$zipString) {
  191.         // first check if city exists by name
  192.         $cityByName $this->getAddressCityByName($cityString);
  193.         if (!empty($cityByName)) {
  194.             return $cityByName;
  195.         }
  196.         // second check if city exists by zip
  197.         $cityByZip $this->getAddressCityByZip($zipString);
  198.         if (!empty($cityByZip)) {
  199.             return $cityByZip;
  200.         }
  201.         // check if city and zip has wrongly be changed
  202.         if (is_numeric($cityString)) {
  203.             $cityByZip $this->getAddressCityByZip($cityString);
  204.             if (!empty($cityByZip)) {
  205.                 return $cityByZip;
  206.             }
  207.         }
  208.         // if no case matches then create a new entry
  209.         $newCity $this->createAddressCity($zipString$cityString);
  210.         return $newCity;
  211.     }
  212.     public function getAddressCityByID($id) {
  213.         return $this->em->getRepository(AddressCity::class)->findOneBy(array('id' => $id));
  214.     }
  215.     public function getAddressByID($id) {
  216.         return $this->em->getRepository(Address::class)->findOneBy(array('id' => $id));
  217.     }
  218.     public function getAddressesByCommaIds($string) {
  219.         $idArray explode(","$string);
  220.         $addressAdded = array();
  221.         $addresses = array();
  222.         foreach ($idArray as $id) {
  223.             if (array_key_exists($id$addressAdded)) {
  224.                 continue;
  225.             }
  226.             $address $this->getAddressByID($id);
  227.             if (empty($address)) {
  228.                 continue;
  229.             }
  230.             array_push($addresses$address);
  231.             $addressAdded[$id] = 1;
  232.         }
  233.         return $addresses;
  234.     }
  235.     public function getAddressCityByName($name) {
  236.         $dqlString 'SELECT city FROM App\Entity\AddressCity city
  237.                     WHERE city.name = :name';
  238.         $query $this->em->createQuery($dqlString);
  239.         $query->setParameter('name'$name);
  240.         $cities $query->getResult();
  241.         // if exactly one result return it
  242.         if (count($cities) == 1) {
  243.             return $cities[0];
  244.         }
  245.         // if more then one result - return first
  246.         if (count($cities) > 1) {
  247.             return $cities[0];
  248.         }
  249.         // default
  250.         return null;
  251.     }
  252.     public function searchAddressCitiesByName($searchText) {
  253.         $sql "SELECT id as id, name as text "
  254.                 "FROM address_city as ac "
  255.                 "WHERE ac.name LIKE :searchText";
  256.         $stmt $this->connection->prepare($sql);
  257.         $stmt->bindValue('searchText'"%$searchText%");
  258.         $stmtResult $stmt->executeQuery();
  259.         return $stmtResult->fetchAllAssociative();
  260.     }
  261.     public function getAddressCityByZip($zip) {
  262.         $sqlText 'SELECT city FROM App\Entity\AddressCity city
  263.                     WHERE city.zip = :zip';
  264.         $query $this->em->createQuery($sqlText);
  265.         $query->setParameter('zip'$zip);
  266.         $cities $query->getResult();
  267.         // if exactly one result return it
  268.         if (count($cities) == 1) {
  269.             return $cities[0];
  270.         }
  271.         // if more then one result - return first
  272.         if (count($cities) > 1) {
  273.             return $cities[0];
  274.         }
  275.         // default
  276.         return null;
  277.     }
  278.     // used in autocomplete controller
  279.     public function getAutocompleteCities($searchText) {
  280.         if (!empty($searchText)) {
  281.             $sqlText "SELECT a FROM App\Entity\AddressCity a WHERE a.name like :searchText OR a.zip like :searchNumber";
  282.             $query $this->em->createQuery($sqlText);
  283.             $query->setParameter('searchNumber'$searchText);
  284.             $query->setParameter('searchText''%' $searchText '%');
  285.             return $query->getResult();
  286.         }
  287.         return array();
  288.     }
  289.     public function getAddressCitiesByZipArray() {
  290.         $cities $this->em->getRepository(AddressCity::class)->findAll();
  291.         $result = array();
  292.         /* @var $city AddressCity */
  293.         foreach ($cities as $city) {
  294.             $zip $city->getZip();
  295.             $result[$zip] = $city;
  296.         }
  297.         return $result;
  298.     }
  299.     public function getAddressTypesByShortNameArray() {
  300.         $types $this->em->getRepository(AddressType::class)->findAll();
  301.         $result = array();
  302.         /* @var $type AddressType */
  303.         foreach ($types as $type) {
  304.             $shortName $type->getShortName();
  305.             $result[$shortName] = $type;
  306.         }
  307.         return $result;
  308.     }
  309.     public function getAddressTypeByShortName($shortName) {
  310.         $result $this->em->getRepository(AddressType::class)->findOneBy(array('shortName' => $shortName));
  311.         return $result;
  312.     }
  313.     public function getAddressDistrict(Address $address null) {
  314.         if ($address == null) {
  315.             return null;
  316.         }
  317.         $city $address->getAddressCity();
  318.         if (empty($city)) {
  319.             return null;
  320.         }
  321.         return $city->getAddressDistrict();
  322.     }
  323.     public function getAddressDistrictByID($id) {
  324.         return $this->em->getRepository(AddressDistrict::class)->findOneBy(array('id' => $id));
  325.     }
  326.     public function getAddressDistrictByName($name) {
  327.         return $this->em->getRepository(AddressDistrict::class)->findOneBy(array('name' => $name));
  328.     }
  329.     public function getAddressDistrictByStateID($stateID) {
  330.         return $this->em->getRepository(AddressDistrict::class)->findBy(array('addressState' => $stateID), array('name' => 'ASC'));
  331.     }
  332.     public function getAddressDistrictByStateName($stateName) {
  333.         $sqlText 'SELECT district FROM App\Entity\AddressDistrict district
  334.                     JOIN district.addressState state 
  335.                     WHERE state.name = :stateName
  336.                     ORDER BY district.name ASC';
  337.         $query $this->em->createQuery($sqlText);
  338.         $query->setParameter('stateName'$stateName);
  339.         return $query->getResult();
  340.     }
  341.     public function getLocationDistricts($locations) {
  342.         $districts = array();
  343.         /* @var $location LocationInterface */
  344.         foreach ($locations as $location) {
  345.             $address $location->getAddress();
  346.             if (empty($address)) {
  347.                 continue;
  348.             }
  349.             /* @var $addressDistrict AddressDistrict */
  350.             $addressDistrict $this->getAddressDistrict($address);
  351.             if (empty($addressDistrict)) {
  352.                 continue;
  353.             }
  354.             // check if the district already is in the array
  355.             if (!in_array($addressDistrict$districts)) {
  356.                 // append it to the array
  357.                 array_push($districts$addressDistrict);
  358.             }
  359.         }
  360.         return $districts;
  361.     }
  362.     public function getStreetParts($streetName): array
  363.     {
  364.         $result = array();
  365.         $result['street'] = null;
  366.         $result['number'] = null;
  367.         if (empty($streetName)) {
  368.             return $result;
  369.         }
  370.         $spaceLast strrpos($streetName" ");
  371.         if (!$spaceLast) {
  372.             $result['street'] = $streetName;
  373.             return $result;
  374.         }
  375.         $result['street'] = trim(substr($streetName0$spaceLast));
  376.         $result['number'] = trim(substr($streetName$spaceLast));
  377.         return $result;
  378.     }
  379.     public function sortLocations(Collection $locations) {
  380.         if (count($locations) < 2) {
  381.             return $locations;
  382.         }
  383.         $arrLocations $locations->toArray();
  384.         uasort($arrLocations, function ($location1$location2) {
  385.             $address1 $location1->getAddress();
  386.             $address2 $location2->getAddress();
  387.             if ($address1->getStreet() > $address2->getStreet()) {
  388.                 return 1;
  389.             } else {
  390.                 return -1;
  391.             }
  392.         });
  393.         return $arrLocations;
  394.     }
  395. }