Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
webdevilopers / ContractController.php
Last active May 25, 2023 08:39
Inject Entity and Repository into Service with Symfony2
<?php
use Plusquam\Bundle\ContractBundle\Service\Invoice;
class ContractAdminController extends Controller
{
/**
* Invoice action
*
* @return Response
@webdevilopers
webdevilopers / ErrorResponseBuilder.php
Last active March 23, 2023 13:57
API friendly error handling with Symfony Messenger and Event Listener
<?php
namespace Acme\Common\Infrastructure\Symfony\Messenger;
use Prooph\EventStore\Exception\ConcurrencyException;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Exception\HandlerFailedException;
use Symfony\Component\Messenger\Exception\RuntimeException;
use Symfony\Component\Messenger\Exception\ValidationFailedException;
@webdevilopers
webdevilopers / SubjectInsideFunctionAdmin.php
Last active February 14, 2023 14:07
How to use form event listener in Sonata Admin
<?php
use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
class SubjectOutsideFunctionAdmin extends Admin
{
protected function configureFormFields(FormMapper $formMapper)
@webdevilopers
webdevilopers / developer.php
Created January 19, 2023 10:27
Early Return Developer vs. OpenAI ChatGTP vs. PHPStorm PHP Inspections
<?php declare(strict_types=1);
final class DocumentFormatValidator
{
private function validatePrefix(Filename $filename, DocumentFormat $format): void
{
$detectedPrefix = $this->detectPrefix($filename);
if ($detectedPrefix === null) {
if (!$format->requiresPrefix()) {
@webdevilopers
webdevilopers / CalculationController.php
Created March 27, 2016 17:26
How to use optional ParamConverter with null default in Route in Symfony Controller
<?php
namespace Sps\Bundle\CalculationBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
class CalculationController extends Controller
{
@webdevilopers
webdevilopers / CompartmentFilter.php
Created February 26, 2015 11:03
Doctrine ORM custom Aware Filter and Event in Symfony
<?php
namespace Plusquam\Bundle\ContractBundle\Filter;
use Doctrine\ORM\Query\Filter\SQLFilter;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Common\Annotations\Reader;
class CompartmentFilter extends SQLFilter
{
@webdevilopers
webdevilopers / DefaultController.php
Last active March 23, 2022 15:58
Sending JWT Token in Guzzle POST with LexikJWTAuthenticationBundle
<?php
namespace AppBundle\Controller;
class DefaultController extends Controller
{
/**
* @Route("/gettoken")
*/
public function getToken()
@webdevilopers
webdevilopers / DoctrineGutterColorRepository.php
Last active March 14, 2022 11:23
Using Value Objects as Custom Doctrine DBAL Type as Identifier in Symfony
<?php
namespace Sps\DormerCalculation\Infrastructure\Persistence\Doctrine;
use Doctrine\ORM\EntityRepository;
/**
* Class DoctrineGutterColorRepository
* @package Sps\DormerCalculation\Infrastructure\Persistence\Doctrine
*/
@webdevilopers
webdevilopers / PayloadMessage.php
Created February 2, 2022 13:55
Automatically populate PHP object from Array
<?php declare(strict_types=1);
namespace Acme\Shared\Application\Service;
trait PayloadMessage
{
private function __construct()
{}
/**
@webdevilopers
webdevilopers / index.txt
Last active January 4, 2022 07:40
Migrating from MySQL to PostgreSQL: Grouping and Ordering
Table structure:
test_id place_id
9b949b1a-414b-43f0-8940-9b821f1e2098 a3358c22-af08-485f-933e-f4d6ce900882
5177ab63-5ecd-4241-913e-36fb9d917efd a3358c22-af08-485f-933e-f4d6ce900882
c16ac09c-8ccf-45b9-8593-7ffceb4d5d9e a3358c22-af08-485f-933e-f4d6ce900882
MySQL: SELECT test_id, place_id FROM transmissions GROUP BY place_id
Result (CORRECT):