Skip to content

Instantly share code, notes, and snippets.

@teklakct
teklakct / AssociatedEntityFilter.php
Last active July 8, 2024 08:11
Naive support for dot notation in filters
<?php
declare(strict_types=1);
namespace Acme\Filter;
use Acme\Form\Filter\Type\AssociatedEntityFilterType;
use Doctrine\ORM\QueryBuilder;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Filter\FilterInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto;
@teklakct
teklakct / PersistenDummySmsProvider.php
Last active March 15, 2021 07:54
Jak otestować wysyłanie SMS
<?php
// Jak lubisz klasyczne podejście (gdy nie chcesz mockować) to się przyda tylko do testów, jak jednak londyńską szkołę to po prostu mockuj `SmsProvider`
final class PersistenDummySmsProvider implements SmsProvider
{
private array $messages = [];
public function send(string $phoneNumber, string $message): void
{
$this->messages[$phone][] = $message;
<?php
declare(strict_types=1);
namespace My\Infrastructure\DoctrineAdapter;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\ORM\EntityManagerInterface;
use My\Domain\FinalGreetingRepository;
use My\Domain\Model\FinalGreeting;
@teklakct
teklakct / Clock.php
Last active November 1, 2018 19:31
Simple example why you should make an interface over DateTime
<?php
declare(strict_types=1);
interface Clock
{
public function now(): \DateTimeInterface;
}
@teklakct
teklakct / CommandValidator.php
Created September 26, 2017 10:50
Symfony callback validator
<?php
namespace Acme;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class CommandValidator
{
public static function validate($object, ExecutionContextInterface $context)
@teklakct
teklakct / exceptionHandler.php
Created June 1, 2017 10:46
GuzzleRequestExceptionHandler
<?php
private function exceptionHandler(GuzzleRequestException $e)
{
if ($e->hasResponse()) {
if ($e->getResponse()->getStatusCode() >= 500) {
throw $e;
}
$error = json_decode($e->getResponse()->getBody()->getContents(), true);
if (isset($error['errors'])) {
@teklakct
teklakct / MySubscriber.php
Created August 2, 2016 09:57
Subscriber for Sculpin
<?php
namespace Bundle\MyBundle;
use Sculpin\Core\Event\SourceSetEvent;
use Sculpin\Core\Sculpin;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MySubscriber implements EventSubscriberInterface
{
/**
@teklakct
teklakct / Person.php
Last active January 16, 2016 15:16
Przykładowa specyfikacja do opisania zachowania Named Constructors wykorzystując PHPSpec
<?php
class Person
{
private $name;
private $surname;
private function __construct($name, $surname)
{
$this->name = $name;
@teklakct
teklakct / StringsCompare.php
Created July 17, 2015 07:57
Strings comparision and check variable is set
<?php
class StringsCompare
{
public function compareStringsStrtoupper($method, $options)
{
if (strtoupper($method) == 'POST'
&& !isset($options['body'])
&& !isset($options['json'])
) {
<?php
namespace Acme\DemoBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeDemoBundle extends Bundle
{
}