xref: /plugin/struct/meta/SearchConfig.php (revision 00f6af489ee7048654a4fd178204876da2e2c77c)
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
16*00f6af48SAndreas Gohr    protected $dynamicParameters;
17*00f6af48SAndreas Gohr
185511bd5bSAndreas Gohr    /**
195511bd5bSAndreas Gohr     * SearchConfig constructor.
20*00f6af48SAndreas 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*00f6af48SAndreas Gohr        // apply dynamic paramters
28*00f6af48SAndreas Gohr        $this->dynamicParameters = new SearchConfigParameters($this);
29*00f6af48SAndreas Gohr        $this->config = $this->dynamicParameters->updateConfig($config);
30*00f6af48SAndreas Gohr
31*00f6af48SAndreas Gohr        // configure search from configuration
325511bd5bSAndreas Gohr        foreach($config['schemas'] as $schema) {
335511bd5bSAndreas Gohr            $this->addSchema($schema[0], $schema[1]);
345511bd5bSAndreas Gohr        }
355511bd5bSAndreas Gohr
365511bd5bSAndreas Gohr        foreach($config['cols'] as $col) {
375511bd5bSAndreas Gohr            $this->addColumn($col);
385511bd5bSAndreas Gohr        }
395511bd5bSAndreas Gohr
40*00f6af48SAndreas Gohr        if(!empty($config['filter'])) foreach($config['filter'] as $filter) {
415511bd5bSAndreas Gohr            $this->addFilter($filter[0], $filter[2], $filter[1], $filter[3]);
425511bd5bSAndreas Gohr        }
43*00f6af48SAndreas Gohr
44*00f6af48SAndreas Gohr        if(!empty($config['sort'])) foreach($config['sort'] as $sort) {
45*00f6af48SAndreas 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        }
51*00f6af48SAndreas Gohr
52*00f6af48SAndreas Gohr        if (!empty($config['offset'])) {
53*00f6af48SAndreas Gohr            $this->setLimit($config['offset']);
54*00f6af48SAndreas Gohr        }
551a07b696SMichael Große    }
565511bd5bSAndreas Gohr
57*00f6af48SAndreas Gohr    /**
58*00f6af48SAndreas Gohr     * Access the dynamic paramters of this search
59*00f6af48SAndreas Gohr     *
60*00f6af48SAndreas Gohr     * Note: This call retruns a clone of the parameters as they were initialized
61*00f6af48SAndreas Gohr     *
62*00f6af48SAndreas Gohr     * @return SearchConfigParameters
63*00f6af48SAndreas Gohr     */
64*00f6af48SAndreas Gohr    public function getDynamicParameters() {
65*00f6af48SAndreas Gohr        return clone $this->dynamicParameters;
665511bd5bSAndreas Gohr    }
675511bd5bSAndreas Gohr
68*00f6af48SAndreas Gohr    /**
69*00f6af48SAndreas Gohr     * Access the current config.
70*00f6af48SAndreas Gohr     *
71*00f6af48SAndreas Gohr     * When no key is given the whole configuration is returned. With a key only
72*00f6af48SAndreas Gohr     * that key's value is returned. Returns NULL on a non-existing key
73*00f6af48SAndreas Gohr     *
74*00f6af48SAndreas Gohr     * @param string $key
75*00f6af48SAndreas Gohr     * @return mixed
76*00f6af48SAndreas Gohr     */
77*00f6af48SAndreas Gohr    public function getConf($key='') {
78*00f6af48SAndreas Gohr        if($key) {
79*00f6af48SAndreas Gohr            if(isset($this->config[$key])) {
80*00f6af48SAndreas Gohr                return $this->config[$key];
81*00f6af48SAndreas Gohr            } else {
82*00f6af48SAndreas Gohr                return null;
83*00f6af48SAndreas Gohr            }
84*00f6af48SAndreas Gohr        } else {
851a07b696SMichael Große            return $this->config;
861a07b696SMichael Große        }
87*00f6af48SAndreas Gohr    }
881a07b696SMichael Große
895511bd5bSAndreas Gohr}
90