peledies/rhonda

一个提供常见PHP任务解决方案的Composer包

1.1.17 2016-09-20 18:20 UTC

README

#帮助我 \Rhonda Rhonda 是一个可安装的Composer包,提供常见PHP任务的解决方案。

##安装

  composer require peledies/rhonda:~1
  composer install

##在项目中添加依赖项 添加以下内容到您的composer.json文件中

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

#类和方法

##\Rhonda\Autoload

递归地加载提供的路径中所有 .php 文件,排除任何 index.php 文件。

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

或者

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

##\Rhonda\UUID

  \Rhonda\UUID::create();

或者

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

##\Rhonda\Request

将所有传入请求数据打包到一个单例中,该单例可以被您的应用程序访问。所有通过 packager 传输的数据都会自动进行 mysql_real_escape,包括数组和对象。

为了使用此类,您需要在将 Rhonda 包包含到项目中后立即运行 packager。

  \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

使用 \Rhonda\Request 获取 mysql 转义请求体

  \Rhonda\RequestBody::get();

或者

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

或者绕过异常

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

##\Rhonda\Response

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

或者

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

##\Rhonda\Success

  echo \Rhonda\Success:: create();

或者

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

##\Rhonda\Error

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

或者

  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);

将 JSON 文件加载到内存中

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

从内存中检索配置对象

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

##\Rhonda\APIGateway

使用自定义头和请求体向外部地址发送请求

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

字符串规范化

  • 删除非单词字符
  • 将字符串转换为小写
  • 将空格和短横线转换为下划线
  • 删除尾部的无效字符

验证类型

测试字符串是否为有效的电子邮件(不抛出异常)

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);
}

测试字符串是否为有效的电子邮件(抛出异常)

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);
}

规范化字符串

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

  // Returns
  some_test_string

##\Rhonda\Headers

检索所有请求头作为一个数组

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

或者

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

##\Rhonda\Mysql

转义字符串

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

转义对象

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

转义数组

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

转换为 Mysql 布尔值

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

##\Rhonda\ServiceChain

如果您使用 ServiceChain,则应将 register() 作为应用程序中的第一件事之一执行,最好是在 composer 自动加载之后立即执行。

register 的默认行为是使用名为 system 的配置对象中的 host 属性。 \Rhonda\ServiceChain::register() 将自动使用该值作为服务名称。

(推荐)使用配置文件将此微服务注册到服务链中

  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();

使用参数将此微服务注册到服务链中

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

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

获取当前服务链状态

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

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

##\Rhonda\CORS

  \Rhonda\CORS::allow_headers();

或者

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

##\Rhonda\Boolean

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

或者

  $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'));

或者

  $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'));

或者

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