Completed
Push — latest ( 7e49ae...5ac9c7 )
by Colin
20s queued 12s
created

src/Exception/InvalidConfigurationException.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the league/commonmark package.
7
 *
8
 * (c) Colin O'Dell <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace League\CommonMark\Exception;
15
16
use Nette\Schema\ValidationException;
17
18
final class InvalidConfigurationException extends \UnexpectedValueException
19
{
20
    /**
21
     * @param string  $option      Name/path of the option
22
     * @param mixed   $valueGiven  The invalid option that was provided
23
     * @param ?string $description Additional text describing the issue (optional)
24
     */
25
    public static function forConfigOption(string $option, $valueGiven, ?string $description = null): self
26
    {
27
        $message = \sprintf('Invalid config option for "%s": %s', $option, self::getDebugValue($valueGiven));
28
        if ($description !== null) {
29
            $message .= \sprintf(' (%s)', $description);
30
        }
31
32
        return new self($message);
33
    }
34
35
    /**
36
     * @param string $option     Description of the option
37
     * @param mixed  $valueGiven The invalid option that was provided
38
     */
39
    public static function forParameter(string $option, $valueGiven): self
40
    {
41
        return new self(\sprintf('Invalid %s: %s', $option, self::getDebugValue($valueGiven)));
42
    }
43
44 33
    public static function fromValidation(ValidationException $ex): self
45
    {
46 33
        return new self($ex->getMessage(), 0, $ex);
47
    }
48
49 6
    public static function missingOption(string $option): self
50
    {
51 6
        return new self(\sprintf('Config option "%s" does not exist', $option));
52
    }
53
54
    /**
55
     * @param mixed $value
56
     */
57
    private static function getDebugValue($value): string
58
    {
59
        if (\is_object($value)) {
60
            return \get_class($value);
61
        }
62
63
        return \print_r($value, true);
0 ignored issues
show
Bug Best Practice introduced by
The expression return print_r($value, true) could return the type true which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
}
66