diff --git a/library/Zend/Validator/Barcode.php b/library/Zend/Validator/Barcode.php index f86003eb83b..74f6edcd535 100644 --- a/library/Zend/Validator/Barcode.php +++ b/library/Zend/Validator/Barcode.php @@ -91,15 +91,20 @@ public function setAdapter($adapter, $options = null) throw new Exception\InvalidArgumentException('Barcode adapter matching "' . $adapter . '" not found'); } - $this->options['adapter'] = new $adapter($options); + $adapter = new $adapter($options); } - - if (!$this->options['adapter'] instanceof Barcode\AdapterInterface) { + + if (!$adapter instanceof Barcode\AdapterInterface) { throw new Exception\InvalidArgumentException( - "Adapter $adapter does not implement Zend\\Validate\\Barcode\\AdapterInterface" + sprintf( + "Adapter %s does not implement Zend\\Validator\\Barcode\\AdapterInterface", + (is_object($adapter) ? get_class($adapter) : gettype($adapter)) + ) ); } + $this->options['adapter'] = $adapter; + return $this; } diff --git a/tests/ZendTest/Validator/BarcodeTest.php b/tests/ZendTest/Validator/BarcodeTest.php index e59ce436008..4a5c020f96e 100644 --- a/tests/ZendTest/Validator/BarcodeTest.php +++ b/tests/ZendTest/Validator/BarcodeTest.php @@ -32,6 +32,15 @@ public function testSetAdapter() $barcode->setAdapter('ean13'); $this->assertTrue($barcode->isValid('0075678164125')); } + + public function testSetCustomAdapter() + { + $barcode = new Barcode([ + 'adapter' => $this->getMock('Zend\Validator\Barcode\AdapterInterface') + ]); + + $this->assertInstanceOf('Zend\Validator\Barcode\AdapterInterface', $barcode->getAdapter()); + } /** * @ZF-4352