Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
8 / 8
CRAP
100.00% covered (success)
100.00%
1 / 1
Query
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
8 / 8
17
100.00% covered (success)
100.00%
1 / 1
 setInputOption
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getInputOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setOutputOption
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getOutputOptions
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 reset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 isOptionSet
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 notWith
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 prepareOptions
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3/**
4 * Karla ImageMagick wrapper library
5 *
6 * PHP Version 8.0<
7 *
8 * @category Utility
9 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
10 * @license  http://www.opensource.org/licenses/mit-license.php MIT
11 * @link     https://github.com/localgod/karla Karla
12 * @since    2013-05-26
13 */
14
15declare(strict_types=1);
16
17namespace Karla;
18
19/**
20 * Class for maintaining query info
21 *
22 * @category Utility
23 * @author   Johannes Skov Frandsen <jsf@greenoak.dk>
24 * @license  http://www.opensource.org/licenses/mit-license.php MIT
25 * @link     https://github.com/localgod/karla Karla
26 */
27class Query
28{
29    /**
30     * This argument is to be considered for the input image
31     *
32     * @var int
33     */
34    public const ARGUMENT_TYPE_INPUT = 0;
35
36    /**
37     * This argument is to be considered for the output image
38     *
39     * @var int
40     */
41    public const ARGUMENT_TYPE_OUTPUT = 1;
42
43    /**
44     * Input option
45     *
46     * @var array<string>
47     */
48    protected array $inputOptions = [];
49
50    /**
51     * Output option
52     *
53     * @var array<string>
54     */
55    protected array $outputOptions = [];
56
57    /**
58     * Set input option
59     *
60     * @param string $option Option to set
61     */
62    public function setInputOption(string $option): void
63    {
64        if ($option != "") {
65            $this->inputOptions[] = $option;
66        }
67    }
68
69    /**
70     * Get input option
71     *
72     * @return array<string>
73     */
74    public function getInputOptions(): array
75    {
76        return $this->inputOptions;
77    }
78
79    /**
80     * Set output option
81     *
82     * @param string $option Option to set
83     */
84    public function setOutputOption(string $option): void
85    {
86        if ($option != "") {
87            $this->outputOptions[] = $option;
88        }
89    }
90
91    /**
92     * Get output options
93     *
94     * @return array<string>
95     */
96    public function getOutputOptions(): array
97    {
98        return $this->outputOptions;
99    }
100
101    /**
102     * Reset the command
103     */
104    public function reset(): void
105    {
106        $this->inputOptions = array();
107        $this->outputOptions = array();
108    }
109
110    /**
111     * Check if an option is already set
112     *
113     * @param string $lookop Option to look up
114     * @param array<string> $optionList Optionlist to look in
115     */
116    final public function isOptionSet(string $lookop, array $optionList): bool
117    {
118        foreach ($optionList as $option) {
119            if (strstr($option, trim($lookop))) {
120                return true;
121            }
122        }
123
124        return false;
125    }
126
127    /**
128     * Raise an error if a method is called in an invalid context
129     *
130     * @param string $method Method to check
131     * @param int $argumentType Is it an input or an output argument
132     *
133     * @throws \BadMethodCallException
134     */
135    public function notWith(string $method, int $argumentType): void
136    {
137        if ($argumentType == Query::ARGUMENT_TYPE_INPUT) {
138            if ($this->isOptionSet($method, $this->inputOptions)) {
139                $message = "'" . $method . "()' can only be called once as in input argument..";
140                throw new \BadMethodCallException($message);
141            }
142        } elseif ($argumentType == Query::ARGUMENT_TYPE_OUTPUT) {
143            if ($this->isOptionSet($method, $this->outputOptions)) {
144                $message = "'" . $method . "()' can only be called once as in output argument..";
145                throw new \BadMethodCallException($message);
146            }
147        }
148    }
149
150    /**
151     * Prepare option collection
152     *
153     * @param array<string> $options Options
154     */
155    final public function prepareOptions(array $options): string
156    {
157        // Filter out empty strings
158        $options = array_filter($options, function ($option) {
159            return $option !== '';
160        });
161
162        $options = implode(' ', $options);
163        if (trim($options) == '') {
164            return '';
165        }
166
167        return trim($options);
168    }
169}