peledies/rhonda

A composer package that provides solutions for common PHP tasks

1.1.17 2016-09-20 18:20 UTC

README

#Help me \Rhonda Rhonda is a composer installable package that provides solutions to common PHP tasks.

##Install

  composer require peledies/rhonda:~1
  composer install

##Require in your project Add the folowing to your composer.json file

  "require": {
    "peledies/rhonda": "~1"
  }

#Classes and Methods

##\Rhonda\Autoload

Recursivly load all .php files in the provided path, excludes any index.php files.

  \Rhonda\Autoload::path(__DIR__."/path/to/load/");

OR

  $load = new \Rhonda\Autoload();
  $load->path(__DIR__."/path/to/load/");

##\Rhonda\UUID

  \Rhonda\UUID::create();

OR

  $uuid = new \Rhonda\UUID();
  $uuid->create();

##\Rhonda\Request

Package all incomming request data into a singleton that can be accessed by your application. ALL data that goes through packager is automatically mysql_real_eacaped even array's and objects recursively.

In order to use this class you need to first run the packager right after Rhonda is included in your project

  \Rhonda\Request::packager();
  # GET parameters
  $thing = \Rhonda\Request::get();
  # GET specific query string value
  $thing = \Rhonda\Request::get('string');
  # POST body
  $thing = \Rhonda\Request::post();

##\Rhonda\RequestBody

use \Rhonda\Request for a mysql escaped request body

  \Rhonda\RequestBody::get();

OR

  $request_body = new \Rhonda\RequestBody();
  $request_body->get();

OR Bypass exception

  $request_body = new \Rhonda\RequestBody();
  $request_body->get(TRUE);

##\Rhonda\Response

  \Rhonda\Response::package($data);

OR

  $response_package = new \Rhonda\Response($data);
  $response_package->package();

##\Rhonda\Success

  echo \Rhonda\Success:: create();

OR

  $msg = new \Rhonda\Success();
  echo $msg->create();

##\Rhonda\Error

  try{
    throw new Exception("Demo Error Exception");
  }catch(\Exception $e){
    echo \Rhonda\Error::handle($e);
  }

OR

  try{
    throw new Exception("Demo Error Exception");
  }catch(\Exception $e){
    $error = new \Rhonda\Error();
    echo $error->handle($e);
  }

##\Rhonda\Config

  $object = new stdClass();
  $object->thing_1 = 'something one';
  $object->thing_2 = 'something two';
  \Rhonda\Config::load_object('test_one', $object);

Load JSON file to memory

  // File path is relative to your project root
  $config->load_file('test_two', 'path/to/file.json');

Retrieve a configuration object from memory

\Rhonda\Config::get('test_one');

##\Rhonda\APIGateway

Make a request to an external address with custom headers and a request body

try{
  $headers = array("Domain"=>"domain_1", "Authorization"=>"sometoken");
  $data = (object) array("handle"=>"demo_1", "password"=>"asdf");
  $api = new \Rhonda\APIGateway('POST','http://elguapo.eventlink.local/authenticateasdf/',$data, $headers);
  $data = $api->run();
}catch(\Exception $e){
  $error = new \Rhonda\Error();
  echo $error->handle($e);
}

##\Rhonda\Strings

String Normalization

  • Removes non word characters
  • Converts the string to lowercse
  • Converts spaces and dashes to underscores
  • Trims trailing invalid characters

Validation Types

Test that a string is a valid email (without exception)

try{
  // PASS
  $string = 'test@test.com';
  \Rhonda\Strings:: validate('email',$string);

  // FAIL
  $string = 'test@test';
  \Rhonda\Strings:: validate('email',$string);

  // Catch will not be invoked
}catch(\Exception $e){
  echo \Rhonda\Error:: handle($e);
}

Test that a string is a valid email (with exception)

try{
  // PASS
  $string = 'test@test.com';
  \Rhonda\Strings:: validate_or_error('email',$string);

  // FAIL
  $string = 'test@test';
  \Rhonda\Strings:: validate_or_error('email',$string);

  // Catch will be invoked
}catch(\Exception $e){
  echo \Rhonda\Error:: handle($e);
}

Normalize a string

  $input = 'Some TEST-@#string-#$-!@';
  \Rhonda\Strings:: normalize($input);

  // Returns
  some_test_string

##\Rhonda\Headers

Retrieve All request headers as an Array

  $headers = \Rhonda\Headers:: getallheaders();

OR

  $headers = new \Rhonda\Headers();
  $headers->getallheaders();

##\Rhonda\Mysql

Escape a String

$string = "that's all folks";
$string = \Rhonda\Mysql::real_escape($string);

Escape an Object

$object = new \stdClass();
$object->thing = "it's for real";
$object = \Rhonda\Mysql::real_escape($object);

Escape an Array

$array = array(
   "ray"=>"it's escaping arrays"
 , "ray2"=>"escape's this one too"
);
$array = \Rhonda\Mysql::real_escape($ray);

Convert to Mysql boolean

  $value = \Rhonda\Mysql:: bool_to_string('true');
  OR
  $mysql = new \Rhonda\Mysql();
  $value = $mysql->bool_to_string('true');

##\Rhonda\ServiceChain

If you are using ServiceChain, register() should be one of the first things you do in your application, preferably immediately after the composer autoload.

The default behavior of register is to use your config object named system for a property named host. \Rhonda\ServiceChain:: register() will automatically use that value for the service name.

(Prefered) Register this micro service to the service chain using a config file

  require_once __DIR__ . '/../vendor/autoload.php';

  // Load your configuration file to memory
  \Rhonda\Config:: load_file('system', 'path/to/file.json');

  // Register your service name
  \Rhonda\ServiceChain:: register();

Register this micro service to the service chain using a parameter

  require_once __DIR__ . '/../vendor/autoload.php';

  // Register your service name
  \Rhonda\ServiceChain:: register('Service-Name');

Get the current service chain state

  // "Returns: service1 => service2 => etc"
  \Rhonda\ServiceChain:: report();

  // "Returns: array("service1", "service2", "etc")
  \Rhonda\ServiceChain:: report(TRUE);

##\Rhonda\CORS

  \Rhonda\CORS::allow_headers();

OR

  $cors = new \Rhonda\CORS();
  $cors->allow_headers();

##\Rhonda\Boolean

  \Rhonda\Boolean::evaluate('yes');

OR

  $boolean = new \Rhonda\Boolean();
  $boolean->evaluate('false');

##\Rhonda\Google

  \Rhonda\Google::geo_code('google_api_key', array('1600 Amphitheatre Parkway', 'Mountain View', 'CA 94043', 'USA'));

OR

  $googleApi = new \Rhonda\Google();
  $googleApi->geo_code('google_api_key', 'array_of_address_parameters');
  \Rhonda\Google::prepare_query_string(array('1600 Amphitheatre Parkway', 'Mountain View', 'CA 94043', 'USA'));

OR

  $googleApi = new \Rhonda\Google();
  $googleApi->prepare_query_string(array('1600 Amphitheatre Parkway', 'Mountain View', 'CA 94043', 'USA'));