1<?php 2 3namespace FINDOLOGIC\Export\Data; 4 5use FINDOLOGIC\Export\Helpers\DataHelper; 6use FINDOLOGIC\Export\Helpers\Serializable; 7use FINDOLOGIC\Export\Helpers\XMLHelper; 8 9class Attribute implements Serializable 10{ 11 private $key; 12 13 private $values; 14 15 /** 16 * @SuppressWarnings(PHPMD.StaticAccess) 17 */ 18 public function __construct($key, $values = []) 19 { 20 $this->key = DataHelper::checkForEmptyValue($key); 21 $this->setValues($values); 22 } 23 24 /** 25 * @SuppressWarnings(PHPMD.StaticAccess) 26 */ 27 public function addValue($value) 28 { 29 array_push($this->values, DataHelper::checkForEmptyValue($value)); 30 } 31 32 public function setValues($values) 33 { 34 $this->values = []; 35 36 foreach ($values as $value) { 37 $this->addValue($value); 38 } 39 } 40 41 public function getKey() 42 { 43 return $this->key; 44 } 45 46 /** 47 * @SuppressWarnings(PHPMD.StaticAccess) 48 * @inheritdoc 49 */ 50 public function getDomSubtree(\DOMDocument $document) 51 { 52 $attributeElem = XMLHelper::createElement($document, 'attribute'); 53 54 $keyElem = XMLHelper::createElementWithText($document, 'key', $this->key); 55 $attributeElem->appendChild($keyElem); 56 57 $valuesElem = XMLHelper::createElement($document, 'values'); 58 $attributeElem->appendChild($valuesElem); 59 60 foreach ($this->values as $value) { 61 $valueElem = XMLHelper::createElementWithText($document, 'value', $value); 62 $valuesElem->appendChild($valueElem); 63 } 64 65 return $attributeElem; 66 } 67 68 /** 69 * @inheritdoc 70 */ 71 public function getCsvFragment() 72 { 73 $encoded = ''; 74 75 foreach ($this->values as $value) { 76 $encoded .= sprintf('%s=%s&', urlencode($this->key), urlencode($value)); 77 } 78 79 $encoded = rtrim($encoded, '&'); 80 81 return $encoded; 82 } 83} 84