xref: /plugin/struct/meta/SearchConfig.php (revision 097f4a53a9f2c54a89643d357681041c89816aed)
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    /**
15     * @var array hold the configuration as parsed and extended by dynamic params
16     */
17    protected $config;
18
19    /**
20     * @var SearchConfigParameters manages dynamic parameters
21     */
22    protected $dynamicParameters;
23
24    /**
25     * SearchConfig constructor.
26     * @param array $config The parsed configuration for this search
27     */
28    public function __construct($config) {
29        parent::__construct();
30
31        // setup schemas and columns
32        foreach($config['schemas'] as $schema) {
33            $this->addSchema($schema[0], $schema[1]);
34        }
35        foreach($config['cols'] as $col) {
36            $this->addColumn($col);
37        }
38
39        // apply dynamic paramters
40        $this->dynamicParameters = new SearchConfigParameters($this);
41        $config = $this->dynamicParameters->updateConfig($config);
42
43        // configure search from configuration
44        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
45            $this->addFilter($filter[0], $filter[2], $filter[1], $filter[3]);
46        }
47
48        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
49            $this->addSort($sort[0], $sort[1]);
50        }
51
52        if(!empty($config['limit'])) {
53            $this->setLimit($config['limit']);
54        }
55
56        if(!empty($config['offset'])) {
57            $this->setLimit($config['offset']);
58        }
59
60        $this->config = $config;
61    }
62
63    /**
64     * Access the dynamic paramters of this search
65     *
66     * Note: This call returns a clone of the parameters as they were initialized
67     *
68     * @return SearchConfigParameters
69     */
70    public function getDynamicParameters() {
71        return clone $this->dynamicParameters;
72    }
73
74    /**
75     * @return array the current config
76     */
77    public function getConf() {
78        return $this->config;
79    }
80
81}
82