Store.php 2.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?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;
    }
}