1: <?php
2:
3:
4: namespace BeerXML;
5:
6: use BeerXML\Exception\InvalidRecord;
7: use BeerXML\Generator\IEquipment;
8: use BeerXML\Generator\IFermentable;
9: use BeerXML\Generator\IHop;
10: use BeerXML\Generator\IMashProfile;
11: use BeerXML\Generator\IMashStep;
12: use BeerXML\Generator\IMisc;
13: use BeerXML\Generator\IRecipe;
14: use BeerXML\Generator\IStyle;
15: use BeerXML\Generator\IWater;
16: use BeerXML\Generator\IYeast;
17: use BeerXML\Generator\Record;
18:
19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: class Generator
36: {
37:
38: 39: 40:
41: private $xmlWriter;
42:
43: 44: 45: 46: 47:
48: private $recordSetTags = array(
49: '\BeerXML\Generator\IHop' => array('tag' => 'HOPS', 'generator' => 'BeerXML\Generator\Hop'),
50: '\BeerXML\Generator\IFermentable' => array(
51: 'tag' => 'FERMENTABLES',
52: 'generator' => 'BeerXML\Generator\Fermentable'
53: ),
54: '\BeerXML\Generator\IYeast' => array('tag' => 'YEASTS', 'generator' => 'BeerXML\Generator\Yeast'),
55: '\BeerXML\Generator\IMisc' => array('tag' => 'MISCS', 'generator' => 'BeerXML\Generator\Misc'),
56: '\BeerXML\Generator\IWater' => array('tag' => 'WATERS', 'generator' => 'BeerXML\Generator\Water'),
57: '\BeerXML\Generator\IStyle' => array('tag' => 'STYLES', 'generator' => 'BeerXML\Generator\Style'),
58: '\BeerXML\Generator\IMashStep' => array('tag' => 'MASH_STEPS', 'generator' => 'BeerXML\Generator\MashStep'),
59: '\BeerXML\Generator\IMash' => array('tag' => 'MASHS', 'generator' => 'BeerXML\Generator\MashProfile'),
60: '\BeerXML\Generator\IRecipe' => array('tag' => 'RECIPES', 'generator' => 'BeerXML\Generator\Recipe'),
61: '\BeerXML\Generator\IEquipment' => array('tag' => 'EQUIPMENTS', 'generator' => 'BeerXML\Generator\Equipment'),
62: );
63:
64: 65: 66: 67: 68:
69: private $records = array();
70:
71: public function __construct()
72: {
73: $this->xmlWriter = new \XMLWriter();
74: }
75:
76:
77: 78: 79:
80: public function addRecord($record)
81: {
82: $this->records[] = $record;
83: }
84:
85: 86: 87: 88: 89:
90: public function render()
91: {
92: $this->xmlWriter->openMemory();
93: $this->xmlWriter->startDocument('1.0', 'UTF-8');
94: $this->xmlWriter->writeComment('Created with php-beerxml: https://github.com/georgeh/php-beerxml');
95:
96: list($setTag, $generator) = $this->getTagGeneratorForObject($this->records[0]);
97:
98: $generator->setXmlWriter($this->xmlWriter);
99: $this->xmlWriter->startElement($setTag);
100: foreach ($this->records as $record) {
101: $generator->setRecord($record);
102: $generator->build();
103: }
104: $this->xmlWriter->endElement();
105: return $this->xmlWriter->outputMemory(true);
106: }
107:
108: 109: 110: 111: 112:
113: private function getTagGeneratorForObject($record)
114: {
115: foreach ($this->recordSetTags as $interface => $tagGen) {
116: if ($record instanceof $interface) {
117: $tag = $tagGen['tag'];
118: $generatorClass = $tagGen['generator'];
119: $generator = new $generatorClass();
120: return array($tag, $generator);
121: }
122: }
123:
124: throw new InvalidRecord('Record did not implement a valid Reader interface');
125: }
126: }