xref: /plugin/struct/meta/SearchConfig.php (revision 9dbc32ae40e6ae835e9edeee14fdde77ad1c59f3)
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        // setup schemas and columns
28        foreach($config['schemas'] as $schema) {
29            $this->addSchema($schema[0], $schema[1]);
30        }
31        foreach($config['cols'] as $col) {
32            $this->addColumn($col);
33        }
34
35        // apply dynamic paramters
36        $this->dynamicParameters = new SearchConfigParameters($this);
37        $this->config = $this->dynamicParameters->updateConfig($config);
38
39        // configure search from configuration
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     * @return array the current config
70     */
71    public function getConf() {
72        return $this->config;
73    }
74
75}
76