Framework  3.9
APIHelper Class Reference

APIHelper is a class designed to simplify the sharing of data between applications. More...

Public Member Functions

 __construct ($class, $searchFilter=null, $outputFilter=null, $constraints="")
 Creates an APIHelper for the specified class. More...
 
 query ()
 Process an API query. More...
 
 registerFormat ($name, $formatter)
 Register a format and its associated formatter. More...
 

Public Attributes

 $class
 
 $constraints
 
 $searchFilter
 
 $outputFilter
 
 $format = "json"
 
 $formatterMap = array()
 

Detailed Description

APIHelper is a class designed to simplify the sharing of data between applications.

It allows you to very quickly implement a RESTful web service that queries a given DataItem class and return the results in many different formats. Output formatting is extensible, but by default supports JSON, XML (in the canonical Fakoli format), Excel and CSV for all objects.

You can provide search and output filters to be applied to your queries, as well as standard constraints as need. Finally, you can easily provide templated output rendering using the easy-to-use template formatter, and full custom output rendering by implementing your own APIFormatter.

Author
andy

Definition at line 232 of file api_helper.inc.

Constructor & Destructor Documentation

◆ __construct()

APIHelper::__construct (   $class,
  $searchFilter = null,
  $outputFilter = null,
  $constraints = "" 
)

Creates an APIHelper for the specified class.

Parameters
string$classthe class name for the DataItem class
Filter$searchFilter(optional) InclusionFilter or ExclusionFilter limiting the search fields
Filter$outputFilter(optional) InclusionFilter or ExclusionFilter limiting the output fields
string$constraints(optional) standard SQL constraint to apply to all queries

Definition at line 248 of file api_helper.inc.

249  {
250  $this->class = $class;
251  $this->searchFilter = $searchFilter;
252  $this->outputFilter = $outputFilter;
253  $this->constraints = $constraints;
254  $this->format = isset($_GET["_format"]) ? $_GET["_format"] : "json";
255 
256  $this->registerFormat("json", new APIJSONFormatter());
257  $this->registerFormat("xml", new APICanonicalXMLFormatter());
258  $this->registerFormat("excel", new APIExcelFormatter());
259  $this->registerFormat("csv", new APICSVFormatter());
260  }
Provides CSV output formatting for APIHelper.
Definition: api_helper.inc:121
Standard XML formatter - formats items as XML in Fakoli's canonical DataItem form.
Definition: api_helper.inc:83
Excel Formatter - formats items as a Microsoft Excel file.
Definition: api_helper.inc:96
registerFormat($name, $formatter)
Register a format and its associated formatter.
Definition: api_helper.inc:296
Standard JSON formatter - formats items as a JSON serialized array of objects.
Definition: api_helper.inc:70

Member Function Documentation

◆ query()

APIHelper::query ( )

Process an API query.

Exceptions
FakoliException

Definition at line 266 of file api_helper.inc.

267  {
268  $target = new $this->class;
269  $target->filter = $this->searchFilter;
270 
271  $parameters = new SearchParameters($target);
272  $parameters->fromGET();
273 
274  $constraints = (Sconstraints) ? $constraints : "WHERE 1=1 ";
275 
276  $query = $parameters->generateConstraint();
277 
278  $results = Query::create($this->class, $query)
279  ->filter($this->outputFilter)
280  ->execute();
281 
282  if (!array_key_exists($this->format, $this->formatterMap))
283  {
284  throw new FakoliException("Unknown format");
285  }
286 
287  return $this->formatterMap[$this->format]->format($results);
288  }
static create($class, $constraints="")
Static factory method to create a new Query.
Definition: query.inc:358
The SearchParameters class interprets the set of input parameters for a search and generates the corr...

◆ registerFormat()

APIHelper::registerFormat (   $name,
  $formatter 
)

Register a format and its associated formatter.

Parameters
string$namethe name of the format.
APIFormatter$formatterthe formatter object that will handle requests in this format.

Definition at line 296 of file api_helper.inc.

297  {
298  $formatter->parent = $this;
299  $this->formatterMap[$name] = $formatter;
300  }

Member Data Documentation

◆ $class

APIHelper::$class

Definition at line 234 of file api_helper.inc.

◆ $constraints

APIHelper::$constraints

Definition at line 235 of file api_helper.inc.

◆ $format

APIHelper::$format = "json"

Definition at line 238 of file api_helper.inc.

◆ $formatterMap

APIHelper::$formatterMap = array()

Definition at line 239 of file api_helper.inc.

◆ $outputFilter

APIHelper::$outputFilter

Definition at line 237 of file api_helper.inc.

◆ $searchFilter

APIHelper::$searchFilter

Definition at line 236 of file api_helper.inc.


The documentation for this class was generated from the following file: