Analyzer
[
class tree: Analyzer
] [
index: Analyzer
] [
all elements
]
Packages:
Analyzer
Base
Cache
Config
Container
Creator
Log
Memory
Pattern
Registry
Stat
Word
Source for file Pattern.php
Documentation is available at
Pattern.php
<?php
/**
* Mai_Analyzer_Pattern
*
* Copyright (C) 2009 Mai (Me Artificial Intelligence)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
@author
Wouter Bulten (wouterbulten@mai-ai.org)
*
@copyright
Copyright (C) 2009 Mai (Me Artificial Intelligence)
*
@license
http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
*
@link
http://www.launchpad.net/mai
*
*
@category
Mai
*
@package
Analyzer
*
@subpackge
Pattern
*
*/
/** Load required file */
if
(
!
Mai
::
getAutoloader
(
))
{
require_once
'Mai/Analyzer/Interface.php'
;
}
/**
* Mai_Analyzer_Pattern compares a pattern with given sources.
*
*
@author
Wouter Bulten (wouterbulten@mai-ai.org)
*
@copyright
Copyright (C) 2009 Mai (Me Artificial Intelligence)
*
@license
http://opensource.org/licenses/gpl-3.0.html GNU General Public License version 3 (GPLv3)
*
@link
http://www.launchpad.net/mai
*
*
@category
Mai
*
@package
Analyzer
*
@subpackage
Pattern
*
*/
class
Mai_Analyzer_Pattern
implements
Mai_Analyzer_Interface
{
/**
*
@var
string
The pattern to analyze.
*/
private
$_patternString
;
/**
*
@var
array
The pattern (in array) to analyze.
*/
private
$_patternArray
;
/**
*
@var
array
An array with different input sources.
*/
private
$_sources
=
array
(
)
;
/**
*
@var
array
An array with information about the response.
*/
private
$_response
=
array
(
)
;
/**
*
@var
int
The type of the response.
*/
private
$_responseType
;
/**
* Check if Mai has been setup and that the creator is Mai_Analyzer.
*/
public
function
__construct
(
$creator
)
{
if
(
!
Mai
::
isInit
(
))
{
trigger_error
(
'Mai has not been setup yet. Please use Mai::init().'
,
E_USER_ERROR
)
;
}
if
(
!
(
$creator
instanceof
Mai_Analyzer
)
&&
!
Mai_Config
::
getVar
(
)
->
analyzer
->
allow_direct_access
)
{
trigger_error
(
'Please use an instance of Mai_Analyzer to create an instance of Mai_Analyzer_Pattern.'
)
;
}
}
/**
* Analyze the pattern.
*/
public
function
run
(
)
{
//patterns must be valid
if
(
empty
(
$this
->
_patternString
)
||
empty
(
$this
->
_patternArray
))
{
throw
new
UnexpectedValueException
(
'Pattern (string or array) has not been correctly set.'
)
;
}
if
(
empty
(
$this
->
_sources
))
{
trigger_error
(
'No sources set!'
,
E_USER_NOTICE
)
;
}
//First step, compare string
$stringCompare
=
$this
->
compareString
(
)
;
//If return is false we go to the next step
if
(
$stringCompare
===
false
)
{
}
else
{
//Set the response and type
$this
->
_response
=
$stringCompare
;
$this
->
_responseType
=
Mai
::
AN_RESPONSE_FULL_PATTERN
;
}
}
/**
* Compare the string version of the pattern.
* Fast and easy but success is limited.
*
@return
array
|
bool
*/
private
function
compareString
(
)
{
foreach
(
$this
->
_sources
as
$name
=>
$data
)
{
foreach
(
$data
as
$key
=>
$value
)
{
if
(
$this
->
_patternString
===
$value
[
'pattern'
]
)
{
//Return data
return
array
(
$key
=>
$value
)
;
}
}
}
//Nothing found
return
false
;
}
/**
* Set data to analyze.
*
@param
string
$type
Can be 'pattern-array' or 'pattern-string'.
*
@param
array
$data
*/
public
function
set
(
$type
,
array
$data
)
{
switch
(
$type
)
{
case
'pattern-array'
:
$this
->
_patternArray
=
$patternArray
;
break
;
case
'pattern-string'
:
$this
->
_patternString
=
$patternString
;
break
;
default
:
trigger_error
(
'Unknown type to set.'
,
E_USER_ERROR
)
;
break
;
}
}
/**
* Add a pattern source to the analyzer.
*
@param
string
$name
*
@param
array
$data
*/
public
function
addSource
(
$name
,
array
$data
)
{
if
(
empty
(
$name
)
||
!
is_string
(
$name
))
{
throw
new
UnexpectedValueException
(
'Name of source has not been set or is invalid!'
)
;
}
$this
->
_sources
[
$name
]
=
$data
;
}
/**
* Remove source.
*
@param
string
$name
*/
public
function
removeSource
(
$name
)
{
if
(
array_key_exists
(
$name
,
$this
->
_sources
))
{
unset
(
$this
->
_sources
[
$name
]
)
;
}
else
{
trigger_error
(
'Source '
.
$name
.
' does not excist.'
,
E_USER_NOTICE
)
;
}
}
}
Documentation generated on Mon, 27 Jul 2009 19:55:11 +0200 by
phpDocumentor 1.4.1