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

Class: File_Fortune

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

Class Overview


File_Fortune: Interface to fortune cookie databases


Author(s):

Version:

  • Release: 0.9.0

Variables

Methods


Child classes:

File_Fortune_Writer
File_Fortune_Writer: Write and modify fortune files

Inherited Variables

Inherited Methods


Class Details

[line 47]
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.

If you wish to be able to write or manipulate fortunes, you may be interested in File_Fortune_Writer.



[ Top ]


Class Variables

$delim =

[line 61]

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

Type:   string


[ Top ]

$file =

[line 68]

Fortune file filehandle
  • Access: protected

Type:   resource


[ Top ]

$filename =

[line 75]

Name of fortune file to use
  • Access: public

Type:   string


[ Top ]

$flags =

[line 82]

Bit field for flags
  • Access: protected

Type:   array


[ Top ]

$fortunes =

[line 89]

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

Type:   array


[ Top ]

$headerFilename =

[line 96]

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

Type:   string


[ Top ]

$maxLength =

[line 103]

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

Type:   int


[ Top ]

$minLength =

[line 110]

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

Type:   int


[ Top ]

$noHeader =  false

[line 118]

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

Type:   bool


[ Top ]

$numstr =

[line 125]

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

Type:   int


[ Top ]

$offsets =

[line 132]

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

Type:   array


[ Top ]

$version =

[line 139]

Version number for fortune file
  • Access: public

Type:   int


[ Top ]

$VERSION =  '0.9.0'

[line 54]

Version
  • Staticvar: string 0:
  • Access: public

Type:   mixed


[ Top ]



Method Detail

__construct (Constructor)   [line 163]

File_Fortune __construct( [string $filename = null], [string $headerFilename = null])

Constructor; initialize a fortune cookie database

Initializes the PEAR_ErrorStack and assigns it to $error.

If a fortune file is provided to the constructor, it attempts to open it. If no $headerFilename is provided, "$filename.dat" will be used. The open operation is performed by open(); if an error occurs with that operation, an error will be pushed onto the $error.

If no data file is given, the class will be instantiated with no data, and you will need to open a file before performing any operations.

  • Throws: File_Fortune_Exception if unable to open file; see open()
  • Access: public

Parameters:

string   $filename   —  Fortune file name
string   $headerFilename   —  Fortune header file name; defaults to "$filename.dat"

[ Top ]

__destruct (Destructor)   [line 181]

void __destruct( )

Destructor

Closes fortune file, utilizing close().

  • Access: public

[ Top ]

close   [line 281]

true close( )

Close fortune file

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

  • Access: public

[ Top ]

getAll   [line 664]

array|false getAll( )

Retrieve all fortunes in a file

Retrieves all fortunes in a file, returning them as an array and placing them in the $fortunes property. If the $numstr property has not been set, or if an error occurs elsewhere along the chain, throws an exception.

  • Throws: File_Fortune_Exception_NotOpen if the fortune file hasn't yet been opened
  • Throws: File_Fortune_Exception_HeaderNotRead if header file hasn't been read
  • Throws: File_Fortune_Exception_Offset if unable to seek to fortune offset
  • Throws: File_Fortune_Exception_InvalidCount if an invalid fortune number is passed
  • Throws: File_Fortune_Exception_NotOpen if fortune file is not open
  • Throws: File_Fortune_Exception_InvalidID if ID is out of range
  • Access: public

[ Top ]

getRandom   [line 628]

string|false getRandom( )

Return a random fortune

Picks a random fortune for you and reads and returns it with read(). If the $numstr propery has not been set, throws an exception.

  • Throws: File_Fortune_Exception_NotOpen if the fortune file hasn't yet been opened
  • Throws: File_Fortune_Exception_HeaderNotRead if header file hasn't been read
  • Throws: File_Fortune_Exception_Offset if unable to seek to fortune offset
  • Throws: File_Fortune_Exception_InvalidCount if an invalid fortune number is passed
  • Throws: File_Fortune_Exception_NotOpen if fortune file is not open
  • Throws: File_Fortune_Exception_InvalidID if ID is out of range
  • Access: public

[ Top ]

getRandomFromSet   [line 705]

string getRandomFromSet( )

Retrieve a random fortune from a list of fortune files

Returns a random fortune from a list of fortune files. The list of files may be either specified as individual arguments, as a single array argument, or as a mixture of individual files and arrays of files. Invalid arguments -- non-files -- will be skipped.

  • Throws: File_Fortune_Exception_NoFilename if no filenames provided
  • Throws: File_Fortune_Exception subclass based on result of open() and getRandom() calls
  • Access: public

[ Top ]

num   [line 498]

int|false num( )

Return number of fortunes

Returns the number of fortunes found by readHeader(). If the $numstr property has not been set, throws an exception.

  • Throws: File_Fortune_Exception_HeaderNotRead if $numstr is empty.
  • Access: public

[ Top ]

open   [line 227]

true open( [string $filename = null], [string $headerFilename = null])

Open a fortune file

Opens a fortune file. If no $headerFilename is given, "$filename.dat" is used; if $headerFilename is invalid or not present, the class will parse the fortune file in order to build the necessary header elements.

Once the file has been opened, its file handle will be assigned to $file. Additionally, a number of other properties will be set:

If no header file is present, open() will read the entire file into memory, and store all fortunes in $fortunes.

Returns true on success; on failure, throws an exception.

Note: If a file is already open when open() is called, that file is first closed.

  • Throws: File_Fortune_Exception_NoFilename if no filename passed and no filename in object
  • Throws: File_Fortune_Exception_BadFile if missing file or unable to read from file
  • Throws: File_Fortune_Exception_BadHeaderFile if unable to open header file, unable to read header file, unable to get header information, or if header information appears corrupted
  • Access: public

Parameters:

string   $filename   —  Name of fortune file (defaults to $filename)
string   $headerFilename   —  Name of fortune file header file (defaults to either $headerFilename or $filename.dat)

[ Top ]

read   [line 538]

string|false read( [int $num = 1])

Return a fortune

Reads string number $num from the open fortune file. $num is ordinal, ie. it must be between 1 and $numstr (inclusive). Throws an exception if you haven't opened the file and read the header, or if $num is out of range. (Opening the file is pretty hard to screw up, since it's taken care of for you by open(), and any real errors should occur during that call.) Returns the text of the fortune as a (possibly) multiline string.

In order to read a fortune, one of the following actions is taken:

  • If $fortunes is populated, it grabs the fortune from there.
  • Otherwise, it utilizes _readFromFile() to grab the fortune from the fortune file.

  • Throws: File_Fortune_Exception_NotOpen if the fortune file hasn't yet been opened
  • Throws: File_Fortune_Exception_InvalidCount if an invalid fortune number is passed
  • Throws: File_Fortune_Exception_NotOpen if fortune file is not open
  • Throws: File_Fortune_Exception_Offset if unable to seek to fortune offset
  • Throws: File_Fortune_Exception_InvalidID if ID is out of range
  • Access: public

Parameters:

int   $num   —  Index of fortune to return

[ Top ]

_computeHeader   [line 442]

true _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. (If this is the case, you may also want to look at File_Fortune_Writer::write().

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_BadFile if unable to open fortune file
  • Access: protected

Parameters:

string   $delim   —  Defaults to '%'

[ Top ]

_fileLoop   [line 733]

array _fileLoop( array $array)

Loop through an array looking for files

Loops through an array, searching for files. An array of unique files found is returned.

  • Access: public

Parameters:

array   $array   — 

[ Top ]

_readFromFile   [line 580]

string|false _readFromFile( int $num)

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_NotOpen if fortune file is not open
  • Throws: File_Fortune_Exception_InvalidID if ID is out of range
  • Throws: File_Fortune_Exception_Offset if unable to seek to fortune offset
  • Access: protected

Parameters:

int   $num   — 

[ Top ]

_readHeader   [line 345]

true _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 numFortunes() method; 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;

_readHeader() throws an exception if there are any problems reading the header file, e.g. if it seems to be corrupt or truncated.

  • Throws: File_Fortune_Exception_BadFile if unable to open fortune file if computing headers directly from fortune file is necessary
  • Throws: File_Fortune_Exception_BadHeaderFile if unable to open header file, unable to read header file, unable to get header information, or if header information appears corrupted
  • Access: protected

[ Top ]


Documentation generated on Mon, 11 Mar 2019 14:18:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.