Store.php 3.41 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
/**
 * Created by PhpStorm.
 * User: crose
 * Date: 1/1/16
 * Time: 6:51 PM
 */

9
namespace qfq\store;
10
11

use qfq\exceptions\CodeException;
12
use qfq;
13

14
15
16
17
18
//use qfq\store\StoreT3Bodytext;

require_once(__DIR__ . '/../../qfq/store/StoreWebVar.php');
require_once(__DIR__ . '/../../qfq/store/StoreT3Bodytext.php');
require_once(__DIR__ . '/../../qfq/Constants.php');
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


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

class Store {

    private static $instance = null;

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

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

    /**
     * @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
61
     * @param string $respectStores
62
63
64
65
     * @param string $sanatizeClass
     * @return string a) if found: value, b) false
     * @throws CodeException
     */
Carsten  Rose's avatar
Carsten Rose committed
66
    public function getVar($key, $respectStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
67
68
69
70

        $value = false;
        // f.e.: $useStore='FSRD'
        $ii = 0;
Carsten  Rose's avatar
Carsten Rose committed
71
        $store = substr($respectStores, $ii, 1);
72
73
74
75
76
77
78

        // 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
79
80
81
82
                case STORE_T3_BODYTEXT:
                    $value = $this->StoreT3Bodytext->getVar($key, $sanatizeClass);
                    break;

83
84
                case STORE_FORM:
                case STORE_SIP:
Carsten  Rose's avatar
Carsten Rose committed
85
                case STORE_RECORD:
86
                case STORE_TABLE_DEFINITION:
87
                case STORE_ZERO:
88
                    throw new CodeException("store not implemented: $store", ERROR_STORE_NOT_IMPLEMENTED);
89
90
91
                    break;

                default:
92
                    throw new CodeException("Unknown store: $store", ERROR_UNKNOWN_STORE);
93
94
95
96
97
98
99
                    break;
            }

            if ($value !== false)
                return $value;

            $ii++;
Carsten  Rose's avatar
Carsten Rose committed
100
            $store = substr($respectStores, $ii, 1);
101
102
103
        }
        return false;
    }
Carsten  Rose's avatar
Carsten Rose committed
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119

    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:
120
            case STORE_TABLE_DEFINITION:
Carsten  Rose's avatar
Carsten Rose committed
121
            case STORE_ZERO:
122
                throw new CodeException("store not implemented: $store", ERROR_STORE_NOT_IMPLEMENTED);
Carsten  Rose's avatar
Carsten Rose committed
123
124
125
                break;

            default:
126
                throw new CodeException("Unknown store: $store", ERROR_UNKNOWN_STORE);
Carsten  Rose's avatar
Carsten Rose committed
127
128
129
                break;
        }
    }
130
131
132
133
134
}