Skip to content

Instantly share code, notes, and snippets.

View anvodev's full-sized avatar

An Vo anvodev

View GitHub Profile
@anvodev
anvodev / CustomerIdGenerator.php
Created May 12, 2020 19:31
Determine how ID is generated
<?php
//src/Doctrine/CustomerIdGenerator.php
namespace App\Doctrine;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Id\AbstractIdGenerator;
class CustomerIdGenerator extends AbstractIdGenerator
@anvodev
anvodev / Customer.php
Last active May 12, 2020 19:35
Custom Id generator in Customer Entity
<?php
//src/Entity/Customer.php
//...
class Customer
{
/**
* @ORM\Id()
* @ORM\GeneratedValue("CUSTOM")
* @ORM\Column(type="integer")
@anvodev
anvodev / CustomerIdGeneratorTest.php
Created May 12, 2020 19:39
Test custom ID generator logic
<?php
//tests/Doctrine/CustomerIdGeneratorTest.php
namespace App\Tests\Doctrine;
use App\Entity\Customer;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class CustomerIdGeneratorTest extends KernelTestCase
@anvodev
anvodev / Consumer.php
Created June 4, 2020 00:05
Consumer need coffee to ready to work
<?php
//Consumer.php
class Consumer
{
private $coffee;
public function __construct(CoffeeInterface $coffee)
{
$this->coffee = $coffee;
<?php
//AtHomeCustomer.php
class AtHomeConsumer
{
public function readyToWork()
{
echo 'I am stay at home!' . PHP_EOL;
$egg = new Egg();
$coffee = new EggCoffee($egg);
<?php
//AtCoffeeShopConsumer.php
class AtCoffeeShopConsumer
{
private $coffee;
public function __construct(CoffeeInterface $coffee)
{
$this->coffee = $coffee;
<?php
//coffee_shop_context.php
//to load Class & Interface file when we need them
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
//at the coffee shop
$egg = new Egg();
<?php
//Milk.php
class Milk
{
public function taste()
{
echo 'It tastes milkyyy...' . PHP_EOL;
}
}
<?php
//MilkCoffee.php
class MilkCoffee implements CoffeeInterface
{
private $milk;
public function __construct(Milk $milk)
{
$this->milk = $milk;
<?php
//coffee_shop_another_day_context.php
//to load Class & Interface file when we need them
spl_autoload_register(function ($class_name) {
include $class_name . '.php';
});
//at the coffee shop in another day
$milk = new Milk();