-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from growthdev-repo/develop
Develop
- Loading branch information
Showing
11 changed files
with
383 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
Oops, something went wrong.