xref: /dokuwiki/vendor/splitbrain/lesserphp/src/Utils/Asserts.php (revision a646a37b3a8a80e780de8ad5cacd58b7f3248acf)
1e6380ba3SAndreas Gohr<?php
2e6380ba3SAndreas Gohr
3e6380ba3SAndreas Gohrnamespace LesserPHP\Utils;
4e6380ba3SAndreas Gohr
5e6380ba3SAndreas Gohruse Exception;
6e6380ba3SAndreas Gohr
7e6380ba3SAndreas Gohrclass Asserts
8e6380ba3SAndreas Gohr{
9e6380ba3SAndreas Gohr    /**
10e6380ba3SAndreas Gohr     * Check that the number of arguments is correct and return them
11e6380ba3SAndreas Gohr     *
12e6380ba3SAndreas Gohr     * @throws Exception
13e6380ba3SAndreas Gohr     */
14e6380ba3SAndreas Gohr    public static function assertArgs($value, $expectedArgs, $name = '')
15e6380ba3SAndreas Gohr    {
16e6380ba3SAndreas Gohr        if ($expectedArgs == 1) {
17e6380ba3SAndreas Gohr            return $value;
18e6380ba3SAndreas Gohr        } else {
19e6380ba3SAndreas Gohr            if ($value[0] !== 'list' || $value[1] != ',') {
20e6380ba3SAndreas Gohr                throw new Exception('expecting list');
21e6380ba3SAndreas Gohr            }
22e6380ba3SAndreas Gohr            $values = $value[2];
23e6380ba3SAndreas Gohr            $numValues = count($values);
24e6380ba3SAndreas Gohr            if ($expectedArgs != $numValues) {
25e6380ba3SAndreas Gohr                if ($name) {
26e6380ba3SAndreas Gohr                    $name = $name . ': ';
27e6380ba3SAndreas Gohr                }
28e6380ba3SAndreas Gohr
29e6380ba3SAndreas Gohr                throw new Exception("{$name}expecting $expectedArgs arguments, got $numValues");
30e6380ba3SAndreas Gohr            }
31e6380ba3SAndreas Gohr
32e6380ba3SAndreas Gohr            return $values;
33e6380ba3SAndreas Gohr        }
34e6380ba3SAndreas Gohr    }
35e6380ba3SAndreas Gohr
36e6380ba3SAndreas Gohr    /**
37e6380ba3SAndreas Gohr     * Check that the number of arguments is at least the expected number and return them
38e6380ba3SAndreas Gohr     *
39e6380ba3SAndreas Gohr     * @throws Exception
40e6380ba3SAndreas Gohr     */
41e6380ba3SAndreas Gohr    public static function assertMinArgs($value, $expectedMinArgs, $name = '')
42e6380ba3SAndreas Gohr    {
43e6380ba3SAndreas Gohr        if ($value[0] !== 'list' || $value[1] != ',') {
44e6380ba3SAndreas Gohr            throw new Exception('expecting list');
45e6380ba3SAndreas Gohr        }
46e6380ba3SAndreas Gohr        $values = $value[2];
47e6380ba3SAndreas Gohr        $numValues = count($values);
48e6380ba3SAndreas Gohr        if ($expectedMinArgs > $numValues) {
49e6380ba3SAndreas Gohr            if ($name) {
50e6380ba3SAndreas Gohr                $name = $name . ': ';
51e6380ba3SAndreas Gohr            }
52e6380ba3SAndreas Gohr
53*a646a37bSAndreas Gohr            throw new Exception("$name expecting at least $expectedMinArgs arguments, got $numValues");
54e6380ba3SAndreas Gohr        }
55e6380ba3SAndreas Gohr
56e6380ba3SAndreas Gohr        return $values;
57e6380ba3SAndreas Gohr    }
58e6380ba3SAndreas Gohr
59e6380ba3SAndreas Gohr    /**
60e6380ba3SAndreas Gohr     * Checks that the value is a number and returns it as float
61e6380ba3SAndreas Gohr     *
62e6380ba3SAndreas Gohr     * @param array $value The parsed value triplet
63e6380ba3SAndreas Gohr     * @param string $error The error message to throw
64e6380ba3SAndreas Gohr     * @throws Exception
65e6380ba3SAndreas Gohr     */
66e6380ba3SAndreas Gohr    public static function assertNumber(array $value, string $error = 'expecting number'): float
67e6380ba3SAndreas Gohr    {
68e6380ba3SAndreas Gohr        if ($value[0] == 'number') return (float)$value[1];
69e6380ba3SAndreas Gohr        throw new Exception($error);
70e6380ba3SAndreas Gohr    }
71e6380ba3SAndreas Gohr
72e6380ba3SAndreas Gohr    /**
73e6380ba3SAndreas Gohr     * @throws Exception
74e6380ba3SAndreas Gohr     */
75e6380ba3SAndreas Gohr    public static function assertColor(array $value, $error = 'expected color value'): array
76e6380ba3SAndreas Gohr    {
77e6380ba3SAndreas Gohr        $color = Color::coerceColor($value);
78e6380ba3SAndreas Gohr        if (is_null($color)) throw new Exception($error);
79e6380ba3SAndreas Gohr        return $color;
80e6380ba3SAndreas Gohr    }
81e6380ba3SAndreas Gohr}
82