-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
674 additions
and
611 deletions.
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
/.idea | ||
.php-cs-fixer.cache |
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,21 @@ | ||
{ | ||
"name": "s-damian/solid-php", | ||
"description": "SOLID - Examples with PHP 8", | ||
"type": "library", | ||
"keywords": ["php", "solid"], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Stephen Damian", | ||
"email": "contact@damian-freelance.fr", | ||
"homepage": "https://github.com/s-damian" | ||
} | ||
], | ||
"require": { | ||
"php": " ^8.1" | ||
}, | ||
"scripts": { | ||
"fix-all": "./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=tools/php-cs-fixer/.php-cs-fixer.dist.php", | ||
"fix-all-dry": "./tools/php-cs-fixer/vendor/bin/php-cs-fixer fix --config=tools/php-cs-fixer/.php-cs-fixer.dist.php -vv --dry-run --diff" | ||
} | ||
} |
72 changes: 36 additions & 36 deletions
72
src/1_single-responsibility-principle/Article/Article.php
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 |
---|---|---|
@@ -1,36 +1,36 @@ | ||
<?php | ||
|
||
namespace Article; | ||
|
||
class Article | ||
{ | ||
public function getTitle(): string | ||
{ | ||
return 'Article Title'; | ||
} | ||
|
||
public function getDescription(): string | ||
{ | ||
return 'Article Description'; | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return '<p>Article Content</p>'; | ||
} | ||
|
||
public function getSlug(): string | ||
{ | ||
return 'article-slug'; | ||
} | ||
|
||
public function getAll(): array | ||
{ | ||
return [ | ||
'title' => $this->getTitle(), | ||
'description' => $this->getDescription(), | ||
'content' => $this->getContent(), | ||
'slug' => $this->getSlug(), | ||
]; | ||
} | ||
} | ||
<?php | ||
|
||
namespace Article; | ||
|
||
class Article | ||
{ | ||
public function getTitle(): string | ||
{ | ||
return 'Article Title'; | ||
} | ||
|
||
public function getDescription(): string | ||
{ | ||
return 'Article Description'; | ||
} | ||
|
||
public function getContent(): string | ||
{ | ||
return '<p>Article Content</p>'; | ||
} | ||
|
||
public function getSlug(): string | ||
{ | ||
return 'article-slug'; | ||
} | ||
|
||
public function getAll(): array | ||
{ | ||
return [ | ||
'title' => $this->getTitle(), | ||
'description' => $this->getDescription(), | ||
'content' => $this->getContent(), | ||
'slug' => $this->getSlug(), | ||
]; | ||
} | ||
} |
20 changes: 10 additions & 10 deletions
20
src/1_single-responsibility-principle/Article/Formatters/FormatterInterface.php
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<?php | ||
|
||
namespace Article\Formatters; | ||
|
||
use Article\Article; | ||
|
||
interface FormatterInterface | ||
{ | ||
public function format(Article $article): string; | ||
} | ||
<?php | ||
|
||
namespace Article\Formatters; | ||
|
||
use Article\Article; | ||
|
||
interface FormatterInterface | ||
{ | ||
public function format(Article $article): string; | ||
} |
26 changes: 13 additions & 13 deletions
26
src/1_single-responsibility-principle/Article/Formatters/JsonFormatter.php
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?php | ||
|
||
namespace Article\Formatters; | ||
|
||
use Article\Article; | ||
|
||
class JsonFormatter implements FormatterInterface | ||
{ | ||
public function format(Article $article): string | ||
{ | ||
return json_encode($article->getAll()); | ||
} | ||
} | ||
<?php | ||
|
||
namespace Article\Formatters; | ||
|
||
use Article\Article; | ||
|
||
class JsonFormatter implements FormatterInterface | ||
{ | ||
public function format(Article $article): string | ||
{ | ||
return json_encode($article->getAll()); | ||
} | ||
} |
26 changes: 13 additions & 13 deletions
26
src/1_single-responsibility-principle/Article/Formatters/XmlFormatter.php
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?php | ||
|
||
namespace Article\Formatters; | ||
|
||
use Article\Article; | ||
|
||
class XmlFormatter implements FormatterInterface | ||
{ | ||
public function format(Article $article): string | ||
{ | ||
return xmlrpc_encode($article->getAll()); | ||
} | ||
} | ||
<?php | ||
|
||
namespace Article\Formatters; | ||
|
||
use Article\Article; | ||
|
||
class XmlFormatter implements FormatterInterface | ||
{ | ||
public function format(Article $article): string | ||
{ | ||
return xmlrpc_encode($article->getAll()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,40 +1,42 @@ | ||
<?php | ||
|
||
/** | ||
* Single Responsibility Principle. | ||
*/ | ||
|
||
require_once 'Article/Article.php'; | ||
require_once 'Article/Formatters/FormatterInterface.php'; | ||
require_once 'Article/Formatters/JsonFormatter.php'; | ||
require_once 'Article/Formatters/XmlFormatter.php'; | ||
|
||
use Article\Article; | ||
use Article\Formatters\JsonFormatter; | ||
use Article\Formatters\XmlFormatter; | ||
|
||
/** | ||
* In this example, the "\Article\Formatters\JsonFormatter" and "\Article\Formatters\XmlFormatter" Formatters implement | ||
* the "\Article\Formatters\FormatterInterface" interface. | ||
* This is useful to ensure that the "format" method is present in all "Formatters". | ||
* | ||
* The "format" methods of Formatters expect an instance of "\Article\Article" as a parameter, | ||
* because these Formatters must return with the "format" method in response the data of an "\Article\Article" in their formats. | ||
* | ||
* In this example, we see that each class has only one responsibility. | ||
*/ | ||
|
||
|
||
$article = new Article(); | ||
|
||
|
||
$jsonFormatter = new JsonFormatter(); | ||
|
||
// return string - Returns the content of the article in JSON format. | ||
echo '<pre>'; var_dump($jsonFormatter->format($article)); | ||
|
||
|
||
$xmlFormatter = new XmlFormatter(); | ||
|
||
// return string - Returns the content of the article in XML format. | ||
echo '<pre>'; var_dump($xmlFormatter->format($article)); | ||
<?php | ||
|
||
/** | ||
* Single Responsibility Principle. | ||
*/ | ||
|
||
require_once 'Article/Article.php'; | ||
require_once 'Article/Formatters/FormatterInterface.php'; | ||
require_once 'Article/Formatters/JsonFormatter.php'; | ||
require_once 'Article/Formatters/XmlFormatter.php'; | ||
|
||
use Article\Article; | ||
use Article\Formatters\JsonFormatter; | ||
use Article\Formatters\XmlFormatter; | ||
|
||
/** | ||
* In this example, the "\Article\Formatters\JsonFormatter" and "\Article\Formatters\XmlFormatter" Formatters implement | ||
* the "\Article\Formatters\FormatterInterface" interface. | ||
* This is useful to ensure that the "format" method is present in all "Formatters". | ||
* | ||
* The "format" methods of Formatters expect an instance of "\Article\Article" as a parameter, | ||
* because these Formatters must return with the "format" method in response the data of an "\Article\Article" in their formats. | ||
* | ||
* In this example, we see that each class has only one responsibility. | ||
*/ | ||
|
||
|
||
$article = new Article(); | ||
|
||
|
||
$jsonFormatter = new JsonFormatter(); | ||
|
||
// return string - Returns the content of the article in JSON format. | ||
echo '<pre>'; | ||
var_dump($jsonFormatter->format($article)); | ||
|
||
|
||
$xmlFormatter = new XmlFormatter(); | ||
|
||
// return string - Returns the content of the article in XML format. | ||
echo '<pre>'; | ||
var_dump($xmlFormatter->format($article)); |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
<?php | ||
|
||
namespace Sport; | ||
|
||
use Sport\Sports\SportInterface; | ||
|
||
class Sport | ||
{ | ||
public function rules(SportInterface $sport): string | ||
{ | ||
return 'The '.$sport->name().' rule is: '.$sport->rules(); | ||
} | ||
} | ||
<?php | ||
|
||
namespace Sport; | ||
|
||
use Sport\Sports\SportInterface; | ||
|
||
class Sport | ||
{ | ||
public function rules(SportInterface $sport): string | ||
{ | ||
return 'The '.$sport->name().' rule is: '.$sport->rules(); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
<?php | ||
|
||
namespace Sport\Sports; | ||
|
||
final class Basketball implements SportInterface | ||
{ | ||
public function name(): string | ||
{ | ||
return 'Basketball'; | ||
} | ||
|
||
public function rules(): string | ||
{ | ||
return 'The rules...'; | ||
} | ||
} | ||
<?php | ||
|
||
namespace Sport\Sports; | ||
|
||
final class Basketball implements SportInterface | ||
{ | ||
public function name(): string | ||
{ | ||
return 'Basketball'; | ||
} | ||
|
||
public function rules(): string | ||
{ | ||
return 'The rules...'; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
<?php | ||
|
||
namespace Sport\Sports; | ||
|
||
final class Football implements SportInterface | ||
{ | ||
public function name(): string | ||
{ | ||
return 'Football'; | ||
} | ||
|
||
public function rules(): string | ||
{ | ||
return 'The rules...'; | ||
} | ||
} | ||
<?php | ||
|
||
namespace Sport\Sports; | ||
|
||
final class Football implements SportInterface | ||
{ | ||
public function name(): string | ||
{ | ||
return 'Football'; | ||
} | ||
|
||
public function rules(): string | ||
{ | ||
return 'The rules...'; | ||
} | ||
} |
20 changes: 10 additions & 10 deletions
20
src/2_open-closed-principle/Sport/Sports/SportInterface.php
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 |
---|---|---|
@@ -1,10 +1,10 @@ | ||
<?php | ||
|
||
namespace Sport\Sports; | ||
|
||
interface SportInterface | ||
{ | ||
public function name(): string; | ||
|
||
public function rules(): string; | ||
} | ||
<?php | ||
|
||
namespace Sport\Sports; | ||
|
||
interface SportInterface | ||
{ | ||
public function name(): string; | ||
|
||
public function rules(): string; | ||
} |
Oops, something went wrong.