Skip to content

Instantly share code, notes, and snippets.

@krusynth
Created May 29, 2014 20:09
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 krusynth/042436d1830f7c761d15 to your computer and use it in GitHub Desktop.
Save krusynth/042436d1830f7c761d15 to your computer and use it in GitHub Desktop.
State Decoded parser for Philadelphia for American Legal data
<?php
/**
* San Francisco parser for State Decoded.
* Extends AmericanLegal base classes.
*
* PHP version 5
*
* @license http://www.gnu.org/licenses/gpl.html GPL 3
* @version 0.8
* @link http://www.statedecoded.com/
* @since 0.3
*/
/**
* This class may be populated with custom functions.
*/
require 'class.AmericanLegal.inc.php';
class State extends AmericanLegalState {}
class Parser extends AmericanLegalParser
{
/*
* Most codes have a Table of Contents as the first LEVEL.
*/
public function pre_parse_chapter(&$chapter)
{
// Get all of the titles.
$titles = $chapter->REFERENCE->TITLE;
$title_count = count($titles);
// We skip the last title, thus " - 1"
for($i = 0; $i < $title_count - 1; $i++)
{
unset($name, $identifier, $order_by);
$title = $titles[$i];
// Clean up the title, strip some html.
$title = trim(str_replace('&quot;', '', $title));
if(preg_match($this->structure_regex, $title, $matches))
{
$name = $matches['name'];
$identifier = $matches['number'];
}
else
{
switch($title)
{
case 'THE PHILADELPHIA CODE' :
$name = 'The Philadelphia Code';
$identifier = 'Philadelphia';
$order_by = '0';
break;
case 'PHILADELPHIA HOME RULE CHARTER' :
$name = 'Philadelphia Home Rule Charter';
$identifier = 'Charter';
$order_by = '1';
break;
}
}
if(isset($name) && isset($identifier))
{
$structure = new stdClass();
$structure->name = $this->clean_title($name);
$structure->label = 'Code';
$structure->identifier = $this->clean_identifier($identifier);
if(!isset($order_by))
{
$order_by = $this->get_structure_order_by($structure);
}
$structure->order_by = $order_by;
if(isset($last_structure))
{
$structure->parent_id = $last_structure->id;
}
$structure->level = count($this->structures) + 1;
// if(isset($matches[3]) && strlen(trim($matches[3])))
// {
// $structure->metadata = new stdClass();
// $structure->metadata->text = trim($matches[3]);
// }
$this->create_structure($structure);
$last_structure = $structure;
$this->structures[] = $structure;
}
else {
$this->logger->message('Cannot get title from structure: "' . $title . '"', 5);
}
}
// If there's two levels,this has a table of contents.
if(count($chapter->LEVEL->LEVEL) == 2 && isset($chapter->LEVEL->LEVEL[1]->LEVEL) && isset($chapter->LEVEL->LEVEL[1]->LEVEL->LEVEL))
{
$this->logger->message('Skipping first level.', 2);
unset($chapter->LEVEL->LEVEL[0]);
}
}
public function get_structure_order_by($structure)
{
// Make room for the charter by adding one.
return 1 + (int) parent::get_structure_order_by($structure);
}
public function get_section_parts($section)
{
/*
* Parse the catch line and section number.
*/
$section_title = $this->clean_title((string) $section->RECORD->HEADING);
$this->logger->message('Title: ' . $section_title, 2);
switch($section_title)
{
case 'PREFACE TO THE TENTH EDITION' :
$section_parts = array(
'catch_line' => 'Preface to the Tenth Edition',
'number' => 'Preface'
);
break;
case 'PREAMBLE' :
$section_parts = array(
'catch_line' => 'Preamble',
'number' => 'Preamble'
);
break;
}
if(!isset($section_parts))
{
$section_parts = parent::get_section_parts($section_title);
}
return $section_parts;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment