/**
  * Resolve new csrf token against provided action
  * @param $action
  * @return mixed
  */
 public static function getNewToken($action)
 {
     // create new csrf model instance, set its action, rest would be auto-populated using default validators
     $csrf = new Csrf();
     $csrf->action = $action;
     if (!$csrf->save(true, false)) {
         WebApplication::exitWithException(new \Exception('Unable to generate csrf token', 400));
     }
     return $csrf->key;
 }
 /**
  * @param $value
  */
 public function resolvePDODatabaseTypeByValue($value)
 {
     if (!is_scalar($value) && !is_null($value)) {
         \GGS\Components\WebApplication::exitWithException(new \Exception('Provided value can not be mapped to PDO data type', 400));
     }
     $dataType = \PDO::PARAM_STR;
     if (is_numeric($value)) {
         $dataType = \PDO::PARAM_INT;
     } else {
         if (is_bool($value)) {
             $dataType = \PDO::PARAM_BOOL;
         } else {
             if (is_null($value)) {
                 $dataType = \PDO::PARAM_NULL;
             }
         }
     }
     return $dataType;
 }
 /**
  * A wrapper around WebApplication's function with same name but with different arguments
  * @param $message
  * @param null $code
  */
 protected static function exitWithException($message, $code = null)
 {
     WebApplication::exitWithException(new \Exception($message, $code));
 }
Пример #4
0
 /**
  * Save current model object
  * @param bool $validate
  * @param bool $throwExceptionOnFailure
  */
 public function save($validate = true, $throwExceptionOnFailure = true)
 {
     if ($validate && !$this->validate()) {
         // wanted to validate but validation failed? bail out.
         return false;
     }
     // call beforeSave hook to handle special processes
     $this->beforeSave();
     // call create() if record is new, update() if it already exists
     $saved = $this->isNew() ? $this->create() : $this->update();
     if (!$saved) {
         if ($throwExceptionOnFailure) {
             // woah, database bailed on app or was there an issue with the query?
             WebApplication::exitWithException(new \Exception("Unable to save record"));
         } else {
             return false;
         }
     }
     // call the after save hook only if record has been saved.
     $this->afterSave();
     // return the primary key value
     return $this->getPkValue();
 }