Skip to content

Commit

Permalink
Mechanisms refined
Browse files Browse the repository at this point in the history
  • Loading branch information
leodido committed Dec 24, 2014
1 parent fa25096 commit c4a553d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 23 deletions.
11 changes: 11 additions & 0 deletions library/Adapter/AbstractOptionsEnabledAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace Conversio\Adapter;

use Conversio\Conversion;
use Conversio\Exception;
use Conversio\ConversionAlgorithmInterface;
use Zend\Stdlib\AbstractOptions;
Expand All @@ -30,6 +31,16 @@ abstract class AbstractOptionsEnabledAdapter implements ConversionAlgorithmInter
*/
public function setOptions(AbstractOptions $options)
{
$optionsClass = Conversion::getAbstractOptionsFullQualifiedClassName($this);
$inputOptionsClass = get_class($options);
if ($inputOptionsClass !== $optionsClass) {
throw new Exception\DomainException(sprintf(
'"%s" expects that options set are an array or a valid "%s" instance; received "%s"',
__METHOD__,
$optionsClass,
$inputOptionsClass
));
}
$this->options = $options;

return $this;
Expand Down
55 changes: 33 additions & 22 deletions library/Conversion.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,12 @@ public function getAdapterOptions()
$this->adapterOptions = $optClass->setFromArray($this->adapterOptions);
return $this->adapterOptions;
}
if (get_class($this->adapterOptions) !== $this->getAbstractOptionsFullQualifiedClassName()) {
$wantedOptionsClass = self::getAbstractOptionsFullQualifiedClassName($this->adapter);
if (get_class($this->adapterOptions) !== $wantedOptionsClass) {
throw new Exception\DomainException(sprintf(
'"%s" expects that options set are an array or a valid "%s" instance; received "%s"',
__METHOD__,
$this->getAbstractOptionsFullQualifiedClassName(),
$wantedOptionsClass,
get_class($this->adapterOptions)
));
}
Expand All @@ -173,7 +174,7 @@ public function getAdapterOptions()
*/
protected function getAbstractOptions()
{
$optClass = $this->getAbstractOptionsFullQualifiedClassName();
$optClass = self::getAbstractOptionsFullQualifiedClassName($this->adapter);
// Does the option class exist?
if (!class_exists($optClass)) {
throw new Exception\DomainException(
Expand All @@ -198,24 +199,6 @@ protected function getAbstractOptions()
return $opts;
}

/**
* TODO: Docs
* @return string
* @throws Exception\RuntimeException
*/
protected function getAbstractOptionsFullQualifiedClassName()
{
if (!$this->adapter) {
throw new Exception\RuntimeException(sprintf(
'"%s" unable to load adapter; adapter not found',
__METHOD__
));
}
$adapterClass = get_class($this->adapter);
$namespace = substr($adapterClass, 0, strrpos($adapterClass, '\\'));
return $namespace . '\\Options\\' . $this->adapter->getName() . 'Options';
}

/**
* Set filter options
*
Expand Down Expand Up @@ -252,8 +235,9 @@ public function setOptions($options)
*/
public function getOptions($option = null)
{
$this->getAdapterOptions();
$this->getAdapterOptions(); // TODO: check that effectively options are set/not empty
return is_null($option) ? $this->options : $this->options[$option];

}

/**
Expand All @@ -266,4 +250,31 @@ public function filter($value)
}
return $this->getAdapter()->convert($value);
}

/**
* Get the full qualified class name of options class for the supplied adapter
*
* @param ConversionAlgorithmInterface|null $adapter
* @return string
* @throws Exception\InvalidArgumentException
*/
public static function getAbstractOptionsFullQualifiedClassName($adapter)
{
if (!$adapter) {
throw new Exception\InvalidArgumentException(sprintf(
'"%s" unable to load adapter; adapter not found',
__METHOD__
));
}
if (!$adapter instanceof ConversionAlgorithmInterface) {
throw new Exception\InvalidArgumentException(sprintf(
'"%s" expects a string or an instance of ConversionAlgorithmInterface; received "%s"',
__METHOD__,
is_object($adapter) ? get_class($adapter) : gettype($adapter)
));
}
$adapterClass = get_class($adapter);
$namespace = substr($adapterClass, 0, strrpos($adapterClass, '\\'));
return $namespace . '\\Options\\' . $adapter->getName() . 'Options';
}
}
2 changes: 1 addition & 1 deletion tests/ConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function testGetAdapterOptionsWhenAdapterHasNotBeenSpecifiedShouldThrowRu
'options' => ['opt1' => 'O1', 'opt2' => 'O2'],
];
$filter = new Conversion($onlyOpts);
$this->setExpectedException('Conversio\Exception\RuntimeException');
$this->setExpectedException('Conversio\Exception\InvalidArgumentException');
$filter->getAdapterOptions();
}

Expand Down

0 comments on commit c4a553d

Please sign in to comment.