Skip to content
This repository was archived by the owner on Feb 17, 2022. It is now read-only.

7.x 2.x typed data

Andy Truong edited this page Mar 17, 2014 · 2 revisions

TypedData API was built to helper developer validate input value must match its schema:

Supported data types:

  1. any
  2. boolean
  3. constant
  4. string
  5. function
  6. integer
  7. list
  8. list<sub_type>
  9. mapping

Examples

Validate a list<string> typed-data:

<?php

$schema = array('type' => 'list<string>');
$input = array('one', 'two', 'three');
$data = at_data($schema, $input);

// Validate
echo $data->validate($error) ? $data->getValue() : $error;

Validate & compute a mapping typed-data:

<?php
$def = array(
  'type' => 'mapping',
  'mapping' => array(
    'title'            => array('type' => 'string'),
    'access arguments' => array('type' => 'list<string>'),
    'page callback'    => array('type' => 'function'),
    'page arguments'   => array('type' => 'list<string>'),
    'type'             => array('type' => 'constant'),
  )
);

$input = array(
  'title'            => 'Menu item',
  'access arguments' => array('access content'),
  'page callback'    => 't',
  'page arguments'   => array('Drupal'),
  'type'             => 'MENU_NORMAL_ITEM',
);

$data = at_data($def, $input);
if ($data->validate($error)) {
  $result = $data->getValue();
}
else {
  echo $error;
}

Clone this wiki locally