-
Notifications
You must be signed in to change notification settings - Fork 7
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
7 changed files
with
180 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 11.07.15 | ||
* Time: 20:35 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Behavioral\Adapter; | ||
|
||
|
||
class EmailClient | ||
{ | ||
public function __construct() | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function sendEmail() | ||
{ | ||
return 'I sent email'; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function sendUtf8Email() | ||
{ | ||
return 'I sent UTF-8 email'; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 11.07.15 | ||
* Time: 20:43 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Behavioral\Adapter; | ||
|
||
|
||
class EmailClientOnlyUtfAdapter implements OnlyUtfSender | ||
{ | ||
private $emailClient; | ||
|
||
function __construct(EmailClient $emailClient) | ||
{ | ||
$this->emailClient = $emailClient; | ||
} | ||
|
||
public function send() | ||
{ | ||
return $this->emailClient->sendUtf8Email(); | ||
} | ||
} |
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,15 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 11.07.15 | ||
* Time: 20:32 | ||
*/ | ||
|
||
namespace Rpodwika\Designpatterns\Behavioral\Adapter; | ||
|
||
|
||
interface OnlyUtfSender | ||
{ | ||
public function send(); | ||
} |
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 @@ | ||
#Adapter pattern | ||
|
||
Adapter pattern allows the interface of existing class to be used from another interface. It is often used to adapt | ||
other classes to work with other without modifying original class source. Adapter lets classes work together despite to | ||
incompatible interfaces. | ||
|
||
You can compare it to real world situation. You have device A and B that sends same data but in different format for example | ||
one sends audio in MP3 and the other in wav so you need adapter that converts audio from one device to be readable by | ||
the other one. | ||
|
||
##When to use | ||
|
||
1. When you want to adapt class behavior to new conditions without modifying source of original class | ||
2. When you want to secure some methods from original class not to be visible for example because they use large | ||
amount of resources. | ||
3. When you want to use existing class but its interface does not match the one you need | ||
4. | ||
|
||
##How to use | ||
|
||
See [tests/DecoratorTest](/tests/DecoratorTest.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
/** | ||
* Created by PhpStorm. | ||
* User: robertpodwikamac | ||
* Date: 11.07.15 | ||
* Time: 20:47 | ||
*/ | ||
|
||
use \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClient; | ||
use \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClientOnlyUtfAdapter; | ||
|
||
class AdapterTest extends PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClient::sendUtf8Email | ||
*/ | ||
public function testEmailClientSendsUtf8() | ||
{ | ||
$emailClient = new EmailClient(); | ||
$this->assertEquals('I sent UTF-8 email', $emailClient->sendUtf8Email()); | ||
} | ||
|
||
/** | ||
* @covers \Rpodwika\Designpatterns\Behavioral\Adapter\EmailClient::sendEmail | ||
*/ | ||
public function testEmailClientSendsPlainEmail() | ||
{ | ||
$emailClient = new EmailClient(); | ||
$this->assertEquals('I sent email', $emailClient->sendEmail()); | ||
} | ||
|
||
public function testEmailAdapterSendsEmail() | ||
{ | ||
$emailClient = new EmailClient(); | ||
$emailClientUffAdapter = new EmailClientOnlyUtfAdapter($emailClient); | ||
$this->assertEquals('I sent UTF-8 email', $emailClientUffAdapter->send()); | ||
} | ||
} |
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