Commit 663a25ae authored by Carsten  Rose's avatar Carsten Rose
Browse files

KeyValueStringParser.php: rewritten to static class

parent f0750f47
......@@ -34,81 +34,36 @@ require_once(__DIR__ . '/../exceptions/UserException.php');
* @package qfq
*/
class KeyValueStringParser {
/**
* @var string delimiter delimiting key and value
*/
private $keyValueDelimiter;
/**
* @var string delimiter delimiting key/value pairs.
*/
private $listDelimiter;
/**
* Constructor
*
* @param string $keyValueDelimiter delimiter of key/values
* @param string $listDelimiter delimiter of key/value pairs
*/
public function __construct($keyValueDelimiter = ":", $listDelimiter = ",") {
$this->keyValueDelimiter = $keyValueDelimiter;
$this->listDelimiter = $listDelimiter;
}
/**
* Get key/value delimiter
*
* @return string key/value delimiter
*/
public function getKeyValueDelimiter() {
return $this->keyValueDelimiter;
}
/**
* Set key/value delimiter
*
* @param string $keyValueDelimiter key/value delimiter
*/
public function setKeyValueDelimiter($keyValueDelimiter) {
$this->keyValueDelimiter = $keyValueDelimiter;
}
/**
* Get key/value pairs delimiter
*
* @return string key/value pairs delimiter
*/
public function getListDelimiter() {
return $this->listDelimiter;
}
/**
* Set key/value pairs delimiter
*
* @param string $listDelimiter key/value pairs delimiter
* @param array $keyValueArray
* @param string $keyValueDelimiter
* @param string $listDelimiter
* @return string
*/
public function setListDelimiter($listDelimiter) {
$this->listDelimiter = $listDelimiter;
}
public function unparse(array $keyValueArray) {
public static function unparse(array $keyValueArray, $keyValueDelimiter = ":", $listDelimiter = ",") {
array_walk($keyValueArray, function (&$value) {
if (!is_string($value) || $value === "" || strlen($value) === 1) {
return;
}
if ($value[0] === " " && $this->isFirstAndLastCharacterIdentical($value[0])) {
if ($value[0] === " " && self::isFirstAndLastCharacterIdentical($value[0])) {
$value = '"' . $value . '"';
}
});
$newKeyValuePairImploded = array();
foreach ($keyValueArray as $key => $value) {
$newKeyValuePairImploded[] = trim($key) . $this->keyValueDelimiter . $value;
$newKeyValuePairImploded[] = trim($key) . $keyValueDelimiter . $value;
}
return implode($this->listDelimiter, $newKeyValuePairImploded);
return implode($listDelimiter, $newKeyValuePairImploded);
}
/**
* @param $string
* @return bool
*/
private function isFirstAndLastCharacterIdentical($string) {
if ($string === "") {
return false;
......@@ -129,12 +84,12 @@ class KeyValueStringParser {
* @throws \qfq\exceptions\UserException
*/
public function parse($keyValueString) {
public static function parse($keyValueString, $keyValueDelimiter = ":", $listDelimiter = ",") {
if ($keyValueString === "") {
return array();
}
$keyValuePairs = explode($this->listDelimiter, $keyValueString);
$keyValuePairs = explode($listDelimiter, $keyValueString);
$returnValue = array();
foreach ($keyValuePairs as $keyValuePairString) {
......@@ -143,7 +98,7 @@ class KeyValueStringParser {
continue;
}
$keyValueArray = explode($this->keyValueDelimiter, $keyValuePairString, 2);
$keyValueArray = explode($keyValueDelimiter, $keyValuePairString, 2);
$key = trim($keyValueArray[0]);
......@@ -159,7 +114,7 @@ class KeyValueStringParser {
if (count($keyValueArray) === 2) {
// "a:1", "a:"
$returnValue[$key] = $this->removeSourroundingQuotes(
$returnValue[$key] = self::removeSourroundingQuotes(
trim($keyValueArray[1])
);
} else {
......@@ -182,7 +137,7 @@ class KeyValueStringParser {
return $string;
}
if (in_array($string[0], $quotes) === true && $this->isFirstAndLastCharacterIdentical($string)) {
if (in_array($string[0], $quotes) === true && self::isFirstAndLastCharacterIdentical($string)) {
return substr($string, 1, strlen($string) - 2);
}
......
......@@ -12,6 +12,7 @@ use qfq\exceptions\CodeException;
use qfq\exceptions\UserException;
//use qfq\helper;
use qfq\helper\OnArray;
use qfq\helper\KeyValueStringParser;
require_once(__DIR__ . '/../../qfq/helper/OnArray.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
......@@ -70,8 +71,7 @@ class Sip {
$sipArray = array();
// Split URL parameter:
$parser = new \qfq\helper\KeyValueStringParser("=", "&");
$paramArray = $parser->parse($urlParam);
$paramArray = KeyValueStringParser::parse($urlParam, "=", "&");
// Split parameter between Script, Client and SIP
$script = $this->splitParamClientSip($paramArray, $clientArray, $sipArray);
......@@ -218,9 +218,7 @@ class Sip {
}
// Decode parameter
$parser = new \qfq\helper\KeyValueStringParser("=", "&");
return $parser->parse($_SESSION[$s]);
return KeyValueStringParser::parse($_SESSION[$s], "=", "&");
}
/**
......
......@@ -10,6 +10,7 @@ namespace qfq\store;
use qfq\exceptions\CodeException;
use qfq;
use \qfq\helper\keyValueStringParser;
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
require_once(__DIR__ . '/../../qfq/helper/Sanatize.php');
......@@ -176,8 +177,7 @@ class Store {
*/
private function fillStoreTypo3($bodytext) {
$parser = new \qfq\helper\KeyValueStringParser("=", "\n");
$arr = $parser->parse($bodytext);
$arr = KeyValueStringParser::parse($bodytext, "=", "\n");
if (isset($GLOBALS["TSFE"]->fe_user->user["username"]))
$arr[TYPO3_FE_USER] = $GLOBALS["TSFE"]->fe_user->user["username"];
......@@ -222,8 +222,7 @@ class Store {
// if session is given, copy values to store
$param = self::$sip->getVarsFromSip($s);
$parser = new \qfq\helper\KeyValueStringParser("=", "&");
self::setVarArray($parser->parse($param), STORE_SIP);
self::setVarArray(KeyValueStringParser::parse($param, "=", "&"), STORE_SIP);
}
}
......
......@@ -13,17 +13,8 @@ require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
/**
* @var KeyValueStringParser
*/
private $keyValueStringParser;
public function setUp() {
$this->keyValueStringParser = new KeyValueStringParser();
}
public function testSingleKeyValuePair() {
$actual = $this->keyValueStringParser->parse("key:value");
$actual = keyValueStringParser::parse("key:value");
$this->assertCount(1, $actual);
$this->assertArrayHasKey('key', $actual);
......@@ -31,14 +22,14 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testKeyWithoutValue() {
$actual = $this->keyValueStringParser->parse("keywithoutvalue");
$actual = keyValueStringParser::parse("keywithoutvalue");
$this->assertCount(1, $actual);
$this->assertArrayHasKey('keywithoutvalue', $actual);
$this->assertSame("", $actual['keywithoutvalue']);
}
public function testGOODNAMEHERE() {
$actual = $this->keyValueStringParser->parse(",,");
$actual = keyValueStringParser::parse(",,");
$this->assertCount(0, $actual);
}
......@@ -47,11 +38,11 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
* @expectedException \qfq\exceptions\UserException
*/
public function testNoKey() {
$this->keyValueStringParser->parse(":value,key:value");
keyValueStringParser::parse(":value,key:value");
}
public function testNoValue() {
$actual = $this->keyValueStringParser->parse("key1:,key2:value2");
$actual = keyValueStringParser::parse("key1:,key2:value2");
$this->assertCount(2, $actual);
$this->assertArrayHasKey('key1', $actual);
$this->assertArrayHasKey('key2', $actual);
......@@ -60,13 +51,13 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testEmptyKeyValuePairString() {
$actual = $this->keyValueStringParser->parse("");
$actual = keyValueStringParser::parse("");
$this->assertCount(0, $actual);
}
public function testMultipleKeyValuePairs() {
$actual = $this->keyValueStringParser->parse("key1:value1,key2:value2");
$actual = keyValueStringParser::parse("key1:value1,key2:value2");
$this->assertCount(2, $actual);
$this->assertArrayHasKey('key1', $actual);
......@@ -76,7 +67,7 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testKeyValueSeparatorInValue() {
$actual = $this->keyValueStringParser->parse("key1:val:ue1,key2:value2");
$actual = keyValueStringParser::parse("key1:val:ue1,key2:value2");
$this->assertCount(2, $actual);
$this->assertArrayHasKey('key1', $actual);
......@@ -86,7 +77,7 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testWhiteSpaceHandling() {
$actual = $this->keyValueStringParser->parse(" key1 : val:ue1 , key2 : value2 ");
$actual = keyValueStringParser::parse(" key1 : val:ue1 , key2 : value2 ");
$this->assertCount(2, $actual);
$this->assertArrayHasKey('key1', $actual);
......@@ -96,7 +87,7 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testSourroundingQuotes() {
$actual = $this->keyValueStringParser->parse("key1:\" val:ue1 \", key2:' value2 ', key3:\"value3', key4:''");
$actual = keyValueStringParser::parse("key1:\" val:ue1 \", key2:' value2 ', key3:\"value3', key4:''");
$expected = [
'key1' => ' val:ue1 ',
......@@ -108,7 +99,7 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testComments() {
$actual = $this->keyValueStringParser->parse(" key1 : val:ue1 , # key2 : value2 , ; : broken key value in comment, key3 : valid ");
$actual = keyValueStringParser::parse(" key1 : val:ue1 , # key2 : value2 , ; : broken key value in comment, key3 : valid ");
$expected = [
'key1' => 'val:ue1',
......@@ -118,8 +109,8 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testUnparse() {
$array = $this->keyValueStringParser->parse("key1:\" val:ue1 \", key2:' value2 ', key3:\"value3'");
$actual = $this->keyValueStringParser->unparse($array);
$array = keyValueStringParser::parse("key1:\" val:ue1 \", key2:' value2 ', key3:\"value3'");
$actual = keyValueStringParser::unparse($array);
$expected = "key1:\" val:ue1 \",key2:\" value2 \",key3:\"value3'";
$this->assertSame($expected, $actual);
......@@ -127,9 +118,8 @@ class KeyValueStringParserTest extends \PHPUnit_Framework_TestCase {
}
public function testKeyValuePairCR() {
$keyValueStringParserCR = new KeyValueStringParser("=", "\n");
$actual = $keyValueStringParserCR->parse("key1=value1\nkey2=value2");
$actual = keyValueStringParser::parse("key1=value1\nkey2=value2", "=", "\n");
$this->assertCount(2, $actual);
$this->assertArrayHasKey('key1', $actual);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment