Commit d79edba4 authored by Carsten  Rose's avatar Carsten Rose
Browse files

Store.php, StoreWebVar.php, StoreTest.php, StoreWebVarTest.php: Initial creation

parent 2e57b15a
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/1/16
* Time: 6:51 PM
*/
namespace qfq;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../qfq/StoreWebVar.php');
require_once(__DIR__ . '/../qfq/Constants.php');
/*
* Stores:
* - SIP
* - webVar
* - record
* - form
* - formElement
*/
class Store {
private static $instance = null;
// Store Classes
public $StoreWebVar = null;
/**
* Design Pattern: Singleton - class 'Store' should only be instanced once.
*/
private function __construct() {
$this->StoreWebVar = StoreWebVar::getInstance();
}
/**
* @return Store
*/
public static function getInstance() {
// Design Pattern: Singleton
if (self::$instance == null) {
self::$instance = new Store;
}
return self::$instance;
}
/**
* @param string $key
* @param string $useStore
* @param string $sanatizeClass
* @return string a) if found: value, b) false
* @throws CodeException
*/
public function getVar($key, $useStore = USE_STORE_DEFAULT, $sanatizeClass = SANATIZE_DEFAULT) {
$value = false;
// f.e.: $useStore='FSRD'
$store = substr($useStore, 0, 1);
$ii = 0;
// Iterate over all stores, to search for parameter $key
while ($store !== false) {
switch ($store) {
case STORE_CLIENT:
$value = $this->StoreWebVar->getVar($key, $sanatizeClass);
break;
case STORE_FORM:
case STORE_SIP:
case STORE_RECORD_ID:
case STORE_DATABASE_DEFINITION:
case STORE_ZERO:
throw new CodeException("Store not implemented: $store", ERROR_STORE_NOT_IMPLEMENTED);
break;
default:
throw new CodeException("Unknown Store: $store", ERROR_UNKNOWN_STORE);
break;
}
if ($value !== false)
return $value;
$store = substr($useStore, $ii, 1);
$ii++;
}
return false;
}
}
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/1/16
* Time: 6:51 PM
*/
namespace qfq;
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/Sanatize.php');
class StoreWebVar {
private static $instance = null;
// $cache[key]: null: never looked for, false: previous looked for, but not found, else: value
private static $cache = array();
private static $defaultSanatizeClass = array();
/**
* Design Pattern: Singleton - class 'StoreWebVar' should only be instanced from this class
*
*/
private function __construct() {
self::$defaultSanatizeClass = [
's' => SANATIZE_DIGIT,
'r ' => SANATIZE_DIGIT,
'keySemId' => SANATIZE_DIGIT,
'keySemIdUser' => SANATIZE_DIGIT,
'pageId' => SANATIZE_DIGIT,
'pageType' => SANATIZE_DIGIT,
'pageLanguage' => SANATIZE_DIGIT,
'form' => SANATIZE_ALNUMX
];
}
/**
* Design Pattern: Singleton - class 'StoreWebVar' should only be instanced once.
*
* @return Store
*/
public static function getInstance() {
// Design Pattern: Singleton
if (self::$instance == null) {
self::$instance = new StoreWebVar;
}
return self::$instance;
}
/**
* Get webvar, sanatize it and store it.
* Further access will be delivered by cache.
* @param string $key
* @param string $sanatizeClass
* @return string a) if not found: false, b) if found but SANATIZE failed: empty string, c) given value
* @throws CodeException
*/
public static function getVar($key, $sanatizeClass = SANATIZE_DEFAULT) {
unset($value);
// Already cached?
if (isset(self::$cache[$key]))
return self::$cache[$key];
if (isset($_GET[$key]))
$value = $_GET[$key];
if (isset($_POST[$key]))
$value = $_POST[$key];
// No value? break
if (!isset($value)) {
self::$cache[$key] = false;
return self::$cache[$key];
}
// Sanatize more specific than the default?
if ($sanatizeClass == SANATIZE_DEFAULT && isset(self::$defaultSanatizeClass[$key]))
$sanatizeClass = self::$defaultSanatizeClass[$key];
// sanatize
self::$cache[$key] = Sanatize::sanatize($value, $sanatizeClass);
return self::$cache[$key];
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/2/16
* Time: 9:16 PM
*/
namespace qfq;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../../qfq/Store.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../exceptions/CodeException.php');
class StoreTest extends \PHPUnit_Framework_TestCase {
public function testGetInstance() {
$a = Store::getInstance();
$b = Store::getInstance();
$this->assertFalse($a === null, "There should be a class");
$this->assertEquals($a, $b, "Both classes should be the same");
}
public function testGetVarClient() {
$store = Store::getInstance();
# Violates SANATIZE class: sanatized string is always an empty string.
# Access are cached: use new variables for every test.
# Test: default sanatize class
$_GET["form"] = 'mytestform';
$this->assertEquals('mytestform', $store->getVar('form', STORE_CLIENT), "FormName: default sanatize class");
# violates default SANATIZE digit: sanatized string is always an empty string.
$_GET["s"] = '1234abcd';
$this->assertEquals('', $store->getVar('s', STORE_CLIENT), "sanatize class digit fails");
}
public function testGetVarClientException() {
$store = Store::getInstance();
$catched = false;
try {
$_GET["ex02"] = 'Hello World';
$store->getVar('ex02', 'fake store');
} catch (CodeException $e) {
$catched = true;
}
$this->assertTrue($catched, 'Exception catched: invalid store');
}
}
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/2/16
* Time: 10:53 PM
*/
namespace qfq;
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../qfq/StoreWebVar.php');
class StoreWebVarTest extends \PHPUnit_Framework_TestCase {
public function testGetInstance() {
$a = StoreWebVar::getInstance();
$b = StoreWebVar::getInstance();
$this->assertFalse($a === null, "There should be a class");
$this->assertEquals($a, $b, "Both classes should be the same");
}
public function testGetVar() {
// Test GET
$_GET['key01'] = '1234';
$this->assertEquals('1234', StoreWebVar::getVar('key01'), "Param: GET");
// Test POST
$_POST['key02'] = '2345';
$this->assertEquals('2345', StoreWebVar::getVar('key02'), "Param: POST");
// Test not found
$this->assertFalse(StoreWebVar::getVar('keyUnknown'), "Param: unknown");
// Test Cache value
$_POST['key03'] = '3456';
StoreWebVar::getVar('key03');
$_POST['key03'] = 'lost';
$this->assertEquals('3456', StoreWebVar::getVar('key03'), "Param: read from cache");
// Test Cache not found
StoreWebVar::getVar('keyUnknwon2');
$this->assertFalse(StoreWebVar::getVar('keyUnknown2'), "Param: unknown from cache");
// Test overwrite default sanatize class
$_POST[CLIENT_FORM] = 'mytestform';
$this->assertEquals('', StoreWebVar::getVar(CLIENT_FORM, SANATIZE_DIGIT), "Param: overwrite default sanatize class");
// Test: POST higher priority than GET
$_GET['key04'] = '4567';
$_POST['key04'] = '5678';
$this->assertEquals('5678', StoreWebVar::getVar('key04'), "Param: POST higher priority than GET");
}
}
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