Store.php 3.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
<?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');
Carsten  Rose's avatar
Carsten Rose committed
14
require_once(__DIR__ . '/../qfq/StoreT3Bodytext.php');
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
require_once(__DIR__ . '/../qfq/Constants.php');


/*
 * Stores:
 * - SIP
 * - webVar
 * - record
 * - form
 * - formElement
 */

class Store {

    private static $instance = null;

    // Store Classes
    public $StoreWebVar = null;
Carsten  Rose's avatar
Carsten Rose committed
33
    public $StoreT3Bodytext = null;
34
35
36
37
38
39

    /**
     * Design Pattern: Singleton - class 'Store' should only be instanced once.
     */
    private function __construct() {
        $this->StoreWebVar = StoreWebVar::getInstance();
Carsten  Rose's avatar
Carsten Rose committed
40
        $this->StoreT3Bodytext = StoreT3Bodytext::getInstance();
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
    }

    /**
     * @return Store
     */
    public static function getInstance() {
        // Design Pattern: Singleton
        if (self::$instance == null) {
            self::$instance = new Store;
        }
        return self::$instance;
    }

    /**
     * @param string $key
Carsten  Rose's avatar
Carsten Rose committed
56
     * @param string $respectStores
57
58
59
60
     * @param string $sanatizeClass
     * @return string a) if found: value, b) false
     * @throws CodeException
     */
Carsten  Rose's avatar
Carsten Rose committed
61
    public function getVar($key, $respectStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
62
63
64
65

        $value = false;
        // f.e.: $useStore='FSRD'
        $ii = 0;
Carsten  Rose's avatar
Carsten Rose committed
66
        $store = substr($respectStores, $ii, 1);
67
68
69
70
71
72
73

        // Iterate over all stores, to search for parameter $key
        while ($store !== false) {
            switch ($store) {
                case STORE_CLIENT:
                    $value = $this->StoreWebVar->getVar($key, $sanatizeClass);
                    break;
Carsten  Rose's avatar
Carsten Rose committed
74
75
76
77
                case STORE_T3_BODYTEXT:
                    $value = $this->StoreT3Bodytext->getVar($key, $sanatizeClass);
                    break;

78
79
                case STORE_FORM:
                case STORE_SIP:
Carsten  Rose's avatar
Carsten Rose committed
80
                case STORE_RECORD:
81
82
83
84
85
86
87
88
89
90
91
92
93
94
                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;

            $ii++;
Carsten  Rose's avatar
Carsten Rose committed
95
            $store = substr($respectStores, $ii, 1);
96
97
98
        }
        return false;
    }
Carsten  Rose's avatar
Carsten Rose committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126

    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;
        }


    }
127
128
129
130
131
}