\PhalApi_Api

PhalApi Api Class

-

  • including Authentication, creating API params by rules, etc.
  • project API should extend this base API class


Generally, class can extend PhalApi_Api like:

 class Api_Demo extends PhalApi_Api {

     public function getRules() {
         return array(
             // ...
         );
     }

     public function doSth() {
         $rs = array();

         // ...

         return $rs;
     }
 }

Summary

Methods
Properties
Constants
__set()
__get()
init()
getApiRules()
getRules()
$whatever
No constants found
createMemberValue()
filterCheck()
userCheck()
isServiceWhitelist()
equalOrIngore()
No protected properties found
N/A
No private methods found
No private properties found
N/A

Properties

$whatever

$whatever : mixed

API parmas

Type

mixed

Methods

__set()

__set(string  $name, mixed  $value) 

Set API param parsed from request with rules

Parameters

string $name

API param name

mixed $value

API param value after parsed

__get()

__get(string  $name) : mixed

Get API param parsed from request with rules

Parameters

string $name

API param name

Throws

\PhalApi_Exception_InternalServerError

throw 500 when try to get undefined API param

Returns

mixed

init()

init() : null

Initialization

Initialization is mainly composed of:

    1. [Required]parse and generate API params by rules
    1. [Optional]exec filter, eg: signature verification
    1. [Optional]user authentication

Returns

null

getApiRules()

getApiRules() : array

Get all API rules

mainly composed of:

    1. [Fixed]the only one system level param, that's service param
    1. application level common API rules in configuration app.apiCommonRules
    1. API level common params rules in the configuration * of API class
    1. API level specified params rules

NOTE: The priority of rules: 1 < 2 < 3 < 4. Otherwise both request method name and config indexes will trans into lowercase before being compared with others.

Returns

array

getRules()

getRules() : array

Get the rules for parmas

  • developer can override it as needed

Returns

array

createMemberValue()

createMemberValue() 

Parse and generate API params by rules

  • according the config of params rules, generate API param and save into class member after parse

filterCheck()

filterCheck() 

Exec filter

  • developer can override it as needed, in order to implemnet filter something, you should:
    1. implemnet interface PhalApi_Filter::check()
    1. register DI()->filter


This is a simple example as below:

 class My_Filter implements PhalApi_Filter {

     public function check() {
         //TODO
     }
 }

 // register DI()->filter in the file init.php
 DI()->filter = 'My_Filter';

Throws

\PhalApi_Exception_BadRequest

throw 400 exception when fail to check

userCheck()

userCheck() 

User authentication

  • developer can override it as needed. Generally, this implementation can be delegated or implement in API base class

Throws

\PhalApi_Exception_BadRequest

throw 400 exception when fail to check

isServiceWhitelist()

isServiceWhitelist() : boolean

Whether service is whitelist or not

Returns

boolean

equalOrIngore()

equalOrIngore(string  $str, string  $cfg) : boolean

Equal or ingore

Parameters

string $str

string to be compared

string $cfg

config rule, * match all

Returns

boolean