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

Namespace adjusted

parent aab01bb1
......@@ -32,7 +32,7 @@
<?php
include("t3/form.php");
qfq\renderForm();
qfq\renderForm("\n# some notes\ndebug = 5\n form = mytestform");
?>
......
......@@ -16,13 +16,16 @@ const SANATIZE_DEFAULT = SANATIZE_DIGIT;
const ERROR_UNKNOW_SANATIZE_CLASS = 1001;
const ERROR_STORE_NOT_IMPLEMENTED = 1002;
const ERROR_UNKNOWN_STORE = 1003;
const ERROR_VALUES_ALREADY_CODPIED = 1004;
const ERROR_SETVARARRAY_NOT_IMPLEMENTED = 1005;
// Store Identifier
const STORE_FORM = "F"; // form, still not saved in database
const STORE_SIP = "S"; // SIP
const STORE_RECORD_ID = "R"; // table.id
const STORE_RECORD = "R"; // selected record from primary table.
const STORE_DATABASE_DEFINITION = "D"; // Default definition in database
const STORE_CLIENT = "C"; // Client: POST variable, if not found: GET variable
const STORE_T3_BODYTEXT = "B"; // Typo3 > Page content record > bodytext
const STORE_ZERO = "0"; // value: 0, might helpfull if variable is empty but used in an SQL statement, which might produce a SQL error otherwise if substituted with an empty string
const USE_STORE_DEFAULT = "FSRD";
......
......@@ -9,11 +9,15 @@
namespace qfq;
use qfq;
require_once(__DIR__ . '/../qfq/Store.php');
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/helper/KeyValueStringParser.php');
class Form {
protected $config = null;
protected $store = null;
// protected $formElements = null;
......@@ -37,15 +41,18 @@ class Form {
*/
$this->store = Store::getInstance();
}
/*
*
*/
public function process($bodytext) {
$this->parseBodyText($bodytext);
public function process() {
$this->loadFormDefinition();
//
return "Hello World from class Form";
}
......@@ -53,11 +60,24 @@ class Form {
*
*/
private function parseBodyText($bodytext) {
$keyValue = new KeyValueStringParser("=", "\n");
$this->store->setVarArray($keyValue->parse($bodytext), STORE_T3_BODYTEXT);
}
private function loadFormDefinition() {
$formName = $this->getFormName();
echo "--$formName--";
}
/*
*
*/
private function getFormName() {
// by bodytext
// by session
......@@ -67,4 +87,5 @@ class Form {
return $formName;
}
}
\ No newline at end of file
......@@ -8,6 +8,7 @@
namespace qfq;
use qfq;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../qfq/Constants.php');
......
......@@ -11,6 +11,7 @@ namespace qfq;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../qfq/StoreWebVar.php');
require_once(__DIR__ . '/../qfq/StoreT3Bodytext.php');
require_once(__DIR__ . '/../qfq/Constants.php');
......@@ -29,12 +30,14 @@ class Store {
// Store Classes
public $StoreWebVar = null;
public $StoreT3Bodytext = null;
/**
* Design Pattern: Singleton - class 'Store' should only be instanced once.
*/
private function __construct() {
$this->StoreWebVar = StoreWebVar::getInstance();
$this->StoreT3Bodytext = StoreT3Bodytext::getInstance();
}
/**
......@@ -50,17 +53,17 @@ class Store {
/**
* @param string $key
* @param string $useStore
* @param string $respectStores
* @param string $sanatizeClass
* @return string a) if found: value, b) false
* @throws CodeException
*/
public function getVar($key, $useStore = USE_STORE_DEFAULT, $sanatizeClass = SANATIZE_DEFAULT) {
public function getVar($key, $respectStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
$value = false;
// f.e.: $useStore='FSRD'
$store = substr($useStore, 0, 1);
$ii = 0;
$store = substr($respectStores, $ii, 1);
// Iterate over all stores, to search for parameter $key
while ($store !== false) {
......@@ -68,9 +71,13 @@ class Store {
case STORE_CLIENT:
$value = $this->StoreWebVar->getVar($key, $sanatizeClass);
break;
case STORE_T3_BODYTEXT:
$value = $this->StoreT3Bodytext->getVar($key, $sanatizeClass);
break;
case STORE_FORM:
case STORE_SIP:
case STORE_RECORD_ID:
case STORE_RECORD:
case STORE_DATABASE_DEFINITION:
case STORE_ZERO:
throw new CodeException("Store not implemented: $store", ERROR_STORE_NOT_IMPLEMENTED);
......@@ -84,11 +91,39 @@ class Store {
if ($value !== false)
return $value;
$store = substr($useStore, $ii, 1);
$ii++;
$store = substr($respectStores, $ii, 1);
}
return false;
}
public function setVarArray(array $dataArray, $store) {
switch ($store) {
case STORE_CLIENT:
throw new CodeException("setVarArray not implemented for store: $store", ERROR_SETVARARRAY_NOT_IMPLEMENTED);
break;
case STORE_T3_BODYTEXT:
$this->StoreT3Bodytext->setVarArray($dataArray);
break;
case STORE_FORM:
case STORE_SIP:
case STORE_RECORD:
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;
}
}
}
......
<?php
/**
* Created by PhpStorm.
* User: crose
* Date: 1/3/16
* Time: 12:52 PM
*/
namespace qfq;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../qfq/Constants.php');
require_once(__DIR__ . '/../qfq/Sanatize.php');
class StoreT3Bodytext {
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 $raw = array();
private static $defaultSanatizeClass = array();
/**
* Design Pattern: Singleton - class should only be instanced internally
*
*/
private function __construct() {
self::$defaultSanatizeClass = [
'debugLoad' => SANATIZE_DIGIT,
'debugSave' => SANATIZE_DIGIT,
'form' => SANATIZE_ALNUMX
];
}
/**
* Design Pattern: Singleton - class should only be instanced once.
*
* @return Store
*/
public static function getInstance() {
// Design Pattern: Singleton
if (self::$instance == null) {
self::$instance = new StoreT3Bodytext;
}
return self::$instance;
}
/**
* @param array $dataArray
*/
public static function setVarArray(array $dataArray) {
if (count(self::$raw) > 0) {
throw new CodeException("Raw values already been copied. Do this only one time.", ERROR_VALUES_ALREADY_CODPIED);
}
self::$raw = $dataArray;
}
/**
* @param $key
* @param string $sanatizeClass
* @return mixed
* @throws exceptions\CodeException
*/
public static function getVar($key, $sanatizeClass = '') {
// Already cached?
if (isset(self::$cache[$key]))
return self::$cache[$key];
// No value? break
if (!isset(self::$raw[$key])) {
self::$cache[$key] = false;
return self::$cache[$key];
}
if ($sanatizeClass === '') {
$sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
}
// sanatize
self::$cache[$key] = Sanatize::sanatize(self::$raw[$key], $sanatizeClass);
return self::$cache[$key];
}
}
\ No newline at end of file
......@@ -21,7 +21,7 @@ class StoreWebVar {
private static $defaultSanatizeClass = array();
/**
* Design Pattern: Singleton - class 'StoreWebVar' should only be instanced from this class
* Design Pattern: Singleton - class should only be instanced internally
*
*/
private function __construct() {
......@@ -38,7 +38,7 @@ class StoreWebVar {
}
/**
* Design Pattern: Singleton - class 'StoreWebVar' should only be instanced once.
* Design Pattern: Singleton - class should only be instanced once.
*
* @return Store
*/
......@@ -58,7 +58,7 @@ class StoreWebVar {
* @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) {
public static function getVar($key, $sanatizeClass = '') {
unset($value);
......@@ -78,9 +78,9 @@ class StoreWebVar {
return self::$cache[$key];
}
// Sanatize more specific than the default?
if ($sanatizeClass == SANATIZE_DEFAULT && isset(self::$defaultSanatizeClass[$key]))
$sanatizeClass = self::$defaultSanatizeClass[$key];
if ($sanatizeClass === '') {
$sanatizeClass = isset(self::$defaultSanatizeClass[$key]) ? self::$defaultSanatizeClass[$key] : SANATIZE_DEFAULT;
}
// sanatize
self::$cache[$key] = Sanatize::sanatize($value, $sanatizeClass);
......
......@@ -8,8 +8,10 @@
* $Id$
*/
namespace qfq\helper;
namespace qfq\exceptions;
namespace qfq;
use qfq;
use qfq\exceptions\UserException;
require_once(__DIR__ . '/../exceptions/UserException.php');
......
......@@ -8,12 +8,13 @@
namespace qfq;
use qfq\exceptions\UserException;
use qfq\exceptions\CodeException;
use qfq;
use qfq\exceptions;
require_once(__DIR__ . '/../qfq/Form.php');
require_once(__DIR__ . '/../exceptions/UserException.php');
require_once(__DIR__ . '/../exceptions/CodeException.php');
require_once(__DIR__ . '/../qfq/exceptions/UserException.php');
require_once(__DIR__ . '/../qfq/exceptions/CodeException.php');
//TODO: unit tests fuer alle abgefangenen Exceptions
......@@ -26,9 +27,10 @@ require_once(__DIR__ . '/../exceptions/CodeException.php');
function renderForm($bodytext = "") {
try {
$form = new \qfq\Form();
echo $form->process();
$form = new Form();
echo $form->process($bodytext);
echo "test";
} catch (UserException $e) {
echo $e->formatMessage();
} catch (CodeException $e) {
......
......@@ -13,7 +13,7 @@ class FormTest extends PHPUnit_Framework_TestCase {
public function testProcess() {
$form = new qfq\Form();
$this->assertEquals("Hello World from class Form", $form->process());
$this->assertEquals("Hello World from class Form", $form->process(''));
}
}
......@@ -3,7 +3,9 @@
* @author Rafael Ostertag <rafael.ostertag@math.uzh.ch>
*/
use qfq\helper\KeyValueStringParser;
namespace qfq;
use qfq;
require_once(__DIR__ . '/../../qfq/helper/KeyValueStringParser.php');
......
......@@ -8,12 +8,12 @@
namespace qfq;
use qfq\Sanatize;
//use qfq\Sanatize;
use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../../qfq/Sanatize.php');
require_once(__DIR__ . '/../../exceptions/CodeException.php');
require_once(__DIR__ . '/../../qfq/exceptions/CodeException.php');
class SanatizeTest extends \PHPUnit_Framework_TestCase {
......@@ -45,16 +45,12 @@ class SanatizeTest extends \PHPUnit_Framework_TestCase {
$this->assertEquals('a+Z09', Sanatize::sanatize('a+Z09', SANATIZE_ALL), "SANATIZE_ALL fails");
}
/**
* @expectedException \qfq\exceptions\CodeException
*
*/
public function testSanatizeException() {
$catched = false;
try {
Sanatize::sanatize('Hello World', 'invalid sanatize class');
} catch (CodeException $e) {
$catched = true;
}
$this->assertTrue($catched, "Exception catched: invalid sanatize class");
Sanatize::sanatize('Hello World', 'invalid sanatize class');
}
}
......@@ -8,11 +8,11 @@
namespace qfq;
use qfq\exceptions\CodeException;
//use qfq\exceptions\CodeException;
require_once(__DIR__ . '/../../qfq/Store.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
require_once(__DIR__ . '/../../exceptions/CodeException.php');
require_once(__DIR__ . '/../../qfq/exceptions/CodeException.php');
class StoreTest extends \PHPUnit_Framework_TestCase {
......@@ -34,24 +34,23 @@ class StoreTest extends \PHPUnit_Framework_TestCase {
# 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");
$_GET[CLIENT_RECORD_ID] = '1234';
$this->assertEquals('1234', $store->getVar(CLIENT_RECORD_ID, 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");
$_GET[CLIENT_SIP] = '1234abcd';
$this->assertEquals('', $store->getVar(CLIENT_SIP, STORE_CLIENT), "sanatize class digit fails");
}
/**
* @expectedException \qfq\exceptions\CodeException
*/
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');
$store->getVar('ex02', 'fake store');
}
public function testStorePriority() {
//TODO: tbd
}
}
......@@ -54,9 +54,5 @@ class StoreWebVarTest extends \PHPUnit_Framework_TestCase {
$_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