Skip to content

Instantly share code, notes, and snippets.

View ker0x's full-sized avatar
🤘
Rock'n Code

Romain Monteil ker0x

🤘
Rock'n Code
View GitHub Profile
@ker0x
ker0x / procedure_distance.sql
Last active August 31, 2020 06:06
Procedure permettant de récupérer des entités situées dans un rayon de X mètres
CREATE DEFINER=`root`@`localhost`
PROCEDURE `getMediaNear`(IN `latitude` DOUBLE, IN `longitude` DOUBLE, IN `distance` FLOAT)
NO SQL
BEGIN
DECLARE `lon1` FLOAT;
DECLARE `lon2` FLOAT;
DECLARE `lat1` FLOAT;
DECLARE `lat2` FLOAT;
SET lon1 = longitude-distance/abs(cos(radians(latitude))*111);
@ker0x
ker0x / Collection.php
Last active February 20, 2018 17:22
Example of using Iterator, Countable, ArrayAccess, JsonSerializable and Serializable PHP interfaces
<?php
namespace Aperitips;
/**
* Class Collection.
*
* Example of using Iterator, Countable, ArrayAccess, JsonSerializable and Serializable interfaces
*
* @author Romain Monteil <[email protected]>
@ker0x
ker0x / .php_cs.dist
Created February 23, 2021 10:31
Laravel PHP-CS-Fixer configuration
<?php
$rules = [
'@PHP74Migration' => true,
'@PHP74Migration:risky' => true,
'@PhpCsFixer' => true,
'@PhpCsFixer:risky' => true,
'binary_operator_spaces' => [
'default' => 'single_space',
'operators' => ['=>' => null]
@ker0x
ker0x / EnhancedCollectionType.php
Last active September 4, 2024 18:53
Enhanced Symfony Form CollectionType with Stimulus
<?php
declare(strict_types=1);
namespace App\Form;
use App\Form\Util\StringUtil;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormInterface;
@ker0x
ker0x / AbstractEnhancedChoiceType.php
Last active July 26, 2021 21:10
Enhanced Symfony ChoiceType with Stimulus
<?php
declare(strict_types=1);
namespace App\Form\Field;
use App\Form\Util\StringUtil;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
@ker0x
ker0x / update_from_aggregate_sum_json.sql
Created September 7, 2022 10:24
Set a column value with an aggregate sum from a JSON column
WITH estimate_inter_durations AS (
SELECT (json_array_elements(estimate_inter.sequences)->>'duration')::float AS duration, estimate_inter.id
FROM estimate_inter
INNER JOIN estimate ON estimate.id = estimate_inter.id
WHERE estimate.id = estimate_inter.id
)
UPDATE estimate
SET duration_value = (
SELECT SUM(duration)
@ker0x
ker0x / find_by_role.php
Last active October 10, 2022 16:01
Symfony User findByRole
<?php
public function findByRole(Role $role): mixed
{
$rsm = $this->createResultSetMappingBuilder('u');
$query = <<<SQL
SELECT %s
FROM user u
WHERE u.roles::jsonb ?? :role
@ker0x
ker0x / CountryTypeExtension.php
Created May 5, 2023 07:58
A Symfony Form extension to display emoji country flag for CountryType
<?php
declare(strict_types=1);
namespace App\Form\Extension;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\ChoiceList\ChoiceList;
use Symfony\Component\Form\ChoiceList\Loader\IntlCallbackChoiceLoader;
use Symfony\Component\Form\Exception\LogicException;
@ker0x
ker0x / UniqueDto.php
Created July 16, 2023 22:16
Symfony UniqueDto Constraint
<?php
declare(strict_types=1);
namespace App\Shared\Infrastructure\Validator\Constraints;
use Symfony\Component\Validator\Attribute\HasNamedArguments;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
@ker0x
ker0x / find_select_with_relation.php
Created June 21, 2024 09:58
Doctrine SELECT with relation
<?php
private function findSelectWithRelation(): array
{
$queryBuilder = $this->createQueryBuilder('invoice')
->select('
invoice.id AS invoiceId,
SUBSTRING_INDEX(
GROUP_CONCAT(history.id ORDER BY history.date DESC SEPARATOR \'#\'),