Skip to content

Instantly share code, notes, and snippets.

@pcdinh
Created July 26, 2009 07:01
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 pcdinh/155452 to your computer and use it in GitHub Desktop.
Save pcdinh/155452 to your computer and use it in GitHub Desktop.
<?php
include_once './FileNameComponent.php';
/**
* Provides methods to parse file name into meaningful parts.
*
* @author pcdinh
* @since July 25, 2009
* @version $Id$
*/
class FileNameParser
{
/**
* Parses file names into meaningful parts.
*
* @param string $fileName
* @return FileNameComponent
*/
public function parse($fileName)
{
$parsed = new FileNameComponent();
$pattern = '#^([a-z]{3}[0-9]{9})\.([0-9]{4})\.([1|1N|2|2N|3|3N|4|4N|A|AMF|COB|K|L|P|U|Y]{1})\.([0-9]{2})\.([L|E]{1})\.([0-9]{2})\.([0-9]{2})\.([A-Z]{3,4})$#i';
$matched = preg_match($pattern, $fileName, $matches);
if (0 === $matched || false === $matched)
{
$parsed->format = FileNameFormat::UNKNOWN_FORMAT;
return $parsed;
}
$parsed->wvbNumber = $matches[1];
$parsed->year = $matches[2];
$parsed->docType = $matches[3];
$parsed->section = $matches[4];
$parsed->language = $matches[5];
$parsed->month = $matches[6];
$parsed->day = $matches[7];
$parsed->extension = $matches[8];
$parsed->format = FileNameFormat::NEW_FORMAT_2;
return $parsed;
}
}
/**
* Exception can be thrown when file name format is not valid.
*
* @author pcdinh
* @since July 24, 2009
*/
class FileNameParsingException extends Exception {}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment