Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'milestones/exceptions' of git://git.zendframework.com/z…
Browse files Browse the repository at this point in the history
…f into serializer-exceptions

Conflicts:

	working/README-exceptions.txt
  • Loading branch information
marc-mabe committed Sep 29, 2010
3 parents 2fdcee4 + 147c99e + 0a9c731 commit d77655e
Show file tree
Hide file tree
Showing 23 changed files with 543 additions and 127 deletions.
38 changes: 16 additions & 22 deletions src/Barcode.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
* @namespace
*/
namespace Zend\Barcode;
use Zend\Barcode\Renderer,
Zend\Loader\PluginLoader,
use Zend\Loader\PluginLoader,
Zend\Loader\ShortNameLocater,
Zend\Config\Config,
Zend;
Zend,
Zend\Barcode\Exception\RendererCreationException,
Zend\Barcode\Exception\InvalidArgumentException;

/**
* Class for generate Barcode
Expand Down Expand Up @@ -78,7 +79,7 @@ public static function setPluginLoader(ShortNameLocater $loader, $type)
self::$_loaders[$type] = $loader;
return;
default:
throw new Exception(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
throw new InvalidArgumentException(sprintf('Invalid type "%s" provided to setPluginLoader()', $type));
}
}

Expand Down Expand Up @@ -107,7 +108,7 @@ public static function getPluginLoader($type)
}
return self::$_loaders[$type];
default:
throw new Exception(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
throw new InvalidArgumentException(sprintf('Invalid type "%s" provided to getPluginLoader()', $type));
}
}

Expand Down Expand Up @@ -166,9 +167,8 @@ public static function factory($barcode,
try {
$barcode = self::makeBarcode($barcode, $barcodeConfig);
$renderer = self::makeRenderer($renderer, $rendererConfig);
} catch (Zend\Exception $e) {
$renderable = ($e instanceof Exception) ? $e->isRenderable() : false;
if ($automaticRenderError && $renderable) {
} catch (Exception $e) {
if ($automaticRenderError && !($e instanceof RendererCreationException)) {
$barcode = self::makeBarcode('error', array( 'text' => $e->getMessage() ));
$renderer = self::makeRenderer($renderer, array());
} else {
Expand Down Expand Up @@ -215,7 +215,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array())
* Verify that barcode parameters are in an array.
*/
if (!is_array($barcodeConfig)) {
throw new Exception(
throw new InvalidArgumentException(
'Barcode parameters must be in an array or a \Zend\Config\Config object'
);
}
Expand All @@ -224,7 +224,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array())
* Verify that an barcode name has been specified.
*/
if (!is_string($barcode) || empty($barcode)) {
throw new Exception(
throw new InvalidArgumentException(
'Barcode name must be specified in a string'
);
}
Expand All @@ -241,7 +241,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array())
* Verify that the object created is a descendent of the abstract barcode type.
*/
if (!$bcAdapter instanceof BarcodeObject) {
throw new Exception(
throw new InvalidArgumentException(
"Barcode class '$barcodeName' does not implement \Zend\Barcode\BarcodeObject"
);
}
Expand All @@ -257,7 +257,7 @@ public static function makeBarcode($barcode, $barcodeConfig = array())
*/
public static function makeRenderer($renderer = 'image', $rendererConfig = array())
{
if ($renderer instanceof Renderer) {
if ($renderer instanceof BarcodeRenderer) {
return $renderer;
}

Expand All @@ -281,22 +281,18 @@ public static function makeRenderer($renderer = 'image', $rendererConfig = array
* Verify that barcode parameters are in an array.
*/
if (!is_array($rendererConfig)) {
$e = new Exception(
throw new RendererCreationException(
'Barcode parameters must be in an array or a \Zend\Config\Config object'
);
$e->setIsRenderable(false);
throw $e;
}

/*
* Verify that an barcode name has been specified.
*/
if (!is_string($renderer) || empty($renderer)) {
$e = new Exception(
throw new RendererCreationException(
'Renderer name must be specified in a string'
);
$e->setIsRenderable(false);
throw $e;
}

$rendererName = self::getPluginLoader(self::RENDERER)->load($renderer);
Expand All @@ -310,12 +306,10 @@ public static function makeRenderer($renderer = 'image', $rendererConfig = array
/*
* Verify that the object created is a descendent of the abstract barcode type.
*/
if (!$rdrAdapter instanceof Renderer) {
$e = new Exception(
if (!$rdrAdapter instanceof BarcodeRenderer) {
throw new RendererCreationException(
"Renderer class '$rendererName' does not implements \Zend\Barcode\Renderer"
);
$e->setIsRenderable(false);
throw $e;
}
return $rdrAdapter;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer.php → src/BarcodeRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
interface Renderer
interface BarcodeRenderer
{
/**
* Constructor
Expand Down
34 changes: 3 additions & 31 deletions src/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,44 +23,16 @@
* @namespace
*/
namespace Zend\Barcode;
use Zend;

/**
* Zend_Barcode_Exception
* Exception for Zend_Barcode component.
*
* @uses \Zend\Exception
* @uses Zend\Exception
* @category Zend
* @package Zend_Barcode
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Exception extends Zend\Exception
interface Exception
{
/**
* Is this exception renderable?
* @var bool
*/
protected $_isRenderable = true;

/**
* Set renderable flag
*
* @param bool $flag
* @return \Zend\Barcode\Exception
*/
public function setIsRenderable($flag)
{
$this->_isRenderable = (bool) $flag;
return $this;
}

/**
* Retrieve renderable flag
*
* @return bool
*/
public function isRenderable()
{
return $this->_isRenderable;
}
}
41 changes: 41 additions & 0 deletions src/Exception/InvalidArgumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Barcode
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Barcode\Exception;
use Zend\Barcode\Exception;

/**
* Exception for Zend_Barcode component.
*
* @uses Zend\Exception
* @category Zend
* @package Zend_Barcode
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class InvalidArgumentException
extends \InvalidArgumentException
implements Exception
{
}
41 changes: 41 additions & 0 deletions src/Exception/RendererCreationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Barcode
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @version $Id$
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

/**
* @namespace
*/
namespace Zend\Barcode\Exception;
use Zend\Barcode\Exception;

/**
* Exception for Zend_Barcode component.
*
* @uses Zend\Exception
* @category Zend
* @package Zend_Barcode
* @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class RendererCreationException
extends \InvalidArgumentException
implements Exception
{
}
32 changes: 18 additions & 14 deletions src/Object/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@

use Zend\Barcode,
Zend\Config\Config,
Zend\Validator\Barcode as BarcodeValidator;
Zend\Validator\Barcode as BarcodeValidator,
Zend\Barcode\Object\Exception\RuntimeException,
Zend\Barcode\Object\Exception\InvalidArgumentException,
Zend\Barcode\Object\Exception\BarcodeValidationException,
Zend\Barcode\Object\Exception\OutOfRangeException;

/**
* Class for generate Barcode
Expand Down Expand Up @@ -311,7 +315,7 @@ public function getType()
public function setBarHeight($value)
{
if (intval($value) <= 0) {
throw new Exception(
throw new OutOfRangeException(
'Bar height must be greater than 0'
);
}
Expand All @@ -337,7 +341,7 @@ public function getBarHeight()
public function setBarThinWidth($value)
{
if (intval($value) <= 0) {
throw new Exception(
throw new OutOfRangeException(
'Bar width must be greater than 0'
);
}
Expand All @@ -363,7 +367,7 @@ public function getBarThinWidth()
public function setBarThickWidth($value)
{
if (intval($value) <= 0) {
throw new Exception(
throw new OutOfRangeException(
'Bar width must be greater than 0'
);
}
Expand All @@ -390,7 +394,7 @@ public function getBarThickWidth()
public function setFactor($value)
{
if (floatval($value) <= 0) {
throw new Exception(
throw new OutOfRangeException(
'Factor must be greater than 0'
);
}
Expand Down Expand Up @@ -421,7 +425,7 @@ public function setForeColor($value)
} elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
$this->_foreColor = intval($value);
} else {
throw new Exception(
throw new InvalidArgumentException(
'Text color must be set as #[0-9A-F]{6}'
);
}
Expand Down Expand Up @@ -450,7 +454,7 @@ public function setBackgroundColor($value)
} elseif (is_numeric($value) && $value >= 0 && $value <= 16777125) {
$this->_backgroundColor = intval($value);
} else {
throw new Exception(
throw new InvalidArgumentException(
'Background color must be set as #[0-9A-F]{6}'
);
}
Expand Down Expand Up @@ -714,7 +718,7 @@ public function setFont($value)
{
if (is_int($value) && $value >= 1 && $value <= 5) {
if (!extension_loaded('gd')) {
throw new Exception(
throw new ExtensionNotLoaded(
'GD extension is required to use numeric font'
);
}
Expand All @@ -727,7 +731,7 @@ public function setFont($value)
} elseif (is_string($value)) {
$this->_font = $value;
} else {
throw new Exception(sprintf(
throw new InvalidArgumentException(sprintf(
'Invalid font "%s" provided to setFont()',
$value
));
Expand Down Expand Up @@ -758,7 +762,7 @@ public function setFontSize($value)
}

if (!is_numeric($value)) {
throw new Exception(
throw new InvalidArgumentException(
'Font size must be a numeric value'
);
}
Expand Down Expand Up @@ -884,7 +888,7 @@ protected function _checkText($value = null)
$value = $this->_text;
}
if (!strlen($value)) {
throw new Exception(
throw new RuntimeException(
'A text must be provide to Barcode before drawing'
);
}
Expand All @@ -902,7 +906,7 @@ protected function _checkRatio($min = 2, $max = 3)
{
$ratio = $this->_barThickWidth / $this->_barThinWidth;
if (!($ratio >= $min && $ratio <= $max)) {
throw new Exception(sprintf(
throw new OutOfRangeException(sprintf(
'Ratio thick/thin bar must be between %0.1f and %0.1f (actual %0.3f)',
$min,
$max,
Expand All @@ -919,7 +923,7 @@ protected function _checkRatio($min = 2, $max = 3)
protected function _checkFontAndOrientation()
{
if (is_numeric($this->_font) && $this->_orientation != 0) {
throw new Exception(
throw new RuntimeException(
'Only drawing with TTF font allow orientation of the barcode.'
);
}
Expand Down Expand Up @@ -1242,7 +1246,7 @@ protected function _validateText($value, $options = array())

if (!$validator->isValid($value)) {
$message = implode("\n", $validator->getMessages());
throw new Exception($message);
throw new BarcodeValidationException($message);
}
}

Expand Down
Loading

0 comments on commit d77655e

Please sign in to comment.