Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -1 +1 @@
C:37:"PHPUnit\Runner\DefaultTestResultCache":263:{a:2:{s:7:"defects";a:0:{}s:5:"times";a:3:{s:59:"Behavioral\Observer\Tests\ObserverTest::testFootballSubject";d:0.001;s:54:"Behavioral\Observer\Tests\ObserverTest::testTeamAction";d:0.001;s:58:"Behavioral\Observer\Tests\ObserverTest::testDetachObserver";d:0.001;}}}
{"version":1,"defects":{"Behavioral\\Observer\\Tests\\ObserverTest::testTeamAction":3},"times":{"Behavioral\\Observer\\Tests\\ObserverTest::testFootballSubject":0.001,"Behavioral\\Observer\\Tests\\ObserverTest::testTeamAction":0.005,"Behavioral\\Observer\\Tests\\ObserverTest::testDetachObserver":0.001}}
16 changes: 0 additions & 16 deletions .travis.yml

This file was deleted.

3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[![Build Status](https://travis-ci.org/Jagepard/PhpDesignPatterns-Observer.svg?branch=master)](https://travis-ci.org/Jagepard/PhpDesignPatterns-Observer)
[![codecov](https://codecov.io/gh/Jagepard/PhpDesignPatterns-Observer/branch/master/graph/badge.svg)](https://codecov.io/gh/Jagepard/PhpDesignPatterns-Observer)

[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/Jagepard/PhpDesignPatterns-Observer/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/Jagepard/PhpDesignPatterns-Observer/?branch=master)
[![Code Climate](https://codeclimate.com/github/Jagepard/PhpDesignPatterns-Observer/badges/gpa.svg)](https://codeclimate.com/github/Jagepard/PhpDesignPatterns-Observer)
[![License: MIT](https://img.shields.io/badge/license-MIT-498e7f.svg)](https://mit-license.org/)
Expand Down
86 changes: 47 additions & 39 deletions api.md
Original file line number Diff line number Diff line change
@@ -1,71 +1,79 @@
## Table of contents
- [Behavioral\Observer\EventInterface](#behavioral_observer_eventinterface)
- [Behavioral\Observer\FootballEvent](#behavioral_observer_footballevent)
- [Behavioral\Observer\FootballObserver](#behavioral_observer_footballobserver)
- [Behavioral\Observer\FootballSubject](#behavioral_observer_footballsubject)
- [Behavioral\Observer\NameTrait](#behavioral_observer_nametrait)
- [Behavioral\Observer\ObserverInterface](#behavioral_observer_observerinterface)
- [Behavioral\Observer\SubjectInterface](#behavioral_observer_subjectinterface)
<hr>

<a id="behavioral_observer_eventinterface"></a>

### Class: Behavioral\Observer\EventInterface
| Visibility | Function |
|:-----------|:---------|
|abstract public|<em><strong>getName</strong>(): string</em><br>|

- [\Behavioral\Observer\EventInterface (interface)](#interface-behavioralobservereventinterface)
- [\Behavioral\Observer\FootballEvent](#class-behavioralobserverfootballevent)
- [\Behavioral\Observer\FootballObserver](#class-behavioralobserverfootballobserver)
- [\Behavioral\Observer\FootballSubject](#class-behavioralobserverfootballsubject)
- [\Behavioral\Observer\ObserverInterface (interface)](#interface-behavioralobserverobserverinterface)
- [\Behavioral\Observer\SubjectInterface (interface)](#interface-behavioralobserversubjectinterface)

<hr />

### Interface: \Behavioral\Observer\EventInterface
<a id="behavioral_observer_footballevent"></a>

### Class: Behavioral\Observer\FootballEvent
##### implements [Behavioral\Observer\EventInterface](#behavioral_observer_eventinterface)
| Visibility | Function |
|:-----------|:---------|
| public | <strong>getName()</strong> : <em>mixed</em> |
|public|<em><strong>__construct</strong>( string $name )</em><br>Sets the name<br>Устанавливает наименование|
|public|<em><strong>getName</strong>(): string</em><br>Gets a name<br>Получает наименование|

<hr />

### Class: \Behavioral\Observer\FootballEvent
<a id="behavioral_observer_footballobserver"></a>

### Class: Behavioral\Observer\FootballObserver
##### implements [Behavioral\Observer\ObserverInterface](#behavioral_observer_observerinterface)
| Visibility | Function |
|:-----------|:---------|
| public | <strong>__construct(</strong><em>\string</em> <strong>$name</strong>)</strong> : <em>void</em> |
| public | <strong>getName()</strong> : <em>mixed</em> |
|public|<em><strong>__construct</strong>( string $name )</em><br>Sets the name<br>Устанавливает наименование|
|public|<em><strong>getName</strong>(): string</em><br>Gets a name<br>Получает наименование|

*This class implements [\Behavioral\Observer\EventInterface](#interface-behavioralobservereventinterface)*

<hr />

### Class: \Behavioral\Observer\FootballObserver
<a id="behavioral_observer_footballsubject"></a>

### Class: Behavioral\Observer\FootballSubject
##### implements [Behavioral\Observer\SubjectInterface](#behavioral_observer_subjectinterface)
| Visibility | Function |
|:-----------|:---------|
| public | <strong>__construct(</strong><em>\string</em> <strong>$name</strong>)</strong> : <em>void</em> |
| public | <strong>getName()</strong> : <em>mixed</em> |

*This class implements [\Behavioral\Observer\ObserverInterface](#interface-behavioralobserverobserverinterface)*
|public|<em><strong>attachObserver</strong>( Behavioral\Observer\ObserverInterface $observer ): void</em><br>Adds an observer<br>Добавляет наблюдателя|
|public|<em><strong>detachObserver</strong>( string $name ): void</em><br>Removes an observer<br>Убирает наблюдателя|
|public|<em><strong>notifyObservers</strong>( Behavioral\Observer\EventInterface $event ): void</em><br>Notifies all observers of an event<br>Уведомляет всех наблюдателей о событии|
|public|<em><strong>__construct</strong>( string $name )</em><br>Sets the name<br>Устанавливает наименование|
|public|<em><strong>getName</strong>(): string</em><br>Gets a name<br>Получает наименование|

<hr />

### Class: \Behavioral\Observer\FootballSubject
<a id="behavioral_observer_nametrait"></a>

### Class: Behavioral\Observer\NameTrait
| Visibility | Function |
|:-----------|:---------|
| public | <strong>__construct(</strong><em>\string</em> <strong>$name</strong>)</strong> : <em>void</em> |
| public | <strong>attachObserver(</strong><em>[\Behavioral\Observer\ObserverInterface](#interface-behavioralobserverobserverinterface)</em> <strong>$observer</strong>)</strong> : <em>void</em> |
| public | <strong>detachObserver(</strong><em>\string</em> <strong>$name</strong>)</strong> : <em>void</em> |
| public | <strong>getName()</strong> : <em>mixed</em> |
| public | <strong>notifyObservers(</strong><em>[\Behavioral\Observer\EventInterface](#interface-behavioralobservereventinterface)</em> <strong>$event</strong>)</strong> : <em>void</em> |

*This class implements [\Behavioral\Observer\SubjectInterface](#interface-behavioralobserversubjectinterface)*
|public|<em><strong>__construct</strong>( string $name )</em><br>Sets the name<br>Устанавливает наименование|
|public|<em><strong>getName</strong>(): string</em><br>Gets a name<br>Получает наименование|

<hr />

### Interface: \Behavioral\Observer\ObserverInterface
<a id="behavioral_observer_observerinterface"></a>

### Class: Behavioral\Observer\ObserverInterface
| Visibility | Function |
|:-----------|:---------|
| public | <strong>getName()</strong> : <em>mixed</em> |
|abstract public|<em><strong>getName</strong>(): string</em><br>|

<hr />

### Interface: \Behavioral\Observer\SubjectInterface
<a id="behavioral_observer_subjectinterface"></a>

### Class: Behavioral\Observer\SubjectInterface
| Visibility | Function |
|:-----------|:---------|
| public | <strong>attachObserver(</strong><em>[\Behavioral\Observer\ObserverInterface](#interface-behavioralobserverobserverinterface)</em> <strong>$observer</strong>)</strong> : <em>void</em> |
| public | <strong>detachObserver(</strong><em>\string</em> <strong>$observerName</strong>)</strong> : <em>void</em> |
| public | <strong>notifyObservers(</strong><em>[\Behavioral\Observer\EventInterface](#interface-behavioralobservereventinterface)</em> <strong>$event</strong>)</strong> : <em>void</em> |
|abstract public|<em><strong>attachObserver</strong>( Behavioral\Observer\ObserverInterface $observer ): void</em><br>|
|abstract public|<em><strong>detachObserver</strong>( string $observerName ): void</em><br>|
|abstract public|<em><strong>notifyObservers</strong>( Behavioral\Observer\EventInterface $event ): void</em><br>|
<hr>

###### created with [Rudra-Documentation-Collector](#https://github.com/Jagepard/Rudra-Documentation-Collector)
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@
"skype": "jagepard.ru"
},
"require": {
"php": ">=7.4"
},
"require-dev" : {
"phpunit/phpunit": "^7"
"php": ">=7.4",
"rudra/docs": "dev-master"
},
"autoload": {
"psr-4": {
Expand Down
3 changes: 3 additions & 0 deletions src/EventInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@

interface EventInterface
{
/**
* @return string
*/
public function getName(): string;
}
26 changes: 26 additions & 0 deletions src/FootballSubject.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class FootballSubject implements SubjectInterface

private array $observers = [];

/**
* Adds an observer
* ---------------------
* Добавляет наблюдателя
*
* @param ObserverInterface $observer
* @return void
*/
public function attachObserver(ObserverInterface $observer): void
{
if (array_key_exists($observer->getName(), $this->observers)) {
Expand All @@ -24,6 +32,14 @@ public function attachObserver(ObserverInterface $observer): void
$this->observers[$observer->getName()] = $observer;
}

/**
* Removes an observer
* -------------------
* Убирает наблюдателя
*
* @param string $name
* @return void
*/
public function detachObserver(string $name): void
{
if (!array_key_exists($name, $this->observers)) {
Expand All @@ -33,6 +49,14 @@ public function detachObserver(string $name): void
unset($this->observers[$name]);
}

/**
* Notifies all observers of an event
* --------------------------------------
* Уведомляет всех наблюдателей о событии
*
* @param EventInterface $event
* @return void
*/
public function notifyObservers(EventInterface $event): void
{
foreach ($this->observers as $observer) {
Expand All @@ -43,5 +67,7 @@ public function notifyObservers(EventInterface $event): void
$event->getName()
);
}

print("\n");
}
}
14 changes: 14 additions & 0 deletions src/NameTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,25 @@ trait NameTrait
{
private string $name;

/**
* Sets the name
* --------------------------
* Устанавливает наименование
*
* @param string $name
*/
public function __construct(string $name)
{
$this->name = $name;
}

/**
* Gets a name
* ---------------------
* Получает наименование
*
* @return string
*/
public function getName(): string
{
return $this->name;
Expand Down
3 changes: 3 additions & 0 deletions src/ObserverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@

interface ObserverInterface
{
/**
* @return string
*/
public function getName(): string;
}
14 changes: 14 additions & 0 deletions src/SubjectInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,21 @@

interface SubjectInterface
{
/**
* @param ObserverInterface $observer
* @return void
*/
public function attachObserver(ObserverInterface $observer): void;

/**
* @param string $observerName
* @return void
*/
public function detachObserver(string $observerName): void;

/**
* @param EventInterface $event
* @return void
*/
public function notifyObservers(EventInterface $event): void;
}
8 changes: 4 additions & 4 deletions tests/ObserverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testTeamAction(): void
$goal = ob_get_clean();

$this->assertEquals(
"John has get information about: Manchester United Goal!!! \nBill has get information about: Manchester United Goal!!! \n",
"John has get information about: Manchester United Goal!!! \nBill has get information about: Manchester United Goal!!! \n\n",
$goal
);

Expand All @@ -46,7 +46,7 @@ public function testTeamAction(): void
$miss = ob_get_clean();

$this->assertEquals(
"John has get information about: Manchester United missing a ball((( \nBill has get information about: Manchester United missing a ball((( \n",
"John has get information about: Manchester United missing a ball((( \nBill has get information about: Manchester United missing a ball((( \n\n",
$miss
);

Expand All @@ -55,7 +55,7 @@ public function testTeamAction(): void
$violation = ob_get_clean();

$this->assertEquals(
"John has get information about: Manchester United getting a yellow card \nBill has get information about: Manchester United getting a yellow card \n",
"John has get information about: Manchester United getting a yellow card \nBill has get information about: Manchester United getting a yellow card \n\n",
$violation
);

Expand All @@ -64,7 +64,7 @@ public function testTeamAction(): void
$random = ob_get_clean();

$this->assertEquals(
"John has get information about: Manchester United random \nBill has get information about: Manchester United random \n",
"John has get information about: Manchester United random \nBill has get information about: Manchester United random \n\n",
$random
);
}
Expand Down