* @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ class WebserviceOutputXMLCore implements WebserviceOutputInterface { public $docUrl = ''; public $languages = []; protected $wsUrl; protected $schemaToDisplay; public function setSchemaToDisplay($schema) { if (is_string($schema)) { $this->schemaToDisplay = $schema; } return $this; } public function getSchemaToDisplay() { return $this->schemaToDisplay; } public function setWsUrl($url) { $this->wsUrl = $url; return $this; } public function getWsUrl() { return $this->wsUrl; } public function getContentType() { return 'text/xml'; } public function __construct($languages = []) { $this->languages = $languages; } public function setLanguages($languages) { $this->languages = $languages; return $this; } public function renderErrorsHeader() { return '' . "\n"; } public function renderErrorsFooter() { return '' . "\n"; } public function renderErrors($message, $code = null) { $str_output = '' . "\n"; if ($code !== null) { $str_output .= '' . "\n"; } $str_output .= '' . "\n"; $str_output .= '' . "\n"; return $str_output; } public function renderField($field) { $ret = ''; $node_content = ''; $ret .= '<' . $field['sqlId']; // display i18n fields if (isset($field['i18n']) && $field['i18n']) { foreach ($this->languages as $language) { $more_attr = ''; if (isset($field['synopsis_details']) || (isset($field['value']) && is_array($field['value']))) { $more_attr .= ' xlink:href="' . $this->getWsUrl() . 'languages/' . $language . '"'; if (isset($field['synopsis_details']) && $this->schemaToDisplay != 'blank') { $more_attr .= ' format="isUnsignedId" '; } } $node_content .= ''; $node_content .= ''; $node_content .= ''; } } else { // display not i18n fields value if (array_key_exists('xlink_resource', $field) && $this->schemaToDisplay != 'blank') { if (!is_array($field['xlink_resource'])) { $ret .= ' xlink:href="' . $this->getWsUrl() . $field['xlink_resource'] . '/' . $field['value'] . '"'; } else { $ret .= ' xlink:href="' . $this->getWsUrl() . $field['xlink_resource']['resourceName'] . '/' . (isset($field['xlink_resource']['subResourceName']) ? $field['xlink_resource']['subResourceName'] . '/' . $field['object_id'] . '/' : '') . $field['value'] . '"'; } } if (isset($field['getter']) && $this->schemaToDisplay != 'blank') { $ret .= ' notFilterable="true"'; } if (isset($field['setter']) && $field['setter'] == false && $this->schemaToDisplay == 'synopsis') { $ret .= ' read_only="true"'; } if (array_key_exists('value', $field)) { $node_content .= ''; } } if (isset($field['encode'])) { $ret .= ' encode="' . $field['encode'] . '"'; } if (isset($field['synopsis_details']) && !empty($field['synopsis_details']) && $this->schemaToDisplay !== 'blank') { foreach ($field['synopsis_details'] as $name => $detail) { $ret .= ' ' . $name . '="' . (is_array($detail) ? implode(' ', $detail) : $detail) . '"'; } } $ret .= '>'; $ret .= $node_content; $ret .= '' . "\n"; return $ret; } public function renderNodeHeader($node_name, $params, $more_attr = null, $has_child = true) { $string_attr = ''; if (is_array($more_attr)) { foreach ($more_attr as $key => $attr) { if ($key === 'xlink_resource') { $string_attr .= ' xlink:href="' . $attr . '"'; } else { $string_attr .= ' ' . $key . '="' . $attr . '"'; } } } $end_tag = (!$has_child) ? '/>' : '>'; return '<' . $node_name . $string_attr . $end_tag . "\n"; } public function getNodeName($params) { $node_name = ''; if (isset($params['objectNodeName'])) { $node_name = $params['objectNodeName']; } return $node_name; } public function renderNodeFooter($node_name, $params) { return '' . "\n"; } public function overrideContent($content) { $xml = '' . "\n"; $xml .= '' . "\n"; $xml .= $content; $xml .= '' . "\n"; return $xml; } public function renderAssociationWrapperHeader() { return '' . "\n"; } public function renderAssociationWrapperFooter() { return '' . "\n"; } public function renderAssociationHeader($obj, $params, $assoc_name, $closed_tags = false) { $end_tag = ($closed_tags) ? '/>' : '>'; $more = ''; if ($this->schemaToDisplay != 'blank') { if (array_key_exists('setter', $params['associations'][$assoc_name]) && !$params['associations'][$assoc_name]['setter']) { $more .= ' readOnly="true"'; } $more .= ' nodeType="' . $params['associations'][$assoc_name]['resource'] . '"'; if (isset($params['associations'][$assoc_name]['virtual_entity']) && $params['associations'][$assoc_name]['virtual_entity']) { $more .= ' virtualEntity="true"'; } else { if (isset($params['associations'][$assoc_name]['api'])) { $more .= ' api="' . $params['associations'][$assoc_name]['api'] . '"'; } else { $more .= ' api="' . $assoc_name . '"'; } } } return '<' . $assoc_name . $more . $end_tag . "\n"; } public function renderAssociationFooter($obj, $params, $assoc_name) { return '' . "\n"; } }