Skip to content

Commit

Permalink
Added assertion controller helper and updated the Zend library (requi…
Browse files Browse the repository at this point in the history
…red for this commit onward)
  • Loading branch information
epixa committed Jan 24, 2011
1 parent d187fc5 commit eee0c13
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 3 deletions.
30 changes: 29 additions & 1 deletion example/application/core/src/Controller/IndexController.php
Expand Up @@ -5,7 +5,9 @@

namespace Core\Controller;

use Core\Form\TestForm;
use Core\Form\TestForm,
LogicException,
Exception;

/**
* Default controller
Expand Down Expand Up @@ -33,4 +35,30 @@ public function indexAction()
var_dump($form->getValues());
die('<p>Core\Controller\IndexController::indexAction</p>');
}

public function assertAction()
{
try {
$this->_helper->assert->isset(null);
} catch (Exception $e) {
echo '<p>Default assertion failure</p>';
}

try {
$this->_helper->assert->isset(null, 'LogicException');
} catch (LogicException $e) {
echo '<p>LogicException assertion failure</p>';
}

try {
$this->_helper->assert->isset(null, 'LogicException', 'Test Message');
} catch (LogicException $e) {
printf(
'<p>LogicException assertion failure with message: %s </p>',
$e->getMessage()
);
}

die('<p>Core\Controller\IndexController::assertAction</p>');
}
}
5 changes: 4 additions & 1 deletion example/config/settings/production.php
Expand Up @@ -15,7 +15,10 @@
'frontController' => array(
'moduleDirectory' => APPLICATION_PATH,
'env' => APPLICATION_ENV,
'prefixDefaultModule' => true
'prefixDefaultModule' => true,
'actionHelperPaths' => array(
'Epixa\\Controller\\Helper\\' => 'Epixa/Controller/Helper'
)
),
'modules' => array(),
'view' => array()
Expand Down
2 changes: 1 addition & 1 deletion example/vendor/Zend
Submodule Zend updated 10019 files
103 changes: 103 additions & 0 deletions library/Epixa/Controller/Helper/Assert.php
@@ -0,0 +1,103 @@
<?php
/**
* Epixa Library
*/

namespace Epixa\Controller\Helper;

use Zend_Controller_Action_Helper_Abstract as AbstractHelper,
BadMethodCallException,
Exception;

/**
* Controller helper that allows for various forms of assertion functionality
*
* @category Epixa
* @package Controller
* @subpackage Helper
* @copyright 2011 epixa.com - Court Ewing
* @license http://github.com/epixa/Epixa/blob/master/LICENSE New BSD
* @author Court Ewing (court@epixa.com)
*/
class Assert extends AbstractHelper
{
/**
* Magic method -- invoked when a called method is not found
*
* If the given method does not exist, checks to see if an appropriate
* assertion method can be found in the current class. Assertion methods
* are protected, so this is the only way to invoke them publicly.
*
* If an assertion is found but returns false, then an exception is thrown.
* The type of exception thrown and exception message can be customized on
* method invokation. e.g.:
*
* $helper->isset($value, '\LogicException', 'Value does not exist')
*
* @param string $method
* @param array $arguments
* @throws BadMethodCallException If assertion method does not exist
* @throws Exception If assertion method returns false
*/
public function __call($method, array $arguments)
{
$methodName = $this->_formatAssertMethodName($method);
if (!method_exists($this, $methodName)) {
throw new BadMethodCallException(sprintf(
'Assertion `%s` does not exist', $method
));
}

$value = array_shift($arguments);
if (!$this->$methodName($value)) {
$exceptionName = array_shift($arguments);
$message = array_shift($arguments);
$this->throwException($exceptionName, $message);
}
}

/**
* Throws an exception with an optional type and message
*
* If $exceptionName is specified, throws an exception of that type.
* Otherwise, throws a standard Exception.
*
* Passes any provided message along to the exception.
*
* @param null|string $exceptionName
* @param null|string $msg
*/
public function throwException($exceptionName = null, $msg = null)
{
if (!$exceptionName) {
$exceptionName = 'Exception';
}

throw new $exceptionName($msg);
}


/**
* Determines if the given property is actually set to something
*
* @link http://php.net/isset
*
* @param mixed $value
* @return boolean
*/
protected function _assertIsset($value)
{
return isset($value);
}

/**
* Formats the given name as an assertion method name
*
* @param string $name
* @return string
*/
protected function _formatAssertMethodName($name)
{
return '_assert' . ucfirst($name);
}
}

0 comments on commit eee0c13

Please sign in to comment.