MatchRegularExpression Class Reference

Inheritance diagram for MatchRegularExpression:

MatchListInterface MatchInterface

List of all members.

Public Member Functions

 setNotFoundValue ($objNotFoundValue)
 getNotFoundValue ()
 setDefaultItemValue ($objDefaultItemValue)
 getDefaultItemValue ()
 setItemList (array $arrItemList, $arrValues=null)
 getItemList ()
 addItem ($objItem, $objValue=null)
 isEmpty ()
 found ($objName)
 match ($objName)

Protected Attributes

 $objNotFoundValue = false
 $objDefaultItemValue = true
 $arrItemList = array()
 $arrValues = array()


Detailed Description

Author:
Thiago Henrique Ramos da Mata <thiago.henrique.mata@gmail.com>
Since:
2009-06-19
Math the string name with a string list

The itens are strings with regular expression and the name is a string. The compare item and name returns true when the regular expression item match with the name by the ereg() native php function.

The values <value> start will boolean values, but is not restrict to it. The not found value <value> by default is false. The default found value <value> it is by default true.

Example:
      $objMatchRegularExpression = new MatchRegularExpression();
      $objMatchRegularExpression->addItem( "M(.*)y" );
      $objMatchRegularExpression->addItem( "A(.*)e" );
      $objMatchRegularExpression->addItem( "Winter(.*)" , "machine" );
      $objMatchRegularExpression->addItem( "^Case$" , "hacker" );
      if ( $objMatchRegularExpression->found( "Molly" ) !== true ) return false;
      if ( $objMatchRegularExpression->match( "Molly" ) !== true ) return false;
      if( $objMatchRegularExpression->match( "Wintermute" ) !== "machine" ) return false;
      if( $objMatchRegularExpression->match( "Flatline" ) !== false ) return false;
      return true;
returns
 true 

Definition at line 36 of file MatchRegularExpression.class.php.


Member Function Documentation

addItem ( objItem,
objValue = null 
)

Add a item into the item list

Implements:
MatchListInterface::addItem( <item> object [, <value> object ])
See also:
MatchListInterface::setItemList( string[] )

MatchRegularExpression::setItemList( string[] )

MatchListInterface::getItemList()

MatchRegularExpression::getItemList()

Parameters:
string $objItem
<value> $objValue
Returns:
MatchListInterface me

Implements MatchListInterface.

Definition at line 190 of file MatchRegularExpression.class.php.

References getDefaultItemValue().

Referenced by setItemList().

00191     {
00192         if( $objValue === null )
00193         {
00194             $objValue = $this->getDefaultItemValue();
00195         }
00196         $intItemKey = sizeof( $this->arrItemList );
00197         $this->arrItemList[ $intItemKey ] = (string) $objItem;
00198         $this->arrValues[ $intItemKey ] = $objValue;
00199     }

found ( objName  ) 

Match the name into the item list and returns true if some item successfully match or false if not

Execution Plan:
  • get the item list
  • for each item into the list
  • try match the item with the name
    • if match returns true
    • if no item match, returns false
Implements:
MatchListInterface::found( <name> object )
Parameters:
string $objName
Returns:
boolean

Implements MatchInterface.

Definition at line 252 of file MatchRegularExpression.class.php.

References getItemList().

00253     {
00254         $strName = (string) $objName;
00255                 foreach( $this->getItemList() as $strItemRegex )
00256                 {
00257                         if( ereg( $strItemRegex , $strName ) )
00258                         {
00259                                 return true;
00260                         } 
00261                 }
00262         return false;
00263     }

getDefaultItemValue (  ) 

Get the default item value.

This value <value> what will be saved on some string item when this setter dont inform its value

Implements:
MatchListInterface::getDefaultItemValue()
Returns:
object <value>

Implements MatchListInterface.

Definition at line 125 of file MatchRegularExpression.class.php.

Referenced by addItem().

00126     {
00127         return $this->objDefaultItemValue;
00128     }

getItemList (  ) 

Get the array with the item list into the match

Implements:
MatchListInterface::getItemList()
Returns:
string[] $arrItemList

Implements MatchListInterface.

Definition at line 173 of file MatchRegularExpression.class.php.

Referenced by found(), isEmpty(), and match().

00174     {
00175         return $this->arrItemList;
00176     }

getNotFoundValue (  ) 

Get the not found value.

This value will be returned when the name received don't match with any element into the item list in the match() method

Implements:
MatchListInterface::getNotFoundValue()
See also:
MatchListInterface::match( <name> object )

MatchRegularExpression::match( string )

Returns:
object <value>

Implements MatchInterface.

Definition at line 93 of file MatchRegularExpression.class.php.

Referenced by match().

00094     {
00095         return $this->objNotFoundValue;
00096     }

isEmpty (  ) 

Returns true if the item list is empty returns false if not.

If is empty the match allways will return the not found value

Execution Plan:
  • read the list itens.
  • check if the list is empty
    • returns true if the list is empty
    • returns false if the list is not empty
Implements:
MatchListInterface::isEmpty()
Returns:
boolean

Implements MatchListInterface.

Definition at line 223 of file MatchRegularExpression.class.php.

References getItemList().

00224     {
00225         return ( sizeof( $this->getItemList() ) == 0 );
00226     }

match ( objName  ) 

Match the string name with the list string item return the value <value> of the first item what match or the not found value when no item match.

Execution Plan:
  • get the item list
  • for each item into the list
  • try match the item with the name
    • if match returns the value of the item
    • if no item match, returns the not found value
Implements:
MatchListInterface::match( <name> object )
See also:
MatchListInterface::getNotFoundValue()

MatchRegularExpression::getNotFoundValue()

MatchListInterface::getItemList()

MatchRegularExpression::getItemList()

Parameters:
string $objName
Returns:
object <value>

Implements MatchInterface.

Definition at line 293 of file MatchRegularExpression.class.php.

References getItemList(), and getNotFoundValue().

00294     {
00295         //print_r( $this->arrValues );
00296         $strName = (string) $objName;
00297         $intKeyValue = false;
00298 
00299         // for each item into the list //
00300         foreach( $this->getItemList() as $intKeyItem => $strItemRegex )
00301         {
00302             // try match the item with the name //
00303             if( ereg( $strItemRegex , $strName ) )
00304                 {
00305                 $intKeyValue = $intKeyItem;
00306                 break;
00307                 } 
00308         }
00309         
00310         if( $intKeyValue === false )
00311         {
00312             // if no item match, returns the not found value //
00313             return $this->getNotFoundValue();
00314         }
00315         else
00316         {
00317             // if match returns the value of the item //
00318             return $this->arrValues[ $intKeyValue ];
00319         }
00320     }

setDefaultItemValue ( objDefaultItemValue  ) 

Set the default item value.

Changing this element will change how the value of the itens string[] what not informed they values into the setItemList() and addItem()

Implements:
MatchListInterface::setDefaultItemValue( <value> object )
See also:
MatchListInterface::setItemList( <item>[] [, <value>[] ] )

MatchRegularExpression::setItemList( string[] [, <value>[] ] )

Parameters:
object $objNotFoundValue
Returns:
MatchRegularExpression me

Implements MatchListInterface.

Definition at line 110 of file MatchRegularExpression.class.php.

References $objDefaultItemValue.

00111     {
00112         $this->objDefaultItemValue = $objDefaultItemValue;
00113         return $this;
00114     }

setItemList ( array $  arrItemList,
arrValues = null 
)

Set the array with the item list into the match. Can receive the array with the values of each item. If the array with values not received, the default value to the itens will be true

Implements:
MatchListInterface::setItemList( <item>[] [ , <value>[] ])
See also:
MatchListInterface::getItemList()

MatchRegularExpression::getItemList()

Parameters:
string[] $arrItemList
<value>[] $arrValues
Returns:
MatchListInterface me
Exceptions:
MatchException 

Implements MatchListInterface.

Definition at line 144 of file MatchRegularExpression.class.php.

References $arrValues, and addItem().

00145     {
00146         if( $arrValues !== null )
00147         {
00148             if( sizeof( $arrValues ) !== sizeof( $arrItemList ) )
00149             {
00150                 throw new MatchException( "Invalid value list to the respective item list" );
00151             }
00152         }
00153         else
00154         {
00155             $arrValues = array_pad( array() , null , sizeof( $arrItemList ) );
00156         }
00157         
00158         foreach( $arrItemList as $intKey => $objItem )
00159         {
00160             $objValue = $arrValues[ $intKey ];
00161             $this->addItem( $objItem , $objValue );
00162         }
00163         
00164         return $this;
00165     }

setNotFoundValue ( objNotFoundValue  ) 

Set the not found value.

Changing this value will change how the result of the match() method when not item match with the received name

Implements:
MatchListInterface::match( <name> object )
See also:
MatchListInterface::setNotFoundValue( <value> object )

MatchRegularExpression::setNotFoundValue( <value> object )

Parameters:
object $objNotFoundValue
Returns:
MatchRegularExpression me

Implements MatchInterface.

Definition at line 76 of file MatchRegularExpression.class.php.

References $objNotFoundValue.

00077     {
00078         $this->objNotFoundValue = $objNotFoundValue;
00079     }


Member Data Documentation

$arrItemList = array() [protected]

string[]

Definition at line 55 of file MatchRegularExpression.class.php.

$arrValues = array() [protected]

<value>[]

Definition at line 61 of file MatchRegularExpression.class.php.

Referenced by setItemList().

$objDefaultItemValue = true [protected]

Default: true
<value> object

Definition at line 48 of file MatchRegularExpression.class.php.

Referenced by setDefaultItemValue().

$objNotFoundValue = false [protected]

Default: false
<value> object

Definition at line 42 of file MatchRegularExpression.class.php.

Referenced by setNotFoundValue().


The documentation for this class was generated from the following file:

Generated on Thu Feb 3 03:55:03 2011 for CodeToDiagram by  doxygen 1.5.9