Skip to content

Commit

Permalink
Merge branch 'DesignPatternsPHP:main' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
nggiahao1999 authored Aug 10, 2022
2 parents 3455df0 + 6add379 commit 7752554
Show file tree
Hide file tree
Showing 446 changed files with 14,052 additions and 4,792 deletions.
38 changes: 3 additions & 35 deletions .github/workflows/designpatternsphp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: shivammathur/setup-php@v1
with:
php-version: '7.4'
extensions: mbstring
coverage: xdebug

- uses: actions/checkout@v1
- uses: actions/checkout@v2

- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache composer dependencies
uses: actions/cache@v1
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Validate composer.json and composer.lock
run: composer validate

- name: Install dependencies
run: composer install --prefer-dist --no-progress --no-suggest --no-interaction

- name: phpunit
run: vendor/bin/phpunit

- name: phpcs
run: vendor/bin/phpcs .

- name: psalm
run: vendor/bin/psalm --show-info=false

- name: check readme refs
run: ./check-refs-readmes
- name: build the stack
run: docker-compose build
9 changes: 4 additions & 5 deletions Behavioral/ChainOfResponsibilities/Handler.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
<?php declare(strict_types=1);
<?php

declare(strict_types=1);

namespace DesignPatterns\Behavioral\ChainOfResponsibilities;

use Psr\Http\Message\RequestInterface;

abstract class Handler
{
private ?Handler $successor = null;

public function __construct(Handler $handler = null)
public function __construct(private ?Handler $successor = null)
{
$this->successor = $handler;
}

/**
Expand Down
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\ChainOfResponsibilities\Responsible;

Expand All @@ -7,13 +9,9 @@

class HttpInMemoryCacheHandler extends Handler
{
private array $data;

public function __construct(array $data, ?Handler $successor = null)
public function __construct(private array $data, ?Handler $successor = null)
{
parent::__construct($successor);

$this->data = $data;
}

protected function processing(RequestInterface $request): ?string
Expand Down
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\ChainOfResponsibilities\Responsible;

Expand Down
4 changes: 3 additions & 1 deletion Behavioral/ChainOfResponsibilities/Tests/ChainTest.php
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\ChainOfResponsibilities\Tests;

Expand Down
9 changes: 4 additions & 5 deletions Behavioral/Command/AddMessageDateCommand.php
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;

Expand All @@ -8,15 +10,12 @@
*/
class AddMessageDateCommand implements UndoableCommand
{
private Receiver $output;

/**
* Each concrete command is built with different receivers.
* There can be one, many or completely no receivers, but there can be other commands in the parameters.
*/
public function __construct(Receiver $console)
public function __construct(private Receiver $output)
{
$this->output = $console;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Behavioral/Command/Command.php
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;

Expand Down
9 changes: 4 additions & 5 deletions Behavioral/Command/HelloCommand.php
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;

Expand All @@ -8,15 +10,12 @@
*/
class HelloCommand implements Command
{
private Receiver $output;

/**
* Each concrete command is built with different receivers.
* There can be one, many or completely no receivers, but there can be other commands in the parameters
*/
public function __construct(Receiver $console)
public function __construct(private Receiver $output)
{
$this->output = $console;
}

/**
Expand Down
4 changes: 3 additions & 1 deletion Behavioral/Command/Invoker.php
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;

Expand Down
6 changes: 4 additions & 2 deletions Behavioral/Command/Receiver.php
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;

Expand All @@ -17,7 +19,7 @@ class Receiver
public function write(string $str)
{
if ($this->enableDate) {
$str .= ' ['.date('Y-m-d').']';
$str .= ' [' . date('Y-m-d') . ']';
}

$this->output[] = $str;
Expand Down
4 changes: 3 additions & 1 deletion Behavioral/Command/Tests/CommandTest.php
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\Tests;

Expand Down
8 changes: 5 additions & 3 deletions Behavioral/Command/Tests/UndoableCommandTest.php
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\Tests;

Expand All @@ -23,11 +25,11 @@ public function testInvocation()
$messageDateCommand->execute();

$invoker->run();
$this->assertSame("Hello World\nHello World [".date('Y-m-d').']', $receiver->getOutput());
$this->assertSame("Hello World\nHello World [" . date('Y-m-d') . ']', $receiver->getOutput());

$messageDateCommand->undo();

$invoker->run();
$this->assertSame("Hello World\nHello World [".date('Y-m-d')."]\nHello World", $receiver->getOutput());
$this->assertSame("Hello World\nHello World [" . date('Y-m-d') . "]\nHello World", $receiver->getOutput());
}
}
4 changes: 3 additions & 1 deletion Behavioral/Command/UndoableCommand.php
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;

Expand Down
10 changes: 10 additions & 0 deletions Behavioral/Interpreter/AbstractExp.php
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;
}
20 changes: 20 additions & 0 deletions Behavioral/Interpreter/AndExp.php
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);
}
}
26 changes: 26 additions & 0 deletions Behavioral/Interpreter/Context.php
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;
}
}
20 changes: 20 additions & 0 deletions Behavioral/Interpreter/OrExp.php
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);
}
}
71 changes: 71 additions & 0 deletions Behavioral/Interpreter/README.rst
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
Loading

0 comments on commit 7752554

Please sign in to comment.