Skip to content

Commit d70dd65

Browse files
committed
add adapter pattern
1 parent e24706b commit d70dd65

File tree

8 files changed

+98
-1
lines changed

8 files changed

+98
-1
lines changed

adapterpattern/AdapterTest.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
require_once __DIR__ . '/../vendor/autoload.php';
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
class AdapterTest extends TestCase
8+
{
9+
public function test_filesystem_adapter() : void
10+
{
11+
$client = new AdapterPattern\App\Client();
12+
13+
$local_filesystem = new AdapterPattern\App\LocalFilesystem();
14+
$this->assertEquals($client->readFile($local_filesystem), 'opening local file');
15+
16+
// this wont work since dropbox doesnt adhere to the same interface
17+
/*
18+
$dropbox_filesystem = new AdapterPattern\App\Dropbox();
19+
$this->assertEquals($client->readFile($dropbox_filesystem), 'opening dropbox file');
20+
*/
21+
22+
// so we use an adapter which implements the interface we want to adhere to
23+
$dropbox_filesystem = new AdapterPattern\App\Dropbox();
24+
$this->assertEquals($client->readFile(new AdapterPattern\App\FilesystemAdapter($dropbox_filesystem)), 'opening dropbox file');
25+
}
26+
}

adapterpattern/app/Client.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace AdapterPattern\App;
4+
5+
class Client
6+
{
7+
public function readFile(FilesystemInterface $filesystem)
8+
{
9+
return $filesystem->open();
10+
}
11+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace AdapterPattern\App;
4+
5+
interface CloudFilesystemInterface
6+
{
7+
public function openFile();
8+
}

adapterpattern/app/Dropbox.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace AdapterPattern\App;
4+
5+
class Dropbox implements CloudFilesystemInterface
6+
{
7+
public function openFile()
8+
{
9+
return 'opening dropbox file';
10+
}
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace AdapterPattern\App;
4+
5+
class FilesystemAdapter implements FilesystemInterface
6+
{
7+
8+
public function __construct(private CloudFilesystemInterface $filesystem)
9+
{
10+
11+
}
12+
13+
public function open()
14+
{
15+
return $this->filesystem->openFile();
16+
}
17+
18+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace AdapterPattern\App;
4+
5+
interface FilesystemInterface
6+
{
7+
public function open();
8+
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace AdapterPattern\App;
4+
5+
class LocalFilesystem implements FilesystemInterface
6+
{
7+
8+
public function open()
9+
{
10+
return 'opening local file';
11+
}
12+
13+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"autoload": {
1010
"psr-4": {
1111
"DecoratorPattern\\App\\": "decoratorpattern/app/",
12-
"FacadePattern\\App\\": "facadepattern/app/"
12+
"FacadePattern\\App\\": "facadepattern/app/",
13+
"AdapterPattern\\App\\": "adapterpattern/app/"
1314
}
1415
},
1516
"require-dev": {

0 commit comments

Comments
 (0)