Skip to content

Develop #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Creational/Builder/Burger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder;

class Burger implements BurgetInterface
{
use BurgerTrait;

public static function builder(): BurgerBuilderInterface
{
return new class implements BurgerBuilderInterface, BurgetInterface {
use BurgerTrait;

public function addBread(string $bread): self
{
$this->bread = $bread;

return $this;
}

public function addPatty(string $patty): self
{
$this->patty = $patty;

return $this;
}

public function addVeggies(string $veggies): self
{
$this->veggies = $veggies;

return $this;
}

public function addSauces(string $sauces): self
{
$this->sauces = $sauces;

return $this;
}

public function addWithExtraCheese(): self
{
$this->withExtraCheese = true;

return $this;
}

public function build(): Burger
{
return new Burger($this);
}
};
}

public function __construct(BurgetInterface $builder)
{
$this->bread = $builder->getBread();
$this->patty = $builder->getPatty();
$this->veggies = $builder->getVeggies();
$this->sauces = $builder->getSauces();
$this->withExtraCheese = $builder->getWithExtraCheese();
}
}
64 changes: 64 additions & 0 deletions src/Creational/Builder/Burger/Burger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder\Burger;

class Burger
{
private string $bread;
private string $patty;
private string $veggies;
private string $sauces;
private bool $withExtraCheese = false;

public function setBread(string $bread): void
{
$this->bread = $bread;
}

public function setPatty(string $patty): void
{
$this->patty = $patty;
}

public function setVeggies(string $veggies): void
{
$this->veggies = $veggies;
}

public function setSauces(string $sauces): void
{
$this->sauces = $sauces;
}

public function setWithExtraCheese(bool $withExtraCheese): void
{
$this->withExtraCheese = $withExtraCheese;
}

public function getBread(): string
{
return $this->bread;
}

public function getPatty(): string
{
return $this->patty;
}

public function getveggies(): string
{
return $this->veggies;
}

public function getSauces(): string
{
return $this->sauces;
}

public function getWithExtraCheese(): bool
{
return $this->withExtraCheese;
}
}
55 changes: 55 additions & 0 deletions src/Creational/Builder/Burger/BurgerBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder\Burger;

class BurgerBuilder implements BurgerBuilderInterface
{
private Burger $burger;

public function __construct()
{
$this->burger = new Burger();
}

public function addBread(string $bread): self
{
$this->burger->setBread($bread);

return $this;
}

public function addPatty(string $patty): self
{
$this->burger->setPatty($patty);

return $this;
}

public function addVeggies(string $veggies): self
{
$this->burger->setVeggies($veggies);

return $this;
}

public function addSauces(string $sauces): self
{
$this->burger->setSauces($sauces);

return $this;
}

public function addWithExtraCheese(): self
{
$this->burger->setWithExtraCheese(true);

return $this;
}

public function build(): Burger
{
return $this->burger;
}
}
20 changes: 20 additions & 0 deletions src/Creational/Builder/Burger/BurgerBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder\Burger;

interface BurgerBuilderInterface
{
public function addBread(string $bread): self;

public function addPatty(string $patty): self;

public function addVeggies(string $veggies): self;

public function addSauces(string $saouces): self;

public function addWithExtraCheese(): self;

public function build(): Burger;
}
13 changes: 13 additions & 0 deletions src/Creational/Builder/Burger/BurgerDirector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder\Burger;

final class BurgerDirector
{
public function buildBurger(BurgerBuilderInterface $builder): Burger
{
return $builder->build();
}
}
20 changes: 20 additions & 0 deletions src/Creational/Builder/BurgerBuilderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder;

interface BurgerBuilderInterface
{
public function addBread(string $bread): self;

public function addPatty(string $patty): self;

public function addVeggies(string $veggies): self;

public function addSauces(string $saouces): self;

public function addWithExtraCheese(): self;

public function build(): Burger;
}
39 changes: 39 additions & 0 deletions src/Creational/Builder/BurgerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder;

trait BurgerTrait
{
private string $bread = '';
private string $patty = '';
private string $veggies = '';
private string $sauces = '';
private bool $withExtraCheese = false;

public function getBread(): string
{
return $this->bread;
}

public function getPatty(): string
{
return $this->patty;
}

public function getVeggies(): string
{
return $this->veggies;
}

public function getSauces(): string
{
return $this->sauces;
}

public function getWithExtraCheese(): bool
{
return $this->withExtraCheese;
}
}
18 changes: 18 additions & 0 deletions src/Creational/Builder/BurgetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Creational\Builder;

interface BurgetInterface
{
public function getBread(): string;

public function getPatty(): string;

public function getVeggies(): string;

public function getSauces(): string;

public function getWithExtraCheese(): bool;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Growthdev\DesignPatterns\Behavioral\Strategy\Payment\PaymentProcessorWithoutStrategy;
use PHPUnit\Framework\TestCase;

final class PaymentProcessorTest extends TestCase
final class PaymentProcessorWithoutStrategyTest extends TestCase
{
public function testCanProcessPaymentWithCashPaymentMethod(): void
{
Expand Down
41 changes: 41 additions & 0 deletions tests/Creational/Builder/BuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Growthdev\DesignPatterns\Tests\Creational\Builder;

use Growthdev\DesignPatterns\Creational\Builder\Burger;
use PHPUnit\Framework\TestCase;

final class BuilderTest extends TestCase
{
public function testCanCreateBurger(): void
{
$burger = Burger::builder()
->addBread("Brown Bread")
->addPatty("Beef")
->addVeggies("Pickles")
->addSauces("All")
->addWithExtraCheese()
->build();

$this->assertEquals("Brown Bread", $burger->getBread());
$this->assertEquals("Beef", $burger->getPatty());
$this->assertEquals("Pickles", $burger->getVeggies());
$this->assertEquals("All", $burger->getSauces());
$this->assertTrue($burger->getWithExtraCheese());
}

public function testCanCreatePartialBurger(): void
{
$burger = Burger::builder()
->addBread("Brown Bread")
->addPatty("Beef")
->addSauces("All")
->build();

$this->assertEquals("Brown Bread", $burger->getBread());
$this->assertEquals("Beef", $burger->getPatty());
$this->assertEquals("All", $burger->getSauces());
}
}
Loading