File_Therion
[ class tree: File_Therion ] [ index: File_Therion ] [ all elements ]

Class: File_Therion

Source Location: /File_Therion-0.4.1/File/Therion.php

Class Overview


Class representing a therion file.


Author(s):

Copyright:

  • 2016 Benedikt Hallinger

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 113]
Class representing a therion file.

Also serves functions to parse and write data to/from File_Therion objects.

Therion (http://therion.speleo.sk/) is an openSource application for managing cave survey data.

The data structure follows mostly the SQL diagram in the therion book (see Chapter 'SQL export'), in short:

  • A .th-file contains surveys.
  • Surveys can be nested.
  • Surveys contain metadata (people etc) and one ore more centreline(s).
  • A Centreline contain shots and stations.
  • Shots and stations can contain flags.
The Data format is specified in the therion book, but basicly a file contains human readable text lines following a specific syntax and describing the therion objects. A logical line may be "wrapped"; that is, its content can be spread out over several physical lines.

There are two basic workflows:

  1.  // 1. Read datasource and parse to objects:
  2.  $src "some/local/file.th";  // physical filename
  3.  $th = new File_Therion($src)// Instanciate new datasource
  4.  $th->fetch();                 // Get contents (read)
  5.  $th->evalInputCMD();          // evaluate 'input' commands recursively
  6.  $th->updateObjects();         // Generate Therion objects to work with
  7.  $surveys $th->getSurveys()// example: retrieve parsed surveys
  8.  
  9.  // The above can be summed up with:
  10.  $th File_Therion::parse($file)// fetch file recursively
  11.  $surveys $th->getSurveys();     // get surveys
  12.  
  13.  // you may also supply a different Reader plugin to fetch from
  14.  // other sources like internet URLs or even databases.
  15.  
  16.  
  17.  // 2. Generate a .th Therion file out of data model:
  18.  $survey = new File_Therion_Survey();
  19.  // $survey->....  // do many things: craft data model
  20.  $tgt "some/local/target.th"// usually local file
  21.  $th = new File_Therion($tgt);  // Instanciate new data target
  22.  $th->setEncoding('UTF-8');     // don't forget to indicate encoding
  23.  $th->addObject($survey);       // associate therion data model objects
  24.  $th->updateLines();            // update internal lines using data objects
  25.  $th->addFormatter($formatter)// add a custom formatter
  26.  $th->write();                  // physically write to data target $tgt
  27.  $th->toString();               // altenatively: fetch  data as string



[ Top ]


Class Variables

$formatters = array()

[line 134]

Registered formatters.
  • Access: protected

Type:   mixed


[ Top ]

$_encoding =  null

[line 150]

Encoding of this file.
  • Access: protected

Type:   string


[ Top ]

$_filename =  ''

[line 143]

Filename and path of this file.

This represents the real physical adress of the content on the disk.

  • Access: protected

Type:   mixed


[ Top ]

$_header =  ""

[line 118]

Header to be put into any generated file
  • Access: protected

Type:   mixed


[ Top ]

$_lines = array()

[line 182]

Lines of this file.

will be populated by parse() or updateLines().

  • Var: of data (File_Therion_Line objects)
  • Access: protected

Type:   array


[ Top ]

$_objects = array()

[line 192]

objects of this file.

will be populated by parse() or updateObjects()

  • Var: of data (File_Therion_* objects)
  • Access: protected

Type:   array


[ Top ]

$_reader =  null

[line 127]

Reader used for this file.

This represents the real physical access of the content.

  • Access: protected

Type:   mixed


[ Top ]

$_supportedEncodings = array(
        'ASCII',
        'CP1250',
        'CP1251',
        'CP1252',
        'CP1253',
        'ISO8859-1',
        'ISO8859-2',
        'ISO8859-5',
        'ISO8859-7',
        'UTF-8',
    )

[line 161]

Currently supported encodings by therion.

You can get an up-to date list with the command 'therion --print-encodings'. If there is a new encoding that is not reflected here, please contact me.

  • Access: protected

Type:   array


[ Top ]



Method Detail

__construct (Constructor)   [line 229]

File_Therion __construct( string $filename)

Create a new therion file object representing content at $filename.

Use this to create a new interface for parsing existing files or writing new ones.

The $filename tells the physical file name of the file. Use fetch() if you want to read the source contents or write() to write the current content to the target.

Example:

  1.  $thFile = new File_Therion('foobar.th')// local file (r/w access)
  2.  $thFile->fetch();  // get contents
  3.  $thFile->updateObjects();  // parse fetched contents
  4.  $surveys $thFile->getObjects('File_Therion_Survey')// get surveys
  5.  $all $thFile->getObjects()// get all parsed objects

  • Throws: File_Therion_Exception with wrapped lower level exception
  • Access: public

Parameters:

string   $filename   —  Name of the file represented by this object

[ Top ]

addFormatter   [line 1470]

void addFormatter( File_Therion_Formatter|array $formatter)

Add a formatter.

The formatters will be called in the order they were added.

  • Access: public

Parameters:

File_Therion_Formatter|array   $formatter   —  formatter(s)

[ Top ]

addLine   [line 514]

void addLine( File_Therion_Line|string $line, [int $index = -1], [bool $replace = false])

Add a line to this file.

The optional index parameter allows to adjust the insertion point; the line will be inserted at the index, pushing already present content one line down (-1=end, 0=start, ...). When replacing, the selected index will be replaced.

Instead of $index 0 and -1 you can use the strings 'start'/'end', this will make your code more readable. Using

  1. addLine(...$lln - 1)
will use logical line number instead of the index (logical = index+1).

Beware that clearLines() will discard any manual insertions. Also be aware that fetch() will clean the line buffer too.

Note that addLine() will not take care of wrapping; make sure that the line content remains consistent.

Be sure to use the correct encoding for your data (-> setEncoding() and encode() for more details).

Example:

  1.  // add a simple line (implicitely to the end):
  2.  $th->addLine(new File_Therion_Line("somecontent"));
  3.  
  4.  // add a line to the start, pushing previous line one down:
  5.  $th->addLine(new File_Therion_Line("startline")0);
  6.  // result: line-0="startline", line-1="somecontent"
  7.  
  8.  // add another simple line (explicitely to the end):
  9.  $th->addLine(new File_Therion_Line("final")'end');
  10.  // result: line-0="startline", line-1="somecontent", line-3="final"
  11.  
  12.  // replace line-1:
  13.  $th->addLine(new File_Therion_Line("othercontent")1true);
  14.  // result: line-0="startline", line-1="othercontent", line-3="final"

Instead of Line objects you may also add a string as $line argument. In this case a new Line object will be parsed for you. Right spaces and newline will be trimmed.

Note that the lines content is expected to be already encoded in the current files encoding. If the codesets differ, encode them first. See encode() for encoding or use phps own functions.

  • Throws: OutOfBoundsException when requested index is not available
  • Throws: InvalidArgumentException
  • Access: public

Parameters:

File_Therion_Line|string   $line   —  Line to add
int   $index   —  At which logical position to add (-1=end, 0=first line, ...)
bool   $replace   —  when true, the target line will be overwritten

[ Top ]

addObject   [line 728]

void addObject( object $thObj)

Add an File_Therion data model object to this file

Associated objects can be written to a file after updateLines() has been called to update the internal line representation.

Be aware that clearObjects() will discard any manual changes made so far, and be warned that updateObjects() will clean them too.

  • Todo: implement me better: checks etc
  • Access: public

Parameters:

object   $thObj   —  File_Therion_* object to add

[ Top ]

checkSyntax   [line 1139]

void checkSyntax( )

Check basic syntax of internal line buffer

This validates basic syntax of internal file buffer. The following checks will be performed:

  • last line should not expect additional data
  • matching multiline commands

  • Todo: implement me
  • Throws: File_Therion_SyntaxException if syntax errors occur
  • Access: public

[ Top ]

clearFormatters   [line 1491]

void clearFormatters( File_Therion_Formatter 0)

Remove all formatters.
  • Access: public

Parameters:

File_Therion_Formatter   0   — 

[ Top ]

clearLines   [line 699]

void clearLines( )

Clear associated lines.

This will wipe out the internal line buffer.

  • Access: public

[ Top ]

clearObjects   [line 711]

void clearObjects( )

Clear associated objects.

This will unassociate all registered objects. You probably want to call clearLines() hereafter to also clean the calculated line content.

  • Access: public

[ Top ]

count   [line 1023]

int count( [boolean $logical = false])

Count (wrapped) lines in this file (SPL Countable).

returns the count of physical (ie. wrapped) lines in this file. To count logical lines (ie. unwrapped, or line objects), use $logical.

  • Return: number of raw lines
  • Access: public

Parameters:

boolean   $logical   —  If true, return logical (unwrapped) count

[ Top ]

encode   [line 1092]

$data encode( string $data, string $toEncoding, string $fromEncoding, [boolean $strictCheck = false])

Encode data.

Will check if target-encoding is supported by therion.

If $strictCheck is set to TRUE, the from encoding will also be verified against supported therion encodings. Use this in case the source encoding is a known therion file.

  • Return: in $toEncoding.
  • Throws: File_Therion_Exception when encoding fails.
  • Throws: InvalidArgumentException in case encoding is not supported.
  • Access: public

Parameters:

string   $data   —  String to encode
string   $toEncoding   —  target encoding
string   $fromEncoding   —  source encoding
boolean   $strictCheck   —  enable from-encoding compatibility check

[ Top ]

evalInputCMD   [line 1184]

void evalInputCMD( [int $lvls = null])

Scan local filebuffer for 'input' commands and execute them.

Therions 'input' statement will include the remote file content at the place of the 'input <filename>' therion command.

This will try to interpret the given filepath in local context; the input-parameter is always a filepath in a filesystem. When the filename has no extension, ".th" is assumed.

For actually fetching the content, the currently associated File_Therion_Reader implementation will be used (see setReader()). With the default FileReader this means that we will assume the filename to be relative to the local file on a local mounted disk.

Please note that referenced files will be converted to the parent file encoding (all 'input' content is encoded to this files codeset).

To limit the number of possible nested levels you may specify the $lvls parameter:

  • null: endless recursion (default)
  • 0: no input
  • 1: only local inputs
  • >1: nested levels

  • Throws: InvalidArgumentException when input command is invalid pointer
  • Throws: File_Therion_IOException in case of reading problems
  • Throws: File_Therion_SyntaxException case of parsing/syntax errors
  • Access: public

Parameters:

int   $lvls   —  remaining levels to input

[ Top ]

extractMultilineCMD   [line 1350]

array extractMultilineCMD( array $lines)

Build structure of local-scope multiline commands with associated lines.

This will order the lines passed into a nested array sorting the lines belonging together into subarrays collected under a common tag.

The first level contains all "seen" tags like surveys. There all survey lines will be collected and ordered into their own array. The special 'LOCAL' array kay holds all lines that where not enclosed into their own contect and are therefore considered "local".

Nested structures (like they are ancountered with surveys) need to be resolved with consecutive calls to this method. The structure holds all the lines of the first levels, so eg. in case of nested structures there will be all lines of the outermost survey command

The structure will look like this:

  1.  array(
  2.       'LOCAL'  => array(local lines without own context)
  3.       'survey' => array(
  4.                       array(lines-of-survey-1),
  5.                       array(lines-of-survey-2),
  6.       'centreline' => array(
  7.                           array(lines-of-centreline-1),
  8.                           array(lines-of-centreline-2),
  9.       ...

  • Return: with ordered lines
  • Throws: InvalidArgumentException when $lines is not strictly Line-objects
  • Access: public

Parameters:

array   $lines   —  array of File_Therion_Line objects

[ Top ]

fetch   [line 413]

void fetch( [File_Therion_Reader $reader = null])

Update the internal line representation of this file from datasource.

This will use the supplied Reader to fetch therion file content. If no Reader was supplied (NULL/missing value), the internal default reader will be used (File_Therion_FileReader). The supplied reader will be also used for subsequent reads, like when resolving input commands.

Be aware that the Reader usually clears the internal line buffer, so any changes made by addLine() get discarded. Please refer to the readers documentation if you want to avoid this.

After fetching physical content, you may call updateObjects() to generate Therion data model objects out of it. (Some Reader implementeations may already do this for you!)

  • Throws: InvalidArgumentException
  • Throws: File_Therion_IOException
  • Access: public

Parameters:

File_Therion_Reader   $reader   —  Reader to use (NULL for default)

[ Top ]

format   [line 1502]

void format( array $lines)

Format lines using registered formatters.
  • Throws: InvalidArgumentException if $lines is no array
  • Access: protected

Parameters:

array   $lines   —  File_Therion_Lines to format

[ Top ]

getAllObjects   [line 796]

array getAllObjects( )

Get all associated objects of this file.

Example:

  1.  $allObjects $thFile->getObjects()// get all

  • Return: of File_Therion_* objects (empty array if no such objects)
  • Access: public

[ Top ]

getCentrelines   [line 830]

array getCentrelines( )

Get all directly associated centrelines of this file.

Note that this kind of object is usually not "lone" in a file but commonly part of a survey. This function returns only "lone" objects without parent structure.

Example:

  1.  $allCentrelines $thFile->getCentrelines();

  • Return: of File_Therion_Centreline objects (or empty array)
  • Access: public

[ Top ]

getEncoding   [line 1070]

string getEncoding( )

Get encoding of input/output files currently active.
  • Return: encoding
  • Access: public

[ Top ]

getFilename   [line 976]

string getFilename( [string $relPath = ""])

Get physical file name of this file object.
  • Return: filename
  • Throws: InvalidArgumentException in case $relPath is no string
  • Access: public

Parameters:

string   $relPath   —  return only relative path compared to argument

[ Top ]

getHeader   [line 1009]

string getHeader( )

Get current header string.
  • Access: public

[ Top ]

getLines   [line 665]

array getLines( )

Get internal line buffer.

The lines will be formatted using the registered formatters (see addFormatter()).

On each consecutive run of getLines() the formatters will be reapplied. This may yield strange results. If you want to format just once, {link removeFormatters()} after formatting was applied.

  • Return: of File_Therion_Line objects
  • Access: public

[ Top ]

getMaps   [line 868]

array getMaps( )

Get all directly associated Maps of this file.

Note that this kind of object is usually not "lone" in a file but commonly part of a survey. This function returns only "lone" objects without parent structure.

Example:

  1.  $allMaps $thFile->getMaps();

  • Return: of File_Therion_Map objects (or empty array)
  • Access: public

[ Top ]

getObjects   [line 750]

array getObjects( [string $filter = null])

Get all associated objects of this file.

You can optionaly query for specific types using $filter. You may ommit the prefix 'File_Therion_' from the filter.

Example:

  1.  $allObjects $thFile->getObjects()// get all
  2.  $surveys    $thFile->getObjects('File_Therion_Survey')// get surveys
  3.  $surveys    $thFile->getObjects('Survey')// get surveys

  • Return: of File_Therion_* objects (empty array if no such objects)
  • Throws: InvalidArgumentException
  • Access: public

Parameters:

string   $filter   —  File_Therion_* class name, retrieve only objects of that kind

[ Top ]

getScraps   [line 849]

array getScraps( )

Get all directly associated Scraps of this file.

Note that this kind of object is usually not "lone" in a file but commonly part of a survey. This function returns only "lone" objects without parent structure.

Example:

  1.  $allScraps $thFile->getScraps();

  • Return: of File_Therion_Scrap objects (or empty array)
  • Access: public

[ Top ]

getSurfaces   [line 887]

array getSurfaces( )

Get all directly associated Surface definitions of this file.

Note that this kind of object is usually not "lone" in a file but commonly part of a survey. This function returns only "lone" objects without parent structure.

Example:

  1.  $allSurfaces $thFile->getSurfaces();

  • Return: of File_Therion_Surface objects (or empty array)
  • Access: public

[ Top ]

getSurveys   [line 811]

array getSurveys( )

Get all directly associated surveys of this file.

Example:

  1.  $allSurveys $thFile->getSurveys();

  • Return: of File_Therion_Survey objects (or empty array)
  • Access: public

[ Top ]

getVersion   [line 201]

string getVersion( )

Returns the current release version, may be called statically
  • Return: version
  • Access: public

[ Top ]

handleEncodingLine   [line 634]

void handleEncodingLine( )

Handle 'encoding' command in internal line buffer.
  • Access: protected

[ Top ]

parse   [line 265]

File_Therion parse( string $url, [int $recurse = null], [File_Therion_Reader $reader = null])

Parse datasource into objects.

This will perform the following operations:

  • create a new File_Therion object pointing to the datasource given
  • fetch() the datasource
  • evalInputCMD() optionally import referenced content ($lvls > 0)
  • updateObjects() represented by the lines
After this procedure the File_Therion object returned is in a consistent state (internal line buffer equals parsed content). Exceptions will be bubbled up.

To limit the number of possible nested levels you may specify the $recurse parameter (null=default: endless, 0: none, >0: nested levels).

Please read also the documentation of fetch(), evalInputCMD() and updateObjects() for informations on their respective capabilitys and pitfalls.

  • Return: object
  • Throws: File_Therion_Exception for generic errors
  • Throws: File_Therion_IOException in case of reading problems
  • Throws: File_Therion_SyntaxException case of parsing/syntax errors
  • Access: public

Parameters:

string   $url   —  path or URL of the file
int   $recurse   —  restrict recursion
File_Therion_Reader   $reader   —  optionally alternative reader

[ Top ]

setEncoding   [line 1054]

void setEncoding( string|null $codeset)

Set encoding of input/output files.

This will tell what encoding to use.

This method only signals which encoding the data should be in and will not actively change encoding. Make sure your data matches the encoding given!

Only a small subset of PHP encoding names are supported by therion, see $_supportedEncodings for a list.

When NULL is given, the current encoding will be reset to "unknown".

  • Throws: InvalidArgumentException when unsupported encoding is used
  • Access: public

Parameters:

string|null   $codeset   — 

[ Top ]

setFilename   [line 959]

void setFilename( string $filename)

Set filename of this file object.

This will just change the path, no data will be read/written!

  • Throws: InvalidArgumentException in case $filename is no string
  • Access: public

Parameters:

string   $filename   —  filename

[ Top ]

setHeader   [line 999]

void setHeader( string $header)

Set header string, that will be printed to generated files.
  • Access: public

Parameters:

string   $header   —  New header string

[ Top ]

setReader   [line 387]

void setReader( File_Therion_Reader $reader)

Set default reader that is used to fetch file content.
  • Access: public

Parameters:

File_Therion_Reader   $reader   — 

[ Top ]

toString   [line 933]

string toString( )

Get file lines as string.

Returns the file line content as string, suitable for writing using PHPs fwrite().

The actual content can optionally be formatted by adding File_Therion_Formatter instances with addFormatter(). This can influence line endings and wrapping for instance.

By default, the line ending used is depending on the current PHP_EOL constant. No wrapping will occur, unless the internal lines have such wrapping.

  • Return: The file contents as string
  • See: toLines()
  • Todo: Line endings should not depend on Line class implementation
  • Access: public

[ Top ]

updateLines   [line 436]

void updateLines( )

Update the internal line representation of this file from contained objects.

This will generate therion file lines out of the associated objects. Be aware that this will clear already existing line content!

  • Todo: check if toString() variants is neccessary (see comments for more info)
  • Access: public

[ Top ]

updateObjects   [line 290]

void updateObjects( )

Parses the internal line buffer to associated Therion objects.

You may use fetch() to update the internal line buffer with real physical content from the datasource. Alternatively you can craft the file yourself using addLine().

Be aware that this function cleans all references to associated objects.

  • Throws: File_Therion_SyntaxException if parse errors occur
  • Throws: InvalidArgumentException
  • Access: public

[ Top ]

write   [line 904]

void write( [File_Therion_Writer $writer = null])

Write this therion file content to the file.

The writing will be carried out by the supplied writer object. If no writer was supplied, a new DirectWriter will be instantiated as default.

  • Throws: File_Therion_Exception for other errors
  • Throws: File_Therion_IOException in case of IO error
  • Access: public

Parameters:

File_Therion_Writer   $writer   —  Writer to use.

[ Top ]

_getEncodingName   [line 1117]

string _getEncodingName( string $name)

Get internal (PHP compatible) name of encoding name.
  • Throws: InvalidArgumentException in case encoding is not supported.
  • Access: protected

Parameters:

string   $name   — 

[ Top ]

__toString   [line 946]

void __toString( )

Magic __toString() method calls toString().
  • Access: public

[ Top ]


Documentation generated on Mon, 11 Mar 2019 15:09:46 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.