mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-09-06 06:44:59 +00:00
cs: Fix code style (safe rules only).
This commit is contained in:
@@ -1,15 +1,25 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Search\Utils;
|
||||
|
||||
use \DateTimeImmutable;
|
||||
use DateTimeImmutable;
|
||||
use function preg_match_all;
|
||||
use function strtr;
|
||||
use function trim;
|
||||
|
||||
class ExtractDateFromPattern
|
||||
{
|
||||
private const DATE_PATTERN = [
|
||||
["([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))", 'Y-m-d'], // 1981-05-12
|
||||
["((0[1-9]|[12]\d|3[01])\/(0[1-9]|1[0-2])\/([12]\d{3}))", 'd/m/Y'], // 15/12/1980
|
||||
["((0[1-9]|[12]\d|3[01])-(0[1-9]|1[0-2])-([12]\d{3}))", 'd-m-Y'], // 15/12/1980
|
||||
['([12]\\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\\d|3[01]))', 'Y-m-d'], // 1981-05-12
|
||||
['((0[1-9]|[12]\\d|3[01])\\/(0[1-9]|1[0-2])\\/([12]\\d{3}))', 'd/m/Y'], // 15/12/1980
|
||||
['((0[1-9]|[12]\\d|3[01])-(0[1-9]|1[0-2])-([12]\\d{3}))', 'd-m-Y'], // 15/12/1980
|
||||
];
|
||||
|
||||
public function extractDates(string $subject): SearchExtractionResult
|
||||
@@ -19,14 +29,15 @@ class ExtractDateFromPattern
|
||||
|
||||
foreach (self::DATE_PATTERN as [$pattern, $format]) {
|
||||
$matches = [];
|
||||
\preg_match_all($pattern, $filteredSubject, $matches);
|
||||
preg_match_all($pattern, $filteredSubject, $matches);
|
||||
|
||||
foreach ($matches[0] as $match) {
|
||||
$date = DateTimeImmutable::createFromFormat($format, $match);
|
||||
|
||||
if (false !== $date) {
|
||||
$dates[] = $date;
|
||||
// filter string: remove what is found
|
||||
$filteredSubject = \trim(\strtr($filteredSubject, [$match => ""]));
|
||||
$filteredSubject = trim(strtr($filteredSubject, [$match => '']));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,28 +1,47 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Search\Utils;
|
||||
|
||||
use LogicException;
|
||||
use function implode;
|
||||
use function preg_match;
|
||||
use function str_split;
|
||||
use function strtr;
|
||||
use function trim;
|
||||
|
||||
class ExtractPhonenumberFromPattern
|
||||
{
|
||||
private const PATTERN = "([\+]{0,1}[0-9\ ]{5,})";
|
||||
private const PATTERN = '([\\+]{0,1}[0-9\\ ]{5,})';
|
||||
|
||||
public function extractPhonenumber(string $subject): SearchExtractionResult
|
||||
{
|
||||
$matches = [];
|
||||
\preg_match(self::PATTERN, $subject,$matches);
|
||||
preg_match(self::PATTERN, $subject, $matches);
|
||||
|
||||
if (0 < count($matches)) {
|
||||
$phonenumber = [];
|
||||
$length = 0;
|
||||
|
||||
foreach (\str_split(\trim($matches[0])) as $key => $char) {
|
||||
foreach (str_split(trim($matches[0])) as $key => $char) {
|
||||
switch ($char) {
|
||||
case '0':
|
||||
$length++;
|
||||
if ($key === 0) { $phonenumber[] = '+32'; }
|
||||
else { $phonenumber[] = $char; }
|
||||
|
||||
if (0 === $key) {
|
||||
$phonenumber[] = '+32';
|
||||
} else {
|
||||
$phonenumber[] = $char;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case '1':
|
||||
case '2':
|
||||
case '3':
|
||||
@@ -34,18 +53,21 @@ class ExtractPhonenumberFromPattern
|
||||
case '9':
|
||||
$length++;
|
||||
$phonenumber[] = $char;
|
||||
|
||||
break;
|
||||
|
||||
case ' ':
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \LogicException("should not match not alnum character");
|
||||
throw new LogicException('should not match not alnum character');
|
||||
}
|
||||
}
|
||||
|
||||
if ($length > 5) {
|
||||
$filtered = \trim(\strtr($subject, [$matches[0] => '']));
|
||||
if (5 < $length) {
|
||||
$filtered = trim(strtr($subject, [$matches[0] => '']));
|
||||
|
||||
return new SearchExtractionResult($filtered, [\implode('', $phonenumber)] );
|
||||
return new SearchExtractionResult($filtered, [implode('', $phonenumber)]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,10 +1,18 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
namespace Chill\MainBundle\Search\Utils;
|
||||
|
||||
class SearchExtractionResult
|
||||
{
|
||||
private string $filteredSubject;
|
||||
|
||||
private array $found;
|
||||
|
||||
public function __construct(string $filteredSubject, array $found)
|
||||
@@ -13,6 +21,11 @@ class SearchExtractionResult
|
||||
$this->found = $found;
|
||||
}
|
||||
|
||||
public function getFilteredSubject(): string
|
||||
{
|
||||
return $this->filteredSubject;
|
||||
}
|
||||
|
||||
public function getFound(): array
|
||||
{
|
||||
return $this->found;
|
||||
@@ -22,9 +35,4 @@ class SearchExtractionResult
|
||||
{
|
||||
return [] !== $this->found;
|
||||
}
|
||||
|
||||
public function getFilteredSubject(): string
|
||||
{
|
||||
return $this->filteredSubject;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user