Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c98bc83

Browse files
author
Dominik Liebler
committed
Merge commit 'refs/pull/origin/86'
2 parents 06b2af4 + bebb526 commit c98bc83

File tree

217 files changed

+1142
-679
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

217 files changed

+1142
-679
lines changed

ChainOfResponsibilities/Handler.php renamed to Behavioral/ChainOfResponsibilities/Handler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\ChainOfResponsibilities;
3+
namespace DesignPatterns\Behavioral\ChainOfResponsibilities;
44

55
/**
66
* Handler is a generic handler in the chain of responsibilities

ChainOfResponsibilities/Request.php renamed to Behavioral/ChainOfResponsibilities/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\ChainOfResponsibilities;
3+
namespace DesignPatterns\Behavioral\ChainOfResponsibilities;
44

55
/**
66
* Request is a request which goes through the chain of responsibilities.

ChainOfResponsibilities/Responsible/FastStorage.php renamed to Behavioral/ChainOfResponsibilities/Responsible/FastStorage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace DesignPatterns\ChainOfResponsibilities\Responsible;
3+
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
44

5-
use DesignPatterns\ChainOfResponsibilities\Handler;
6-
use DesignPatterns\ChainOfResponsibilities\Request;
5+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
6+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Request;
77

88
/**
99
* Class FastStorage

ChainOfResponsibilities/Responsible/SlowStorage.php renamed to Behavioral/ChainOfResponsibilities/Responsible/SlowStorage.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace DesignPatterns\ChainOfResponsibilities\Responsible;
3+
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
44

5-
use DesignPatterns\ChainOfResponsibilities\Handler;
6-
use DesignPatterns\ChainOfResponsibilities\Request;
5+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
6+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Request;
77

88
/**
99
* This is mostly the same code as FastStorage but in fact, it may greatly differs

Tests/ChainOfResponsibilities/ChainTest.php renamed to Behavioral/ChainOfResponsibilities/Tests/ChainTest.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
<?php
22

3-
namespace DesignPatterns\Tests\ChainOfResponsibilities;
3+
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Tests;
44

5-
use DesignPatterns\ChainOfResponsibilities\Request;
6-
use DesignPatterns\ChainOfResponsibilities\Responsible;
5+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Request;
6+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage;
7+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage;
8+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
79

810
/**
911
* ChainTest tests the CoR
1012
*/
1113
class ChainTest extends \PHPUnit_Framework_TestCase
1214
{
1315

16+
/**
17+
* @var FastStorage
18+
*/
1419
protected $chain;
1520

1621
protected function setUp()
1722
{
18-
$this->chain = new Responsible\FastStorage(array('bar' => 'baz'));
19-
$this->chain->append(new Responsible\SlowStorage(array('bar' => 'baz', 'foo' => 'bar')));
23+
$this->chain = new FastStorage(array('bar' => 'baz'));
24+
$this->chain->append(new SlowStorage(array('bar' => 'baz', 'foo' => 'bar')));
2025
}
2126

2227
public function makeRequest()
@@ -40,7 +45,8 @@ public function testFastStorage($request)
4045
$this->assertObjectHasAttribute('response', $request);
4146
$this->assertEquals('baz', $request->response);
4247
// despite both handle owns the 'bar' key, the FastStorage is responding first
43-
$this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\FastStorage', $request->forDebugOnly);
48+
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\FastStorage';
49+
$this->assertEquals($className, $request->forDebugOnly);
4450
}
4551

4652
/**
@@ -55,7 +61,8 @@ public function testSlowStorage($request)
5561
$this->assertObjectHasAttribute('response', $request);
5662
$this->assertEquals('bar', $request->response);
5763
// FastStorage has no 'foo' key, the SlowStorage is responding
58-
$this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly);
64+
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
65+
$this->assertEquals($className, $request->forDebugOnly);
5966
}
6067

6168
/**
@@ -68,6 +75,7 @@ public function testFailure($request)
6875

6976
$this->assertFalse($ret);
7077
// the last responsible :
71-
$this->assertEquals('DesignPatterns\ChainOfResponsibilities\Responsible\SlowStorage', $request->forDebugOnly);
78+
$className = 'DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible\SlowStorage';
79+
$this->assertEquals($className, $request->forDebugOnly);
7280
}
7381
}

Command/CommandInterface.php renamed to Behavioral/Command/CommandInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\Command;
3+
namespace DesignPatterns\Behavioral\Command;
44

55
/**
66
* class CommandInterface

Command/HelloCommand.php renamed to Behavioral/Command/HelloCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\Command;
3+
namespace DesignPatterns\Behavioral\Command;
44

55
/**
66
* This concrete command calls "print" on the Receiver, but an external

Command/Invoker.php renamed to Behavioral/Command/Invoker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\Command;
3+
namespace DesignPatterns\Behavioral\Command;
44

55
/**
66
* Invoker is using the command given to it.
File renamed without changes.

Command/Receiver.php renamed to Behavioral/Command/Receiver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace DesignPatterns\Command;
3+
namespace DesignPatterns\Behavioral\Command;
44

55
/**
66
* Receiver is specific service with its own contract and can be only concrete

Tests/Command/CommandTest.php renamed to Behavioral/Command/Tests/CommandTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
11
<?php
22

3-
namespace DesignPatterns\Tests\Command;
3+
namespace DesignPatterns\Behavioral\Command\Tests;
44

5-
use DesignPatterns\Command\Invoker;
6-
use DesignPatterns\Command\Receiver;
7-
use DesignPatterns\Command\HelloCommand;
5+
use DesignPatterns\Behavioral\Command\Invoker;
6+
use DesignPatterns\Behavioral\Command\Receiver;
7+
use DesignPatterns\Behavioral\Command\HelloCommand;
88

99
/**
1010
* CommandTest has the role of the Client in the Command Pattern
1111
*/
1212
class CommandTest extends \PHPUnit_Framework_TestCase
1313
{
1414

15+
/**
16+
* @var Invoker
17+
*/
1518
protected $invoker;
19+
20+
/**
21+
* @var Receiver
22+
*/
1623
protected $receiver;
1724

1825
protected function setUp()
1926
{
20-
// this is the context of the application
2127
$this->invoker = new Invoker();
2228
$this->receiver = new Receiver();
2329
}

Behavioral/Iterator/Book.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class Book
6+
{
7+
8+
private $author;
9+
10+
private $title;
11+
12+
public function __construct($title, $author)
13+
{
14+
$this->author = $author;
15+
$this->title = $title;
16+
}
17+
18+
public function getAuthor()
19+
{
20+
return $this->author;
21+
}
22+
23+
public function getTitle()
24+
{
25+
return $this->title;
26+
}
27+
28+
public function getAuthorAndTitle()
29+
{
30+
return $this->getTitle() . ' by ' . $this->getAuthor();
31+
}
32+
}

Behavioral/Iterator/BookList.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class BookList implements \Countable
6+
{
7+
8+
private $books;
9+
10+
public function getBook($bookNumberToGet)
11+
{
12+
if ((int)$bookNumberToGet <= $this->count()) {
13+
return $this->books[$bookNumberToGet];
14+
}
15+
16+
return null;
17+
}
18+
19+
public function addBook(Book $book)
20+
{
21+
$this->books[] = $book;
22+
23+
return $this->count();
24+
}
25+
26+
public function removeBook(Book $bookToRemove)
27+
{
28+
foreach ($this as $key => $book) {
29+
/** @var Book $book */
30+
if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
31+
unset($this->books[$key]);
32+
}
33+
}
34+
35+
return $this->count();
36+
}
37+
38+
public function count()
39+
{
40+
return count($this->books);
41+
}
42+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class BookListIterator implements \Iterator
6+
{
7+
8+
/**
9+
* @var BookList
10+
*/
11+
protected $bookList;
12+
13+
/**
14+
* @var int
15+
*/
16+
protected $currentBook = 0;
17+
18+
public function __construct(BookList $bookList)
19+
{
20+
$this->bookList = $bookList;
21+
}
22+
23+
/**
24+
* Return the current book
25+
* @link http://php.net/manual/en/iterator.current.php
26+
* @return Book Can return any type.
27+
*/
28+
public function current()
29+
{
30+
return $this->bookList->getBook($this->currentBook);
31+
}
32+
33+
/**
34+
* (PHP 5 &gt;= 5.0.0)<br/>
35+
* Move forward to next element
36+
* @link http://php.net/manual/en/iterator.next.php
37+
* @return void Any returned value is ignored.
38+
*/
39+
public function next()
40+
{
41+
$this->currentBook++;
42+
}
43+
44+
/**
45+
* (PHP 5 &gt;= 5.0.0)<br/>
46+
* Return the key of the current element
47+
* @link http://php.net/manual/en/iterator.key.php
48+
* @return mixed scalar on success, or null on failure.
49+
*/
50+
public function key()
51+
{
52+
return $this->currentBook;
53+
}
54+
55+
/**
56+
* (PHP 5 &gt;= 5.0.0)<br/>
57+
* Checks if current position is valid
58+
* @link http://php.net/manual/en/iterator.valid.php
59+
* @return boolean The return value will be casted to boolean and then evaluated.
60+
* Returns true on success or false on failure.
61+
*/
62+
public function valid()
63+
{
64+
return $this->currentBook < $this->bookList->count();
65+
}
66+
67+
/**
68+
* (PHP 5 &gt;= 5.0.0)<br/>
69+
* Rewind the Iterator to the first element
70+
* @link http://php.net/manual/en/iterator.rewind.php
71+
* @return void Any returned value is ignored.
72+
*/
73+
public function rewind()
74+
{
75+
$this->currentBook = 0;
76+
}
77+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\Iterator;
4+
5+
class BookListReverseIterator extends BookListIterator
6+
{
7+
8+
public function __construct(BookList $bookList)
9+
{
10+
$this->bookList = $bookList;
11+
$this->currentBook = $this->bookList->count() - 1;
12+
}
13+
14+
public function next()
15+
{
16+
$this->currentBook--;
17+
}
18+
19+
public function valid()
20+
{
21+
return 0 <= $this->currentBook;
22+
}
23+
}
File renamed without changes.

0 commit comments

Comments
 (0)