Example #1
0
 /**
  * @param array|\Traversable $subject
  * @param mixed $value
  *
  * @return bool
  */
 public static function contain($subject, $value)
 {
     if (is_array($subject)) {
         return in_array($value, $subject, true);
     }
     if ($subject instanceof \Traversable) {
         foreach ($subject as $entry) {
             if ($entry === $value) {
                 return true;
             }
         }
         return false;
     }
     throw new \InvalidArgumentException(sprintf('Subject of type "%s" is not iterable.', TypeHelper::getType($subject)));
 }
Example #2
0
 /**
  * @param mixed $key
  *
  * @return mixed
  * @throws InvalidKeyTypeException
  */
 private function convertKeyToHash(&$key)
 {
     if (is_scalar($key)) {
         return $key;
     }
     if (is_object($key) && method_exists($key, '__toString')) {
         return (string) $key;
     }
     throw new InvalidKeyTypeException(TypeHelper::getType($key));
 }
Example #3
0
 /**
  * @param string    $var
  * @param string    $expectedType
  * @param int       $code
  * @param Exception $previous
  */
 public function __construct($var, $expectedType, $code = 0, Exception $previous = null)
 {
     parent::__construct(sprintf('Expected type "%s", but "%s" given.', $expectedType, TypeHelper::getType($var)), $code, $previous);
 }
Example #4
0
 /**
  * @param mixed $element
  *
  * @return bool
  */
 private function isValidType($element)
 {
     return TypeHelper::isOfType($element, $this->type);
 }