Overview

Packages

  • Phery

Classes

  • Phery
  • PheryFunction
  • PheryResponse

Exceptions

  • PheryException
  • Overview
  • Package
  • Class

Class Phery

Main class for Phery.js

Phery implements ArrayAccess
Package: Phery
License: MIT License
Author: Paulo Cesar
Located at Phery.php
Methods summary
public
# __construct( array $config = array() )

Construct the new Phery instance

Construct the new Phery instance

Parameters

$config
array
$config Config array
public Phery
# callback( array $callbacks )

Set callbacks for before and after filters. Callbacks are useful for example, if you have 2 or more AJAX functions, and you need to perform the same data manipulation, like removing an 'id' from the $_POST['args'], or to check for potential CSRF or SQL injection attempts on all the functions, clean data or perform START TRANSACTION for database, etc

Set callbacks for before and after filters. Callbacks are useful for example, if you have 2 or more AJAX functions, and you need to perform the same data manipulation, like removing an 'id' from the $_POST['args'], or to check for potential CSRF or SQL injection attempts on all the functions, clean data or perform START TRANSACTION for database, etc

Parameters

$callbacks
array
$callbacks The callbacks
array(

    // Set a function to be called BEFORE
    // processing the request, if it's an
    // AJAX to be processed request, can be
    // an array of callbacks

    'before' => array|function,

    // Set a function to be called AFTER
    // processing the request, if it's an AJAX
    // processed request, can be an array of
    // callbacks

    'after' => array|function
);
The callback function should be

// $additional_args is passed using the callback_data() function,
// in this case, a before callback

function before_callback($ajax_data, $internal_data){
  // Do stuff
  $_POST['args']['id'] = $additional_args['id'];
  return true;
}

// after callback would be to save the data perhaps? Just to keep the code D.R.Y.

function after_callback($ajax_data, $internal_data, $PheryResponse){
  $this->database->save();
  $PheryResponse->merge(PheryResponse::factory('#loading')->fadeOut());
  return true;
}
Returning false on the callback will make the process() phase to RETURN, but won't exit. You may manually exit on the after callback if desired Any data that should be modified will be inside $_POST['args'] (can be accessed freely on 'before', will be passed to the AJAX function)

Returns

Phery
protected static boolean
# exception( Phery $phery, string $exception, integer $code )

Throw an exception if enabled

Throw an exception if enabled

Parameters

$phery
Phery
$phery Instance
$exception
string
$exception
$code
integer
$code

Returns

boolean

Throws

PheryException
public Phery
# data( mixed $args,… )

Set any data to pass to the callbacks

Set any data to pass to the callbacks

Parameters

$args,…
mixed
$args,... Parameters, can be anything

Returns

Phery
public static string
# args( array $data, string $encoding = 'UTF-8' )

Encode PHP code to put inside data-phery-args, usually for updating the data there

Encode PHP code to put inside data-phery-args, usually for updating the data there

Parameters

$data
array
$data Any data that can be converted using json_encode
$encoding
string
$encoding Encoding for the arguments

Returns

string
Return json_encode'd and htmlentities'd string
public boolean
# get_csrf_token( )

Get the current token from the $_SESSION

Get the current token from the $_SESSION

Returns

boolean
public string|boolean
# csrf( boolean $check = false, boolean $force = false )

Output the meta HTML with the token. This method needs to use sessions through session_start

Output the meta HTML with the token. This method needs to use sessions through session_start

Parameters

$check
boolean
$check Check if the current token is valid
$force
boolean
$force It will renew the current hash every call

Returns

string|boolean
public static boolean
# is_ajax( boolean $is_phery = false )

Check if the current call is an ajax call

Check if the current call is an ajax call

Parameters

$is_phery
boolean
$is_phery Check if is an ajax call and a phery specific call

Returns

boolean
public static
# error_handler( integer $errno, string $errstr, string $errfile, integer $errline )

Default error handler

Default error handler

Parameters

$errno
integer
$errno
$errstr
string
$errstr
$errfile
string
$errfile
$errline
integer
$errline
public static
# shutdown_handler( boolean $errors = false, boolean $handled = false )

Default shutdown handler

Default shutdown handler

Parameters

$errors
boolean
$errors
$handled
boolean
$handled
public static string
# respond( string|PheryResponse $response, boolean $echo = true )

Helper function to properly output the headers for a PheryResponse in case you need to manually return it (like when following a redirect)

Helper function to properly output the headers for a PheryResponse in case you need to manually return it (like when following a redirect)

Parameters

$response
string|PheryResponse
$response The response or a string
$echo
boolean
$echo Echo the response

Returns

string
public Phery
# views( array $views )

Set the callback for view portions, as defined in Phery.view()

Set the callback for view portions, as defined in Phery.view()

Parameters

$views
array
$views Array consisting of array('#id_of_view' => callback) The callback is like a normal phery callback, but the second parameter receives different data. But it MUST always return a PheryResponse with render_view(). You can do any manipulation like you would in regular callbacks. If you want to manipulate the DOM AFTER it was rendered, do it javascript side, using the afterHtml callback when setting up the views.
Phery::instance()->views(array(
    'section#container' => function($data, $params){
         return
             PheryResponse::factory()
             ->render_view('html', array('extra data like titles, menus, etc'));
     }
));

Returns

Phery
protected
# before_user_func( )

Initialize stuff before calling the AJAX function

Initialize stuff before calling the AJAX function

public boolean
# process( boolean $last_call = true )

Process the AJAX requests if any.

Process the AJAX requests if any.

Parameters

$last_call
boolean
$last_call Set this to false if any other further calls to process() will happen, otherwise it will exit

Returns

boolean
Return false if any error happened

Throws

PheryException
public Phery|string|array
# config( string|array $config = null )

Config the current instance of Phery

Config the current instance of Phery

array(
    // Defaults to true, stop further script execution
    'exit_allowed' => true|false,

    // Throw exceptions on errors
    'exceptions' => true|false,

    // Return the responses in the process() call instead of echo'ing
    'return' => true|false,

    // Error reporting temporarily using error_reporting(). 'false' disables
    // the error_reporting and wont try to catch any error.
    // Anything else than false will throw a PheryResponse->exception() with
    // the message
    'error_reporting' => false|E_ALL|E_DEPRECATED|...

    // By default, the function Phery::instance()->set() will only
    // register functions when the current request is an AJAX call,
    // to save resources. In order to use Phery::instance()->get_function()
    // anytime, you need to set this config value to true
    'set_always_available' => false|true
);

If you pass a string, it will return the current config for the key specified Anything else, will output the current config as associative array

Parameters

$config
string|array
$config Associative array containing the following options

Returns

Phery|string|array
public static Phery
# instance( array $config = array() )

Generates just one instance. Useful to use in many included files. Chainable

Generates just one instance. Useful to use in many included files. Chainable

Parameters

$config
array
$config Associative config array

Returns

Phery

See

Phery::__construct()
Phery::config()
public Phery
# set( array $functions )

Sets the functions to respond to the ajax call. For security reasons, these functions should not be reacheable through POST/GET requests. These will be set only for AJAX requests as it will only be set in case of an ajax request, to save resources.

Sets the functions to respond to the ajax call. For security reasons, these functions should not be reacheable through POST/GET requests. These will be set only for AJAX requests as it will only be set in case of an ajax request, to save resources.

You may set the config option "set_always_available" to true to always register the functions regardless of if it's an AJAX function or not going on.

The answer/process function, should have the following structure:

function func($ajax_data, $callback_data, $phery){
  $r = new PheryResponse; // or PheryResponse::factory();

  // Sometimes the $callback_data will have an item called 'submit_id',
  // is the ID of the calling DOM element.
  // if (isset($callback_data['submit_id'])) {  }
  // $phery will be the current phery instance that called this callback

  $r->jquery('#id')->animate(...);
  return $r; //Should always return the PheryResponse unless you are dealing with plain text
}

Parameters

$functions
array
$functions An array of functions to register to the instance.
array(
  'function1' => 'function',
  'function2' => array($this, 'method'),
  'function3' => 'StaticClass::name',
  'function4' => array(new ClassName, 'method'),
  'function5' => function($data){}
);

Returns

Phery
public Phery
# unset_function( string $name )

Unset a function previously set with set()

Unset a function previously set with set()

Parameters

$name
string
$name Name of the function

Returns

Phery

See

Phery::set()
public callable|array|string|PheryResponse|null
# get_function( string $function_name, array $args = array() )

Get previously function set with set() method If you pass aditional arguments, the function will be executed and this function will return the PheryResponse associated with that function

Get previously function set with set() method If you pass aditional arguments, the function will be executed and this function will return the PheryResponse associated with that function

Phery::get_function('render', ['<html></html>'])->appendTo('body');

Parameters

$function_name
string
$function_name The name of the function registed with set
$args
array
$args Any arguments to pass to the function

Returns

callable|array|string|PheryResponse|null

See

Phery::set()
public static Phery
# factory( array $config = array() )

Create a new instance of Phery that can be chained, without the need of assigning it to a variable

Create a new instance of Phery that can be chained, without the need of assigning it to a variable

Parameters

$config
array
$config Associative config array

Returns

Phery

See

Phery::config()
protected static string
# common_check( array & $attributes, boolean $include_method = true )

Common check for all static factories

Common check for all static factories

Parameters

$attributes
array
$attributes
$include_method
boolean
$include_method

Returns

string
public static string
# link_to( string $content, string $function, array $attributes = array(), Phery $phery = null, boolean $no_close = false )

Helper function that generates an ajax link, defaults to "A" tag

Helper function that generates an ajax link, defaults to "A" tag

Parameters

$content
string
$content The content of the link. This is ignored for self closing tags, img, input, iframe
$function
string
$function The PHP function assigned name on Phery::set()
$attributes
array
$attributes Extra attributes that can be passed to the link, like class, style, etc
array(
    // Display confirmation on click
    'confirm' => 'Are you sure?',

    // The tag for the item, defaults to a. If the tag is set to img, the
    // 'src' must be set in attributes parameter
    'tag' => 'a',

    // Define another URI for the AJAX call, this defines the HREF of A
    'href' => '/path/to/url',

    // Extra arguments to pass to the AJAX function, will be stored
    // in the data-phery-args attribute as a JSON notation
    'args' => array(1, "a"),

    // Set the "href" attribute for non-anchor (a) AJAX tags (like buttons or spans).
    // Works for A links too, but it won't function without javascript, through data-phery-target
    'target' => '/default/ajax/controller',

    // Define the data-phery-type for the expected response, json, xml, text, etc
    'phery-type' => 'json',

    // Enable clicking on structural HTML, like DIV, HEADER, HGROUP, etc
    'clickable' => true,

    // Force cache of the response
    'cache' => true,

    // Aggregate data from other DOM elements, can be forms, inputs (textarea, selects),
    // pass multiple selectors, like "#input1,#form1,~ input:hidden,select.job"
    // that are searched in this order:
    // - $(this).find(related)
    // - $(related)
    // So you can use sibling, children selectors, like ~, +, >, :parent
    // You can also, through Javascript, append a jQuery object to the related, using
    // $('#element').phery('data', 'related', $('#other_element'));
    'related' => true,

    // Disables the AJAX on element while the last action is not completed
    'only' => true,

    // Set the encoding of the data, defaults to UTF-8
    'encoding' => 'UTF-8',

    // Set the method (for restful responses)
    'method' => 'PUT'
);
$phery
Phery
$phery Pass the current instance of phery, so it can check if the functions are defined, and throw exceptions
$no_close
boolean
$no_close Don't close the tag, useful if you want to create an AJAX DIV with a lot of content inside, but the DIV itself isn't clikable
  <?php echo Phery::link_to('', 'remote', array('target' => '/another-url', 'args' => array('id' => 1), 'class' => 'ajaxified'), null, true); ?>
    <p>This new content</p>
    <div class="result></div>
  </div>
  <?php echo Phery::link_to('', 'remote', array('target' => '/another-url', 'args' => array('id' => 2), 'class' => 'ajaxified'), null, true); ?>
    <p>Another content will have div result filled</p>
    <div class="result></div>
  </div>

  <script>
    $('.ajaxified').phery('remote');
  </script>

Returns

string
The mounted HTML tag
public static string
# form_for( string $action, string $function, array $attributes = array(), Phery $phery = null )

Create a <form> tag with ajax enabled. Must be closed manually with </form>

Create a <form> tag with ajax enabled. Must be closed manually with </form>

Parameters

$action
string
$action where to go, can be empty
$function
string
$function Registered function name
$attributes
array
$attributes Configuration of the element plus any HTML attributes
array(
    //Confirmation dialog
    'confirm' => 'Are you sure?',

    // Type of call, defaults to JSON (to use PheryResponse)
    'phery-type' => 'json',

    // 'all' submits all elements on the form, even empty ones
    // 'disabled' enables submitting disabled elements
    'submit' => array('all' => true, 'disabled' => true),

    // Disables the AJAX on element while the last action is not completed
    'only' => true,

    // Set the encoding of the data, defaults to UTF-8
    'encoding' => 'UTF-8',
);
$phery
Phery
$phery Pass the current instance of phery, so it can check if the functions are defined, and throw exceptions

Returns

string
The mounted <form> HTML tag
public static string
# select_for( string $function, array $items, array $attributes = array(), Phery $phery = null )

Create a <select> element with ajax enabled "onchange" event.

Create a <select> element with ajax enabled "onchange" event.

Parameters

$function
string
$function Registered function name
$items
array
$items Options for the select, 'value' => 'text' representation
$attributes
array
$attributes Configuration of the element plus any HTML attributes
array(
    // Confirmation dialog
    'confirm' => 'Are you sure?',

    // Type of call, defaults to JSON (to use PheryResponse)
    'phery-type' => 'json',

    // The URL where it should call, translates to data-phery-target
    'target' => '/path/to/php',

    // Extra arguments to pass to the AJAX function, will be stored
    // in the args attribute as a JSON notation, translates to data-phery-args
    'args' => array(1, "a"),

    // Set the encoding of the data, defaults to UTF-8
    'encoding' => 'UTF-8',

    // Disables the AJAX on element while the last action is not completed
    'only' => true,

    // The current selected value, or array(1,2) for multiple
    'selected' => 1

    // Set the method (for restful responses)
    'method' => 'PUT'
);
$phery
Phery
$phery Pass the current instance of phery, so it can check if the functions are defined, and throw exceptions

Returns

string
The mounted <select> with <option>s inside
public boolean
# offsetExists( mixed $offset )

OffsetExists

OffsetExists

Parameters

$offset
mixed
$offset

Returns

boolean

Implementation of

ArrayAccess::offsetExists()
public
# offsetUnset( mixed $offset )

OffsetUnset

OffsetUnset

Parameters

$offset
mixed
$offset

Implementation of

ArrayAccess::offsetUnset()
public mixed|null
# offsetGet( mixed $offset )

OffsetGet

OffsetGet

Parameters

$offset
mixed
$offset

Returns

mixed|null

Implementation of

ArrayAccess::offsetGet()
public
# offsetSet( mixed $offset, mixed $value )

offsetSet

offsetSet

Parameters

$offset
mixed
$offset
$value
mixed
$value

Implementation of

ArrayAccess::offsetSet()
public
# __set( string $name, mixed $value )

Set shared data

Set shared data

Parameters

$name
string
$name
$value
mixed
$value
public mixed
# __get( string $name )

Get shared data

Get shared data

Parameters

$name
string
$name

Returns

mixed
public static mixed
# coalesce( mixed $args,… )

Utility function taken from MYSQL. To not raise any E_NOTICES (if enabled in your error reporting), call it with @ before the variables. Eg.: Phery::coalesce(@$var1, @$var['asdf']);

Utility function taken from MYSQL. To not raise any E_NOTICES (if enabled in your error reporting), call it with @ before the variables. Eg.: Phery::coalesce(@$var1, @$var['asdf']);

Parameters

$args,…
mixed
$args,... Any number of arguments

Returns

mixed
Constants summary
integer ERROR_CALLBACK 0
#

Exception on callback() function

Exception on callback() function

See

Phery::callback()

Type

int
integer ERROR_PROCESS 1
#

Exception on process() function

Exception on process() function

See

Phery::process()
integer ERROR_SET 2
#

Exception on set() function

Exception on set() function

See

Phery::set()
integer ERROR_CSRF 4
#

Exception when the CSRF is invalid

Exception when the CSRF is invalid

See

Phery::process()
integer ERROR_TO 3
#

Exception on static functions

Exception on static functions

See

Phery::link_to()
Phery::select_for()
Phery::form_for()
Properties summary
public static string $encoding 'UTF-8'
#

Default encoding for your application

Default encoding for your application

public static boolean $expose_paths false
#

Expose the paths on PheryResponse exceptions

Expose the paths on PheryResponse exceptions

protected array $functions array()
#

The functions registered

The functions registered

protected array $callbacks array()
#

The callbacks registered

The callbacks registered

protected array $data array()
#

The callback data to be passed to callbacks and responses

The callback data to be passed to callbacks and responses

protected static Phery $instance null
#

Static instance for singleton

Static instance for singleton

protected array $views array()
#

Render view function

Render view function

protected array $config array()
#

Config

Config

'exit_allowed' (boolean)
'exceptions' (boolean)
'return' (boolean)
'error_reporting' (int)
'csrf' (boolean)
'set_always_available' (boolean)
'auto_session' (boolean)

See

Phery::config()
Phery API documentation generated by ApiGen 2.8.0