GNU GPL FREE

The GNU GPL is an
open-source license.
Read full license

Modified Binpress Personal $4.99

Unlimited sites, servers No source distribution Commercial use allowed Can modify source Read full license | More Info

You need to log-in or create an account
  • Create an account
  • Log-in
  • Please use your real name.
  • Account activation link will be sent to this address.
  • Minimum 8 characters

Clicking this button confirms you read and agreed to the terms of use and privacy policy.

Starting from FREE

View Pricing 14 days money-back guarantee
(1 ratings)

JSONBuilder

A json_encode wrapper that allows javascript functions

Share and win up to $15k in prizes each month!Learn more

Share

Description

This small component eases server side configuration of javascript components. The developer can write js functions inside PHP arrays wrapping them in a thin class and then serialize the array using the JSONBuilder class. This class act as a wrapper to the built-in json_encode PHP function that uses pcre regexps to recognize js functions and conveniently unquote and unescape them.

This component is unit tested (tests are in the distribution) and requires PHP 5.3+ with PCRE and json_encode support.

Back to top

Using the JSONBuilder

Install

To install this component just drop it inside your project. If you are using PHP namespaces and autoloading you should take in count that all classes of the component are inside the root namespace A2PLab\Component\JSONBuilder.

Usage

To use this component include the JSONBuilder and the JSONFunction classes (refer to your autoloading method for this). Then you can create a PHP array containing javascript functions doing:

<?php
  $config = array(
      'firstValue' => 'value1', // A value
      'secondValue => true, // Another value (boolean)
      'firstHandler' => new JSONFunction('handlerRef'), // A function (in this case the name of a callback)
      'secondHandler' => new JSONFunction('function (event) { alert("event fired!"); }'), // Another function (inline function)
  );

To get the JSON from an array you just do:

$builder = new JSONBuilder();
$json = $builder->buildJSON($config);

The buildJSON method accepts as second parameter flags to give to the json_encode built-in function.

Questions & Comments

Leave a comment

You must be logged-in to leave a comment.
Log-in now or register for a free account.
No comments have been posted yet.
You must be logged-in to vote. Log-in to your account or register now.

User Reviews

No reviews have been submitted yet.

Starting from FREE

View Pricing 14 days money-back guarantee