Compare commits

..
10 Commits
4 changed files with 174 additions and 56 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
{ {
"name": "urfat/rr", "name": "inforsistemas/rr",
"description": "Requests and responses", "description": "Requests and responses",
"type": "library", "type": "library",
"authors": [ "authors": [
+65 -26
View File
@@ -6,12 +6,12 @@ class Flash{
private static $_instance; private static $_instance;
// Message types and shortcuts // Message types and shortcuts
const INFO = 'i'; const FLASH_INFO = 'i';
const SUCCESS = 's'; const FLASH_SUCCESS = 's';
const WARNING = 'w'; const FLASH_WARNING = 'w';
const ERROR = 'e'; const FLASH_ERROR = 'e';
const SESSION_NAME = 'flashes'; const FLASH_SESSION_NAME = 'flashes';
const defaultType = self::INFO; const defaultType = self::INFO;
@@ -22,8 +22,8 @@ class Flash{
self::INFO => 'info', self::INFO => 'info',
]; ];
public $old_messages = Array(); public $oldMessages = Array();
public $new_messages = Array(); public $newMessages = Array();
public $_flashes = Array(); public $_flashes = Array();
//SINGLETON============================================== //SINGLETON==============================================
@@ -33,7 +33,7 @@ class Flash{
session_start(); session_start();
} }
if( isset($_SESSION['flashes']) ){ if( isset($_SESSION['flashes']) ){
$this->old_messages = $_SESSION['flashes']; $this->oldMessages = $_SESSION['flashes'];
} }
unset( $_SESSION[self::SESSION_NAME] ); unset( $_SESSION[self::SESSION_NAME] );
} }
@@ -55,46 +55,80 @@ class Flash{
//======================================================= //=======================================================
public function merge(){ 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(){ public function loadToSession(){
$_SESSION[self::SESSION_NAME] = $this->new_messages; $_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 = self::getInstance();
$instance->new_messages[$type] = $message; $instance->newMessages[$type] = $message;
$instance->load_to_session(); $instance->loadToSession();
} }
public static function add_info($message){ /**
*
* @ctag Flash::addInfo('');
*
*/
public static function addInfo($message){
$instance = self::getInstance(); $instance = self::getInstance();
$instance->new_messages[self::INFO] = $message; $instance->newMessages[self::INFO] = $message;
$instance->load_to_session(); $instance->loadToSession();
} }
public static function add_error($message){ /**
*
* @ctag Flash::addError('');
*
*/
public static function addError($message){
$instance = self::getInstance(); $instance = self::getInstance();
$instance->new_messages[self::ERROR] = $message; $instance->newMessages[self::ERROR] = $message;
$instance->load_to_session(); $instance->loadToSession();
} }
public static function get_errors(){ /**
if(!self::has_errors()){ *
* @ctag Flash::getErrors();
*
*/
public static function getErrors(){
if(!self::hasErrors()){
return Array(); return Array();
} }
return self::getInstance()->_flashes[self::ERROR]; 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 Array();
} }
return self::getInstance()->_flashes[self::INFO]; return self::getInstance()->_flashes[self::INFO];
} }
public static function has_errors(){ /**
*
* @ctag Flash::addErrors();
*
*/
public static function hasErrors(){
$instance = self::getInstance(); $instance = self::getInstance();
$instance->merge(); $instance->merge();
if( array_key_exists(self::ERROR, $instance->_flashes ) ){ 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 = self::getInstance();
$instance->merge(); $instance->merge();
if( array_key_exists(self::INFO, $instance->_flashes ) ){ if( array_key_exists(self::INFO, $instance->_flashes ) ){
+27 -8
View File
@@ -10,7 +10,7 @@ class Request{
const PARAM_INT = 'integer'; const PARAM_INT = 'integer';
const PARAM_DOUBLE = 'double'; 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. // POST has precedence.
if (isset($_POST[$parname])) { if (isset($_POST[$parname])) {
$param = $_POST[$parname]; $param = $_POST[$parname];
@@ -19,21 +19,36 @@ class Request{
} else { } else {
return $default; return $default;
} }
/*$param = self::filter($type, $param);
$param = self::filter($type, $param);
foreach ($options as $key => $option){ foreach ($options as $key => $option){
self::$key($param, $option); self::$key($param, $option);
} }*/
return $param; return $param;
} }
public static function requiredParamArray($parname, $type, $default = '', $options = Array() ){ 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(){ public static function optionalParamArray(){
@@ -53,7 +68,11 @@ class Request{
} }
break; break;
case 'alpha': case 'alpha':
return $paramname;
break;
case 'alphanum':
return $paramname;
break;
} }
} }
+77 -17
View File
@@ -29,14 +29,14 @@ class Response{
private function __construct(){ private function __construct(){
} }
private static function newObj(){ private static function newObj() : Response {
if (!isset( self::$_instance )) { if (!isset( self::$_instance )) {
self::$_instance = new Response(); self::$_instance = new Response();
} }
return self::$_instance; return self::$_instance;
} }
public function getInstance(){ public static function getInstance() : Response {
if (!isset(self::$_instance)) { if (!isset(self::$_instance)) {
return self::newObj(); return self::newObj();
} }
@@ -45,13 +45,23 @@ class Response{
//======================================================= //=======================================================
/**
*
* @ctag Response::header($key, $value);
*
*/
public static function header($key, $value){ public static function header($key, $value){
$istance = self::getInstance(); $istance = self::getInstance();
$istance->_headers[$key] = $value; $istance->_headers[$key] = $value;
return $istance; return $istance;
} }
public static function body_prepend($body = null){ /**
*
* @ctag Response::bodyPrepend($value);
*
*/
public static function bodyPrepend($body = null){
$instance = self::getInstance(); $instance = self::getInstance();
if (null !== $body) { if (null !== $body) {
$instance->_body = (string) $body . $instance->_body; $instance->_body = (string) $body . $instance->_body;
@@ -60,6 +70,11 @@ class Response{
return $instance; return $instance;
} }
/**
*
* @ctag Response::body($value);
*
*/
public static function body($body = null){ public static function body($body = null){
$instance = self::getInstance(); $instance = self::getInstance();
if (null !== $body) { if (null !== $body) {
@@ -69,7 +84,12 @@ class Response{
return $instance; return $instance;
} }
public static function body_append($body = null){ /**
*
* @ctag Response::bodyAppend($value);
*
*/
public static function bodyAppend($body = null){
$instance = self::getInstance(); $instance = self::getInstance();
if (null !== $body) { if (null !== $body) {
$instance->_body .= (string) $body; $instance->_body .= (string) $body;
@@ -78,51 +98,91 @@ class Response{
return $instance; return $instance;
} }
private function send_headers(){ private function sendHeaders(){
foreach ($this->_headers as $key => $value) { foreach ($this->_headers as $key => $value) {
header($key .': '. $value, false); header($key .': '. $value, true);
} }
return $this; return $this;
} }
private function send_body(){ private function sendBody(){
if(!empty($this->_body)){
echo (string) $this->_body; echo (string) $this->_body;
}
return $this; return $this;
} }
/**
*
* @ctag Response::bodySend();
*
*/
public static function send(){ public static function send(){
$instance = self::getInstance(); $instance = self::getInstance();
$instance->send_headers(); $instance->sendHeaders();
$instance->send_body(); $instance->sendBody();
http_response_code($instance->_code);
return $instance; 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 = self::getInstance();
$instance->body(''); $instance->body('');
$json = $object;
if(!is_string($object)){
$json = json_encode($object); $json = json_encode($object);
if (null !== $jsonp_prefix) { }
if (null !== $jsonPrefix) {
$instance->header('Content-Type', 'text/javascript'); $instance->header('Content-Type', 'text/javascript');
$instance->body("$jsonp_prefix($json);"); $instance->body("$jsonPrefix($json);");
} else { } else {
$instance->header('Content-Type', 'application/json'); $instance->header('Content-Type', 'application/json');
$instance->body($json); $instance->body($json);
} }
return $instance; 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 = self::getInstance();
$instance->_code = $code; $instance->_code = $code;
$instance->header('Location', $url);
$instance->body(''); if(is_ajax_request()){
$instance->send(); 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(){ public static function back(){
$instance = self::getInstance(); $instance = self::getInstance();
@@ -132,7 +192,7 @@ class Response{
$instance->send(); $instance->send();
} }
function done(){ public function done(){
die; die;
} }
} }