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

Class: File_Fortune

Source Location: /File_Fortune-1.0.0/File/Fortune.php

Class Overview


File_Fortune


Author(s):

Version:

  • @release-version@

Variables

Methods


Inherited Variables

Inherited Methods


Class Details

[line 35]
File_Fortune

File_Fortune: Interface to fortune cookie databases

The <em>fortune</em> program is a small but important part of *nix culture, and this package aims to provide support for its "fortune cookie" databases to PHP programmers.

  • Tutorial: File_Fortune.cls
  • Version: @release-version@
  • Uses: Iterator
  • Uses: Countable
  • Uses: ArrayAccess


[ Top ]


Class Variables

$delim =  '%'

[line 43]

Character that delimits fortunes (defaults to '%')
  • Access: public

Type:   string


[ Top ]

$maxLength =

[line 49]

Length of longest string in the fortune file
  • Access: public

Type:   int


[ Top ]

$minLength =

[line 55]

Length of shortest string in the fortune file
  • Access: public

Type:   int


[ Top ]

$numstr =

[line 128]

Number of strings (fortunes) in the file
  • Access: public

Type:   int


[ Top ]

$offsets =

[line 134]

Array of file offsets (fortune indices)
  • Access: protected

Type:   array


[ Top ]

$version =

[line 61]

Version number for fortune file
  • Access: public

Type:   int


[ Top ]

$_changed =  false

[line 67]

Flag - has an operation changed the file?
  • Access: protected

Type:   boolean


[ Top ]

$_curOffset =  1

[line 73]

Current offset (used by iterator)
  • Access: protected

Type:   int


[ Top ]

$_directory =

[line 79]

Directory containing fortune files
  • Access: protected

Type:   string


[ Top ]

$_file =

[line 85]

Fortune file filehandle
  • Access: protected

Type:   resource


[ Top ]

$_filename =

[line 91]

Name of fortune file to use
  • Access: protected

Type:   string


[ Top ]

$_files =

[line 97]

Array of fortune files found in $_directory
  • Access: protected

Type:   array


[ Top ]

$_flags =

[line 103]

Bit field for flags
  • Access: protected

Type:   array


[ Top ]

$_fortunes =

[line 109]

Array of fortunes; unset by default.
  • Access: protected

Type:   array


[ Top ]

$_headerFile =

[line 115]

Name of fortune header file (.dat file)
  • Access: protected

Type:   string


[ Top ]

$_noHeader =  false

[line 122]

Flag indicating whether or not a header file is present. Defaults to false.
  • Access: protected

Type:   bool


[ Top ]



Method Detail

__construct (Constructor)   [line 147]

void __construct( [string|array $file = null], [string $headerFile = null])

Constructor

Optionally pass a filename or directory name to set the fortune file or directory, and, if passing a fortune file name, optionally pass the name of the header file.

  • Access: public

Parameters:

string|array   $file     
string   $headerFile     

[ Top ]

__destruct (Destructor)   [line 167]

void __destruct( )

Destructor

Close open files

  • Access: public

[ Top ]

add   [line 540]

File_Fortune add( string $fortune)

Add a new fortune
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

string   $fortune     

[ Top ]

count   [line 299]

int count( )

Count of fortunes in current active file, or of all fortunes in all files
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

create   [line 669]

void create( $fortunes, [string $file = null])

Create a new fortune file from an array of fortunes
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

array   $fortunes     
string   $file     

[ Top ]

current   [line 178]

string current( )

Return current fortune in iterator
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

delete   [line 577]

void delete( int $index)

Delete an existing fortune
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

int   $index     

[ Top ]

getAll   [line 614]

array getAll( )

Retrieve all fortunes from the current file or set of files
  • Access: public

[ Top ]

getDirectory   [line 528]

string getDirectory( )

Retrieve current directory of fortune files
  • Access: public

[ Top ]

getFile   [line 430]

string getFile( )

Retrieve current fortune file name
  • Access: public

[ Top ]

getFiles   [line 497]

array getFiles( )

Return list of fortune files in use

Returns a list of fortune files in use, either as registered via setFiles() or as determined by traversing the directory set via setDirectory().

  • Access: public

[ Top ]

getHeaderFile   [line 452]

string getHeaderFile( )

Retrieve current header file name
  • Access: public

[ Top ]

getRandom   [line 592]

string getRandom( )

Retrieve random fortune
  • Access: public

[ Top ]

key   [line 201]

int key( )

Return current iterator key
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

next   [line 224]

string|false next( )

Retrieve next element in iterator
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

offsetExists   [line 333]

boolean offsetExists( int $offset)

Does fortune exist at the given offset?
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

int   $offset     

[ Top ]

offsetGet   [line 349]

string offsetGet( int $offset)

Retrieve fortune by offset
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

int   $offset     

[ Top ]

offsetSet   [line 366]

void offsetSet( int $offset, string $value)

Set a fortune at a given offset
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

int   $offset     
string   $value     

[ Top ]

offsetUnset   [line 386]

void offsetUnset( int $offset)

Delete a fortune at a given offset
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

int   $offset     

[ Top ]

rewind   [line 252]

bool rewind( )

Rewind iterator to first element
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

save   [line 650]

void save( )

Save changes
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

setDirectory   [line 511]

File_Fortune setDirectory( string $directory)

Set directory from which to randomly select a fortune file
  • Access: public

Parameters:

string   $directory     

[ Top ]

setFile   [line 410]

File_Fortune setFile( string $file, [string $headerFile = null])

Set fortune file

Setting a file overwrites any directory set as well.

  • Access: public

Parameters:

string   $file     
string   $headerFile     

[ Top ]

setFiles   [line 463]

File_Fortune setFiles( )

Set one or more files to search for fortunes
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

setHeaderFile   [line 441]

File_Fortune setHeaderFile( string $headerFile)

Set header file name
  • Access: public

Parameters:

string   $headerFile     

[ Top ]

update   [line 560]

void update( int $index, string $fortune)

Update an existing fortune
  • Throws: File_Fortune_Exception
  • Access: public

Parameters:

int   $index     
string   $fortune     

[ Top ]

valid   [line 276]

bool valid( )

Current element is valid (iterator)
  • Throws: File_Fortune_Exception
  • Access: public

[ Top ]

_close   [line 938]

void _close( )

Close fortune file

Closes the fortune file if it's open, and unsets all header-related properties (except $delim); does nothing otherwise.

  • Access: protected

[ Top ]

_computeHeader   [line 886]

void _computeHeader( [string $delim = '%'])

Compute fortune file header information

Reads the contents of the fortune file and computes the header information that would normally be found in a header (.dat) file and read by _readHeader(). This is useful if you maintain a file of fortunes by hand and do not have the corresponding data file.

An optional delimiter argument may be passed to this function; if present, that delimiter will be used to separate entries in the fortune file. If not provided, a percent sign ("%") will be used (this is the standard fortune file delimiter).

Additionally, the $noHeader property will be set to true.

NOTE: It is most efficient to use fortune files that have header files. In order to get offsets and fortunes, this method actually must read the entire fortune file, and stores all fortunes in the $_fortunes array.

  • Throws: File_Fortune_Exception
  • Access: protected

Parameters:

string   $delim     Defaults to '%'

[ Top ]

_getAll   [line 1190]

void _getAll( )

Retrieve all fortunes from the current active file
  • Access: protected

[ Top ]

_getOne   [line 1172]

string _getOne( int $offset)

Retrieve a single fortune by offset
  • Throws: File_Fortune_Exception
  • Access: protected

Parameters:

int   $offset     

[ Top ]

_getRandomFile   [line 762]

void _getRandomFile( )

Randomly select a fortune file from a registered directory
  • Access: protected

[ Top ]

_initChanges   [line 997]

void _initChanges( )

Initialize changes
  • Access: protected

[ Top ]

_offsetExists   [line 1147]

boolean _offsetExists( int $offset)

Retrieve by offset in file
  • Access: protected

Parameters:

int   $offset     

[ Top ]

_open   [line 694]

void _open( )

Open a fortune file
  • Throws: File_Fortune_Exception
  • Access: protected

[ Top ]

_readFromFile   [line 973]

string _readFromFile( int $offset)

Read a fortune from the file

Reads a fortune directly from the file. If an error occurs, an exception is thrown. Otherwise, on sucess, the fortune is returned.

  • Throws: File_Fortune_Exception
  • Access: protected

Parameters:

int   $offset     

[ Top ]

_readHeader   [line 808]

void _readHeader( )

Read a fortune database header file

Reads the header file associated with this fortune database. The name of the header file is determined by the constructor: either it is based on the name of the data file, or supplied by the caller.

If the header file does not exist, this function calls _computeHeader() automatically, which has the same effect as reading the header from a file.

The header contains the following values, which are stored as attributes of the File_Fortune object:

  • $version; version number
  • $numstr; number of strings (fortunes) in the file
  • $maxLength; length of longest string in the file
  • $minLength; length of shortest string in the file
  • $flags; bit field for flags (see strfile(1) man page)
  • $delim; character that delimits fortunes

$numstr is available via the count() function (count($fortunes)); if you're interested in the others, you'll have to go grubbing through the File_Fortune object, e.g.:

  1.  $fortune_file = new Fortune('fortunes');
  2.  $delim        $fortune_file->delim;
  3.  $maxLength    $fortune_file->maxLength;

  • Throws: File_Fortune_Exception
  • Access: protected

[ Top ]

_save   [line 1013]

void _save( )

Save changes to disc

Changes to fortunes happen in-memory; this method saves them to disc.

  • Throws: File_Fortune_Exception
  • Access: protected

[ Top ]

_traverseDirectory   [line 732]

void _traverseDirectory( )

Traverse a registered directory to get a list of files
  • Throws: File_Fortune_Exception
  • Access: protected

[ Top ]

_writeHeader   [line 1101]

void _writeHeader( )

Write the header file for a fortune file

Writes a fortune header file to $headerFilename, using $version, $numstr, $maxLength, $minLength, $flags, $delim, and $offsets.

If an error occurs, an exception is thrown.

  • Throws: File_Fortune_Exception
  • Access: protected

[ Top ]


Documentation generated on Tue, 24 Jul 2007 09:30:09 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.