Hoa central
Behavior.php
Go to the documentation of this file.
1 <?php
2 
38 
39 use Hoa\Praspel;
40 
49 class Behavior extends Clause
50 {
56  const NAME = 'behavior';
57 
63  protected static $_allowedClauses = [
64  'requires',
65  'behavior',
66  'default',
67  'ensures',
68  'throwable',
69  'description'
70  ];
71 
77  protected $_clauses = [];
78 
84  protected $_identifier = null;
85 
86 
87 
95  public function getClause($clause)
96  {
97  if (isset($this->_clauses[$clause])) {
98  return $this->_clauses[$clause];
99  }
100 
101  $handle = null;
102 
103  if (false === in_array($clause, static::getAllowedClauses())) {
104  throw new Praspel\Exception\Model(
105  'Clause @%s is not allowed in @%s.',
106  0,
107  [$clause, $this->getId()]
108  );
109  }
110 
111  switch ($clause) {
112  case 'is':
113  $handle = new Is($this);
114 
115  break;
116 
117  case 'requires':
118  $handle = new Requires($this);
119 
120  break;
121 
122  case 'ensures':
123  if (true === $this->clauseExists('behavior')) {
124  throw new Praspel\Exception\Model(
125  'Cannot add the @ensures clause, since a @behavior ' .
126  'clause exists at the same level.',
127  1
128  );
129  }
130 
131  $handle = new Ensures($this);
132 
133  break;
134 
135  case 'throwable':
136  if (true === $this->clauseExists('behavior')) {
137  throw new Praspel\Exception\Model(
138  'Cannot add the @throwable clause, since a @behavior ' .
139  'clause exists at the same level.',
140  2
141  );
142  }
143 
144  $handle = new Throwable($this);
145 
146  break;
147 
148  case 'invariant':
149  $handle = new Invariant($this);
150 
151  break;
152 
153  case 'behavior':
154  if (true === $this->clauseExists('ensures') ||
155  true === $this->clauseExists('throwable')) {
156  throw new Praspel\Exception\Model(
157  'Cannot add the @behavior clause, since an @ensures ' .
158  'or a @throwable clause exists at the same level.',
159  3
160  );
161  }
162 
163  $handle = new Collection(
164  new self($this),
165  function (Behavior $clause, $identifier) {
166  $clause->setIdentifier($identifier);
167 
168  return;
169  }
170  );
171 
172  break;
173 
174  case 'default':
175  if (true === $this->clauseExists('ensures') ||
176  true === $this->clauseExists('throwable')) {
177  throw new Praspel\Exception\Model(
178  'Cannot add the @default clause, since an @ensures ' .
179  'or a @throwable clause exists at the same level.',
180  4
181  );
182  }
183 
184  if (false === $this->clauseExists('behavior')) {
185  throw new Praspel\Exception\Model(
186  'Cannot add a @default clause if at least one ' .
187  '@behavior clause has not been declared.',
188  5
189  );
190  }
191 
192  $handle = new DefaultBehavior($this);
193 
194  break;
195 
196  case 'description':
197  $handle = new Description($this);
198 
199  break;
200 
201  default:
202  throw new Praspel\Exception\Model(
203  'Clause @%s is unknown.',
204  6,
205  [$clause, $this->getName()]
206  );
207  }
208 
209  return $this->_clauses[$clause] = $handle;
210  }
211 
218  public function addClause(Clause $clause)
219  {
220  $name = $clause->getName();
221 
222  if (false === in_array($name, static::getAllowedClauses())) {
223  throw new Praspel\Exception\Model(
224  'Clause @%s is not allowed in @%s.',
225  7,
226  [$name, $this->getId()]
227  );
228  }
229 
230  $clause->setParent($this);
231 
232  return $this->_clauses[$name] = $clause;
233  }
234 
241  public function clauseExists($clause)
242  {
243  return isset($this->_clauses[$clause]);
244  }
245 
251  public static function getAllowedClauses()
252  {
253  return static::$_allowedClauses;
254  }
255 
261  public function setIdentifier($identifier)
262  {
263  $old = $this->_identifier;
264  $this->_identifier = $identifier;
265 
266  return $old;
267  }
268 
274  public function getIdentifier()
275  {
276  return $this->_identifier;
277  }
278 
284  protected function _getId()
285  {
286  return $this->getName() . '_' . $this->getIdentifier();
287  }
288 }
setParent(Clause $parent)
Definition: Clause.php:86
addClause(Clause $clause)
Definition: Behavior.php:218
setIdentifier($identifier)
Definition: Behavior.php:261