Преглед на файлове

introduce VariableHelper::isInstanceOf

alexlcdee преди 8 години
родител
ревизия
8a41357168
променени са 1 файла, в които са добавени 14 реда и са изтрити 0 реда
  1. 14 0
      src/helpers/VariableHelper.php

+ 14 - 0
src/helpers/VariableHelper.php

@@ -23,6 +23,20 @@ class VariableHelper
         }
     }
 
+    /**
+     * Checks if variable is instance of className. Throws exception if not
+     * @param mixed $variable
+     * @param string $className
+     * @param string $message
+     * @throws InvalidConfigException
+     */
+    public static function isInstanceOf($variable, $className, $message = null)
+    {
+        if (!($variable instanceof $className)) {
+            throw new InvalidConfigException($message, sprintf('Variable must be instance of class %s', $className));
+        }
+    }
+
     /**
      * Checks if variable is null and throws InvalidConfigException if it is null
      * @param mixed $variable variable to check