File tree Expand file tree Collapse file tree 11 files changed +383
-1
lines changed
Behavioral/Strategy/Payment Expand file tree Collapse file tree 11 files changed +383
-1
lines changed Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder ;
6
+
7
+ class Burger implements BurgetInterface
8
+ {
9
+ use BurgerTrait;
10
+
11
+ public static function builder (): BurgerBuilderInterface
12
+ {
13
+ return new class implements BurgerBuilderInterface, BurgetInterface {
14
+ use BurgerTrait;
15
+
16
+ public function addBread (string $ bread ): self
17
+ {
18
+ $ this ->bread = $ bread ;
19
+
20
+ return $ this ;
21
+ }
22
+
23
+ public function addPatty (string $ patty ): self
24
+ {
25
+ $ this ->patty = $ patty ;
26
+
27
+ return $ this ;
28
+ }
29
+
30
+ public function addVeggies (string $ veggies ): self
31
+ {
32
+ $ this ->veggies = $ veggies ;
33
+
34
+ return $ this ;
35
+ }
36
+
37
+ public function addSauces (string $ sauces ): self
38
+ {
39
+ $ this ->sauces = $ sauces ;
40
+
41
+ return $ this ;
42
+ }
43
+
44
+ public function addWithExtraCheese (): self
45
+ {
46
+ $ this ->withExtraCheese = true ;
47
+
48
+ return $ this ;
49
+ }
50
+
51
+ public function build (): Burger
52
+ {
53
+ return new Burger ($ this );
54
+ }
55
+ };
56
+ }
57
+
58
+ public function __construct (BurgetInterface $ builder )
59
+ {
60
+ $ this ->bread = $ builder ->getBread ();
61
+ $ this ->patty = $ builder ->getPatty ();
62
+ $ this ->veggies = $ builder ->getVeggies ();
63
+ $ this ->sauces = $ builder ->getSauces ();
64
+ $ this ->withExtraCheese = $ builder ->getWithExtraCheese ();
65
+ }
66
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder \Burger ;
6
+
7
+ class Burger
8
+ {
9
+ private string $ bread ;
10
+ private string $ patty ;
11
+ private string $ veggies ;
12
+ private string $ sauces ;
13
+ private bool $ withExtraCheese = false ;
14
+
15
+ public function setBread (string $ bread ): void
16
+ {
17
+ $ this ->bread = $ bread ;
18
+ }
19
+
20
+ public function setPatty (string $ patty ): void
21
+ {
22
+ $ this ->patty = $ patty ;
23
+ }
24
+
25
+ public function setVeggies (string $ veggies ): void
26
+ {
27
+ $ this ->veggies = $ veggies ;
28
+ }
29
+
30
+ public function setSauces (string $ sauces ): void
31
+ {
32
+ $ this ->sauces = $ sauces ;
33
+ }
34
+
35
+ public function setWithExtraCheese (bool $ withExtraCheese ): void
36
+ {
37
+ $ this ->withExtraCheese = $ withExtraCheese ;
38
+ }
39
+
40
+ public function getBread (): string
41
+ {
42
+ return $ this ->bread ;
43
+ }
44
+
45
+ public function getPatty (): string
46
+ {
47
+ return $ this ->patty ;
48
+ }
49
+
50
+ public function getveggies (): string
51
+ {
52
+ return $ this ->veggies ;
53
+ }
54
+
55
+ public function getSauces (): string
56
+ {
57
+ return $ this ->sauces ;
58
+ }
59
+
60
+ public function getWithExtraCheese (): bool
61
+ {
62
+ return $ this ->withExtraCheese ;
63
+ }
64
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder \Burger ;
6
+
7
+ class BurgerBuilder implements BurgerBuilderInterface
8
+ {
9
+ private Burger $ burger ;
10
+
11
+ public function __construct ()
12
+ {
13
+ $ this ->burger = new Burger ();
14
+ }
15
+
16
+ public function addBread (string $ bread ): self
17
+ {
18
+ $ this ->burger ->setBread ($ bread );
19
+
20
+ return $ this ;
21
+ }
22
+
23
+ public function addPatty (string $ patty ): self
24
+ {
25
+ $ this ->burger ->setPatty ($ patty );
26
+
27
+ return $ this ;
28
+ }
29
+
30
+ public function addVeggies (string $ veggies ): self
31
+ {
32
+ $ this ->burger ->setVeggies ($ veggies );
33
+
34
+ return $ this ;
35
+ }
36
+
37
+ public function addSauces (string $ sauces ): self
38
+ {
39
+ $ this ->burger ->setSauces ($ sauces );
40
+
41
+ return $ this ;
42
+ }
43
+
44
+ public function addWithExtraCheese (): self
45
+ {
46
+ $ this ->burger ->setWithExtraCheese (true );
47
+
48
+ return $ this ;
49
+ }
50
+
51
+ public function build (): Burger
52
+ {
53
+ return $ this ->burger ;
54
+ }
55
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder \Burger ;
6
+
7
+ interface BurgerBuilderInterface
8
+ {
9
+ public function addBread (string $ bread ): self ;
10
+
11
+ public function addPatty (string $ patty ): self ;
12
+
13
+ public function addVeggies (string $ veggies ): self ;
14
+
15
+ public function addSauces (string $ saouces ): self ;
16
+
17
+ public function addWithExtraCheese (): self ;
18
+
19
+ public function build (): Burger ;
20
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder \Burger ;
6
+
7
+ final class BurgerDirector
8
+ {
9
+ public function buildBurger (BurgerBuilderInterface $ builder ): Burger
10
+ {
11
+ return $ builder ->build ();
12
+ }
13
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder ;
6
+
7
+ interface BurgerBuilderInterface
8
+ {
9
+ public function addBread (string $ bread ): self ;
10
+
11
+ public function addPatty (string $ patty ): self ;
12
+
13
+ public function addVeggies (string $ veggies ): self ;
14
+
15
+ public function addSauces (string $ saouces ): self ;
16
+
17
+ public function addWithExtraCheese (): self ;
18
+
19
+ public function build (): Burger ;
20
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder ;
6
+
7
+ trait BurgerTrait
8
+ {
9
+ private string $ bread = '' ;
10
+ private string $ patty = '' ;
11
+ private string $ veggies = '' ;
12
+ private string $ sauces = '' ;
13
+ private bool $ withExtraCheese = false ;
14
+
15
+ public function getBread (): string
16
+ {
17
+ return $ this ->bread ;
18
+ }
19
+
20
+ public function getPatty (): string
21
+ {
22
+ return $ this ->patty ;
23
+ }
24
+
25
+ public function getVeggies (): string
26
+ {
27
+ return $ this ->veggies ;
28
+ }
29
+
30
+ public function getSauces (): string
31
+ {
32
+ return $ this ->sauces ;
33
+ }
34
+
35
+ public function getWithExtraCheese (): bool
36
+ {
37
+ return $ this ->withExtraCheese ;
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Creational \Builder ;
6
+
7
+ interface BurgetInterface
8
+ {
9
+ public function getBread (): string ;
10
+
11
+ public function getPatty (): string ;
12
+
13
+ public function getVeggies (): string ;
14
+
15
+ public function getSauces (): string ;
16
+
17
+ public function getWithExtraCheese (): bool ;
18
+ }
Original file line number Diff line number Diff line change 7
7
use Growthdev \DesignPatterns \Behavioral \Strategy \Payment \PaymentProcessorWithoutStrategy ;
8
8
use PHPUnit \Framework \TestCase ;
9
9
10
- final class PaymentProcessorTest extends TestCase
10
+ final class PaymentProcessorWithoutStrategyTest extends TestCase
11
11
{
12
12
public function testCanProcessPaymentWithCashPaymentMethod (): void
13
13
{
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ declare (strict_types=1 );
4
+
5
+ namespace Growthdev \DesignPatterns \Tests \Creational \Builder ;
6
+
7
+ use Growthdev \DesignPatterns \Creational \Builder \Burger ;
8
+ use PHPUnit \Framework \TestCase ;
9
+
10
+ final class BuilderTest extends TestCase
11
+ {
12
+ public function testCanCreateBurger (): void
13
+ {
14
+ $ burger = Burger::builder ()
15
+ ->addBread ("Brown Bread " )
16
+ ->addPatty ("Beef " )
17
+ ->addVeggies ("Pickles " )
18
+ ->addSauces ("All " )
19
+ ->addWithExtraCheese ()
20
+ ->build ();
21
+
22
+ $ this ->assertEquals ("Brown Bread " , $ burger ->getBread ());
23
+ $ this ->assertEquals ("Beef " , $ burger ->getPatty ());
24
+ $ this ->assertEquals ("Pickles " , $ burger ->getVeggies ());
25
+ $ this ->assertEquals ("All " , $ burger ->getSauces ());
26
+ $ this ->assertTrue ($ burger ->getWithExtraCheese ());
27
+ }
28
+
29
+ public function testCanCreatePartialBurger (): void
30
+ {
31
+ $ burger = Burger::builder ()
32
+ ->addBread ("Brown Bread " )
33
+ ->addPatty ("Beef " )
34
+ ->addSauces ("All " )
35
+ ->build ();
36
+
37
+ $ this ->assertEquals ("Brown Bread " , $ burger ->getBread ());
38
+ $ this ->assertEquals ("Beef " , $ burger ->getPatty ());
39
+ $ this ->assertEquals ("All " , $ burger ->getSauces ());
40
+ }
41
+ }
You can’t perform that action at this time.
0 commit comments