Wallogit.com
2017 © Pedro Peláez
A collection of useful libraries to make life easier.
A Set of lightweight PHP libraries to make your life easier., (*1)
FAQ: Why the name Sangria?, (*2)
Ans: Because I like this song and I like to drink/eat fruits., (*3)
"gokulsrinivas/sangria":"dev-master" to "require" in your composer.json. composer install
include "vendor/autoload.php" in your file. vendor is the folder that is created after the composer install.use Sangria\JSONResponse / IMAPAuth / LDAPAuth / HelperFunctions
Sangria\JSONResponse
This class is helpful to API Developers who need to return JSON representations of strings, objects, collections etc., (*4)
This class follows the following JSON structure., (*5)
{
"status_code": 200,
"message": "OK"
}
use Sangria\JSONResponse;
JSONResponse::response($status_code=400,$message=NULL,$strict_mode=false)
$status_code can be HTTP status codes or your own custom status codes.$status_code is a HTTP status code and the message is NULL, a default
reason phrase is added. (If $strict_mode is false (default) )$strict_mode is set to true, the data given as $message is encoded.
So, NULL is encoded as null.$status_code defaults to 400. Sangria\JSONResponse can be safely used in Laravel as all eloquent models and collections can be passed as $message.Sample Code, (*6)
<?php
use Sangria\JSONResponse;
$status_code = 200;
$message = new StdClass();
$message->prop1 = "asdf";
$message->prop2 = array("asdf");
$message->prop3 = array(array("asdf"));
$message->prop4 = true;
$response_string = JSONResponse::response($status_code,$message);
echo $response_string;
Output, (*7)
{
"status_code": 200,
"message": {
"prop1": "asdf",
"prop2": [
"asdf"
],
"prop3": [
[
"asdf"
]
],
"prop4": true
}
}
Sangria\IMAPAuth
This class is useful for developers who just need to authenticate a username/password combo via IMAP., (*8)
use Sangria\IMAPAuth;
IMAPAuth::auth($user_name,$user_pass,$imap_option="/imap/ssl/novalidate-cert",$imap_retries=0)
$imap_option is a string beginning with / listed as Optional flags for names as given in the page for imap on php.net.$imap_retries is the number of retries.Configuration, (*9)
__SANGRIA_IMAP_SERVER_ADDR__ and __SANGRIA_IMAP_SERVER_PORT__ to the desired address and port in config.php in vendor/gokulsrinivas/sangria if installed via composer.Sample Code, (*10)
<?php
use Sangria\IMAPAuth;
if(IMAPAuth::auth('username','********'))
{
echo "Authenticated";
}
else
{
echo "Authentication Failed";
}
Output, (*11)
Authenticated
Sangria\LDAPAuth
This class is useful for developers who just need to authenticate a username/password combo via LDAP., (*12)
use Sangria\LDAPAuth;
LDAPAuth::auth($user_name,$user_pass)
Configuration, (*13)
__SANGRIA_LDAP_SERVER_ADDR__ to the desired address in config.php in vendor/gokulsrinivas/sangria if installed via composer.Sample Code, (*14)
<?php
use Sangria\LDAPAuth;
if(LDAPAuth::auth('username','********'))
{
echo "Authenticated";
}
else
{
echo "Authentication Failed";
}
Output, (*15)
Authenticated
Sangria\HelperFunctions
This class is helpful for development and debugging. This includes dd(), ddp() and isEmail() functions to speed up your development., (*16)
dd()
While debugging, you might need to var_dump() the variable and die(). This function helps you to do just that., (*17)
Source Code, (*18)
<?php use Sangria\HelperFunctions; $a = 4*"2"; Helperfunctions::dd($a); echo "done";
Output, (*19)
int(8)
ddp()
While debugging, you might need to var_dump() the variable and die(). If your objects are complex or if you want to use the dumped data in javascript, you might need it in JSON format. This function helps you to do just that., (*20)
Source Code, (*21)
<?php use Sangria\HelperFunctions; $a = new StdClass(); $a->name = "Hello"; $a->surname = "World"; $a->nicknames = ["Hi",["My","Name","Is"],"Sangria"]; $a->adult = true; $a->balance = 2334.34; $a->age = 19; Helperfunctions::ddp($a); echo "done";
Output, (*22)
{
"name": "Hello",
"surname": "World",
"nicknames": [
"Hi",
[
"My",
"Name",
"Is"
],
"Sangria"
],
"adult": true,
"balance": 2334.34,
"age": 19
}
isEmail()
This function is useful to check whether a given string is a valid email., (*23)
Source Code, (*24)
<?php
use Sangria\HelperFunctions;
$email_string = "hi @hi.com";
if(Helperfunctions::isEmail($email_string))
{
echo "Valid Email";
}
else
{
echo "Invalid Email";
}
Output, (*25)
Invalid Email
There can never be enough tests. Feel free to write more test cases!, (*26)
If you want to add features, improve them, or report issues, feel free to send a pull request! Please include tests for new features., (*27)
Gokul Srinivas, (*28)
MIT, (*29)