File tree Expand file tree Collapse file tree 15 files changed +458
-3
lines changed
Expand file tree Collapse file tree 15 files changed +458
-3
lines changed Original file line number Diff line number Diff line change 11/vendor /
2- .idea /
2+ /.idea /
3+ /coverage /
Original file line number Diff line number Diff line change 77Running Unit Tests
88
99* Run ` composer install `
10- * Now launch UTs running ` vendor/bin/phpunit Creational / `
10+ * Now launch UTs running ` vendor/bin/phpunit Tests / `
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PhpDesignPatterns \Structural \Composite ;
4+
5+ /**
6+ * Class Phone (Composite object).
7+ * @package PhpDesignPatterns\Structural\Composite
8+ */
9+ class Phone extends PhoneElement
10+ {
11+ /**
12+ * List of PhoneElement.
13+ *
14+ * @var array of PhoneElement
15+ */
16+ private $ elements ;
17+
18+ /**
19+ * Build a phone.
20+ *
21+ * @return string
22+ */
23+ public function build ()
24+ {
25+ $ phone = "" ;
26+ if (empty ($ this ->elements )) {
27+ return $ phone ;
28+ }
29+
30+ foreach ($ this ->elements as $ element )
31+ {
32+ $ phone .= $ element ->build ();
33+ }
34+ return $ phone ;
35+ }
36+
37+ /**
38+ * Add a PhoneElement child.
39+ *
40+ * @param PhoneElement $element
41+ * @return PhoneDisplay
42+ */
43+ public function add (PhoneElement $ element )
44+ {
45+ $ this ->elements [] = $ element ;
46+ return $ this ;
47+ }
48+
49+ /**
50+ * Removes a child.
51+ *
52+ * @param mixed $child_key
53+ * @return PhoneDisplay
54+ */
55+ public function remove ($ child_key )
56+ {
57+ if (!empty ($ this ->elements [$ child_key ])) {
58+ unset($ this ->elements [$ child_key ]);
59+ }
60+ return $ this ;
61+ }
62+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PhpDesignPatterns \Structural \Composite ;
4+
5+ /**
6+ * Class PhoneButton
7+ * @package PhpDesignPatterns\Structural\Composite
8+ */
9+ class PhoneButton extends PhoneElement
10+ {
11+ /**
12+ * Build a phone button.
13+ *
14+ * @return string
15+ */
16+ public function build ()
17+ {
18+ return "This is a phone button. " .PHP_EOL ;
19+ }
20+
21+ /**
22+ * Add a PhoneElement child.
23+ *
24+ * @param PhoneElement $element
25+ * @return PhoneButton
26+ */
27+ public function add (PhoneElement $ element )
28+ {
29+ return $ this ;
30+ }
31+
32+ /**
33+ * Removes a child.
34+ *
35+ * @param mixed $child_key
36+ * @return PhoneButton
37+ */
38+ public function remove ($ child_key )
39+ {
40+ return $ this ;
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PhpDesignPatterns \Structural \Composite ;
4+
5+ /**
6+ * Class PhoneDisplay
7+ * @package PhpDesignPatterns\Structural\Composite
8+ */
9+ class PhoneDisplay extends PhoneElement
10+ {
11+ /**
12+ * Build a phone display.
13+ *
14+ * @return string
15+ */
16+ public function build ()
17+ {
18+ return "This is a phone display. " .PHP_EOL ;
19+ }
20+
21+ /**
22+ * Add a PhoneElement child.
23+ *
24+ * @param PhoneElement $element
25+ * @return PhoneDisplay
26+ */
27+ public function add (PhoneElement $ element )
28+ {
29+ return $ this ;
30+ }
31+
32+ /**
33+ * Removes a child.
34+ *
35+ * @param mixed $child_key
36+ * @return PhoneDisplay
37+ */
38+ public function remove ($ child_key )
39+ {
40+ return $ this ;
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace PhpDesignPatterns \Structural \Composite ;
4+
5+ /**
6+ * Class PhoneElement
7+ * @package PhpDesignPatterns\Structural\Composite
8+ */
9+ abstract class PhoneElement
10+ {
11+ /**
12+ * Builds an element.
13+ * @return string
14+ */
15+ abstract public function build ();
16+
17+ /**
18+ * @param PhoneElement $element Element to add.
19+ * @return PhoneElement
20+ */
21+ abstract public function add (PhoneElement $ element );
22+
23+ /**
24+ * Remove a PhoneElement child.
25+ *
26+ * @param mixed $child_key
27+ * @return PhoneElement
28+ */
29+ abstract public function remove ($ child_key );
30+ }
Original file line number Diff line number Diff line change 1+ # Composite Design Pattern
2+
3+ ### Intent
4+
5+ “Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.”
6+
7+ Excerpt From: Erich Gamma. “Design Patterns: Elements of Reusable Object-Oriented Software.”
8+
You can’t perform that action at this time.
0 commit comments