1<?php 2 3namespace FINDOLOGIC\Export\Helpers; 4 5/** 6 * Class UsergroupAwareSimpleValue 7 * @package FINDOLOGIC\Export\Helpers 8 * 9 * Simple values that can differ per usergroup, but have one value at most for each. 10 */ 11abstract class UsergroupAwareSimpleValue implements Serializable 12{ 13 private $collectionName; 14 private $itemName; 15 private $values = []; 16 17 public function __construct($collectionName, $itemName) 18 { 19 $this->collectionName = $collectionName; 20 $this->itemName = $itemName; 21 } 22 23 public function getValues() 24 { 25 return $this->values; 26 } 27 28 /** 29 * @SuppressWarnings(PHPMD.StaticAccess) 30 */ 31 public function setValue($value, $usergroup = '') 32 { 33 $this->values[$usergroup] = $this->validate($value); 34 } 35 36 /** 37 * Validates given value. 38 * Basic implementation is validating against an empty string, 39 * but is overridden when checking values more specific. 40 * 41 * When valid returns given value. 42 * When not valid an exception is thrown. 43 * 44 * @param $value string|int Validated value. 45 * @return string string|int 46 * @throws EmptyValueNotAllowedException 47 */ 48 protected function validate($value) 49 { 50 $value = trim($value); 51 52 if ($value === '') { 53 throw new EmptyValueNotAllowedException(); 54 } 55 56 return $value; 57 } 58 59 /** 60 * @SuppressWarnings(PHPMD.StaticAccess) 61 * @inheritdoc 62 */ 63 public function getDomSubtree(\DOMDocument $document) 64 { 65 $collectionElem = XMLHelper::createElement($document, $this->collectionName); 66 67 foreach ($this->values as $usergroup => $value) { 68 $itemElem = XMLHelper::createElementWithText($document, $this->itemName, $value); 69 $collectionElem->appendChild($itemElem); 70 71 if ($usergroup !== '') { 72 $itemElem->setAttribute('usergroup', $usergroup); 73 } 74 } 75 76 return $collectionElem; 77 } 78 79 /** 80 * @inheritdoc 81 */ 82 public function getCsvFragment() 83 { 84 if (array_key_exists('', $this->values)) { 85 $value = $this->values['']; 86 } else { 87 $value = ''; 88 } 89 90 return $value; 91 } 92} 93