xref: /plugin/struct/meta/SearchConfig.php (revision 668e4f8edc224fee78c7e6e1fbf52252a0e72206)
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
14*668e4f8eSAndreas Gohr    /**
15*668e4f8eSAndreas Gohr     * @var array hold the configuration as parsed and extended by dynamic params
16*668e4f8eSAndreas Gohr     */
171a07b696SMichael Große    protected $config;
181a07b696SMichael Große
19*668e4f8eSAndreas Gohr    /**
20*668e4f8eSAndreas Gohr     * @var SearchConfigParameters manages dynamic parameters
21*668e4f8eSAndreas Gohr     */
2200f6af48SAndreas Gohr    protected $dynamicParameters;
2300f6af48SAndreas Gohr
245511bd5bSAndreas Gohr    /**
255511bd5bSAndreas Gohr     * SearchConfig constructor.
2600f6af48SAndreas Gohr     * @param array $config The parsed configuration for this search
275511bd5bSAndreas Gohr     */
285511bd5bSAndreas Gohr    public function __construct($config) {
295511bd5bSAndreas Gohr        parent::__construct();
305511bd5bSAndreas Gohr
313ad292a8SAndreas Gohr        // setup schemas and columns
323ad292a8SAndreas Gohr        foreach($config['schemas'] as $schema) {
333ad292a8SAndreas Gohr            $this->addSchema($schema[0], $schema[1]);
343ad292a8SAndreas Gohr        }
353ad292a8SAndreas Gohr        foreach($config['cols'] as $col) {
363ad292a8SAndreas Gohr            $this->addColumn($col);
373ad292a8SAndreas Gohr        }
383ad292a8SAndreas Gohr
3900f6af48SAndreas Gohr        // apply dynamic paramters
4000f6af48SAndreas Gohr        $this->dynamicParameters = new SearchConfigParameters($this);
41*668e4f8eSAndreas Gohr        $config = $this->dynamicParameters->updateConfig($config);
4200f6af48SAndreas Gohr
4300f6af48SAndreas Gohr        // configure search from configuration
4400f6af48SAndreas Gohr        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
455511bd5bSAndreas Gohr            $this->addFilter($filter[0], $filter[2], $filter[1], $filter[3]);
465511bd5bSAndreas Gohr        }
4700f6af48SAndreas Gohr
4800f6af48SAndreas Gohr        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
4900f6af48SAndreas Gohr            $this->addSort($sort[0], $sort[1] === 'ASC');
501a07b696SMichael Große        }
511a07b696SMichael Große
521a07b696SMichael Große        if(!empty($config['limit'])) {
531a07b696SMichael Große            $this->setLimit($config['limit']);
541a07b696SMichael Große        }
5500f6af48SAndreas Gohr
5600f6af48SAndreas Gohr        if(!empty($config['offset'])) {
5700f6af48SAndreas Gohr            $this->setLimit($config['offset']);
5800f6af48SAndreas Gohr        }
59*668e4f8eSAndreas Gohr
60*668e4f8eSAndreas Gohr        $this->config = $config;
611a07b696SMichael Große    }
625511bd5bSAndreas Gohr
6300f6af48SAndreas Gohr    /**
6400f6af48SAndreas Gohr     * Access the dynamic paramters of this search
6500f6af48SAndreas Gohr     *
66*668e4f8eSAndreas Gohr     * Note: This call returns a clone of the parameters as they were initialized
6700f6af48SAndreas Gohr     *
6800f6af48SAndreas Gohr     * @return SearchConfigParameters
6900f6af48SAndreas Gohr     */
7000f6af48SAndreas Gohr    public function getDynamicParameters() {
7100f6af48SAndreas Gohr        return clone $this->dynamicParameters;
725511bd5bSAndreas Gohr    }
735511bd5bSAndreas Gohr
7400f6af48SAndreas Gohr    /**
7507993756SAndreas Gohr     * @return array the current config
7600f6af48SAndreas Gohr     */
7707993756SAndreas Gohr    public function getConf() {
781a07b696SMichael Große        return $this->config;
791a07b696SMichael Große    }
801a07b696SMichael Große
815511bd5bSAndreas Gohr}
82