Subversion-Projekte lars-tiefland.content-management

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
// $Id: EbatNs_LegacyMapper.php 4066 2011-11-03 08:13:59Z tiefland $
3
// $Log: EbatNs_LegacyMapper.php,v $
4
// Revision 1.2  2008-05-02 15:04:05  carsten
5
// Initial, PHP5
6
//
7
//
8
/*
9
	- Only assignments are allowed (=)
10
	- Local vars are enclosed in []
11
	- Any expression might start with / to access the destination object
12
	-
13
 
14
	local var [last] is set to $dest->SalesTax
15
		if SalesTax does not exist it will be created with the given type.
16
	[last]=/SalesTax|new:CategoryType
17
 
18
	local var [last] is set to $dest->SalesTax
19
		if SalesTax does not exist [last] will be null and operation will
20
		return.
21
	[last]=/SalesTax|exist
22
 
23
	The element of local object is assigned a literal constant (e.g. $last->Element = 'Const')
24
	[last]/Element = Const | "My Value"
25
 
26
	The same, but the local object is assumed as a XsdType with an attribute
27
	[last]/@Attribute=Const
28
 
29
	$dest->SalesTax is set to the reference of local var [last]
30
	/SalesTax=[last]
31
 
32
	$dest->SalesTax[] is set to the reference of local var [last]
33
 
34
	/()SalesTax=[last]
35
 
36
	$dest->SalesTax[index] is set. index will be calculated by the data with index 0 (or $data['myvar']
37
	/({0})SalesTax=[last]
38
	/({myvar})SalesTax=[last]
39
 
40
*/
41
 
42
/**
43
 * EbatNs_MapExpression
44
 *
45
 * @package EbatNs
46
 * @author Carsten Harnisch
47
 * @copyright Copyright (c) 2005, IntradeSys Limited
48
 * @version $Id: EbatNs_LegacyMapper.php 4066 2011-11-03 08:13:59Z tiefland $
49
 * @access public
50
 */
51
class EbatNs_MapExpression
52
{
53
	protected $_expression;
54
	protected $_mapper;
55
 
56
	/**
57
	 * EbatNs_MapExpression::EbatNs_MapExpression()
58
	 *
59
	 * @param mixed $mapper
60
	 * @param mixed $expression
61
	 * @return
62
	 */
63
	function EbatNs_MapExpression(&$mapper, $expression)
64
	{
65
		$this->_mapper = &$mapper;
66
		$this->_expression = $expression;
67
	}
68
 
69
	/**
70
	 * EbatNs_MapExpression::evalExpression()
71
	 *
72
	 * @return
73
	 */
74
	function &evalExpression()
75
	{
76
		// echo "evalExpression " . $this->_expression . "<br>";
77
		if ($this->_expression[0] == "[")
78
		{
79
			if (strstr($this->_expression, '/') !== false)
80
			{
81
				// an expression like [xxx]/element
82
				// or [xxx]/element|*type
83
				list($localname, $element) = explode('/', $this->_expression);
84
				$localname = trim($localname, "[]");
85
				$t = &$this->_mapper->_getLocal($localname);
86
 
87
				if (!$t)
88
				{
89
					// we could not the local var
90
					// so this is an error !
91
					return null;
92
				}
93
 
94
				$pieces = explode("|", $element);
95
				$creationType = null;
96
				if (count($pieces) == 1)
97
				{
98
					$memberName = $pieces[0];
99
					$typename = null;
100
				}
101
				else
102
				{
103
					$memberName = $pieces[0];
104
					$creationType = $pieces[1];
105
 
106
					if ($creationType[0] == '*')
107
						$typename = substr($creationType, 1);
108
					else
109
						$typename = 'stdClass';
110
				}
111
				// echo "memberName $memberName <br>";
112
				// is this an index access ?
113
				if ($memberName[0] == "(")
114
				{
115
					$isIndexAccess = true;
116
					$accessIndex = $this->_parseIndex($memberName);
117
					// echo "indexAccess $memberName [ $accessIndex ] ($typename) <br>";
118
					// print_r($t);
119
					// die("STOP");
120
					if ($accessIndex)
121
					{
122
						// die("STOP");
123
						if (isset($t->
124
								{
125
									$memberName}
126
								[$accessIndex]))
127
						$target = $target = &$t->
128
						{
129
							$memberName}
130
						[$accessIndex];
131
						else
132
						{
133
							// echo "indexAccess NOT THERE $memberName [ $accessIndex ] ($typename)<br>";
134
							// die("STOP");
135
							$target = null;
136
						}
137
					}
138
					else
139
					{
140
						// echo "indexAccess generic() $memberName [ $accessIndex ] <br>";
141
						// an generic index will create an new entry anyway
142
						$target = null;
143
					}
144
				}
145
				else
146
				{
147
					$isIndexAccess = false;
148
					$target = &$t->
149
					{
150
						$memberName} ;
151
				}
152
 
153
				if (!$target && $typename)
154
				{
155
					$this->_mapper->includeType($typename);
156
 
157
					if ($isIndexAccess)
158
					{
159
						$t->
160
						{
161
							$memberName}
162
						[$accessIndex] = &new $typename;
163
						$target = &$t->
164
						{
165
							$memberName}
166
						[$accessIndex];
167
						// echo "indexAccess CREATED new $typename $memberName [ $accessIndex ] as <br>";
168
						// print_r($t);
169
						// die("STOP");
170
						// echo "<hr>";
171
					}
172
					else
173
					{
174
						$t->
175
						{
176
							$memberName} = &new $typename;
177
						$target = &$t->
178
						{
179
							$memberName} ;
180
					}
181
				}
182
 
183
				return $target;
184
				// echo "Eval Return : <br>";
185
				// print_r($t);
186
				// echo "<hr>";
187
			}
188
			else
189
			{
190
				// plain local object  like [xxx]
191
				$localname = trim($this->_expression, "[]");
192
				$t = $this->_mapper->_getLocal($localname);
193
 
194
				if ($this->_expression == "[sd]")
195
				{
196
					// echo "Eval Return : <br>";
197
					// print_r($t);
198
					// echo "<hr>";
199
				}
200
				return $t;
201
			}
202
		} elseif ($this->_expression[0] == "{")
203
		{
204
			$datakey = trim($this->_expression, "{}");
205
			$t = &$this->_mapper->getData($datakey);
206
			// echo "Eval Return : <br>";
207
			// print_r($t);
208
			// echo "<hr>";
209
			return $t;
210
		} elseif ($this->_expression[0] == "/")
211
		{
212
			$pieces = explode("|", substr($this->_expression, 1));
213
			$creationType = null;
214
			if (count($pieces) == 1)
215
				$creationType = $pieces[0];
216
			else
217
			{
218
				$targetName = $pieces[0];
219
 
220
				$t = &$this->_mapper->getTarget($targetName);
221
				if (!$t)
222
				{
223
					$creationType = $pieces[1];
224
				}
225
			}
226
 
227
			if ($creationType[0] == '*')
228
			{
229
				$typename = substr($creationType, 1);
230
				$this->_mapper->includeType($typename);
231
				$t = &new $typename;
232
			}
233
 
234
			return $t;
235
		}
236
		else
237
		{
238
			// echo "Return Plain<br>";
239
			return $this->_expression;
240
		}
241
		echo "ERROR";
242
	}
243
 
244
	/**
245
	 * EbatNs_MapExpression::_parseIndex()
246
	 *
247
	 * @param mixed $name
248
	 * @return
249
	 */
250
	function _parseIndex(&$name)
251
	{
252
		list($tmpIndex, $name) = explode(")", $name);
253
		$tmpIndex = trim($tmpIndex, "(");
254
		if ($tmpIndex == "")
255
			return null;
256
		else
257
		{
258
			if ($tmpIndex[0] == "{")
259
			{
260
				$tmpIndex = trim($tmpIndex, "{}");
261
				$tmpIndex = $this->_mapper->getData($tmpIndex);
262
			}
263
			return $tmpIndex;
264
		}
265
	}
266
}
267
 
268
/**
269
 * EbatNs_MapTarget
270
 *
271
 * @package EbatNs
272
 * @author Carsten Harnisch
273
 * @copyright Copyright (c) 2005, IntradeSys Limited
274
 * @version $Id: EbatNs_LegacyMapper.php 4066 2011-11-03 08:13:59Z tiefland $
275
 * @access public
276
 */
277
class EbatNs_MapTarget
278
{
279
	protected $_mapper;
280
 
281
	protected $_targetObject;
282
 
283
	protected $_targetName;
284
	protected $_targetIsArray;
285
	protected $_targetIndex;
286
 
287
	protected $_targetIsLocal;
288
	protected $_localName;
289
 
290
	/**
291
	 * EbatNs_MapTarget::EbatNs_MapTarget()
292
	 *
293
	 * @param mixed $mapper
294
	 * @param mixed $target
295
	 * @return
296
	 */
297
	function EbatNs_MapTarget(&$mapper, $target)
298
	{
299
		$this->_mapper = &$mapper;
300
		$this->parse($target);
301
	}
302
 
303
	/**
304
	 * EbatNs_MapTarget::setTarget()
305
	 *
306
	 * @param mixed $value
307
	 * @return
308
	 */
309
	function setTarget(&$value)
310
	{
311
		// echo "setTarget <br>";
312
		// print_r($this);
313
		if ($this->_targetIsLocal)
314
		{
315
			// echo "setTarget local $this->_localName $this->_targetName is_array: $this->_targetIsArray index: $this->_targetIndex<br>";
316
			if (!$this->_targetName)
317
			{
318
				// echo "setTarget <br>";
319
				$mapper = &$this->_mapper;
320
				$mapper->_setLocal($this->_localName, $value);
321
			}
322
			else
323
			{
324
				$mapper = &$this->_mapper;
325
				// print_r($this->_mapper);
326
				$this->_targetObject = &$mapper->_getLocal($this->_localName);
327
				$targetObject = &$this->_targetObject;
328
				// echo "TargetObject <br>";
329
				// print_r($targetObject);
330
				if ($this->_targetIsArray)
331
				{
332
					if ($this->_targetIndex)
333
						$targetObject->
334
					{
335
						$this->_targetName}
336
					[$this->_targetIndex] = &$value;
337
					else
338
						$targetObject->
339
					{
340
						$this->_targetName}
341
					[] = &$value;
342
				}
343
				else
344
				{
345
					if ($this->_targetName[0] == "@")
346
					{
347
						$targetObject->_attributeValues[substr($this->_targetName, 1)] = $value;
348
						// print_r($targetObject);
349
						// die("123");
350
					}
351
					else
352
					{
353
						// echo "$this->_targetName <br>";
354
						$targetObject->
355
						{
356
							$this->_targetName} = &$value;
357
					}
358
				}
359
 
360
				$mapper->_setLocal($this->_localName, $targetObject);
361
			}
362
		}
363
		else
364
		{
365
			$mapper = &$this->_mapper;
366
			// echo "Before mapper->_setTarget <br>";
367
			// print_r($mapper->_destObject);
368
			$mapper->_setTarget($this->_targetName, $value, $this->_targetIsArray, $this->_targetIndex);
369
		}
370
 
371
		return true;
372
	}
373
 
374
	/**
375
	 * EbatNs_MapTarget::_parseIndex()
376
	 *
377
	 * @param mixed $name
378
	 * @return
379
	 */
380
	function _parseIndex(&$name)
381
	{
382
		list($tmpIndex, $name) = explode(")", $name);
383
		$tmpIndex = trim($tmpIndex, "(");
384
		if ($tmpIndex == "")
385
			return null;
386
		else
387
		{
388
			if ($tmpIndex[0] == "{")
389
			{
390
				$tmpIndex = trim($tmpIndex, "{}");
391
				$tmpIndex = $this->_mapper->getData($tmpIndex);
392
			}
393
			return $tmpIndex;
394
		}
395
	}
396
 
397
	/**
398
	 * EbatNs_MapTarget::parse()
399
	 *
400
	 * @param mixed $target
401
	 * @return
402
	 */
403
	function parse($target)
404
	{
405
		$this->_targetName = $target;
406
 
407
		if ($target[0] == '/')
408
		{
409
			$this->_targetIsLocal = false;
410
			list($dummy, $this->_targetName) = explode("/", $target);
411
 
412
			if ($this->_targetName[0] == "(")
413
			{
414
				$this->_targetIsArray = true;
415
				$this->_targetIndex = $this->_parseIndex($this->_targetName);
416
			}
417
			else
418
			{
419
				$this->_targetIsArray = false;
420
				$this->_targetIndex = null;
421
			}
422
			// echo "Target is destination : $target $this->_targetName IsArray: $this->_targetIsArray Index: $this->_targetIndex<br>";
423
		}
424
		else
425
		{
426
			$this->_targetIsLocal = true;
427
			list($dummy, $this->_targetName) = explode("/", $target);
428
 
429
			if ($this->_targetName[0] == "(")
430
			{
431
				$this->_targetIsArray = true;
432
				$this->_targetIndex = $this->_parseIndex($this->_targetName);
433
				$this->_targetName = trim($this->_targetName, "()");
434
			}
435
			else
436
			{
437
				$this->_targetIsArray = false;
438
				$this->_targetIndex = null;
439
			}
440
 
441
			list($targetName, $dummy) = explode("/", $target);
442
			$this->_localName = trim($targetName, "[]");
443
			// echo "Target is local $this->_localName $this->_targetName IsArray: $this->_targetIsArray Index: $this->_targetIndex<br>";
444
		}
445
		// echo "need Parse target " . $target . "<br>";
446
	}
447
}
448
 
449
/**
450
 * EbatNs_MapMapping
451
 *
452
 * @package easylister
453
 * @author michael
454
 * @copyright Copyright (c) 2005
455
 * @version $Id: EbatNs_LegacyMapper.php 4066 2011-11-03 08:13:59Z tiefland $
456
 * @access public
457
 */
458
class EbatNs_MapMapping
459
{
460
	protected $_EbatNs_MapTarget;
461
	protected $_EbatNs_MapExpression;
462
	protected $_mapper;
463
 
464
	/**
465
	 * EbatNs_MapMapping::EbatNs_MapMapping()
466
	 *
467
	 * @param mixed $mapper
468
	 * @param mixed $mapping
469
	 * @return
470
	 */
471
	function __construct($mapper, $mapping)
472
	{
473
		$this->_valueMap = $valueMap;
474
		$this->_mapper = &$mapper;
475
		$this->parse($mapping);
476
	}
477
 
478
	/**
479
	 * EbatNs_MapMapping::map()
480
	 *
481
	 * @return
482
	 */
483
	function map()
484
	{
485
		$this->_EbatNs_MapTarget->setTarget($this->_EbatNs_MapExpression->evalExpression());
486
		return true;
487
	}
488
 
489
	/**
490
	 * EbatNs_MapMapping::parse()
491
	 *
492
	 * @param mixed $mapping
493
	 * @return
494
	 */
495
	function parse($mapping)
496
	{
497
		// empty or comment line
498
		if ($mapping == '' || $mapping[0] == ';')
499
			return;
500
 
501
		list($target, $expression) = explode('=', $mapping);
502
		$mapper = $this->_mapper;
503
		$this->_EbatNs_MapExpression = new EbatNs_MapExpression($mapper, $expression);
504
		$this->_EbatNs_MapTarget = new EbatNs_MapTarget($mapper, $target);
505
	}
506
}
507
 
508
/**
509
 * EbatNs_LegacyMapper
510
 *
511
 * @package easylister
512
 * @author michael
513
 * @copyright Copyright (c) 2005
514
 * @version $Id: EbatNs_LegacyMapper.php 4066 2011-11-03 08:13:59Z tiefland $
515
 * @access public
516
 */
517
class EbatNs_LegacyMapper
518
{
519
	protected $_mappings;
520
	protected $_src;
521
	protected $_data;
522
 
523
	protected $_destObject;
524
 
525
	protected $_locals = null;
526
	protected $_valueMap;
527
	protected $_isValid = false;
528
 
529
 
530
	/**
531
	 * EbatNs_LegacyMapper::EbatNs_LegacyMapper()
532
	 *
533
	 * @param mixed $mapping
534
	 * @param mixed $destObject
535
	 * @param mixed $data
536
	 * @param mixed $valueMapString
537
	 * @param mixed $defaultData
538
	 * @return
539
	 */
540
	function __construct($mapping, $destObject, $data, $valueMapString, $defaultData = null)
541
	{
542
		$this->_destObject = $destObject;
543
		if (is_array($data))
544
			$this->_data = $data;
545
		else
546
			$this->_data[] = $data;
547
 
548
		if ($defaultData)
549
			$this->setDefaults($defaultData);
550
 
551
		if ($valueMapString)
552
			$this->parseValueMap($valueMapString);
553
 
554
		$this->_isValid = $this->Parse($mapping);
555
	}
556
 
557
	/**
558
	 * EbatNs_LegacyMapper::parseValueMap()
559
	 *
560
	 * @param mixed $valueMapString
561
	 * @return
562
	 */
563
	function parseValueMap($valueMapString)
564
	{
565
		$this->_valueMap = null;
566
		$parts = explode("|", $valueMapString);
567
 
568
		foreach ($parts as $part)
569
		{
570
			list($key, $value) = explode("=", $part);
571
			$this->_valueMap[$key] = $value;
572
		}
573
	}
574
 
575
	/**
576
	 * EbatNs_LegacyMapper::Parse()
577
	 *
578
	 * @param mixed $mapping
579
	 * @return
580
	 */
581
	function Parse($mapping)
582
	{
583
		$mapping = str_replace("\r\n", "\n", $mapping);
584
		$lines = explode("\n", $mapping);
585
		$this->_mappings = null;
586
 
587
		$thisObject = &$this;
588
		foreach ($lines as $line)
589
		{
590
			if ($line == '' || $line[0] == ';')
591
				continue;
592
			if (trim($line))
593
			{
594
				$aMapLine = new EbatNs_MapMapping($thisObject, $line);
595
				$this->_mappings[] = $aMapLine;
596
			}
597
		}
598
 
599
		return count($this->_mappings);
600
	}
601
 
602
	/**
603
	 * EbatNs_LegacyMapper::Map()
604
	 *
605
	 * @return
606
	 */
607
	function Map()
608
	{
609
		if (!$this->_isValid)
610
			return;
611
		foreach($this->_mappings as $mapping)
612
		{
613
			if (!$mapping->map())
614
				break;
615
		}
616
	}
617
 
618
	/**
619
	 * EbatNs_LegacyMapper::_setLocal()
620
	 *
621
	 * @param mixed $key
622
	 * @param mixed $value
623
	 * @return
624
	 */
625
	function _setLocal($key, &$value)
626
	{
627
		$this->_locals[$key] = &$value;
628
	}
629
 
630
	/**
631
	 * EbatNs_LegacyMapper::_getLocal()
632
	 *
633
	 * @param mixed $key
634
	 * @return
635
	 */
636
	function _getLocal($key)
637
	{
638
		return $this->_locals[$key];
639
	}
640
 
641
	/**
642
	 * EbatNs_LegacyMapper::_setTarget()
643
	 *
644
	 * @param mixed $targetName
645
	 * @param mixed $value
646
	 * @param mixed $isArray
647
	 * @param mixed $index
648
	 * @return
649
	 */
650
	function _setTarget($targetName, &$value, $isArray = false, $index = null)
651
	{
652
		$dest = &$this->_destObject;
653
 
654
		if ($isArray)
655
		{
656
			if ($index)
657
				$dest->{$targetName}[$index] = $value;
658
			else
659
				$dest->{$targetName}[] = $value;
660
		}
661
		else
662
		{
663
			if ($targetName[0] == "@")
664
				$dest->_attributeValues[substr($targetName, 1)] = $value;
665
			else
666
				$dest->{$targetName} = $value;
667
		}
668
	}
669
 
670
	/**
671
	 * EbatNs_LegacyMapper::getTarget()
672
	 *
673
	 * @param mixed $name
674
	 * @param mixed $index
675
	 * @return
676
	 */
677
	function &getTarget($name, $index = null)
678
	{
679
		$dest = $this->_destObject;
680
 
681
		if (isset($dest->{$name}))
682
		{
683
			if ($index)
684
				return $dest->{$name}[$index];
685
			else
686
				return $dest->{$name} ;
687
		}
688
		else
689
			return null;
690
	}
691
 
692
	/**
693
	 * EbatNs_LegacyMapper::getData()
694
	 *
695
	 * @param mixed $indexKey
696
	 * @return
697
	 */
698
	function getData($indexKey)
699
	{
700
		$pieces = explode(":", $indexKey);
701
		if (count($pieces) == 1)
702
			return $this->_data[$indexKey];
703
		else
704
			return $this->_call_data_function($pieces[0], $this->_data[$pieces[1]]);
705
	}
706
 
707
	/**
708
	 * EbatNs_LegacyMapper::includeType()
709
	 *
710
	 * @param mixed $typeName
711
	 * @return
712
	 */
713
	function includeType($typeName)
714
	{
715
		if (!class_exists($typeName))
716
			require_once $typeName . '.php';
717
	}
718
 
719
	/**
720
	 * EbatNs_LegacyMapper::_call_data_function()
721
	 *
722
	 * @param mixed $funcName
723
	 * @param mixed $arg
724
	 * @return
725
	 */
726
	function _call_data_function($funcName, $arg)
727
	{
728
		switch ($funcName)
729
		{
730
			case 'utf8_encode':
731
			case 'utf8encode':
732
				return utf8_encode($arg);
733
			case 'legacy-map':
734
			case 'mapped-options':
735
				return $this->mapValue($arg);
736
			case 'bool':
737
			case 'boolean':
738
				return $this->SoapBoolean($arg);
739
 
740
			case 'cdata':
741
			// return '<![CDATA[' . $arg . ']]>';
742
			default:
743
				return $arg;
744
		}
745
	}
746
 
747
	/**
748
	 * EbatNs_LegacyMapper::mapValue()
749
	 *
750
	 * @param mixed $arg
751
	 * @return
752
	 */
753
	function mapValue($arg)
754
	{
755
		if (isset($this->_valueMap[$arg]))
756
			return $this->_valueMap[$arg];
757
		else
758
			return $arg;
759
	}
760
 
761
	/**
762
	 * EbatNs_LegacyMapper::setDefaults()
763
	 *
764
	 * @param mixed $arrayData
765
	 * @return
766
	 */
767
	function setDefaults($arrayData)
768
	{
769
		$this->_data = array_merge($this->_data, $arrayData);
770
	}
771
 
772
	/**
773
	 * EbatNs_LegacyMapper::SoapBoolean()
774
	 *
775
	 * @param mixed $value
776
	 * @return
777
	 */
778
	function SoapBoolean($value)
779
	{
780
		if (is_numeric($value) || is_bool($value))
781
		{
782
			if ($value)
783
				return 'true';
784
			else
785
				return 'false';
786
		}
787
		else
788
			return $value;
789
	}
790
}
791
?>