cs: Fix code style (safe rules only).

This commit is contained in:
Pol Dellaiera
2021-11-23 14:06:38 +01:00
parent 149d7ce991
commit 8f96a1121d
1223 changed files with 65199 additions and 64625 deletions

View File

@@ -1,20 +1,12 @@
<?php
/*
* Copyright (C) 2017 Champs Libres Cooperative <info@champs-libres.coop>
/**
* Chill is a software for social workers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
@@ -22,16 +14,21 @@ use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
*
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class GetJsonFieldByKey extends FunctionNode
{
private $expr1;
private $expr2;
public function getSql(SqlWalker $sqlWalker)
{
return sprintf(
'(%s->%s)',
$this->expr1->dispatch($sqlWalker),
$this->expr2->dispatch($sqlWalker)
);
}
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
@@ -41,13 +38,4 @@ class GetJsonFieldByKey extends FunctionNode
$this->expr2 = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return sprintf(
'(%s->%s)',
$this->expr1->dispatch($sqlWalker),
$this->expr2->dispatch($sqlWalker)
);
}
}

View File

@@ -1,20 +1,12 @@
<?php
/*
* Copyright (C) 2017 Champs Libres Cooperative <info@champs-libres.coop>
/**
* Chill is a software for social workers
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
@@ -24,16 +16,14 @@ use Doctrine\ORM\Query\SqlWalker;
/**
* Return an aggregation of values in a json representation, as a string.
*
*
* Internally, this function use the postgresql `jsonb_agg` function. Using
* json allow to aggregate data from different types without have to cast them.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class JsonAggregate extends FunctionNode
{
private $expr;
public function getSql(SqlWalker $sqlWalker)
{
return sprintf('jsonb_agg(%s)', $this->expr->dispatch($sqlWalker));

View File

@@ -1,8 +1,12 @@
<?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\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
@@ -11,7 +15,7 @@ use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
* Return the length of an array
* Return the length of an array.
*/
class JsonbArrayLength extends FunctionNode
{

View File

@@ -1,8 +1,12 @@
<?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\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
@@ -10,13 +14,10 @@ use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
/**
*
@author Julien Fastré <julien.fastre@champs-libres.coop>
*/
class JsonbExistsInArray extends FunctionNode
{
private $expr1;
private $expr2;
public function getSql(SqlWalker $sqlWalker): string

View File

@@ -1,5 +1,12 @@
<?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.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Doctrine\DQL;
@@ -8,23 +15,24 @@ use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\AST\PathExpression;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Exception;
/**
* DQL function for OVERLAPS function in postgresql
* DQL function for OVERLAPS function in postgresql.
*
* If a value is null in period start, it will be replaced by -infinity.
* If a value is null in period end, it will be replaced by infinity
*/
class OverlapsI extends FunctionNode
{
private $firstPeriodStart;
private $firstPeriodEnd;
private $secondPeriodStart;
private $firstPeriodStart;
private $secondPeriodEnd;
private $secondPeriodStart;
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return sprintf(
@@ -36,36 +44,6 @@ class OverlapsI extends FunctionNode
);
}
protected function makeCase($sqlWalker, $part, string $position): string
{
switch ($position) {
case 'start' :
$p = '-infinity';
break;
case 'end':
$p = 'infinity';
break;
default:
throw new \Exception('Unexpected position value.');
}
if ($part instanceof PathExpression) {
return sprintf(
"CASE WHEN %s IS NOT NULL THEN %s ELSE '%s'::date END",
$part->dispatch($sqlWalker),
$part->dispatch($sqlWalker),
$p
);
}
return sprintf(
"CASE WHEN %s::date IS NOT NULL THEN %s::date ELSE '%s'::date END",
$part->dispatch($sqlWalker),
$part->dispatch($sqlWalker),
$p
);
}
public function parse(Parser $parser): void
{
$parser->match(Lexer::T_IDENTIFIER);
@@ -92,4 +70,38 @@ class OverlapsI extends FunctionNode
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
protected function makeCase($sqlWalker, $part, string $position): string
{
switch ($position) {
case 'start':
$p = '-infinity';
break;
case 'end':
$p = 'infinity';
break;
default:
throw new Exception('Unexpected position value.');
}
if ($part instanceof PathExpression) {
return sprintf(
"CASE WHEN %s IS NOT NULL THEN %s ELSE '%s'::date END",
$part->dispatch($sqlWalker),
$part->dispatch($sqlWalker),
$p
);
}
return sprintf(
"CASE WHEN %s::date IS NOT NULL THEN %s::date ELSE '%s'::date END",
$part->dispatch($sqlWalker),
$part->dispatch($sqlWalker),
$p
);
}
}

View File

@@ -1,46 +1,33 @@
<?php
/*
/**
* Chill is a software for social workers
* Copyright (C) 2019 Champs-Libres Coopérative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
/**
*
*
*/
class Replace extends FunctionNode
{
protected $string;
protected $from;
protected $string;
protected $to;
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker): string
{
return 'REPLACE('.
$this->string->dispatch($sqlWalker).
', '.
$this->from->dispatch($sqlWalker).
', '.
$this->to->dispatch($sqlWalker).
return 'REPLACE(' .
$this->string->dispatch($sqlWalker) .
', ' .
$this->from->dispatch($sqlWalker) .
', ' .
$this->to->dispatch($sqlWalker) .
')';
}
@@ -48,17 +35,17 @@ class Replace extends FunctionNode
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->string = $parser->StringPrimary();
$parser->match(Lexer::T_COMMA);
$this->from = $parser->StringPrimary();
$parser->match(Lexer::T_COMMA);
$this->to = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}

View File

@@ -1,28 +1,19 @@
<?php
/*
/**
* Chill is a software for social workers
* Copyright (C) 2018 Champs-Libres Coopérative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
/**
* Geometry function 'ST_CONTAINS', added by postgis
* Geometry function 'ST_CONTAINS', added by postgis.
*/
class STContains extends FunctionNode
{
@@ -32,8 +23,8 @@ class STContains extends FunctionNode
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'ST_CONTAINS('.$this->firstPart->dispatch($sqlWalker).
', ' . $this->secondPart->dispatch($sqlWalker) .")";
return 'ST_CONTAINS(' . $this->firstPart->dispatch($sqlWalker) .
', ' . $this->secondPart->dispatch($sqlWalker) . ')';
}
public function parse(\Doctrine\ORM\Query\Parser $parser)

View File

@@ -1,21 +1,12 @@
<?php
/*
/**
* Chill is a software for social workers
* Copyright (C) 2018 Champs-Libres Coopérative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Doctrine\DQL;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
@@ -29,8 +20,8 @@ class Similarity extends FunctionNode
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'SIMILARITY('.$this->firstPart->dispatch($sqlWalker).
', ' . $this->secondPart->dispatch($sqlWalker) .")";
return 'SIMILARITY(' . $this->firstPart->dispatch($sqlWalker) .
', ' . $this->secondPart->dispatch($sqlWalker) . ')';
}
public function parse(\Doctrine\ORM\Query\Parser $parser)

View File

@@ -1,5 +1,12 @@
<?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\Doctrine\DQL;
use Doctrine\ORM\Query\Lexer;
@@ -12,13 +19,13 @@ class StrictWordSimilarityOPS extends \Doctrine\ORM\Query\AST\Functions\Function
private $secondPart;
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
public function getSql(SqlWalker $sqlWalker)
{
return $this->firstPart->dispatch($sqlWalker).
return $this->firstPart->dispatch($sqlWalker) .
' <<% ' . $this->secondPart->dispatch($sqlWalker);
}
public function parse(\Doctrine\ORM\Query\Parser $parser)
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);

View File

@@ -1,21 +1,10 @@
<?php
/*
/**
* Chill is a software for social workers
* Copyright (C) 2015 Champs-Libres Coopérative <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\MainBundle\Doctrine\DQL;
@@ -24,30 +13,27 @@ use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
/**
* Unaccent string using postgresql extension unaccent :
* http://www.postgresql.org/docs/current/static/unaccent.html
*
* Usage : StringFunction UNACCENT(string)
* Unaccent string using postgresql extension unaccent :
* http://www.postgresql.org/docs/current/static/unaccent.html.
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* Usage : StringFunction UNACCENT(string)
*/
class Unaccent extends FunctionNode
{
private $string;
public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
{
return 'UNACCENT(' . $this->string->dispatch($sqlWalker) .")";
return 'UNACCENT(' . $this->string->dispatch($sqlWalker) . ')';
}
public function parse(\Doctrine\ORM\Query\Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->string = $parser->StringPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
}

View File

@@ -1,36 +1,37 @@
<?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\Doctrine\Event;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Chill\MainBundle\Doctrine\Model\TrackCreationInterface;
use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface;
use Symfony\Component\Security\Core\Security;
class TrackCreateUpdateSubscriber implements EventSubscriber
{
private Security $security;
/**
* @param Security $security
*/
public function __construct(Security $security)
{
$this->security = $security;
}
/**
* {@inheritDoc}
*/
public function getSubscribedEvents()
{
return [
Events::prePersist,
Events::preUpdate
Events::preUpdate,
];
}
@@ -41,7 +42,7 @@ class TrackCreateUpdateSubscriber implements EventSubscriber
if ($object instanceof TrackCreationInterface
&& $this->security->getUser() instanceof User) {
$object->setCreatedBy($this->security->getUser());
$object->setCreatedAt(new \DateTimeImmutable('now'));
$object->setCreatedAt(new DateTimeImmutable('now'));
}
$this->onUpdate($object);
@@ -56,10 +57,10 @@ class TrackCreateUpdateSubscriber implements EventSubscriber
protected function onUpdate(object $object): void
{
if ($object instanceof TrackUpdateInterface
if ($object instanceof TrackUpdateInterface
&& $this->security->getUser() instanceof User) {
$object->setUpdatedBy($this->security->getUser());
$object->setUpdatedAt(new \DateTimeImmutable('now'));
$object->setUpdatedAt(new DateTimeImmutable('now'));
}
}
}

View File

@@ -1,12 +1,19 @@
<?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\Doctrine\Migrations;
use Doctrine\Migrations\Version\Comparator;
use Doctrine\Migrations\Version\Version;
use function strcmp;
use function explode;
use function strcmp;
/**
* Compare Version classes names from different namespaces and order them.
@@ -15,18 +22,15 @@ use function explode;
*/
final class VersionComparator implements Comparator
{
public function compare(Version $a, Version $b): int
{
return strcmp($this->getNumber($a), $this->getNumber($b));
}
private function getNumber(Version $version): string
{
$names = explode("\\", (string) $version);
$names = explode('\\', (string) $version);
return end($names);
}
public function compare(Version $a, Version $b): int
{
$q = strcmp($this->getNumber($a), $this->getNumber($b));
return $q;
}
}

View File

@@ -1,25 +1,79 @@
<?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\Doctrine\Model;
use \JsonSerializable;
use Exception;
use JsonSerializable;
use function json_encode;
class Point implements JsonSerializable {
private ?float $lat;
private ?float $lon;
class Point implements JsonSerializable
{
public static string $SRID = '4326';
private ?float $lat;
private ?float $lon;
private function __construct(?float $lon, ?float $lat)
{
$this->lat = $lat;
$this->lon = $lon;
}
public function toGeoJson(): string
public static function fromArrayGeoJson(array $array): self
{
$array = $this->toArrayGeoJson();
if ('Point' === $array['type'] && isset($array['coordinates'])) {
return self::fromLonLat($array['coordinates'][0], $array['coordinates'][1]);
}
return \json_encode($array);
throw new Exception('Unable to build a point from input data.');
}
public static function fromGeoJson(string $geojson): self
{
$a = json_decode($geojson);
if (null === $a) {
throw PointException::badJsonString($geojson);
}
if (null === $a->type || null === $a->coordinates) {
throw PointException::badJsonString($geojson);
}
if ('Point' !== $a->type) {
throw PointException::badGeoType();
}
[$lon, $lat] = $a->coordinates;
return Point::fromLonLat($lon, $lat);
}
public static function fromLonLat(float $lon, float $lat): self
{
if ((-180 < $lon && 180 > $lon) && (-90 < $lat && 90 > $lat)) {
return new Point($lon, $lat);
}
throw PointException::badCoordinates($lon, $lat);
}
public function getLat(): float
{
return $this->lat;
}
public function getLon(): float
{
return $this->lon;
}
public function jsonSerialize(): array
@@ -35,59 +89,15 @@ class Point implements JsonSerializable {
];
}
public function toGeoJson(): string
{
$array = $this->toArrayGeoJson();
return json_encode($array);
}
public function toWKT(): string
{
return sprintf("SRID=%s;POINT(%s %s)", self::$SRID, $this->lon, $this->lat);
}
public static function fromGeoJson(string $geojson): self
{
$a = json_decode($geojson);
if (null === $a) {
throw PointException::badJsonString($geojson);
}
if (null === $a->type || null === $a->coordinates) {
throw PointException::badJsonString($geojson);
}
if ($a->type !== 'Point'){
throw PointException::badGeoType();
}
[$lon, $lat] = $a->coordinates;
return Point::fromLonLat($lon, $lat);
}
public static function fromLonLat(float $lon, float $lat): self
{
if (($lon > -180 && $lon < 180) && ($lat > -90 && $lat < 90)) {
return new Point($lon, $lat);
}
throw PointException::badCoordinates($lon, $lat);
}
public static function fromArrayGeoJson(array $array): self
{
if ($array['type'] === 'Point' && isset($array['coordinates'])) {
return self::fromLonLat($array['coordinates'][0], $array['coordinates'][1]);
}
throw new \Exception('Unable to build a point from input data.');
}
public function getLat(): float
{
return $this->lat;
}
public function getLon(): float
{
return $this->lon;
return sprintf('SRID=%s;POINT(%s %s)', self::$SRID, $this->lon, $this->lat);
}
}

View File

@@ -1,27 +1,33 @@
<?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\Doctrine\Model;
use \Exception;
use Exception;
/**
* Description of PointException
*
* Description of PointException.
*/
class PointException extends Exception {
class PointException extends Exception
{
public static function badCoordinates($lon, $lat): self
{
return new self("Input coordinates are not valid in the used coordinate system (longitude = $lon , latitude = $lat)");
}
public static function badJsonString($str): self
{
return new self("The JSON string is not valid: $str");
return new self("Input coordinates are not valid in the used coordinate system (longitude = {$lon} , latitude = {$lat})");
}
public static function badGeoType(): self
{
return new self("The geoJSON object type is not valid");
return new self('The geoJSON object type is not valid');
}
public static function badJsonString($str): self
{
return new self("The JSON string is not valid: {$str}");
}
}

View File

@@ -1,12 +1,20 @@
<?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\Doctrine\Model;
use Chill\MainBundle\Entity\User;
use DateTimeInterface;
interface TrackCreationInterface
{
public function setCreatedBy(User $user): self;
public function setCreatedAt(DateTimeInterface $datetime): self;
public function setCreatedAt(\DateTimeInterface $datetime): self;
public function setCreatedBy(User $user): self;
}

View File

@@ -1,12 +1,20 @@
<?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\Doctrine\Model;
use Chill\MainBundle\Entity\User;
use DateTimeInterface;
interface TrackUpdateInterface
{
public function setUpdatedBy(User $user): self;
public function setUpdatedAt(DateTimeInterface $datetime): self;
public function setUpdatedAt(\DateTimeInterface $datetime): self;
public function setUpdatedBy(User $user): self;
}

View File

@@ -1,5 +1,12 @@
<?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.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Doctrine\ORM\Hydration;
@@ -16,17 +23,6 @@ final class FlatHierarchyEntityHydrator extends ObjectHydrator
return array_values(iterator_to_array($this->flatListGenerator($this->buildChildrenHashmap(parent::hydrateAllData()))));
}
private function flatListGenerator(array $hashMap, ?object $parent = null): Generator
{
$parent = null === $parent ? null : spl_object_id($parent);
$hashMap += [$parent => []];
foreach ($hashMap[$parent] as $node) {
yield spl_object_id($node) => $node;
yield from $this->flatListGenerator($hashMap, $node);
}
}
private function buildChildrenHashmap(array $nodes): array
{
return array_reduce(
@@ -44,4 +40,15 @@ final class FlatHierarchyEntityHydrator extends ObjectHydrator
);
}
private function flatListGenerator(array $hashMap, ?object $parent = null): Generator
{
$parent = null === $parent ? null : spl_object_id($parent);
$hashMap += [$parent => []];
foreach ($hashMap[$parent] as $node) {
yield spl_object_id($node) => $node;
yield from $this->flatListGenerator($hashMap, $node);
}
}
}

View File

@@ -1,20 +1,68 @@
<?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\Doctrine\Type;
use Doctrine\DBAL\Types\DateIntervalType;
use DateInterval;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\DateIntervalType;
use Exception;
use function current;
use function preg_match;
use function reset;
/**
* Re-declare the date interval to use the implementation of date interval in
* postgreql
*
* @author Julien Fastré <julien.fastre@champs-libres.coop>
* postgreql.
*/
class NativeDateIntervalType extends DateIntervalType
{
const FORMAT = '%rP%YY%MM%DDT%HH%IM%SS';
public const FORMAT = '%rP%YY%MM%DDT%HH%IM%SS';
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}
if ($value instanceof DateInterval) {
return $value->format(self::FORMAT);
}
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if (null === $value || $value instanceof DateInterval) {
return $value;
}
try {
$strings = explode(' ', $value);
if (count($strings) === 0) {
return null;
}
$intervalSpec = 'P';
reset($strings);
do {
$intervalSpec .= $this->convertEntry($strings);
} while (next($strings) !== false);
return new DateInterval($intervalSpec);
} catch (Exception $exception) {
throw $this->createConversionException($value, $exception);
}
}
public function getName(): string
{
@@ -26,86 +74,52 @@ class NativeDateIntervalType extends DateIntervalType
return 'INTERVAL';
}
/**
* {@inheritdoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}
if ($value instanceof \DateInterval) {
return $value->format(self::FORMAT);
}
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'DateInterval']);
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === null || $value instanceof \DateInterval) {
return $value;
}
try {
$strings = explode(' ', $value);
if (count($strings) === 0) {
return null;
}
$intervalSpec = 'P';
\reset($strings);
do {
$intervalSpec .= $this->convertEntry($strings);
} while (next($strings) !== FALSE);
return new \DateInterval($intervalSpec);
} catch (\Exception $exception) {
throw $this->createConversionException($value, $exception);
}
}
private function convertEntry(&$strings)
{
$current = \current($strings);
if (is_numeric($current)) {
$next = \next($strings);
switch($next) {
case 'year':
case 'years':
$unit = 'Y';
break;
case 'mon':
case 'mons':
$unit = 'M';
break;
case 'day':
case 'days':
$unit = 'D';
break;
default:
throw $this->createConversionException(implode('', $strings));
}
return $current.$unit;
} elseif (\preg_match('/([0-9]{2}\:[0-9]{2}:[0-9]{2})/', $current) === 1) {
$tExploded = explode(':', $current);
$intervalSpec = 'T';
$intervalSpec.= $tExploded[0].'H';
$intervalSpec.= $tExploded[1].'M';
$intervalSpec.= $tExploded[2].'S';
return $intervalSpec;
}
}
protected function createConversionException($value, $exception = null)
{
return ConversionException::conversionFailedFormat($value, $this->getName(), 'xx year xx mons xx days 01:02:03', $exception);
}
private function convertEntry(&$strings)
{
$current = current($strings);
if (is_numeric($current)) {
$next = \next($strings);
switch ($next) {
case 'year':
case 'years':
$unit = 'Y';
break;
case 'mon':
case 'mons':
$unit = 'M';
break;
case 'day':
case 'days':
$unit = 'D';
break;
default:
throw $this->createConversionException(implode('', $strings));
}
return $current . $unit;
}
if (preg_match('/([0-9]{2}\:[0-9]{2}:[0-9]{2})/', $current) === 1) {
$tExploded = explode(':', $current);
$intervalSpec = 'T';
$intervalSpec .= $tExploded[0] . 'H';
$intervalSpec .= $tExploded[1] . 'M';
$intervalSpec .= $tExploded[2] . 'S';
return $intervalSpec;
}
}
}

View File

@@ -1,69 +1,76 @@
<?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.
*/
declare(strict_types=1);
namespace Chill\MainBundle\Doctrine\Type;
use Chill\MainBundle\Doctrine\Model\Point;
use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Chill\MainBundle\Doctrine\Model\PointException;
use Doctrine\DBAL\Types\Type;
/**
* A Type for Doctrine to implement the Geography Point type
* implemented by Postgis on postgis+postgresql databases
* implemented by Postgis on postgis+postgresql databases.
*/
class PointType extends Type
{
public const POINT = 'point';
/**
* @return string
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
public function canRequireSQLConversion()
{
return 'geometry(POINT,'.Point::$SRID.')';
return true;
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
if (null === $value) {
return null;
}
return $value->toWKT();
}
public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
{
return $sqlExpr;
}
/**
* @param mixed $value
*
* @return ?Point
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ($value === NULL){
return NULL;
if (null === $value) {
return null;
}
return Point::fromGeoJson($value);
}
public function convertToPHPValueSQL($sqlExpr, $platform)
{
return 'ST_AsGeoJSON(' . $sqlExpr . ') ';
}
public function getName()
{
return self::POINT;
}
public function convertToDatabaseValue($value, AbstractPlatform $platform)
/**
* @return string
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
{
if ($value === NULL){
return NULL;
}
return $value->toWKT();
}
public function canRequireSQLConversion()
{
return true;
}
public function convertToPHPValueSQL($sqlExpr, $platform)
{
return 'ST_AsGeoJSON('.$sqlExpr.') ';
}
public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform)
{
return $sqlExpr;
return 'geometry(POINT,' . Point::$SRID . ')';
}
}