-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParserFactoryI.php
133 lines (124 loc) · 4.31 KB
/
ParserFactoryI.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/*
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* <[email protected]> wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some
* day, and you think this stuff is worth it, you can buy me a beer in return.
* Benjamin Wöster
* ----------------------------------------------------------------------------
*/
/**
* @author Benjamin Wöster <[email protected]>
* @package libIwParsers
* @subpackage interfaces
*/
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/**
* A factory for instantiating parsers for a given text.
*
* Usage is intended to be as follows:
*
* @code
* $text = ... //get the text from somewhere...
* $parserFactoryClassName = ... //get the classname of a class implementing
* //ParserFactoryI
* $rcParserFactory = new ReflectionClass($parserFactoryClassName);
* $parser = NULL;
* $parserResult = NULL;
*
*
* //--- get the parser -------------------------------------------------------
*
* if( $rcParserFactory->implementsInterface('ParserFactoryI') )
* {
* $parserFactory = $parserFactoryClassName::getInstance();
* $aParserIds = $parserFactory->getParserIdsFor( $text );
*
* if( count($aParserIds) === 0 )
* {
* //error handling
* }
* elseif( count($aParserIds) === 1 )
* {
* $parser = $parserFactory->getParser( $text );
* }
* elseif( count($aParserIds) > 1 )
* {
* //implement mechanism to let the user decide which parser shall be
* //used to parse the text.
* //Then, use syntax:
* //$parser = $parserFactory->getParser( $text, $selectedParserId );
* }
* }
* else
* {
* //error handling
* }
*
*
* //--- parse the text -------------------------------------------------------
*
* if( $parser instanceof ParserI )
* {
* $parserResult = new DTOParserResultC( $parser );
* $parser->parse( $parserResult );
* }
* else
* {
* //error handling
* }
*
*
* //--- proceed --------------------------------------------------------------
*
* if( $parserResult instanceof DTOParserResultC )
* {
* //do whatever you need to do...
* }
*
* @endcode
*/
interface ParserFactoryI
{
///////////////////////////////////////////////////////////////////////
/**
* Returns an instance of the factory.
*
* @return object instance of the factory.
*/
static public function getInstance();
///////////////////////////////////////////////////////////////////////
/**
* Checks for all known parsers if they can parse the provided text.
*
* @param $text string, the text to be parsed
*
* @return array of strings, ids of the parsers, that can parse the
* provided text.
*/
public function getParserIdsFor($text);
///////////////////////////////////////////////////////////////////////
/**
* Gets an instance of the first parser the factory knows, that can
* parse the provided text. You can define the parser that is to be
* used to parse the text, by providing its id as second parameter.
*
* @param $text string, the text to be parsed
* @param $parserId string, the id of a known parser that shall be used
* to parse the provided text
*
* @return mixed
* ParserI - instance of the parser that can be used to parse
* the provided text.
* boolean - false, if
* 1) no parser was found that could parse the provided
* text
* 2) you provided a parser id that does not exist
* 3) you provided a parser id of an existent parser, but
* that parser isn't able to parse the provided text.
*/
public function getParser($text, $parserId = '');
}