Created
January 24, 2012 14:17
-
-
Save david4worx/1670386 to your computer and use it in GitHub Desktop.
Adapters
This file contains hidden or 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
<?php | |
interface FilterAdapter | |
{ | |
public function filter($value); | |
} | |
class FilterBase | |
{ | |
protected $_adapter; | |
public function getAdapter() | |
{ | |
return $this->_adapter; | |
} | |
public function setAdapter(FilterAdapter $filter) | |
{ | |
$this->_adapter = $filter; | |
return $this; | |
} | |
public function filter($value) | |
{ | |
return $this->getAdapter()->filter($value); | |
} | |
} | |
//Old filter adapter | |
class FilterArrayAdapterHard implements FilterAdapter | |
{ | |
public function filter($value) | |
{ | |
if (!is_array($value)) { | |
throw new InvalidArgumentException('$value should be an array.'); | |
} | |
$tmp = array(); | |
foreach ($value as $k => $v) { | |
if (!empty($v)) { | |
$tmp[$k] = $v; | |
} | |
} | |
return $tmp; | |
} | |
} | |
//New filter adapter | |
class FilterArrayAdapterEasy implements FilterAdapter | |
{ | |
public function filter($value) | |
{ | |
if (!is_array($value)) { | |
throw new InvalidArgumentException('$value should be an array.'); | |
} | |
return array_filter($value); | |
} | |
} | |
$foo = array('foo', 'bar', 0); | |
$filter = new FilterBase(); | |
var_dump($filter->setAdapter(new FilterArrayAdapterEasy())->filter($foo)); |
This file contains hidden or 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
<?php | |
interface FilterAdapter | |
{ | |
public function filter($value); | |
} | |
class FilterBase | |
{ | |
protected $_adapter; | |
public function getAdapter() | |
{ | |
return $this->_adapter; | |
} | |
public function setAdapter(FilterAdapter $filter) | |
{ | |
$this->_adapter = $filter; | |
return $this; | |
} | |
public function filter($value) | |
{ | |
return $this->getAdapter()->filter($value); | |
} | |
} | |
class FilterArrayAdapterHard implements FilterAdapter | |
{ | |
public function filter($value) | |
{ | |
if (!is_array($value)) { | |
throw new InvalidArgumentException('$value should be an array.'); | |
} | |
$tmp = array(); | |
foreach ($value as $k => $v) { | |
if (!empty($v)) { | |
$tmp[$k] = $v; | |
} | |
} | |
return $tmp; | |
} | |
} | |
$foo = array('foo', 'bar', 0); | |
$filter = new FilterBase(); | |
var_dump($filter->setAdapter(new FilterArrayAdapterHard())->filter($foo)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment