Skip to content

Instantly share code, notes, and snippets.

@useless-stuff
Last active February 16, 2016 23:54
Show Gist options
  • Select an option

  • Save useless-stuff/43918bc07b29b1e6e4da to your computer and use it in GitHub Desktop.

Select an option

Save useless-stuff/43918bc07b29b1e6e4da to your computer and use it in GitHub Desktop.
PHP - EmptyIterator
<?php
/**
* Class Adapter
*/
abstract class Adapter extends RecursiveArrayIterator{
}
/**
* Class GoogleDriveAdapter
*/
class GoogleDriveAdapter extends Adapter{
}
/**
* Class AmazonS3Adapter
*/
class AmazonS3Adapter extends Adapter{
}
/**
* Class AdaptersFactory
*/
class AdaptersFactory{
const ADAPTER_GOOGLE_DRIVE = 'GoogleDriveAdapter';
const ADAPTER_AMAZON_S3 = 'AmazonS3Adapter';
protected $adapter;
/**
* AdaptersFactory constructor.
* @param $type
*/
public function __construct($type)
{
$oClass = new ReflectionClass(__CLASS__);
if(!in_array($type,$oClass->getConstants())){
// EmptyIterator
$this->adapter = new EmptyIterator();
}
$this->adapter = new $type($this->getOptions()[$type]);
}
/**
* @return mixed
*/
public function getAdapter(){
return $this->adapter;
}
/**
* @return array
*/
public function getOptions(){
return array(
'GoogleDriveAdapter' => array(
'access_token' => 'test1',
'app_id' => 'test1',
'app_config' => array(
'default_folder' => array(
'test' => 'test'
)
)
),
'AmazonS3Adapter' => array(
'access_token' => 'test2',
'app_id' => 'test2',
'app_config' => array(
'default_folder' => array(
'test' => 'test'
)
)
)
);
}
}
$adapterFactory = new AdaptersFactory(AdaptersFactory::ADAPTER_AMAZON_S3);
foreach($adapterFactory->getAdapter() as $adapter){
var_dump($adapter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment