Skip to content

Instantly share code, notes, and snippets.

@nichtich
Created June 22, 2010 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nichtich/448497 to your computer and use it in GitHub Desktop.
Save nichtich/448497 to your computer and use it in GitHub Desktop.
<?php
/**
* This script reuses the RELAX NG Compact schema files that can be found at
* http://bitbucket.org/bdarcus/csl-schema
*/
// load types and variables from RNC schema
$csl_types = array();
foreach( file('csl-types.rnc') as $line ) {
if ( preg_match('/"(.+)"/', $line, $match) ) {
$csl_types[$match[1]] = true;
}
}
$csl_variables = array();
$type = 'variable';
foreach( file('csl-variables.rnc') as $line ) {
if ( preg_match('/"(.+)"/', $line, $match) )
$csl_variables[$match[1]] = $type;
elseif ( preg_match('/cs-(.+)s/', $line, $match) )
$type = $match[1];
}
$csl_name_particles = array("family"=>1,"given"=>1,"suffix"=>1,
"dropping-particle"=>1,"non-dropping-particle"=>1
// TODO: comma-suffix, static-ordering ?
);
// returns false only if the data is a valid CSL input record
function invalid_cls_data( $data ) {
if (!is_object($data)) return 'expected object as csl input record';
foreach ( $data as $key => $value ) {
$type = $csl_variables[$key];
if (!$type) return 'unknown variable '.$key;
if ($type == 'name') {
if (!is_array($value)) return 'expected array of names';
$n = count($name);
if (!$n) return 'expected non-empty array of names';
foreach ($value as $name) {
if (!is_object($name)) return 'expected object as name';
if (!count($name)) {
if ($n>1) return 'only the last name can be empty';
continue;
}
$n--;
if ($name["literal"]) {
if (count($name)!=1)
return 'name must either be literal or not';
if (!is_scalar($name["literal"]))
return 'literal name must be a string';
}
foreach ($name as $dnkey => $nval) {
if(!$csl_name_particles[$dnkey])
return 'unknown name particle '. $dnkey;
if (!is_scalar($nval))
return 'name particle must be a string';
}
}
} elseif ($type == 'date') {
if (!is_object($value)) return 'expected object for date';
$dtype = '';
foreach ($value as $dkey => $dval) {
if ($dkey == 'circa') {
if (!is_scalar($dval)) return 'expected scalar for "circa"';
} elseif ($dkey == 'date-parts') {
if ($dkey) return 'date cannot be literal an date-parted';
$dtype = $dkey;
# TODO: array of one or two arrays
} elseif ($dkey == 'literal') {
if ($dkey) return 'date cannot be literal an date-parted';
$dtype = $dkey;
if (!is_scalar($dval)) return 'literal date must be string';
} # TODO: season?
}
} else {
if (!is_string($str)) return 'expected string';
# TODO: parse rich text and handle empty strings
}
}
return false;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment