PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wp-lister-for-ebay/includes/EbatNs/EbatNs_LegacyMapper.php

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