Store.php 3.82 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
//use qfq\store\StoreT3Bodytext;

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


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

class Store {

    private static $instance = null;

36
    // store Classes
37
    public $StoreWebVar = null;
Carsten  Rose's avatar
Carsten Rose committed
38
    public $StoreT3Bodytext = null;
39
    public $StoreSystem = null;
40
41

    /**
42
     * Design Pattern: Singleton - class 'store' should only be instanced once.
43
44
45
     */
    private function __construct() {
        $this->StoreWebVar = StoreWebVar::getInstance();
Carsten  Rose's avatar
Carsten Rose committed
46
        $this->StoreT3Bodytext = StoreT3Bodytext::getInstance();
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

        $this->StoreSystem = StoreSystem::getInstance();
        try {
            $config = parse_ini_file(QFQ_INI, false);
        } catch (Exception $e) {
            throw new qfq\exceptions\UserException ("Error read file " . QFQ_INI . ": " . $e->getMessage(), ERROR_READ_FILE);
        }
        $this->StoreSystem->setVarArray($config);
    }

    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_TABLE_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;
        }
81
82
83
    }

    /**
84
     *
85
86
87
     * @return Store
     */
    public static function getInstance() {
88

89
90
91
92
93
94
95
96
97
        // Design Pattern: Singleton
        if (self::$instance == null) {
            self::$instance = new Store;
        }
        return self::$instance;
    }

    /**
     * @param string $key
Carsten  Rose's avatar
Carsten Rose committed
98
     * @param string $respectStores
99
100
101
102
     * @param string $sanatizeClass
     * @return string a) if found: value, b) false
     * @throws CodeException
     */
Carsten  Rose's avatar
Carsten Rose committed
103
    public function getVar($key, $respectStores = USE_STORE_DEFAULT, $sanatizeClass = '') {
104
105
106
107

        $value = false;
        // f.e.: $useStore='FSRD'
        $ii = 0;
Carsten  Rose's avatar
Carsten Rose committed
108
        $store = substr($respectStores, $ii, 1);
109
110
111
112
113
114
115

        // 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
116
117
118
119
                case STORE_T3_BODYTEXT:
                    $value = $this->StoreT3Bodytext->getVar($key, $sanatizeClass);
                    break;

120
121
                case STORE_FORM:
                case STORE_SIP:
Carsten  Rose's avatar
Carsten Rose committed
122
                case STORE_RECORD:
123
                case STORE_TABLE_DEFINITION:
124
                case STORE_ZERO:
125
                    throw new CodeException("store not implemented: $store", ERROR_STORE_NOT_IMPLEMENTED);
126
127
128
                    break;

                default:
129
                    throw new CodeException("Unknown store: $store", ERROR_UNKNOWN_STORE);
130
131
132
133
134
135
136
                    break;
            }

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

            $ii++;
Carsten  Rose's avatar
Carsten Rose committed
137
            $store = substr($respectStores, $ii, 1);
138
139
140
141
142
143
144
145
        }
        return false;
    }
}