Skip to content
Snippets Groups Projects
Commit 3680f11f authored by Carsten  Rose's avatar Carsten Rose
Browse files

Support: creation of generic helper functions.

parent 0b1572c8
No related branches found
No related tags found
No related merge requests found
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/28/16
* Time: 8:05 AM
*/
namespace qfq;
require_once(__DIR__ . '/Sanatize.php');
class Support {
/**
* @return string If exist content of $_GET[], otherwise the name of the first GET Parameter
* @throws exceptions\CodeException
*/
public static function getCurrentPage() {
if (isset($_GET['id'])) {
$page = $_GET['id'];
} else {
$page = explode('&', $_SERVER['QUERY_STRING'])[0];
$page = explode('=', $page)[0];
}
return Sanatize::sanatize($page, SANATIZE_ALLOW_ALNUMX);
}
/**
* @param array $queryArray Empty or prefilled assoc array with url parameter
*/
public static function appendTypo3ParameterToArray(array &$queryArray) {
if (isset($_GET['L']))
$queryArray['L'] = $_GET['L'];
if (isset($_GET['type']))
$queryArray['type'] = $_GET['type'];
}
/**
* Builds a urlencoded query string of assoc array
* @param array $queryArray
* @return string
*/
public static function arrayToQueryString(array $queryArray) {
$items = array();
foreach ($queryArray as $key => $value) {
if (is_int($key)) {
$items[] = urlencode($value);
} else {
$items[] = $key . '=' . urlencode($value);
}
}
return implode('&', $items);
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/28/16
* Time: 9:58 AM
*/
namespace qfq;
require_once(__DIR__ . '/../../qfq/helper/Support.php');
class SupportTest extends \PHPUnit_Framework_TestCase {
public function testGetCurrentPage() {
unset($_GET);
$_SERVER['QUERY_STRING'] = '';
$this->assertEquals('', Support::getCurrentPage());
$_SERVER['QUERY_STRING'] = 'firstpage&a=100';
$this->assertEquals('firstpage', Support::getCurrentPage());
$_GET['id'] = 'secondpage';
$this->assertEquals('secondpage', Support::getCurrentPage());
$_GET['id'] = 'secondpage<forbidden';
$this->assertEquals('', Support::getCurrentPage());
}
public function testAppendTypo3ParameterToArray() {
unset($_GET);
$queryArray = array();
Support::appendTypo3ParameterToArray($queryArray);
$this->assertEquals(array(), $queryArray);
$queryArray = ['id' => '', 'a' => '100'];
$expected = $queryArray;
Support::appendTypo3ParameterToArray($queryArray);
$this->assertEquals($expected, $queryArray);
$_GET['L'] = 1;
$_GET['type'] = 100;
$queryArray = ['id' => '', 'a' => '100'];
$expected = array_merge($_GET, $queryArray);
Support::appendTypo3ParameterToArray($queryArray);
$this->assertEquals($expected, $queryArray);
unset($_GET);
$_GET['L'] = 1;
$queryArray = ['id' => '', 'a' => '100'];
$expected = array_merge($_GET, $queryArray);
Support::appendTypo3ParameterToArray($queryArray);
$this->assertEquals($expected, $queryArray);
unset($_GET);
$_GET['type'] = 1;
$queryArray = ['id' => '', 'a' => '100'];
$expected = array_merge($_GET, $queryArray);
Support::appendTypo3ParameterToArray($queryArray);
$this->assertEquals($expected, $queryArray);
}
public function testArrayToQueryString() {
$actual = Support::arrayToQueryString(array());
$expected = '';
$this->assertEquals($expected, $actual);
$actual = Support::arrayToQueryString(['id', 'name']);
$expected = '';
$this->assertEquals('id&name', $actual);
$actual = Support::arrayToQueryString(['id' => '55', 'name']);
$expected = '';
$this->assertEquals('id=55&name', $actual);
$actual = Support::arrayToQueryString(['id' => '55', 'name' => 'Heinz']);
$expected = '';
$this->assertEquals('id=55&name=Heinz', $actual);
$actual = Support::arrayToQueryString(['id' => '55', 'name' => 'He<b>inz']);
$expected = '';
$this->assertEquals('id=55&name=He%3Cb%3Einz', $actual);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment