Skip to content

Commit

Permalink
add adapter pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
anchetaWern committed Jan 14, 2023
1 parent e24706b commit d70dd65
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 1 deletion.
26 changes: 26 additions & 0 deletions adapterpattern/AdapterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use PHPUnit\Framework\TestCase;

class AdapterTest extends TestCase
{
public function test_filesystem_adapter() : void
{
$client = new AdapterPattern\App\Client();

$local_filesystem = new AdapterPattern\App\LocalFilesystem();
$this->assertEquals($client->readFile($local_filesystem), 'opening local file');

// this wont work since dropbox doesnt adhere to the same interface
/*
$dropbox_filesystem = new AdapterPattern\App\Dropbox();
$this->assertEquals($client->readFile($dropbox_filesystem), 'opening dropbox file');
*/

// so we use an adapter which implements the interface we want to adhere to
$dropbox_filesystem = new AdapterPattern\App\Dropbox();
$this->assertEquals($client->readFile(new AdapterPattern\App\FilesystemAdapter($dropbox_filesystem)), 'opening dropbox file');
}
}
11 changes: 11 additions & 0 deletions adapterpattern/app/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AdapterPattern\App;

class Client
{
public function readFile(FilesystemInterface $filesystem)
{
return $filesystem->open();
}
}
8 changes: 8 additions & 0 deletions adapterpattern/app/CloudFilesystemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace AdapterPattern\App;

interface CloudFilesystemInterface
{
public function openFile();
}
11 changes: 11 additions & 0 deletions adapterpattern/app/Dropbox.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace AdapterPattern\App;

class Dropbox implements CloudFilesystemInterface
{
public function openFile()
{
return 'opening dropbox file';
}
}
18 changes: 18 additions & 0 deletions adapterpattern/app/FilesystemAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace AdapterPattern\App;

class FilesystemAdapter implements FilesystemInterface
{

public function __construct(private CloudFilesystemInterface $filesystem)
{

}

public function open()
{
return $this->filesystem->openFile();
}

}
9 changes: 9 additions & 0 deletions adapterpattern/app/FilesystemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace AdapterPattern\App;

interface FilesystemInterface
{
public function open();

}
13 changes: 13 additions & 0 deletions adapterpattern/app/LocalFilesystem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace AdapterPattern\App;

class LocalFilesystem implements FilesystemInterface
{

public function open()
{
return 'opening local file';
}

}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"autoload": {
"psr-4": {
"DecoratorPattern\\App\\": "decoratorpattern/app/",
"FacadePattern\\App\\": "facadepattern/app/"
"FacadePattern\\App\\": "facadepattern/app/",
"AdapterPattern\\App\\": "adapterpattern/app/"
}
},
"require-dev": {
Expand Down

0 comments on commit d70dd65

Please sign in to comment.