forked from DesignPatternsPHP/DesignPatternsPHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'DesignPatternsPHP:main' into master
- Loading branch information
Showing
446 changed files
with
14,052 additions
and
4,792 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
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
4 changes: 3 additions & 1 deletion
4
Behavioral/ChainOfResponsibilities/Responsible/SlowDatabaseHandler.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
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
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,4 +1,6 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DesignPatterns\Behavioral\Command; | ||
|
||
|
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DesignPatterns\Behavioral\Command; | ||
|
||
|
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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
<?php declare(strict_types=1); | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DesignPatterns\Behavioral\Command; | ||
|
||
|
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DesignPatterns\Behavioral\Interpreter; | ||
|
||
abstract class AbstractExp | ||
{ | ||
abstract public function interpret(Context $context): 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DesignPatterns\Behavioral\Interpreter; | ||
|
||
/** | ||
* This NoTerminalExpression | ||
*/ | ||
class AndExp extends AbstractExp | ||
{ | ||
public function __construct(private AbstractExp $first, private AbstractExp $second) | ||
{ | ||
} | ||
|
||
public function interpret(Context $context): bool | ||
{ | ||
return $this->first->interpret($context) && $this->second->interpret($context); | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DesignPatterns\Behavioral\Interpreter; | ||
|
||
use Exception; | ||
|
||
class Context | ||
{ | ||
private array $poolVariable; | ||
|
||
public function lookUp(string $name): bool | ||
{ | ||
if (!key_exists($name, $this->poolVariable)) { | ||
throw new Exception("no exist variable: $name"); | ||
} | ||
|
||
return $this->poolVariable[$name]; | ||
} | ||
|
||
public function assign(VariableExp $variable, bool $val) | ||
{ | ||
$this->poolVariable[$variable->getName()] = $val; | ||
} | ||
} |
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 DesignPatterns\Behavioral\Interpreter; | ||
|
||
/** | ||
* This NoTerminalExpression | ||
*/ | ||
class OrExp extends AbstractExp | ||
{ | ||
public function __construct(private AbstractExp $first, private AbstractExp $second) | ||
{ | ||
} | ||
|
||
public function interpret(Context $context): bool | ||
{ | ||
return $this->first->interpret($context) || $this->second->interpret($context); | ||
} | ||
} |
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,71 @@ | ||
`Interpreter`__ | ||
=============== | ||
|
||
Purpose | ||
------- | ||
|
||
For a given language, it defines the representation of its grammar as | ||
"No Terminal Expression" and "Terminal Expression", | ||
as well as an interpreter for the sentences of that language. | ||
|
||
Examples | ||
-------- | ||
|
||
- An example of a binary logic interpreter, each definition is defined by its own class | ||
|
||
UML Diagram | ||
----------- | ||
|
||
.. image:: uml/uml.png | ||
:alt: Alt Interpreter UML Diagram | ||
:align: center | ||
|
||
Code | ||
---- | ||
|
||
You can also find this code on `GitHub`_ | ||
|
||
AbstractExp.php | ||
|
||
.. literalinclude:: AbstractExp.php | ||
:language: php | ||
:linenos: | ||
|
||
Context.php | ||
|
||
.. literalinclude:: Context.php | ||
:language: php | ||
:linenos: | ||
|
||
|
||
VariableExp.php | ||
|
||
.. literalinclude:: VariableExp.php | ||
:language: php | ||
:linenos: | ||
|
||
|
||
AndExp.php | ||
|
||
.. literalinclude:: AndExp.php | ||
:language: php | ||
:linenos: | ||
|
||
|
||
OrExp.php | ||
|
||
.. literalinclude:: OrExp.php | ||
:language: php | ||
:linenos: | ||
|
||
Test | ||
---- | ||
|
||
Tests/InterpreterTest.php | ||
|
||
.. literalinclude:: Tests/InterpreterTest.php | ||
:language: php | ||
:linenos: | ||
|
||
.. _`GitHub`: https://github.com/domnikl/DesignPatternsPHP/tree/main/Behavioral/Interpreter | ||
.. __: https://en.wikipedia.org/wiki/Interpreter_pattern |
Oops, something went wrong.