1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13:
14: namespace Jyxo\Gettext\Parser;
15:
16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: class Item
27: {
28: 29: 30: 31: 32:
33: protected $msgid = '';
34:
35: 36: 37: 38: 39: 40: 41: 42:
43: protected $msgstr = '';
44:
45: 46: 47: 48: 49:
50: protected $plural = '';
51:
52: 53: 54: 55: 56:
57: protected $location = '';
58:
59: 60: 61: 62: 63:
64: protected $fuzzy = false;
65:
66: 67: 68: 69: 70:
71: protected $obsolete = false;
72:
73:
74: 75: 76: 77: 78: 79: 80: 81:
82: public function __construct($chunk)
83: {
84: $array = explode("\n", $chunk);
85: $this->parse($array);
86:
87: if (empty($this->msgid)) {
88: throw new Exception('Msgid is empty which is not acceptable');
89: }
90: }
91:
92: 93: 94: 95: 96:
97: protected function parse(array $chunks)
98: {
99:
100: foreach ($chunks as $chunk) {
101:
102: if (preg_match('/^"(.*)"/', $chunk, $matches)) {
103: $this->{$lastChunkType} .= $matches[1];
104: continue;
105: }
106:
107: if (preg_match('/^msgid "(.*)"/', $chunk, $matches)) {
108: $lastChunkType = 'msgid';
109: $this->msgid = $matches[1];
110: } elseif (preg_match('/^msgstr "(.*)"/', $chunk, $matches)) {
111: $lastChunkType = 'msgstr';
112: $this->msgstr = $matches[1];
113: } elseif (preg_match('/^#~ msgid "(.*)"/', $chunk, $matches)) {
114: $lastChunkType = 'msgid';
115: $this->obsolete = true;
116: $this->msgid = $matches[1];
117: } elseif (preg_match('/^#~ msgstr "(.*)"/', $chunk, $matches)) {
118: $lastChunkType = 'msgstr';
119: $this->obsolete = true;
120: $this->msgstr = $matches[1];
121: } elseif (preg_match('/^(#: .+)$/', $chunk, $matches)) {
122: $lastChunkType = 'location';
123: $this->location .= $matches[1];
124: } elseif (preg_match('/^#, fuzzy/', $chunk)) {
125: $lastChunkType = 'fuzzy';
126: $this->fuzzy = true;
127: } elseif (preg_match('/^msgid_plural "(.*)"/', $chunk, $matches)) {
128: $lastChunkType = 'plural';
129: $this->plural = $matches[1];
130: $this->msgstr = array();
131: } elseif (preg_match('/^msgstr\[([0-9])+\] "(.*)"/', $chunk, $matches)) {
132: $lastChunkType = 'msgstr';
133: $this->msgstr[$matches[1]] = $matches[2];
134: }
135: }
136: }
137:
138: 139: 140: 141: 142:
143: public function isFuzzy()
144: {
145: return $this->fuzzy;
146: }
147:
148: 149: 150: 151: 152:
153: public function isObsolete()
154: {
155: return $this->obsolete;
156: }
157:
158: 159: 160: 161: 162:
163: public function hasPlural()
164: {
165: return !empty($this->plural);
166: }
167:
168: 169: 170: 171: 172:
173: public function getLocation()
174: {
175: return $this->location;
176: }
177:
178: 179: 180: 181: 182:
183: public function getMsgid()
184: {
185: return $this->msgid;
186: }
187:
188: 189: 190: 191: 192:
193: public function getMsgstr()
194: {
195: return $this->msgstr;
196: }
197:
198: 199: 200: 201: 202:
203: public function getPlural()
204: {
205: return $this->plural;
206: }
207: }
208: