1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_XmlConnect
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * XmlConnect fixed Varien SimpleXML Element class
29: *
30: * @category Mage
31: * @package Mage_XmlConnect
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_XmlConnect_Model_Simplexml_Element extends Varien_Simplexml_Element
35: {
36: /**
37: * Appends $source to current node
38: *
39: * @param Mage_XmlConnect_Model_Simplexml_Element $source
40: * @return Mage_XmlConnect_Model_Simplexml_Element
41: */
42: public function appendChild($source)
43: {
44: if (sizeof($source->children())) {
45: $name = $source->getName();
46: $child = $this->addChild($name);
47: } else {
48: $child = $this->addChild($source->getName(), $this->xmlentities($source));
49: }
50: $child->setParent($this);
51:
52: $attributes = $source->attributes();
53: foreach ($attributes as $key=>$value) {
54: $child->addAttribute($key, $this->xmlAttribute($value));
55: }
56:
57: foreach ($source->children() as $sourceChild) {
58: $child->appendChild($sourceChild);
59: }
60: return $this;
61: }
62:
63: /**
64: * Escape xml entities
65: *
66: * @param mixed $data
67: * @param bool $stripTags
68: * @param array $allowedTags
69: * @return mixed
70: */
71: public function escapeXml($data, $stripTags = true, $allowedTags = null)
72: {
73: if (is_array($data)) {
74: $result = array();
75: foreach ($data as $item) {
76: if ($stripTags) {
77: $item = Mage::helper('core')->stripTags($item, $allowedTags);
78: }
79: $result[] = $this->xmlentities($item);
80: }
81: } else {
82: if (is_null($data)) {
83: $data = $this;
84: }
85: $data = (string)$data;
86:
87: if ($stripTags) {
88: $data = Mage::helper('core')->stripTags($data, $allowedTags);
89: }
90: $result = $this->xmlentities($data);
91: }
92: return $result;
93: }
94:
95: /**
96: * Converts meaningful xml character (") to xml attribute specification
97: *
98: * @param string $value
99: * @param bool $stripTags
100: * @return string|this
101: */
102: public function xmlAttribute($value = null, $stripTags = true)
103: {
104: if (is_null($value)) {
105: $value = $this;
106: }
107: $value = (string)$value;
108:
109: if ($stripTags) {
110: $value = Mage::helper('core')->stripTags($value);
111: }
112: $value = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $value);
113: return $value;
114: }
115:
116: /**
117: * Add field to fieldset
118: *
119: * @param string $elementName
120: * @param string $elementType
121: * @param array $config
122: * @return Mage_XmlConnect_Model_Simplexml_Element
123: */
124: public function addField($elementName, $elementType, array $config)
125: {
126: $newField = $this->addChild('field');
127: $newField->addAttribute('name', $this->xmlAttribute($elementName));
128: $newField->addAttribute('type', $this->xmlAttribute($elementType));
129: foreach ($config as $key => $val) {
130: $newField->addAttribute($key, $this->xmlAttribute($val));
131: }
132: return $newField;
133: }
134:
135: /**
136: * Add custom field to SimpleXML element
137: *
138: * @param string $childName
139: * @param string $value
140: * @param array $config
141: * @return Mage_XmlConnect_Model_Simplexml_Element
142: */
143: public function addCustomChild($childName, $value = null, $config = null)
144: {
145: $customFiled = $this->addChild($childName, $this->xmlentities($value));
146:
147: if (is_array($config)) {
148: foreach ($config as $key => $val) {
149: $customFiled->addAttribute($key, $this->xmlAttribute($val));
150: }
151: }
152: return $customFiled;
153: }
154: }
155: