Gumbo PHP Library API Documentation
Filter
[
class tree
] [
index
] [
all elements
]
Todo List
Packages:
Buffer
Collection
Composite
Config
Converter
Curl
Date
DB
Debug
Encryption
Error
Factory
Filter
Flyweight
Http
Input
Iterator
List
Load
Lockable
Log
Map
Number
Observer
Output
Query
Record
Router
Session
Setting
Singleton
Template
Timer
Utility
Valid
Source for file Preg.class.php
Documentation is available at
Preg.class.php
<?php
/**
* Gumbo Library Framework
*
* LICENSE
* This library is being released under the terms of the New BSD License. A
* copy of the license is packaged with the software (LICENSE.txt). If no
* copy is found, a copy of the license template can be found at:
* http://www.opensource.org/licenses/bsd-license.php
*
*
@category
Gumbo
*
@package
Filter
*
@copyright
Copyright (c) 2007, iBayou, Michael Luster
*
@license
http://www.opensource.org/licenses/bsd-license.php New BSD License
*
@author
Michael Luster <mluster79@yahoo.com>
*
@link
http://sourceforge.net/projects/phpgumbo
*
@version
0.0.1
*/
/**
* PREG Filter Class
*
*
@category
Gumbo
*
@package
Filter
*
@copyright
Copyright (c) 2007, iBayou, Michael Luster
*
@license
http://www.opensource.org/licenses/bsd-license.php New BSD License
*
@author
Michael Luster <mluster79@yahoo.com>
*
@link
http://sourceforge.net/projects/phpgumbo
*
@desc
PREG Filter Class
*
@version
0.0.1
*/
gumbo_load
(
"Interface_Filter"
)
;
class
Gumbo_Filter_Preg
implements
Gumbo_Interface_Filter
{
/**
@var
string
$_pattern
regular expression pattern */
private
$_pattern
;
/**
@var
string
$_char
replacement character */
private
$_char
;
/**
@var
int
$_limit
*/
private
$_limit
= -
1
;
/**
@var
int
$_count
occurences of pattern */
private
$_count
=
0
;
/**
* Constructor
*
@param
string
$pattern
regular expression pattern
*
@param
string
$char
replacement character
*
@param
int
$limit
*/
public
function
__construct
(
$pattern
=
null
,
$char
=
null
,
$limit
=
null
)
{
if
(
!
is_null
(
$pattern
))
{
$this
->
setPattern
(
$pattern
)
;
}
if
(
!
is_null
(
$limit
))
{
$this
->
setLimit
(
$limit
)
;
}
$this
->
setChar
(
$char
)
;
}
/** ACTION METHODS **/
/**
* Runs the Filter process, returning the results
*
@param
string
|
array
$data
data to filter
*
@return
string
|
array
*/
public
function
run
(
$data
)
{
$res
=
$data
;
try
{
// verify precondition
if
(
!
$this
->
getPattern
(
))
{
throw
new
Gumbo_Exception
(
"Filter Missing Required Pattern"
)
;
}
if
(
!
is_string
(
$data
)
&&
!
is_array
(
$data
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
data
:
str
|
arr
' => {
$data
}
:
"
.
gettype
(
$data
))
;
}
$count
=
0
;
$res
=
preg_replace
(
$this
->
getPattern
(
)
,
$this
->
getChar
(
)
,
$data
,
$this
->
getLimit
(
)
,
$count
)
;
$this
->
setCount
(
$count
)
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
return
$res
;
}
/** MUTATOR METHODS **/
/**
* Sets the Regular Expression Pattern
*
@param
string
$pattern
*
@throws
Gumbo_Exception
*/
public
function
setPattern
(
$pattern
)
{
try
{
// verify precondition
if
(
!
is_string
(
$pattern
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
pattern
:
str
' => {
$pattern
}
:
"
.
gettype
(
$pattern
))
;
}
$this
->
_pattern
=
$pattern
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/**
* Sets the replacement character
*
@param
string
$char
*
@throws
Gumbo_Exception
*/
public
function
setChar
(
$char
=
null
)
{
try
{
// verify precondition
if
(
!
is_null
(
$char
)
&&
!
is_string
(
$char
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
char
:
str
|
null
' => {
$char
}
:
"
.
gettype
(
$char
))
;
}
$this
->
_char
=
$char
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/**
* Sets the Limit
*
@precondition
$limit >= -1 (no limit)
*
@param
int
$limit
*
@throws
Gumbo_Exception
*/
public
function
setLimit
(
$limit
)
{
try
{
// verify precondition
if
(
!
is_int
(
$limit
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
limit
:
int
' => {
$limit
}
:
"
.
gettype
(
$limit
))
;
}
if
(
$limit
< -
1
)
{
throw
new
Gumbo_Exception
(
"
Out
Of
Range
'
limit
>= -1' => {
$limit
}
"
)
;
}
$this
->
_limit
=
$limit
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/**
* Sets the count
*
@precondition
$count >= 0
*
@param
int
$count
*
@throws
Gumbo_Exception
*/
protected
function
setCount
(
$count
)
{
try
{
// verify precondition
if
(
!
is_int
(
$count
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
count
:
int
' => {
$count
}
:
"
.
gettype
(
$count
))
;
}
if
(
$count
<
0
)
{
throw
new
Gumbo_Exception
(
"
Out
Of
Range
'
count
>= 0' => {
$count
}
"
)
;
}
$this
->
_count
=
$count
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/** ACCESSOR METHODS **/
/**
* Returns the Regular Expression Pattern
*
@return
string
*/
public
function
getPattern
(
)
{
return
$this
->
_pattern
;
}
/**
* Returns the replacement character
*
@return
string
*/
public
function
getChar
(
)
{
return
$this
->
_char
;
}
/**
* Returns the limit
*
@return
int
*/
public
function
getLimit
(
)
{
return
$this
->
_limit
;
}
/**
* Returns the count of occurences of the pattern
*
@return
int
*/
public
function
getCount
(
)
{
return
$this
->
_count
;
}
}
?>