mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-11-25 13:18:30 +00:00
115 lines
2.7 KiB
PHP
115 lines
2.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Chill is a software for social workers.
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*
|
|
* @see https://www.champs-libres.coop/
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Chill\PersonBundle\Repository;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
|
use Doctrine\ORM\QueryBuilder;
|
|
use Exception;
|
|
|
|
use function count;
|
|
use function in_array;
|
|
|
|
class PersonRepository extends EntityRepository
|
|
{
|
|
/**
|
|
* @param $centers
|
|
*
|
|
* @throws \Doctrine\ORM\NoResultException
|
|
* @throws \Doctrine\ORM\NonUniqueResultException
|
|
*/
|
|
public function countByPhone(
|
|
string $phonenumber,
|
|
$centers,
|
|
array $only = ['mobile', 'phone']
|
|
): int {
|
|
$qb = $this->createQueryBuilder('p');
|
|
$qb->select('COUNT(p)');
|
|
|
|
$this->addByCenters($qb, $centers);
|
|
$this->addPhoneNumber($qb, $phonenumber, $only);
|
|
|
|
return $qb->getQuery()->getSingleScalarResult();
|
|
}
|
|
|
|
/**
|
|
* @param $centers
|
|
* @param $firstResult
|
|
* @param $maxResults
|
|
*
|
|
* @throws Exception
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public function findByPhone(
|
|
string $phonenumber,
|
|
$centers,
|
|
$firstResult,
|
|
$maxResults,
|
|
array $only = ['mobile', 'phone']
|
|
) {
|
|
$qb = $this->createQueryBuilder('p');
|
|
$qb->select('p');
|
|
|
|
$this->addByCenters($qb, $centers);
|
|
$this->addPhoneNumber($qb, $phonenumber, $only);
|
|
|
|
$qb->setFirstResult($firstResult)
|
|
->setMaxResults($maxResults);
|
|
|
|
return $qb->getQuery()->getResult();
|
|
}
|
|
|
|
protected function addByCenters(QueryBuilder $qb, array $centers)
|
|
{
|
|
if (count($centers) > 0) {
|
|
$qb->andWhere($qb->expr()->in('p.center', ':centers'));
|
|
$qb->setParameter('centers', $centers);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
protected function addPhoneNumber(QueryBuilder $qb, string $phonenumber, array $only)
|
|
{
|
|
if (count($only) === 0) {
|
|
throw new Exception('No array field to search');
|
|
}
|
|
|
|
$phonenumber = $this->parsePhoneNumber($phonenumber);
|
|
|
|
$orX = $qb->expr()->orX();
|
|
|
|
if (in_array('mobile', $only, true)) {
|
|
$orX->add($qb->expr()->like("REPLACE(p.mobilenumber, ' ', '')", ':phonenumber'));
|
|
}
|
|
|
|
if (in_array('phone', $only, true)) {
|
|
$orX->add($qb->expr()->like("REPLACE(p.phonenumber, ' ', '')", ':phonenumber'));
|
|
}
|
|
|
|
$qb->andWhere($orX);
|
|
|
|
$qb->setParameter('phonenumber', '%' . $phonenumber . '%');
|
|
}
|
|
|
|
/**
|
|
* @param $phonenumber
|
|
*/
|
|
protected function parsePhoneNumber($phonenumber): string
|
|
{
|
|
return str_replace(' ', '', $phonenumber);
|
|
}
|
|
}
|