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