1: <?php
2:
3: namespace webfilesframework\core\datasystem\file\format;
4: use webfilesframework\MWebfilesFrameworkException;
5:
6: 7: 8: 9: 10: 11: 12: 13:
14: class MWebfile {
15:
16: protected $m_iId = 0;
17:
18: 19: 20: 21: 22: 23:
24: public $m_iTime;
25:
26:
27: 28: 29: 30: 31: 32: 33: 34:
35: public function marshall($usePreamble = true)
36: {
37: $out = "";
38: $attributes = $this->getAttributes();
39: if ($usePreamble) {
40: $out .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
41: }
42: $out .= "<object classname=\"" . static::classname() . "\">\n";
43: foreach ($attributes as $attribute) {
44:
45: $attributeName = $attribute->getName();
46:
47: if (MWebfile::isSimpleDatatype($attributeName)) {
48: $attribute->setAccessible(true);
49: $attributeFieldName = static::getSimplifiedAttributeName($attributeName);
50: $out .= "\t<" . $attributeFieldName . "><![CDATA[" . $attribute->getValue($this) . "]]></" . $attributeFieldName . ">\n";
51: }
52: }
53: $out .= "</object>";
54:
55: return $out;
56: }
57:
58: 59: 60: 61: 62: 63: 64: 65:
66: public function unmarshall($data)
67: {
68: static::genericUnmarshall($data,$this);
69: }
70:
71: 72: 73: 74: 75: 76: 77: 78: 79:
80: public static function staticUnmarshall($xmlAsString)
81: {
82: return static::genericUnmarshall($xmlAsString);
83: }
84:
85:
86: 87: 88: 89: 90: 91: 92: 93:
94: private static function genericUnmarshall($xmlAsString, &$targetObject = null) {
95:
96: $root = simplexml_load_string($xmlAsString);
97:
98: if ($root == null) {
99: throw new MWebfilesFrameworkException("Error on reading initial xml: " . $xmlAsString);
100: }
101:
102: if ($root->getName() == "reference") {
103: $url = $root->url;
104: $xmlAsString = file_get_contents($url);
105: $root = simplexml_load_string($xmlAsString);
106:
107: if ($root == null) {
108: throw new MWebfilesFrameworkException("Error on reading reference xml: " . $xmlAsString);
109: }
110: }
111:
112: if ( $targetObject == null ) {
113: $classname = (string)$root->attributes()->classname;
114:
115:
116: $ref = new \ReflectionClass($classname);
117: $targetObject = $ref->newInstanceWithoutConstructor();
118: }
119:
120: $objectAttributes = $root->children();
121: $attributes = $targetObject->getAttributes();
122:
123:
124: foreach ($objectAttributes as $value) {
125:
126: foreach ($attributes as $attribute) {
127:
128: $attribute->setAccessible(true);
129: $attributeName = $attribute->getName();
130: if ($value->getName() == static::getSimplifiedAttributeName($attributeName)) {
131: $attribute->setValue($targetObject, $value->__toString());
132: }
133: }
134: }
135: return $targetObject;
136: }
137:
138: 139: 140: 141: 142: 143:
144: public function presetForTemplateSearch()
145: {
146: $attributes = $this->getAttributes();
147: foreach ($attributes as $attribute) {
148:
149: $attributeName = $attribute->getName();
150:
151: if (MWebfile::isSimpleDatatype($attributeName)) {
152: $attribute->setAccessible(true);
153: $attribute->setValue($this, "[any]");
154: }
155: }
156: }
157:
158: 159: 160: 161: 162: 163:
164: public function matchesTemplate(MWebfile $template) {
165:
166: if ( $template::classname() == static::classname() ) {
167:
168: $attributes = $template->getAttributes(true);
169:
170:
171: foreach ($attributes as $attribute) {
172:
173: $attribute->setAccessible(true);
174: $templateValue = $attribute->getValue($template);
175:
176: if (
177: $templateValue != "[any]"
178: && !($templateValue instanceof MIDatastoreFunction)
179: ) {
180:
181: $webfileValue = $attribute->getValue($this);
182: if ($templateValue != $webfileValue) {
183: return false;
184: }
185: }
186: }
187: return true;
188: } else {
189: return false;
190: }
191:
192: }
193:
194: 195: 196: 197: 198: 199: 200:
201: public static function isSimpleDatatype($datatypeName)
202: {
203: if (!self::isObject($datatypeName) && substr($datatypeName, 2, 1) != "_") {
204: return true;
205: } else {
206: return false;
207: }
208: }
209:
210: 211: 212: 213: 214:
215: public static function isObject($attributeName)
216: {
217: if (substr($attributeName, 2, 1) == "o") {
218: return true;
219: } else {
220: return false;
221: }
222:
223: }
224:
225:
226: 227: 228: 229: 230: 231: 232: 233: 234:
235: public static function getAttributes($simpleDatatypesOnly = false)
236: {
237: $oSelfReflection = new \ReflectionClass(static::classname());
238: $oPropertyArray = $oSelfReflection->getProperties(
239: \ReflectionProperty::IS_PUBLIC |
240: \ReflectionProperty::IS_PROTECTED |
241: \ReflectionProperty::IS_PRIVATE);
242:
243: $count = 0;
244: while ($count < count($oPropertyArray)) {
245: $sAttributeName = $oPropertyArray[$count]->getName();
246: if (
247:
248: substr($sAttributeName, 1, 1) != "_" ||
249: substr($sAttributeName, 2, 1) == "_" ||
250: ( $simpleDatatypesOnly && substr($sAttributeName, 2, 1) == "o")
251: ) {
252: unset($oPropertyArray[$count]);
253: }
254: $count++;
255: }
256: return $oPropertyArray;
257: }
258:
259: 260: 261: 262: 263: 264: 265:
266: public static function getClassInformation()
267: {
268:
269: $returnValue = "<classinformation>\n";
270: $returnValue .= "\t<author>simpleserv.de</author>\n";
271: $returnValue .= "\t<classname>" . static::classname() . "</classname>\n";
272: $returnValue .= "\t<attributes>\n";
273:
274: $attributes = static::getAttributes();
275:
276: foreach ($attributes as $attribute) {
277:
278: $attributeName = $attribute->getName();
279:
280: if (MWebfile::isSimpleDatatype($attributeName)) {
281: $attributeFieldName = static::getSimplifiedAttributeName($attributeName);
282: $attributeFieldType = MWebfile::getDatatypeFromAttributeName($attributeName);
283: $returnValue .= "\t\t<attribute name=\"" . $attributeFieldName . "\" type=\"" . $attributeFieldType . "\" />\n";
284: }
285:
286:
287: }
288: $returnValue .= "\t</attributes>\n";
289: $returnValue .= "</classinformation>";
290: return $returnValue;
291:
292: }
293:
294: 295: 296: 297: 298: 299:
300: public static function getDatatypeFromAttributeName($attributeName)
301: {
302:
303: $datatypeToken = substr($attributeName, 2, 1);
304:
305: if ($datatypeToken == "s") {
306: return "shorttext";
307: } else if ($datatypeToken == "l") {
308: return "longtext";
309: } else if ($datatypeToken == "h") {
310: return "htmllongtext";
311: } else if ($datatypeToken == "d") {
312: return "date";
313: } else if ($datatypeToken == "t") {
314: return "time";
315: } else if ($datatypeToken == "i") {
316: return "integer";
317: } else if ($datatypeToken == "f") {
318: return "float";
319: } else if ($datatypeToken == "o") {
320: return "object";
321: }
322: return null;
323: }
324:
325: 326: 327: 328: 329: 330: 331:
332: public function getDataset()
333: {
334: $dataset = array();
335:
336: $attributes = $this->getAttributes();
337:
338: foreach ($attributes as $attribute) {
339:
340: $attributeName = $attribute->getName();
341: $attribute->setAccessible(true);
342: $attributeValue = $attribute->getValue($this);
343:
344: if (MWebfile::isSimpleDatatype($attributeName)) {
345: $attributeFieldName = static::getSimplifiedAttributeName($attributeName);
346: $dataset[$attributeFieldName] = $attributeValue;
347: }
348: }
349: return $dataset;
350: }
351:
352:
353: 354: 355: 356: 357: 358:
359: public static function getSimplifiedAttributeName($p_sFieldName)
360: {
361:
362: $sDatabaseFieldName = substr($p_sFieldName, 3);
363: $sDatabaseFieldName = strtolower($sDatabaseFieldName);
364: return $sDatabaseFieldName;
365: }
366:
367: public function getId()
368: {
369: return $this->m_iId;
370: }
371:
372: public function setId($itemId)
373: {
374: $this->m_iId = $itemId;
375: }
376:
377: 378: 379:
380: public function getTime()
381: {
382: return $this->m_iTime;
383: }
384:
385: 386: 387:
388: public function setTime($time)
389: {
390: $this->m_iTime = $time;
391: }
392:
393: public function getGeograficPosition()
394: {
395: return NULL;
396: }
397:
398: public static function classname() {
399: return get_called_class();
400: }
401:
402: 403: 404: 405: 406: 407: 408:
409: public static function createWebfileByClassname($classname) {
410: $ref = new \ReflectionClass($classname);
411: $webfile = $ref->newInstanceWithoutConstructor();
412: if (! $webfile instanceof MWebfile ) {
413: throw new MWebfilesFrameworkException("given class '" . $classname . " does not extend MWebfile.");
414: }
415: return $webfile;
416: }
417:
418: }