zendframework/zf2 — pull request #5365 by texdc (2013-10-27 22:17:18+00:00)
Title: [stdlib] Add guard utils and traits

The check for `array` or `Traversable` code is littered throughout the framework. This is a prime opportunity for a trait. Included a couple extra traits for good measure.

For example, https://github.com/zendframework/zf2/blob/master/library/Zend/Form/Fieldset.php#L297 could be:

```php
class Fieldset extends Element implements FieldsetInterface
{
use \Zend\Stdlib\Guard\ArrayOrTraversableGuardTrait;

// ...

public function setMessages($messages)
{
// php 5.4
$this->guardForArrayOrTraversable(
$messages, 'Messages', 'Zend\Form\Exception\InvalidArgumentException'
);

// or php 5.3
\Zend\Stdlib\Guard\GuardUtils::guardForArrayOrTraversable(
$messages, 'Messages', 'Zend\Form\Exception\InvalidArgumentException'
);

foreach ($messages as $elementName => $elementMessages) {
$this->get($elementName)->setMessages($elementMessages);
}

return $this;
}

// ...

}
```

Please see http://verraes.net/2013/09/extract-till-you-drop/
Note: resubmitting to clean up failed rebase.