Example #1
0
 /**
  * Checks the type of the arguments of the method caller with the type passed
  * in <i>$dataTypes</i>.<br>
  * If the number of elements in <i>$dataTypes</i> isn't the same of the number
  * of the arguments of the caller, it will throw an exception.<br>
  * If the type mismatch then it will be throw a {@link MWrongTypeException MWrongTypeException}.
  *
  * @param array|int $dataTypes,... unlimited OPTIONAL The possible values of the elements of the array are: case
  *                                 MDataType::INT, MDataType::LONG, MDataType::BOOLEAN, MDataType::FLOAT: return
  *                                 "MDataType::FLOAT, MDataType::DOUBLE, MDataType::STRING, MDataType::NULL,
  *                                 MDataType::__ARRAY, MDataType::OBJECT, MDataType::RESOURCE, MDataType::MIXED
  * @throws \Exception|MWrongTypeException
  */
 public static function mustBe($dataTypes)
 {
     $params = func_get_args();
     if (isset($params[0]) && is_array($params[0])) {
         $dataTypeArray = $params[0];
     } else {
         $dataTypeArray = $params;
     }
     $trace = debug_backtrace();
     $caller = $trace[1];
     $args = $caller["args"];
     for ($i = 0; $i < count($args); $i++) {
         $dataType = $dataTypeArray[$i];
         if ($dataType === MDataType::MIXED || $dataType === MDataType::OBJECT) {
             continue;
         }
         if ($dataType & MDataType::getType($args[$i])) {
             continue;
         }
         $callerName = isset($caller["class"]) ? $caller["class"] . "::" . $caller["function"] : $caller["function"];
         $mustBe = MDataType::getTypeName($dataType);
         $itIs = MDataType::getTypeName(MDataType::getType($args[$i]));
         $message = "Argument " . ($i + 1) . " passed to " . $callerName . " must be of the type " . $mustBe . ", " . $itIs . " given, called in " . $caller["file"] . " on line " . $caller["line"] . " and defined";
         throw new MWrongTypeException($message);
     }
 }