Your IP : 18.191.103.151
<?php
namespace Epages\Modules\Google;
class XmlNode
{
private $selectedNode = null;
private $nodesArray = [
'title' => [
'representation' => '<title>#VALUE#</title>',
'value_type' => 'string',
],
'link' => [
'representation' => '<link>#VALUE#</link>',
'value_type' => 'url',
],
'description' => [
'representation' => '<description>#VALUE#</description>',
'value_type' => 'text',
],
'item' => [
'representation' => '<item>#VALUE#</item>',
'value_type' => 'string',
],
'id' => [
'representation' => '<g:id>#VALUE#</g:id>',
'value_type' => 'int',
],
'item_group_id' => [
'representation' => '<g:item_group_id>#VALUE#</g:item_group_id>',
'value_type' => 'int',
],
'availability' => [
'representation' => '<g:availability>#VALUE#</g:availability>',
'value_type' => 'list',
'values_list' => [
'Y' => 'in stock',
'N' => 'out of stock',
'P' => 'preorder',
],
],
'price' => [
'representation' => '<g:price>#VALUE#</g:price>',
'value_type' => 'price',
],
'product_type' => [
'representation' => '<g:product_type>#VALUE#</g:product_type>',
'value_type' => 'string',
],
'image' => [
'representation' => '<g:image_link>#VALUE#</g:image_link>',
'value_type' => 'url',
],
'additional_image' => [
'representation' => '<g:additional_image_link>#VALUE#</g:additional_image_link>',
'value_type' => 'url',
],
'condition' => [
'representation' => '<g:condition>#VALUE#</g:condition>',
'value_type' => 'list',
'values_list' => [
'N' => 'new',
'U' => 'used',
'R' => 'refurbished',
],
],
'brand' => [
'representation' => '<g:brand>#VALUE#</g:brand>',
'value_type' => 'string',
],
'gtin' => [
'representation' => '<g:gtin>#VALUE#</g:gtin>',
'value_type' => 'string',
],
'color' => [
'representation' => '<g:color>#VALUE#</g:color>',
'value_type' => 'string',
],
'gender' => [
'representation' => '<g:gender>#VALUE#</g:gender>',
'value_type' => 'list',
'values_list' => [
'M' => 'male',
'F' => 'female',
'U' => 'unisex',
],
],
'material' => [
'representation' => '<g:material>#VALUE#</g:material>',
'value_type' => 'string',
],
'weight' => [
'representation' => '<g:shipping_weight>#VALUE#</g:shipping_weight>',
'value_type' => 'string',
],
'width' => [
'representation' => '<g:shipping_width>#VALUE#</g:shipping_width>',
'value_type' => 'string',
],
'length' => [
'representation' => '<g:shipping_length>#VALUE#</g:shipping_length>',
'value_type' => 'string',
],
'height' => [
'representation' => '<g:shipping_height>#VALUE#</g:shipping_height>',
'value_type' => 'string',
],
'google_category' => [
'representation' => '<g:google_product_category>#VALUE#</g:google_product_category>',
'value_type' => 'int',
],
'age' => [
'representation' => '<g:age_group>#VALUE#</g:age_group>',
'value_type' => 'list',
'values_list' => [
'newborn' => 'newborn',
'infant' => 'infant',
'toddler' => 'toddler',
'kids' => 'kids',
'adult' => 'adult',
],
],
];
public function getNode($nodeName) {
return ($this->setSelectedNode($nodeName)) ? $this->getCurrentNode() : false;
}
public function setValue($value) {
if (!empty($value)) {
if (!is_array($value)) {
$value = $this->format($value);
if ($this->validate($value)) {
return $this->setValueToTag($this->selectedNode, $value);
}
} else {
foreach ($value as $key => $val) {
$val = $this->format($val);
if ($this->validate($val)) {
return $this->setValueToTag($this->selectedNode, $val);
}
}
unset($key, $val);
}
} else {
return false;
}
}
protected function format($value) {
switch ($this->selectedNode['value_type']) {
case 'int':
return $this->xmlFormat(floatval($value));
break;
case 'string':
return $this->xmlFormat($value);
break;
case 'list':
return $this->selectedNode['values_list'][$value];
break;
case 'price':
preg_match_all('#(\d{1,7}\.\d{2})+\s+([A-Z]{3})#', $value, $matches);
return $this->xmlFormat(floatval($matches[1][0]).' '.$matches[2][0]);
break;
case 'url':
return $this->xmlFormat($value);
break;
case 'text':
return $this->cData(mb_substr($this->xmlFormat(strip_tags($value)), 0, 5000));
break;
default:
return $value;
break;
}
}
protected function validate($value) {
switch ($this->selectedNode['value_type']) {
case 'int':
return is_numeric($value);
break;
case 'string':
case 'text':
return is_string($value);
break;
case 'list':
return in_array($value, $this->selectedNode['values_list']);
break;
case 'price':
preg_match_all('#(\d{1,7}\.{0,1}\d{2})+\s+([A-Z]{3})#', $value, $matches);
return is_numeric($matches[1][0]) && is_string($matches[2][0]);
break;
case 'url':
return true;//filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED | FILTER_FLAG_HOST_REQUIRED | FILTER_FLAG_PATH_REQUIRED);
break;
default:
return true;
break;
}
}
protected function xmlFormat($value) {
$value = preg_replace('/&(?!#?[a-z0-9]+;)/', '&', $value);
return $value;
}
protected function cData($text) {
$text = str_replace(
['<![CDATA[', ']]>'],
['<![CDATA[', ']]>'],
$text
);
return '<![CDATA[' . PHP_EOL . $text . PHP_EOL . ']]>';
}
protected function setSelectedNode($nodeName) {
return $this->selectedNode = (array_key_exists($nodeName, $this->nodesArray)) ? $this->nodesArray[$nodeName] : false;
}
protected function getCurrentNode() {
return $this;
}
protected function getCurrentNodeField($field) {
return $this->selectedNode[$field];
}
protected function setValueToTag($tag, $value) {
return str_replace('#VALUE#', $value, $tag['representation']);
}
}