xref: /plugin/struct/meta/SearchConfig.php (revision 3ad292a83faf1dbb06dc6bed3e873e36bfb93c33)
15511bd5bSAndreas Gohr<?php
25511bd5bSAndreas Gohr
35511bd5bSAndreas Gohrnamespace plugin\struct\meta;
45511bd5bSAndreas Gohr
55511bd5bSAndreas Gohr/**
65511bd5bSAndreas Gohr * Class SearchConfig
75511bd5bSAndreas Gohr *
85511bd5bSAndreas Gohr * The same as @see Search but can be initialized by a configuration array
95511bd5bSAndreas Gohr *
105511bd5bSAndreas Gohr * @package plugin\struct\meta
115511bd5bSAndreas Gohr */
125511bd5bSAndreas Gohrclass SearchConfig extends Search {
135511bd5bSAndreas Gohr
141a07b696SMichael Große    protected $config;
151a07b696SMichael Große
1600f6af48SAndreas Gohr    protected $dynamicParameters;
1700f6af48SAndreas Gohr
185511bd5bSAndreas Gohr    /**
195511bd5bSAndreas Gohr     * SearchConfig constructor.
2000f6af48SAndreas Gohr     * @param array $config The parsed configuration for this search
215511bd5bSAndreas Gohr     */
225511bd5bSAndreas Gohr    public function __construct($config) {
231a07b696SMichael Große        $this->config = $config;
241a07b696SMichael Große
255511bd5bSAndreas Gohr        parent::__construct();
265511bd5bSAndreas Gohr
27*3ad292a8SAndreas Gohr        // setup schemas and columns
28*3ad292a8SAndreas Gohr        foreach($config['schemas'] as $schema) {
29*3ad292a8SAndreas Gohr            $this->addSchema($schema[0], $schema[1]);
30*3ad292a8SAndreas Gohr        }
31*3ad292a8SAndreas Gohr        foreach($config['cols'] as $col) {
32*3ad292a8SAndreas Gohr            $this->addColumn($col);
33*3ad292a8SAndreas Gohr        }
34*3ad292a8SAndreas Gohr
3500f6af48SAndreas Gohr        // apply dynamic paramters
3600f6af48SAndreas Gohr        $this->dynamicParameters = new SearchConfigParameters($this);
3700f6af48SAndreas Gohr        $this->config = $this->dynamicParameters->updateConfig($config);
3800f6af48SAndreas Gohr
3900f6af48SAndreas Gohr        // configure search from configuration
4000f6af48SAndreas Gohr        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
415511bd5bSAndreas Gohr            $this->addFilter($filter[0], $filter[2], $filter[1], $filter[3]);
425511bd5bSAndreas Gohr        }
4300f6af48SAndreas Gohr
4400f6af48SAndreas Gohr        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
4500f6af48SAndreas Gohr            $this->addSort($sort[0], $sort[1] === 'ASC');
461a07b696SMichael Große        }
471a07b696SMichael Große
481a07b696SMichael Große        if(!empty($config['limit'])) {
491a07b696SMichael Große            $this->setLimit($config['limit']);
501a07b696SMichael Große        }
5100f6af48SAndreas Gohr
5200f6af48SAndreas Gohr        if(!empty($config['offset'])) {
5300f6af48SAndreas Gohr            $this->setLimit($config['offset']);
5400f6af48SAndreas Gohr        }
551a07b696SMichael Große    }
565511bd5bSAndreas Gohr
5700f6af48SAndreas Gohr    /**
5800f6af48SAndreas Gohr     * Access the dynamic paramters of this search
5900f6af48SAndreas Gohr     *
6000f6af48SAndreas Gohr     * Note: This call retruns a clone of the parameters as they were initialized
6100f6af48SAndreas Gohr     *
6200f6af48SAndreas Gohr     * @return SearchConfigParameters
6300f6af48SAndreas Gohr     */
6400f6af48SAndreas Gohr    public function getDynamicParameters() {
6500f6af48SAndreas Gohr        return clone $this->dynamicParameters;
665511bd5bSAndreas Gohr    }
675511bd5bSAndreas Gohr
6800f6af48SAndreas Gohr    /**
6907993756SAndreas Gohr     * @return array the current config
7000f6af48SAndreas Gohr     */
7107993756SAndreas Gohr    public function getConf() {
721a07b696SMichael Große        return $this->config;
731a07b696SMichael Große    }
741a07b696SMichael Große
755511bd5bSAndreas Gohr}
76