xref: /plugin/struct/meta/SearchConfig.php (revision 53ed31251c00ac2e5b0c768ea5bae4cf4ab80c26)
1<?php
2
3namespace plugin\struct\meta;
4
5/**
6 * Class SearchConfig
7 *
8 * The same as @see Search but can be initialized by a configuration array
9 *
10 * @package plugin\struct\meta
11 */
12class SearchConfig extends Search {
13
14    protected $config;
15
16    protected $dynamicParameters;
17
18    /**
19     * SearchConfig constructor.
20     * @param array $config The parsed configuration for this search
21     */
22    public function __construct($config) {
23        $this->config = $config;
24
25        parent::__construct();
26
27        // apply dynamic paramters
28        $this->dynamicParameters = new SearchConfigParameters($this);
29        $this->config = $this->dynamicParameters->updateConfig($config);
30
31        // configure search from configuration
32        foreach($config['schemas'] as $schema) {
33            $this->addSchema($schema[0], $schema[1]);
34        }
35
36        foreach($config['cols'] as $col) {
37            $this->addColumn($col);
38        }
39
40        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
41            $this->addFilter($filter[0], $filter[2], $filter[1], $filter[3]);
42        }
43
44        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
45            $this->addSort($sort[0], $sort[1] === 'ASC');
46        }
47
48        if (!empty($config['limit'])) {
49            $this->setLimit($config['limit']);
50        }
51
52        if (!empty($config['offset'])) {
53            $this->setLimit($config['offset']);
54        }
55    }
56
57    /**
58     * Access the dynamic paramters of this search
59     *
60     * Note: This call retruns a clone of the parameters as they were initialized
61     *
62     * @return SearchConfigParameters
63     */
64    public function getDynamicParameters() {
65        return clone $this->dynamicParameters;
66    }
67
68    /**
69     * Access the current config.
70     *
71     * When no key is given the whole configuration is returned. With a key only
72     * that key's value is returned. Returns NULL on a non-existing key
73     *
74     * @param string $key
75     * @return mixed
76     */
77    public function getConf($key='') {
78        if($key) {
79            if(isset($this->config[$key])) {
80                return $this->config[$key];
81            } else {
82                return null;
83            }
84        } else {
85            return $this->config;
86        }
87    }
88
89}
90