Ejemplo n.º 1
0
 /**
  *
  * The flush Function updates the database with the requiered changes or insert an new entry
  *
  * @return void
  */
 public function flush($entityObject = '')
 {
     if ($entityObject) {
         $this->entityObject = $entityObject;
     }
     //init the entityFirst
     $entityFirst = $this->entityFirst;
     if (!empty($this->query)) {
         //when the query isn't empty then it creates an new dbEntry
         //send the request
         $this->db->query($this->query) or die('It went something wrong with the DataBase');
     } elseif (is_object($this->entityObject)) {
         //when it is an object, then it is one Entity to update in the database
         //parse the entityObject in an Array and update the changes in database
         $cleanEntityObject = $this->cleanEntityObject($this->entityObject);
         foreach ($cleanEntityObject as $key => $value) {
             if ($value !== $entityFirst[$key]) {
                 $query = "UPDATE {$this->dbUser}.{$this->entityObjectName} SET {$key} = '{$cleanEntityObject[$key]}' WHERE ID = {$cleanEntityObject['id']} ";
                 $request = $this->db->query($query) or die('It went something wrong with the DataBase');
             }
         }
     } elseif (is_array($this->entityObject)) {
         //when it is an array, then it has more then one entity to update the database
         //parse every entityObjects in Arrays and save it in an 2dimensional Array then UPDATE all changes
         $entityObject = array();
         foreach ($this->entityObject as $key => $value) {
             $entityObject[] = $this->cleanEntityObject($value);
         }
         foreach ($entityFirst as $key => $value) {
             foreach ($value as $key2 => $value2) {
                 if ($value2 !== $entityObject[$key][$key2]) {
                     $query = "UPDATE {$this->dbUser}.{$this->entityObjectName} SET {$key2} = '" . $entityObject[$key][$key2] . "' WHERE ID = " . $value['ID'];
                     $request = $this->db->query($query) or die('It went something wrong with the DataBase');
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 /**
  * Actual validation of request with rules implied
  * from its child classes.
  *
  * @param null $route
  * @return bool|void
  */
 public function validate($route = null)
 {
     for ($i = 0; $i < count($this->request); $i++) {
         $field = array_keys($this->request);
         if (array_key_exists($field[$i], $this->rules)) {
             $rule = explode('|', $this->rules[$field[$i]]);
             for ($z = 0; $z < count($rule); $z++) {
                 if ($rule[$z] == 'required') {
                     if (strlen($this->request[$field[$i]]) == 0) {
                         $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " is required.";
                         break;
                     }
                 }
                 if (preg_match('/unique/i', $rule[$z])) {
                     $db = new Database();
                     $value = $this->request[$field[$i]];
                     foreach ($rule as $item) {
                         if ($item == 'password') {
                             $value = Hash::encode($value);
                             break;
                         }
                     }
                     if ($db->table(explode(':', $rule[$z])[1])->where($field[$i], $value)->exists()) {
                         $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " not available.";
                         break;
                     }
                 }
                 if ($rule[$z] == 'email') {
                     if (!preg_match('/@/', $this->request[$field[$i]])) {
                         $this->errors[$field[$i]] = "Enter a valid e-mail.";
                         break;
                     }
                 }
                 if ($rule[$z] == 'alphanumeric') {
                     if (!preg_match('/[^A-Za-z0-9]/i', $this->request[$field[$i]])) {
                         $this->errors[$field[$i]] = "Only alphanumeric characters are allowed.";
                         break;
                     }
                 }
                 if ($rule[$z] == 'letters') {
                     if (!preg_match('/^[A-Za-z]/i', $this->request[$field[$i]])) {
                         $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . "  accepts letters only.";
                         break;
                     }
                 }
                 if ($rule[$z] == 'number' || $rule[$z] == 'numeric') {
                     if (!preg_match('/[0-9]/', $this->request[$field[$i]])) {
                         $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . "  should be numeric.";
                         break;
                     }
                 }
                 if (preg_match('/match/i', $rule[$z])) {
                     $compare = explode(':', $rule[$z])[1];
                     if ($this->request[$field[$i]] !== $this->request[$compare]) {
                         $this->errors[$field[$i]] = "Field did not match to {$compare}.";
                         $this->errors[$compare] = "Field did not match to {$field[$i]}.";
                         break;
                     }
                 }
                 if (preg_match('/min/i', $rule[$z])) {
                     $min = explode(':', $rule[$z])[1];
                     if (strlen($this->request[$field[$i]]) < $min) {
                         $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " requires a minimum of {$min} characters.";
                         break;
                     }
                 }
                 if (preg_match('/max/i', $rule[$z])) {
                     $max = explode(':', $rule[$z])[1];
                     if (strlen($this->request[$field[$i]]) > $max) {
                         $this->errors[$field[$i]] = str_replace('_', ' ', $field[$i]) . " requires a maximum of {$max} characters.";
                         break;
                     }
                 }
             }
         }
     }
     $fileRules = array_keys($this->rules);
     for ($f = 0; $f < count($fileRules); $f++) {
         if (array_key_exists($fileRules[$f], $_FILES)) {
             if (empty($_FILES[$fileRules[$f]]['name'])) {
                 $this->errors[$fileRules[$f]] = str_replace('_', ' ', $fileRules[$f]) . " is required.";
             }
         }
     }
     $redirectRoute = !is_null($route) ? $route : $this->route;
     if (is_null($this->errors)) {
         return true;
     } else {
         $_SESSION['__ERRORS__'] = $this->errors;
         $_SESSION['__FIELDS__'] = $this->request;
         return header("location: {$redirectRoute}");
     }
 }