<?php
namespace App\Repository\Fuel;
use App\Entity\Fuel\FuelStations;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method FuelStations|null find($id, $lockMode = null, $lockVersion = null)
* @method FuelStations|null findOneBy(array $criteria, array $orderBy = null)
* @method FuelStations[] findAll()
* @method FuelStations[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class FuelStationsRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, FuelStations::class);
}
/**
* Returns a station identified by Name/Alias or Phone Formatted/Simp from FuelStations
* SELECT * FROM `fuel_stations` WHERE (`name` like 'Exxon%' OR `name` like 'Express Mart%^')
* AND (`phone` = '(843) 626-8878' OR `phone` = '843-626-8878')
*/
public function identifyOne($title, $alias = '', $fphone, $simp): ?FuelStations
{
return $this->createQueryBuilder('s')
->andWhere('(s.name like :title OR s.name like :alias)')
->andWhere('(s.phone = :fphone OR s.phone = :simp)')
->setParameter('title', $title.'%')
->setParameter('alias', $alias.'%')
->setParameter('fphone', $fphone)
->setParameter('simp', $simp)
->orderBy('s.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
/**
* Returns a station identified by Name/Alias or Address
*/
public function identifyTwo($title, $alias = '', $address): ?FuelStations
{
$aparts = explode(',', trim($address));
$parts = explode(' ', trim($aparts[0]));
foreach ($parts as $k => $part) {
if (strlen($part) <= 1) {
unset($parts[$k]);
}
}
$saddress = implode(' ', $parts);
$address = $aparts[0];
return $this->createQueryBuilder('s')
->andWhere('(s.name like :title OR s.name like :alias)')
->andWhere('(s.address = :address OR s.address = :saddress)')
->setParameter('title', $title.'%')
->setParameter('alias', $alias.'%')
->setParameter('address', $address)
->setParameter('saddress', $saddress)
->orderBy('s.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
/**
* Returns a station identified by Name/Alias or Address
*/
public function identifyThree($title, $alias = '', $lat, $lng): ?FuelStations
{
return $this->createQueryBuilder('s')
->andWhere('(s.name like :title OR s.name like :alias)')
->andWhere('(s.lat like :lat OR s.lng like :lng)')
->setParameter('title', $title.'%')
->setParameter('alias', $alias.'%')
->setParameter('lat', $lat.'%')
->setParameter('lng', $lng.'%')
->orderBy('s.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
/**
* Returns a station identified by Name/Alias or Address
*/
public function identifyFour($title, $alias = '', $lat, $lng): ?FuelStations
{
$title = preg_split('/[\s,]+/', $title, 2);
$alias = preg_split('/[\s,]+/', $alias, 2);
if(isset($title[1])) {
$title = $title[0].' '.$title[1];
} else {
$title = $title[0];
}
if(isset($alias[1])) {
$alias = $alias[0].' '.$alias[1];
} else {
$alias = $alias[0];
}
return $this->createQueryBuilder('s')
->andWhere('(s.name like :title OR s.name like :alias)')
->andWhere('(s.lat like :lat OR s.lng like :lng)')
->setParameter('title', $title.'%')
->setParameter('alias', $alias.'%')
->setParameter('lat', $lat.'%')
->setParameter('lng', $lng.'%')
->orderBy('s.id', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
public function stationsByGeoHrs($lat,$lng,$rad = 5, $hrlimit = 72) {
$updated = date('Y-m-d G:i:s', strtotime('-'.$hrlimit.' hours'));
return $listings = $this->createQueryBuilder('b')
->addSelect('(3959 * acos(cos(radians(:lat)) * cos(radians( b.lat )) * cos(radians( b.lng ) - radians( :lng )) + sin(radians( :lat )) * sin(radians( b.lat )))) AS distance')
->andWhere('b.modified < :hrlimit')
->setParameter('hrlimit', $updated)
->setParameter('lat', $lat)
->setParameter('lng', $lng)
->setParameter('rad', $rad)
->having('distance < :rad')
->orderBy('distance', 'ASC')
->setMaxResults(5000)
->getQuery()
->getScalarResult()
;
}
// /**
// * @return FuelStations[] Returns an array of FuelStations objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('f')
->andWhere('f.exampleField = :val')
->setParameter('val', $value)
->orderBy('f.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?FuelStations
{
return $this->createQueryBuilder('f')
->andWhere('f.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}