Php86.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Polyfill\Php86;
  11. /**
  12. * @author kylekatarnls <kylekatarnls@gmail.com>
  13. *
  14. * @internal
  15. */
  16. final class Php86
  17. {
  18. /**
  19. * @template Value
  20. * @template Minimum
  21. * @template Maximum
  22. *
  23. * @param Value $value
  24. * @param Minimum $min
  25. * @param Maximum $max
  26. *
  27. * @return Value|Minimum|Maximum
  28. */
  29. public static function clamp($value, $min, $max)
  30. {
  31. if (\is_float($min) && is_nan($min)) {
  32. self::throwValueErrorIfAvailable('clamp(): Argument #2 ($min) must not be NAN');
  33. }
  34. if (\is_float($max) && is_nan($max)) {
  35. self::throwValueErrorIfAvailable('clamp(): Argument #3 ($max) must not be NAN');
  36. }
  37. if ($max < $min) {
  38. self::throwValueErrorIfAvailable('clamp(): Argument #2 ($min) must be smaller than or equal to argument #3 ($max)');
  39. }
  40. if ($value > $max) {
  41. return $max;
  42. }
  43. if ($value < $min) {
  44. return $min;
  45. }
  46. return $value;
  47. }
  48. private static function throwValueErrorIfAvailable(string $message): void
  49. {
  50. if (!class_exists(\ValueError::class)) {
  51. throw new \InvalidArgumentException($message);
  52. }
  53. throw new \ValueError($message);
  54. }
  55. public static function grapheme_strrev(string $string)
  56. {
  57. if (false === $units = grapheme_str_split($string)) {
  58. return false;
  59. }
  60. return implode('', array_reverse($units));
  61. }
  62. }