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