1: <?php
2:
3:
4: namespace BeerXML;
5:
6:
7: use BeerXML\Generator\IEquipment;
8: use BeerXML\Generator\IFermentable;
9: use BeerXML\Generator\IHop;
10: use BeerXML\Generator\IMashProfile;
11: use BeerXML\Generator\IMisc;
12: use BeerXML\Generator\IRecipe;
13: use BeerXML\Generator\IStyle;
14: use BeerXML\Generator\IWater;
15: use BeerXML\Generator\IYeast;
16: use BeerXML\Parser\RecordFactory;
17: use BeerXML\Parser\Record;
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33: class Parser
34: {
35: 36: 37:
38: private $xmlReader;
39:
40: 41: 42:
43: private $recordFactory;
44:
45: 46: 47:
48: private $recipeParser;
49:
50: private $tagParsers = array(
51: 'RECIPE' => '\BeerXML\Parser\Recipe',
52: 'HOP' => '\BeerXML\Parser\Hop',
53: 'FERMENTABLE' => '\BeerXML\Parser\Fermentable',
54: 'YEAST' => '\BeerXML\Parser\Yeast',
55: 'MISC' => '\BeerXML\Parser\Misc',
56: 'WATER' => '\BeerXML\Parser\Water',
57: 'EQUIPMENT' => '\BeerXML\Parser\Equipment',
58: 'STYLE' => '\BeerXML\Parser\Style',
59: 'MASH_STEP' => '\BeerXML\Parser\MashStep',
60: 'MASH' => '\BeerXML\Parser\Mash'
61: );
62:
63: 64: 65:
66: public function __construct(RecordFactory $factory = null)
67: {
68: $this->xmlReader = new \XMLReader();
69: if (is_null($factory)) {
70: $factory = new RecordFactory();
71: }
72: $this->recordFactory = $factory;
73: }
74:
75: 76: 77: 78: 79: 80:
81: public function parse($xml)
82: {
83: $this->xmlReader->XML($xml);
84: $records = array();
85:
86: while ($this->xmlReader->read()) {
87:
88: if ($this->xmlReader->nodeType == \XMLReader::ELEMENT && isset($this->tagParsers[$this->xmlReader->name])) {
89: $recordParser = new $this->tagParsers[$this->xmlReader->name];
90:
91: $recordParser->setXmlReader($this->xmlReader);
92: $recordParser->setRecordFactory($this->recordFactory);
93: $records[] = $recordParser->parse();
94: }
95: }
96:
97: $this->xmlReader->close();
98:
99: return $records;
100: }
101:
102: }
103: