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 Language.class.php
Documentation is available at
Language.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
*/
/**
* Language 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
Language Filter Class
*
@version
0.0.1
*/
gumbo_load
(
"Filter_Preg"
)
;
class
Gumbo_Filter_Language
extends
Gumbo_Filter_Preg
{
/**
@var
array
$_words
words (or word patterns) to filter */
private
$_words
=
array
(
)
;
/**
* Constructor
*
@param
string
$char
replacement character
*/
public
function
__construct
(
$char
=
null
)
{
parent
::
__construct
(
null
,
$char
,
null
)
;
$this
->
resetWords
(
)
;
}
/** ACTION METHODS **/
/**
* Runs the Filter process, returning the results
*
@param
string
$data
data to filter
*
@return
string
*/
public
function
run
(
$data
)
{
$this
->
setPattern
(
$this
->
getWords
(
true
))
;
return
parent
::
run
(
$data
)
;
}
/**
* Adds a Word (or Word Pattern) to the list
*
@param
string
$word
*
@throws
Gumbo_Exception
*/
public
function
addWord
(
$word
)
{
try
{
// verify precondition
if
(
!
is_string
(
$word
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
word
:
str
' => {
$word
}
:
"
.
gettype
(
$word
))
;
}
if
(
$this
->
isWord
(
$word
))
{
throw
new
Gumbo_Exception
(
"
Word
(
Pattern
)
Exists
: {
$word
}
"
)
;
}
$this
->
_words
[
]
=
$word
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/**
* Removes a Word (or Word Pattern) from the list
*
@param
string
$word
*
@throws
Gumbo_Exception
*/
public
function
removeWord
(
$word
)
{
try
{
// verify precondition
if
(
!
is_string
(
$word
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
word
:
str
' => {
$word
}
:
"
.
gettype
(
$word
))
;
}
if
(
!
$this
->
isWord
(
$word
))
{
throw
new
Gumbo_Exception
(
"
Word
(
Pattern
)
Undefined
: {
$word
}
"
)
;
}
foreach
(
$this
->
getWords
(
)
as
$key
=>
$val
)
{
if
(
$tag
===
$val
)
{
unset
(
$this
->
_words
[
$key
]
)
;
break
;
}
}
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/**
* Resets ALL the Words
*
@postcondition
words to default
*/
public
function
resetWords
(
)
{
$this
->
_words
=
array
(
)
;
$this
->
_words
[
]
=
"\b([\w\d]*)?[fF][uU\W][cC\W][kK\W]([\w\d]*)?\b"
;
$this
->
_words
[
]
=
"
\b(
[
\w\d
]
*)?
[
sS
$
]
[
hH
\W
]
[
iI
\W
]
[
tT
\W
]
(
[
\w\d
]
*)?\b
"
;
$this
->
_words
[
]
=
"\b[aA@][sS\$]{2}([\w\d]*)?\b"
;
$this
->
_words
[
]
=
"\b[cC][oO0\W][cC\W][kK]([sS][uU][cC][kK]([\w\d]*)?)?\b"
;
$this
->
_words
[
]
=
"\b([gG][oO][dD])?[dD][aA@\W][mM][nN]([\w\d]*)?\b"
;
$this
->
_words
[
]
=
"\b([\w\d]*)?[bB][iI][tT][cC][hH]([\w\d]*)?\b"
;
$this
->
_words
[
]
=
"\b[cC][uU][nN][tT]([\w\d]*)\b"
;
}
/** MUTATOR METHODS **/
/**
* Sets the Words (or Word Patterns) to Filter
*
@param
string
$words
*
@throws
Gumbo_Exception
*/
public
function
setWords
(
$words
=
null
)
{
if
(
is_null
(
$words
))
{
return
;
}
try
{
// verify precondition
if
(
!
is_string
(
$words
)
&&
!
is_array
(
$words
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
tag
:
str
|
arr
' => {
$words
}
:
"
.
gettype
(
$words
))
;
}
// check for multiple arguments
if
(
func_num_args
(
)
>
1
)
{
$words
=
func_get_args
(
)
;
}
// check if an array
if
(
is_array
(
$words
))
{
foreach
(
$words
as
$val
)
{
$this
->
setTags
(
$val
)
;
}
return
;
}
$this
->
addWord
(
$words
)
;
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
}
/** ACCESSOR METHODS **/
/**
* Returns ALL the Filter Words
*
@param
bool
$as_string
formats as a string pattern
*
@return
array
|
string
*/
public
function
getWords
(
$as_string
=
false
)
{
try
{
// verify precondition
if
(
!
is_bool
(
$as_string
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
as_string
:
bool
' => {
$as_string
}
:
"
.
gettype
(
$as_string
))
;
}
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
$as_string
=
false
;
}
if
(
$as_string
)
{
$pattern
=
"("
;
foreach
(
$this
->
getWords
(
)
as
$val
)
{
$pattern
.=
$val
.
"|"
;
}
return
substr
(
$pattern
,
0
,
strlen
(
$pattern
)
-
1
)
.
")"
;
}
return
$this
->
_words
;
}
/**
* Returns if the Word (Pattern) Exists
*
@param
string
$word
*
@return
bool
*/
public
function
isWord
(
$word
)
{
$found
=
false
;
try
{
// verify precondition
if
(
!
is_string
(
$word
))
{
throw
new
Gumbo_Exception
(
"
Invalid
Argument
'
word
:
str
' => {
$word
}
:
"
.
gettype
(
$word
))
;
}
foreach
(
$this
->
getWords
(
)
as
$val
)
{
if
(
$word
===
$val
)
{
$found
=
true
;
break
;
}
}
}
catch
(
Gumbo_Exception
$e
)
{
$e
->
setFunction
(
__METHOD__
)
;
gumbo_trigger
(
$e
)
;
}
return
$found
;
}
}
?>