Example #1
0
File: Form.php Project: keradus/ker
 public static function __constructStatic()
 {
     static $isInitialized = false;
     if ($isInitialized) {
         return;
     }
     $isInitialized = true;
     static::$checks = array("isEmail" => array("check" => function (&$_value, $_rule, FormType $_type) {
         // jesli check jest ustawiony ale nieaktywny - wartosc zawsze jest poprawna wzgledem tego checka
         if (!$_rule) {
             return true;
         }
         return preg_match("/^[\\w-+]+(?:\\.[\\w-+]+)*@(?:[\\w-]+\\.)+[a-zA-Z]{2,7}\$/", $_value);
     }, "error" => function ($_rule, FormType $_type) {
         return Res::call("validate_isEmail_error", $_rule);
     }), "isInt" => array("check" => function (&$_value, $_rule, FormType $_type) {
         // jesli check jest ustawiony ale nieaktywny - wartosc zawsze jest poprawna wzgledem tego checka
         if (!$_rule) {
             return true;
         }
         return preg_match("/^[-+]?\\d+\$/", $_value);
     }, "error" => function ($_rule, FormType $_type) {
         return Res::call("validate_isInt_error", $_rule);
     }), "isFloat" => array("check" => function (&$_value, $_rule, FormType $_type) {
         // jesli check jest ustawiony ale nieaktywny - wartosc zawsze jest poprawna wzgledem tego checka
         if (!$_rule) {
             return true;
         }
         return preg_match("/^[-+]?\\d+(?:[.,]\\d+)?\$/", $_value);
     }, "error" => function ($_rule, FormType $_type) {
         return Res::call("validate_isFloat_error", $_rule);
     }), "isHttpUrl" => array("check" => function (&$_value, $_rule, FormType $_type) {
         // jesli check jest ustawiony ale nieaktywny - wartosc zawsze jest poprawna wzgledem tego checka
         if (!$_rule) {
             return true;
         }
         return preg_match('/^((?:http|https)(?::\\/{2}[\\w]+)(?:[\\/|\\.]?)(?:[^\\s"]*))$/', $_value);
     }, "error" => function ($_rule, FormType $_type) {
         return Res::call("validate_isHttpUrl_error", $_rule);
     }), "max" => array("check" => function (&$_value, $_rule, FormType $_type) {
         if ($_type->isStringType()) {
             return strlen($_value) <= $_rule;
         }
         if ($_type->equals(FormType::checkbox)) {
             return count($_value) <= $_rule;
         }
         if ($_type->equals(FormType::radio)) {
             throw new \LogicException("Radio form could has only one value, max check is illogical.");
         }
         if ($_type->equals(FormType::select)) {
             throw new \LogicException("Select form must has only one value, max check is illogical.");
         }
     }, "error" => function ($_rule, FormType $_type) {
         if ($_type->isStringType()) {
             return Res::call("validate_max_string_error", $_rule);
         }
         return Res::call("validate_max_selection_error", $_rule);
     }), "maxNumber" => array("check" => function (&$_value, $_rule, FormType $_type) {
         return $_value <= $_rule;
     }, "error" => function ($_rule, FormType $_type) {
         return Res::call("validate_maxNumber_error", $_rule);
     }), "min" => array("check" => function (&$_value, $_rule, FormType $_type) {
         if ($_type->isStringType()) {
             return strlen($_value) >= $_rule;
         }
         if ($_type->equals(FormType::checkbox)) {
             return count($_value) >= $_rule;
         }
         if ($_type->equals(FormType::radio)) {
             return isset($_value);
         }
         if ($_type->equals(FormType::select)) {
             throw new \LogicException("Select form must has only one value, min check is illogical.");
         }
         if ($_type->equals(FormType::file)) {
             return \Ker\Utils\File::fileWasSent($_value);
         }
     }, "error" => function ($_rule, FormType $_type) {
         if ($_type->isStringType()) {
             if ($_rule === 1) {
                 return Res::call("validate_empty_string_error");
             }
             return Res::call("validate_min_string_error", $_rule);
         }
         if ($_type->equals(FormType::radio)) {
             return Res::call("validate_empty_radio_error");
         }
         // TASK: #149 - wyniesc do zasobow
         if ($_type->equals(FormType::file)) {
             return "Brak pliku!";
         }
         return Res::call("validate_min_selection_error", $_rule);
     }), "minNumber" => array("check" => function (&$_value, $_rule, FormType $_type) {
         return $_value >= $_rule;
     }, "error" => function ($_rule, FormType $_type) {
         return Res::call("validate_minNumber_error", $_rule);
     }), "re" => array("check" => function (&$_value, $_rule, FormType $_type) {
         return preg_match($_rule, $_value);
     }, "error" => function ($_rule, FormType $_type) {
         return Res::call("validate_re_error");
     }), "fileReceived" => array("check" => function (&$_value, $_rule, FormType $_type) {
         // jesli nie przeslano pliku - nie sprawdzamy
         if (!\Ker\Utils\File::fileWasSent($_value)) {
             return true;
         }
         return \Ker\Utils\File::fileWasReceived($_value);
     }, "error" => function ($_rule, FormType $_type) {
         return "Błąd przesyłania pliku!";
     }), "fileTypes" => array("check" => function (&$_value, $_rule, FormType $_type) {
         // jesli nie przeslano pliku - nie sprawdzamy
         if (!\Ker\Utils\File::fileWasSent($_value)) {
             return true;
         }
         $types = explode(" ", $_rule);
         return \Ker\Utils\File::typeIsAcceptable($_value, $types);
     }, "error" => function ($_rule, FormType $_type) {
         $types = explode(" ", $_rule);
         return "Niewłaściwy format pliku! Możliwe formaty: " . implode(", ", $types);
     }));
     static::$hooksPredefined = array("htmlCleaner" => function (&$_items) {
         foreach ($_items as &$item) {
             if ($item["type"]->isTextType() && (!isset($item["allowHtml"]) || !$item["allowHtml"])) {
                 $item["value"] = strip_tags($item["value"]);
             }
         }
     }, "textTrim" => function (&$_items) {
         foreach ($_items as &$item) {
             if ($item["type"]->isTextType()) {
                 $item["value"] = trim($item["value"]);
             }
         }
     }, "normalizeEmail" => function (&$_items) {
         foreach ($_items as &$item) {
             if (isset($item["check"]) && isset($item["check"]["isEmail"]) && $item["check"]["isEmail"]) {
                 $item["value"] = strtolower($item["value"]);
             }
         }
     }, "normalizeFloat" => function (&$_items) {
         foreach ($_items as &$item) {
             if (isset($item["check"]) && isset($item["check"]["isFloat"]) && $item["check"]["isFloat"]) {
                 $item["value"] = str_replace(",", ".", $item["value"]);
             }
         }
     });
 }