InvalidOptionException::stringifyOptionValue()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
/*
4
 * This file is part of the `liip/LiipImagineBundle` project.
5
 *
6
 * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
7
 *
8
 * For the full copyright and license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Liip\ImagineBundle\Exception\Imagine\Filter\PostProcessor;
13
14
use Liip\ImagineBundle\Exception\ExceptionInterface;
15
16
class InvalidOptionException extends \RuntimeException implements ExceptionInterface
17
{
18
    public function __construct(string $message, array $options = [])
19
    {
20
        parent::__construct(sprintf('Invalid post-processor configuration provided (%s) with options %s.',
21
            $message, $this->stringifyOptions($options)));
22
    }
23
24
    private function stringifyOptions(array $options = []): string
25
    {
26
        if (0 === \count($options)) {
27
            return '[]';
28
        }
29
30
        $options = array_map([$this, 'stringifyOptionValue'], $options);
31
32
        array_walk($options, function (&$o, $name) {
33
            $o = sprintf('%s="%s"', $name, $o);
34
        });
35
36
        return sprintf('[%s]', implode(', ', $options));
37
    }
38
39
    /**
40
     * @param mixed $value
41
     */
42
    private function stringifyOptionValue($value): string
43
    {
44
        if (is_scalar($value)) {
45
            return $value;
46
        }
47
48
        return json_encode($value);
49
    }
50
}
51