Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added assertion controller helper and updated the Zend library (requi…

…red for this commit onward)
  • Loading branch information...
commit eee0c133bab6fd65b2a415aeaddd41048d0dcddc 1 parent d187fc5
@epixa authored
View
30 example/application/core/src/Controller/IndexController.php
@@ -5,7 +5,9 @@
namespace Core\Controller;
-use Core\Form\TestForm;
+use Core\Form\TestForm,
+ LogicException,
+ Exception;
/**
* Default controller
@@ -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>');
+ }
}
View
5 example/config/settings/production.php
@@ -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()
2  example/vendor/Zend
@@ -1 +1 @@
-Subproject commit c66314889739fae2a2f9f66cc023f9bc9639a10e
+Subproject commit f4875fc9747ac63ece9e505935dcb39c600131e0
View
103 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);
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.