1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LesserPhp\Library; |
4
|
|
|
|
5
|
|
|
use LesserPhp\Exception\GeneralException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* lesserphp |
9
|
|
|
* https://www.maswaba.de/lesserphp |
10
|
|
|
* |
11
|
|
|
* LESS CSS compiler, adapted from http://lesscss.org |
12
|
|
|
* |
13
|
|
|
* Copyright 2013, Leaf Corcoran <[email protected]> |
14
|
|
|
* Copyright 2016, Marcus Schwarz <[email protected]> |
15
|
|
|
* Licensed under MIT or GPLv3, see LICENSE |
16
|
|
|
* @package LesserPhp |
17
|
|
|
*/ |
18
|
|
|
class Assertions |
19
|
|
|
{ |
20
|
|
|
private $coerce; |
21
|
|
|
|
22
|
64 |
|
/** |
23
|
|
|
* Assertions constructor. |
24
|
64 |
|
* |
25
|
64 |
|
* @param \LesserPhp\Library\Coerce $coerce |
26
|
|
|
*/ |
27
|
2 |
|
public function __construct(Coerce $coerce) |
28
|
|
|
{ |
29
|
2 |
|
$this->coerce = $coerce; |
30
|
2 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $value |
34
|
2 |
|
* @param string $error |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
4 |
|
* @throws \LesserPhp\Exception\GeneralException |
38
|
|
|
*/ |
39
|
4 |
|
public function assertColor(array $value, $error = 'expected color value') |
40
|
4 |
|
{ |
41
|
|
|
$color = $this->coerce->coerceColor($value); |
42
|
|
|
if ($color === null) { |
43
|
|
|
throw new GeneralException($error); |
44
|
|
|
} |
45
|
2 |
|
|
46
|
|
|
return $color; |
47
|
2 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
2 |
|
* @param array $value |
51
|
|
|
* @param string $error |
52
|
|
|
* |
53
|
2 |
|
* @return mixed |
54
|
2 |
|
* @throws \LesserPhp\Exception\GeneralException |
55
|
2 |
|
*/ |
56
|
|
|
public function assertNumber(array $value, $error = 'expecting number') |
57
|
|
|
{ |
58
|
|
|
if ($value[0] === 'number') { |
59
|
|
|
return $value[1]; |
60
|
|
|
} |
61
|
|
|
throw new GeneralException($error); |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param array $value |
66
|
|
|
* @param int $expectedArgs |
67
|
|
|
* @param string $name |
68
|
4 |
|
* |
69
|
|
|
* @return array |
70
|
|
|
* @throws \LesserPhp\Exception\GeneralException |
71
|
4 |
|
*/ |
72
|
4 |
|
public function assertArgs(array $value, $expectedArgs, $name = '') |
73
|
4 |
|
{ |
74
|
2 |
|
if ($expectedArgs === 1) { |
75
|
2 |
|
return $value; |
76
|
|
|
} else { |
77
|
|
|
if ($value[0] !== 'list' || $value[1] !== ',') { |
78
|
2 |
|
throw new GeneralException('expecting list'); |
79
|
|
|
} |
80
|
|
|
$values = $value[2]; |
81
|
2 |
|
$numValues = count($values); |
82
|
|
|
if ($expectedArgs !== $numValues) { |
83
|
1 |
|
if ($name) { |
84
|
|
|
$name .= ': '; |
85
|
|
|
} |
86
|
|
|
throw new GeneralException("${name}expecting $expectedArgs arguments, got $numValues"); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return $values; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param array $value |
95
|
|
|
* @param int $expectedMinArgs |
96
|
|
|
* @param string $name |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
* @throws \LesserPhp\Exception\GeneralException |
100
|
|
|
*/ |
101
|
|
|
public function assertMinArgs(array $value, $expectedMinArgs, $name = '') |
102
|
|
|
{ |
103
|
|
|
if ($value[0] !== 'list' || $value[1] !== ',') { |
104
|
|
|
throw new GeneralException('expecting list'); |
105
|
|
|
} |
106
|
|
|
$values = $value[2]; |
107
|
|
|
$numValues = count($values); |
108
|
|
|
if ($expectedMinArgs > $numValues) { |
109
|
|
|
if ($name) { |
110
|
|
|
$name .= ': '; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
throw new GeneralException("${name}expecting at least $expectedMinArgs arguments, got $numValues"); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $values; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|