1: <?php
2:
3:
4: namespace BeerXML\Generator;
5:
6:
7: use BeerXML\Record\Yeast;
8:
9: abstract class Record
10: {
11: /**
12: * @var \XMLWriter
13: */
14: protected $xmlWriter;
15:
16: protected $record;
17:
18: /**
19: * @var string
20: */
21: protected $tagName;
22:
23: /**
24: * <TAG> => getterMethod()
25: *
26: * @var array
27: */
28: protected $simpleValues = array();
29:
30: /**
31: * <TAG> => getterMethod()
32: *
33: * @var array
34: */
35: protected $optionalSimpleValues = array();
36:
37: protected $complexValues = array();
38:
39: /**
40: * <TAG> => array('generator' => 'BeerXML\Generator\Class', 'values' => 'getRecords')
41: *
42: * @var array
43: */
44: protected $complexValueSets = array();
45:
46: /**
47: * The interface for the optional display fields in Appendix A of the spec
48: *
49: * @var string
50: */
51: protected $displayInterface;
52:
53: /**
54: * <TAG> => getterMethod()
55: *
56: * For the optional tags from the display fields in Appendix A of the spec
57: *
58: * @var array
59: */
60: protected $displayValues = array();
61:
62: /**
63: * @param \XMLWriter $xmlWriter
64: */
65: public function setXmlWriter($xmlWriter)
66: {
67: $this->xmlWriter = $xmlWriter;
68: }
69:
70: /**
71: * @param IHop|IFermentable|IEquipment|IYeast|IMisc|IWater|IStyle|IMashStep|IMashProfile|IRecipe $record
72: */
73: public function setRecord($record)
74: {
75: $this->record = $record;
76: }
77:
78: /**
79: * Construct the record in XMLWriter
80: *
81: * @return null
82: */
83: public function build()
84: {
85: $this->xmlWriter->startElement($this->tagName);
86: // Simple required elements
87: foreach ($this->simpleValues as $tag => $method) {
88: $this->xmlWriter->writeElement($tag, $this->record->{$method}());
89: }
90:
91: foreach ($this->optionalSimpleValues as $tag => $method) {
92: $value = $this->record->{$method}();
93: if (!empty($value)) {
94: $this->xmlWriter->writeElement($tag, $value);
95: }
96: }
97:
98: foreach ($this->complexValues as $generatorClass => $method) {
99: $value = $this->record->{$method}();
100: if ($value) {
101: /** @var Record $generator */
102: $generator = new $generatorClass();
103: $generator->setXmlWriter($this->xmlWriter);
104: $generator->setRecord($value);
105: $generator->build();
106: }
107: }
108:
109: foreach ($this->complexValueSets as $tag => $complex) {
110: $this->xmlWriter->startElement($tag);
111: $method = $complex['values']; // getter method, should return an array
112: $generatorClass = $complex['generator']; // Should be a subclass of BeerXML\Generator\Record
113:
114: $values = $this->record->{$method}();
115: $generator = new $generatorClass();
116: $generator->setXmlWriter($this->xmlWriter);
117: foreach ($values as $record) {
118: $generator->setRecord($record);
119: $generator->build();
120: }
121: $this->xmlWriter->endElement();
122: }
123:
124: // If the given record implements the interface for the display fields, write those too
125: if ($this->record instanceof $this->displayInterface) {
126: foreach ($this->displayValues as $tag => $method) {
127: $value = $this->record->{$method}();
128: if (!empty($value)) {
129: $this->xmlWriter->writeElement($tag, $value);
130: }
131: }
132: }
133:
134: // Call the parser to allow it to add any other weird fields
135: $this->additionalFields();
136:
137: $this->xmlWriter->endElement();
138: }
139:
140: /**
141: * Converts a boolean to the string format expected by BeerXML
142: *
143: * @param boolean $bool
144: * @return string 'TRUE' or 'FALSE'
145: */
146: protected function boolToString($bool)
147: {
148: return ($bool) ? 'TRUE' : 'FALSE';
149: }
150:
151: /**
152: * Runs before closing out the build sequence, to add fields that require logic
153: */
154: protected function additionalFields()
155: {
156:
157: }
158: }