aboutsummaryrefslogtreecommitdiffstats
path: root/Model
diff options
context:
space:
mode:
authorIvan Enderlin <ivan.enderlin@hoa-project.net>2013-06-10 14:17:22 +0200
committerIvan Enderlin <ivan.enderlin@hoa-project.net>2013-06-10 14:20:55 +0200
commit95d8e9a76566f980fb7e08fb7c27b6b884d53f57 (patch)
tree10f88f78936fb40395f7d9c5fbc77ee9071a2744 /Model
parent824e66a0399b20931678e1fdea2ce5fa018f6319 (diff)
downloadPraspel-95d8e9a76566f980fb7e08fb7c27b6b884d53f57.zip
Praspel-95d8e9a76566f980fb7e08fb7c27b6b884d53f57.tar.gz
Praspel-95d8e9a76566f980fb7e08fb7c27b6b884d53f57.tar.bz2
Add the allowedClauses attributes.
It helps to simplify the code.
Diffstat (limited to 'Model')
-rw-r--r--Model/Behavior.php57
-rw-r--r--Model/Specification.php87
2 files changed, 67 insertions, 77 deletions
diff --git a/Model/Behavior.php b/Model/Behavior.php
index 852d1a7..6279cbc 100644
--- a/Model/Behavior.php
+++ b/Model/Behavior.php
@@ -49,6 +49,11 @@ from('Hoa')
-> import('Praspel.Model.Clause')
/**
+ * \Hoa\Praspel\Model\Is
+ */
+-> import('Praspel.Model.Is')
+
+/**
* \Hoa\Praspel\Model\Requires
*/
-> import('Praspel.Model.Requires')
@@ -71,7 +76,12 @@ from('Hoa')
/**
* \Hoa\Praspel\Model\Collection
*/
--> import('Praspel.Model.Collection');
+-> import('Praspel.Model.Collection')
+
+/**
+ * \Hoa\Praspel\Model\Description
+ */
+-> import('Praspel.Model.Description');
}
@@ -97,18 +107,31 @@ class Behavior extends Clause {
const NAME = 'behavior';
/**
+ * Allowed clauses.
+ *
+ * @var \Hoa\Praspel\Model\Behavior array
+ */
+ protected static $_allowedClauses = array(
+ 'requires',
+ 'ensures',
+ 'throwable',
+ 'invariant',
+ 'behavior'
+ );
+
+ /**
* Clauses.
*
* @var \Hoa\Praspel\Model\Behavior array
*/
- protected $_clauses = array();
+ protected $_clauses = array();
/**
* Identifier (@behavior <identifier> { … }).
*
* @var \Hoa\Praspel\Model\Behavior string
*/
- protected $_identifier = null;
+ protected $_identifier = null;
@@ -127,8 +150,17 @@ class Behavior extends Clause {
$handle = null;
+ if(false === in_array($clause, static::getAllowedClauses()))
+ throw new \Hoa\Praspel\Exception\Model(
+ 'Clause @%s is not allowed in @%s.',
+ 1, array($clause, $this->getId()));
+
switch($clause) {
+ case 'is':
+ $handle = new Is($this);
+ break;
+
case 'requires':
$handle = new Requires($this);
break;
@@ -157,10 +189,14 @@ class Behavior extends Clause {
);
break;
+ case 'description':
+ $handle = new Description($this);
+ break;
+
default:
throw new \Hoa\Praspel\Exception\Model(
- 'Clause @%s is unknown or not allowed in %s.',
- 0, array($clause, $this->getName()));
+ 'Clause @%s is unknown.',
+ 1, array($clause, $this->getName()));
}
return $this->_clauses[$clause] = $handle;
@@ -179,6 +215,17 @@ class Behavior extends Clause {
}
/**
+ * Get allowed clauses.
+ *
+ * @access public
+ * @return array
+ */
+ public static function getAllowedClauses ( ) {
+
+ return static::$_allowedClauses;
+ }
+
+ /**
* Set identifier.
*
* @access public
diff --git a/Model/Specification.php b/Model/Specification.php
index b5ac837..c731e75 100644
--- a/Model/Specification.php
+++ b/Model/Specification.php
@@ -44,16 +44,6 @@ from('Hoa')
-> import('Praspel.Model.Behavior')
/**
- * \Hoa\Praspel\Model\Is
- */
--> import('Praspel.Model.Is')
-
-/**
- * \Hoa\Praspel\Model\Description
- */
--> import('Praspel.Model.Description')
-
-/**
* \Hoa\Realdom\Disjunction
*/
-> import('Realdom.Disjunction', true);
@@ -81,6 +71,21 @@ class Specification extends Behavior {
*/
const NAME = '';
+ /**
+ * Allowed clauses.
+ *
+ * @var \Hoa\Praspel\Model\Specification array
+ */
+ protected static $_allowedClauses = array(
+ 'is',
+ 'requires',
+ 'ensures',
+ 'throwable',
+ 'invariant',
+ 'behavior',
+ 'description'
+ );
+
/**
@@ -95,68 +100,6 @@ class Specification extends Behavior {
}
/**
- * Get a specific clause.
- *
- * @access public
- * @param string $clause Clause (without leading arobase).
- * @return \Hoa\Praspel\Model\Clause
- * @throw \Hoa\Praspel\Exception\Model
- */
- public function getClause ( $clause ) {
-
- if(isset($this->_clauses[$clause]))
- return $this->_clauses[$clause];
-
- $handle = null;
-
- switch($clause) {
-
- case 'is':
- $handle = new Is($this);
- break;
-
- case 'requires':
- $handle = new Requires($this);
- break;
-
- case 'ensures':
- $handle = new Ensures($this);
- break;
-
- case 'throwable':
- $handle = new Throwable($this);
- break;
-
- case 'invariant':
- $handle = new Invariant($this);
- break;
-
- case 'behavior':
- $handle = new Collection(
- new Behavior($this),
- function ( Behavior $clause, $identifier ) {
-
- $clause->setIdentifier($identifier);
-
- return;
- }
- );
- break;
-
- case 'description':
- $handle = new Description($this);
- break;
-
- default:
- throw new \Hoa\Praspel\Exception\Model(
- 'Clause @%s is unknown.',
- 0, $clause);
- }
-
- return $this->_clauses[$clause] = $handle;
- }
-
- /**
* Get identifier (fallback).
*
* @access protected