Compare commits
10
Commits
a87e54e687
...
b4e8758d5d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4e8758d5d | ||
|
|
73843d4935 | ||
|
|
743ad02060 | ||
|
|
a8089e483b | ||
|
|
189c69d2aa | ||
|
|
87e1934a58 | ||
|
|
ebd8d8f439 | ||
|
|
ec8d551a7d | ||
|
|
a0f2928469 | ||
|
|
2c4a82b782 |
+1
-1
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"name": "urfat/rr",
|
||||
"name": "inforsistemas/rr",
|
||||
"description": "Requests and responses",
|
||||
"type": "library",
|
||||
"authors": [
|
||||
|
||||
+65
-26
@@ -6,12 +6,12 @@ class Flash{
|
||||
private static $_instance;
|
||||
|
||||
// Message types and shortcuts
|
||||
const INFO = 'i';
|
||||
const SUCCESS = 's';
|
||||
const WARNING = 'w';
|
||||
const ERROR = 'e';
|
||||
const FLASH_INFO = 'i';
|
||||
const FLASH_SUCCESS = 's';
|
||||
const FLASH_WARNING = 'w';
|
||||
const FLASH_ERROR = 'e';
|
||||
|
||||
const SESSION_NAME = 'flashes';
|
||||
const FLASH_SESSION_NAME = 'flashes';
|
||||
|
||||
const defaultType = self::INFO;
|
||||
|
||||
@@ -22,8 +22,8 @@ class Flash{
|
||||
self::INFO => 'info',
|
||||
];
|
||||
|
||||
public $old_messages = Array();
|
||||
public $new_messages = Array();
|
||||
public $oldMessages = Array();
|
||||
public $newMessages = Array();
|
||||
public $_flashes = Array();
|
||||
|
||||
//SINGLETON==============================================
|
||||
@@ -33,7 +33,7 @@ class Flash{
|
||||
session_start();
|
||||
}
|
||||
if( isset($_SESSION['flashes']) ){
|
||||
$this->old_messages = $_SESSION['flashes'];
|
||||
$this->oldMessages = $_SESSION['flashes'];
|
||||
}
|
||||
unset( $_SESSION[self::SESSION_NAME] );
|
||||
}
|
||||
@@ -55,46 +55,80 @@ class Flash{
|
||||
//=======================================================
|
||||
|
||||
public function merge(){
|
||||
$this->_flashes = array_merge_recursive($this->new_messages, $this->old_messages);
|
||||
$this->_flashes = array_merge_recursive($this->newMessages, $this->oldMessages);
|
||||
}
|
||||
|
||||
public function load_to_session(){
|
||||
$_SESSION[self::SESSION_NAME] = $this->new_messages;
|
||||
public function loadToSession(){
|
||||
$_SESSION[self::SESSION_NAME] = $this->newMessages;
|
||||
}
|
||||
|
||||
public static function add_message($type = self::defaultType, $message){
|
||||
/*
|
||||
*
|
||||
* @ctag Flash:addMessage();
|
||||
* @ctag Flash:addMessage(Flash::FLASH_INFO, '');
|
||||
* @ctag Flash:addMessage(Flash::FLASH_SUCCESS, '');
|
||||
* @ctag Flash:addMessage(Flash::FLASH_WARNING, '');
|
||||
* @ctag Flash:addMessage(Flash::FLASH_ERROR, '');
|
||||
*
|
||||
*/
|
||||
public static function addMessage($type = self::defaultType, $message){
|
||||
$instance = self::getInstance();
|
||||
$instance->new_messages[$type] = $message;
|
||||
$instance->load_to_session();
|
||||
$instance->newMessages[$type] = $message;
|
||||
$instance->loadToSession();
|
||||
}
|
||||
|
||||
public static function add_info($message){
|
||||
/**
|
||||
*
|
||||
* @ctag Flash::addInfo('');
|
||||
*
|
||||
*/
|
||||
public static function addInfo($message){
|
||||
$instance = self::getInstance();
|
||||
$instance->new_messages[self::INFO] = $message;
|
||||
$instance->load_to_session();
|
||||
$instance->newMessages[self::INFO] = $message;
|
||||
$instance->loadToSession();
|
||||
}
|
||||
|
||||
public static function add_error($message){
|
||||
/**
|
||||
*
|
||||
* @ctag Flash::addError('');
|
||||
*
|
||||
*/
|
||||
public static function addError($message){
|
||||
$instance = self::getInstance();
|
||||
$instance->new_messages[self::ERROR] = $message;
|
||||
$instance->load_to_session();
|
||||
$instance->newMessages[self::ERROR] = $message;
|
||||
$instance->loadToSession();
|
||||
}
|
||||
|
||||
public static function get_errors(){
|
||||
if(!self::has_errors()){
|
||||
/**
|
||||
*
|
||||
* @ctag Flash::getErrors();
|
||||
*
|
||||
*/
|
||||
public static function getErrors(){
|
||||
if(!self::hasErrors()){
|
||||
return Array();
|
||||
}
|
||||
return self::getInstance()->_flashes[self::ERROR];
|
||||
}
|
||||
|
||||
public static function get_infos(){
|
||||
if(!self::has_info()){
|
||||
/**
|
||||
*
|
||||
* @ctag Flash::addInfos();
|
||||
*
|
||||
*/
|
||||
public static function getInfos(){
|
||||
if(!self::hasInfo()){
|
||||
return Array();
|
||||
}
|
||||
return self::getInstance()->_flashes[self::INFO];
|
||||
}
|
||||
|
||||
public static function has_errors(){
|
||||
/**
|
||||
*
|
||||
* @ctag Flash::addErrors();
|
||||
*
|
||||
*/
|
||||
public static function hasErrors(){
|
||||
$instance = self::getInstance();
|
||||
$instance->merge();
|
||||
if( array_key_exists(self::ERROR, $instance->_flashes ) ){
|
||||
@@ -104,7 +138,12 @@ class Flash{
|
||||
}
|
||||
}
|
||||
|
||||
public static function has_info(){
|
||||
/**
|
||||
*
|
||||
* @ctag Flash::hasInfo();
|
||||
*
|
||||
*/
|
||||
public static function hasInfo(){
|
||||
$instance = self::getInstance();
|
||||
$instance->merge();
|
||||
if( array_key_exists(self::INFO, $instance->_flashes ) ){
|
||||
|
||||
+27
-8
@@ -10,7 +10,7 @@ class Request{
|
||||
const PARAM_INT = 'integer';
|
||||
const PARAM_DOUBLE = 'double';
|
||||
|
||||
public static function requiredParam($parname, $type, $options = Array(), $default = ''){
|
||||
public static function requiredParam($parname, $type, $default = '', $options = Array()){
|
||||
// POST has precedence.
|
||||
if (isset($_POST[$parname])) {
|
||||
$param = $_POST[$parname];
|
||||
@@ -19,21 +19,36 @@ class Request{
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$param = self::filter($type, $param);
|
||||
/*$param = self::filter($type, $param);
|
||||
|
||||
foreach ($options as $key => $option){
|
||||
self::$key($param, $option);
|
||||
}
|
||||
}*/
|
||||
return $param;
|
||||
}
|
||||
|
||||
public static function requiredParamArray($parname, $type, $default = '', $options = Array() ){
|
||||
|
||||
// POST has precedence.
|
||||
if (isset($_POST[$parname])) {
|
||||
$param = $_POST[$parname];
|
||||
} else if (isset($_GET[$parname])) {
|
||||
$param = $_GET[$parname];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
return $param;
|
||||
}
|
||||
|
||||
public static function optionalParam(){
|
||||
|
||||
public static function optionalParam($parname, $type, $default = '', $options = Array()){
|
||||
// POST has precedence.
|
||||
if (isset($_POST[$parname])) {
|
||||
$param = $_POST[$parname];
|
||||
} else if (isset($_GET[$parname])) {
|
||||
$param = $_GET[$parname];
|
||||
} else {
|
||||
return $default;
|
||||
}
|
||||
return $param;
|
||||
}
|
||||
|
||||
public static function optionalParamArray(){
|
||||
@@ -53,7 +68,11 @@ class Request{
|
||||
}
|
||||
break;
|
||||
case 'alpha':
|
||||
|
||||
return $paramname;
|
||||
break;
|
||||
case 'alphanum':
|
||||
return $paramname;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+77
-17
@@ -29,14 +29,14 @@ class Response{
|
||||
private function __construct(){
|
||||
}
|
||||
|
||||
private static function newObj(){
|
||||
private static function newObj() : Response {
|
||||
if (!isset( self::$_instance )) {
|
||||
self::$_instance = new Response();
|
||||
}
|
||||
return self::$_instance;
|
||||
}
|
||||
|
||||
public function getInstance(){
|
||||
public static function getInstance() : Response {
|
||||
if (!isset(self::$_instance)) {
|
||||
return self::newObj();
|
||||
}
|
||||
@@ -45,13 +45,23 @@ class Response{
|
||||
|
||||
//=======================================================
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag Response::header($key, $value);
|
||||
*
|
||||
*/
|
||||
public static function header($key, $value){
|
||||
$istance = self::getInstance();
|
||||
$istance->_headers[$key] = $value;
|
||||
return $istance;
|
||||
}
|
||||
|
||||
public static function body_prepend($body = null){
|
||||
/**
|
||||
*
|
||||
* @ctag Response::bodyPrepend($value);
|
||||
*
|
||||
*/
|
||||
public static function bodyPrepend($body = null){
|
||||
$instance = self::getInstance();
|
||||
if (null !== $body) {
|
||||
$instance->_body = (string) $body . $instance->_body;
|
||||
@@ -60,6 +70,11 @@ class Response{
|
||||
return $instance;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag Response::body($value);
|
||||
*
|
||||
*/
|
||||
public static function body($body = null){
|
||||
$instance = self::getInstance();
|
||||
if (null !== $body) {
|
||||
@@ -69,7 +84,12 @@ class Response{
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function body_append($body = null){
|
||||
/**
|
||||
*
|
||||
* @ctag Response::bodyAppend($value);
|
||||
*
|
||||
*/
|
||||
public static function bodyAppend($body = null){
|
||||
$instance = self::getInstance();
|
||||
if (null !== $body) {
|
||||
$instance->_body .= (string) $body;
|
||||
@@ -78,51 +98,91 @@ class Response{
|
||||
return $instance;
|
||||
}
|
||||
|
||||
private function send_headers(){
|
||||
private function sendHeaders(){
|
||||
foreach ($this->_headers as $key => $value) {
|
||||
header($key .': '. $value, false);
|
||||
header($key .': '. $value, true);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function send_body(){
|
||||
private function sendBody(){
|
||||
if(!empty($this->_body)){
|
||||
echo (string) $this->_body;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag Response::bodySend();
|
||||
*
|
||||
*/
|
||||
public static function send(){
|
||||
$instance = self::getInstance();
|
||||
$instance->send_headers();
|
||||
$instance->send_body();
|
||||
$instance->sendHeaders();
|
||||
$instance->sendBody();
|
||||
http_response_code($instance->_code);
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function json($object, $jsonp_prefix = null){
|
||||
/**
|
||||
*
|
||||
* @ctag Response::json($object);
|
||||
* @ctag Response::json($object, $prefix);
|
||||
*
|
||||
*/
|
||||
public static function json($object, $jsonPrefix = null): Response{
|
||||
$instance = self::getInstance();
|
||||
|
||||
$instance->body('');
|
||||
|
||||
$json = $object;
|
||||
if(!is_string($object)){
|
||||
$json = json_encode($object);
|
||||
if (null !== $jsonp_prefix) {
|
||||
}
|
||||
|
||||
if (null !== $jsonPrefix) {
|
||||
$instance->header('Content-Type', 'text/javascript');
|
||||
$instance->body("$jsonp_prefix($json);");
|
||||
$instance->body("$jsonPrefix($json);");
|
||||
} else {
|
||||
$instance->header('Content-Type', 'application/json');
|
||||
$instance->body($json);
|
||||
}
|
||||
|
||||
return $instance;
|
||||
}
|
||||
|
||||
public static function redirect($url, $code = 302){
|
||||
/**
|
||||
*
|
||||
* @ctag Response::redirect($url);
|
||||
* @ctag Response::redirect($url, $code);
|
||||
*
|
||||
*/
|
||||
public static function redirect($url, $code = 303){
|
||||
$instance = self::getInstance();
|
||||
|
||||
$instance->_code = $code;
|
||||
$instance->header('Location', $url);
|
||||
|
||||
$instance->body('');
|
||||
$instance->send();
|
||||
if(is_ajax_request()){
|
||||
return $instance->json(['location' => $url])->send();
|
||||
}
|
||||
|
||||
## TODO
|
||||
header("Location: $url");
|
||||
die;
|
||||
|
||||
$instance->header('Location: ', $url)->send();
|
||||
|
||||
return $instance->send()->done();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @ctag Response::back();
|
||||
*
|
||||
*/
|
||||
public static function back(){
|
||||
$instance = self::getInstance();
|
||||
|
||||
@@ -132,7 +192,7 @@ class Response{
|
||||
$instance->send();
|
||||
}
|
||||
|
||||
function done(){
|
||||
public function done(){
|
||||
die;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user