Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Max
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 execute
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3/**
4 * @license MIT
5 * @author hazuki3417<hazuki3417@gmail.com>
6 * @copyright 2022 hazuki3417 all rights reserved.
7 */
8
9namespace Selen\Schema\Validate\Values;
10
11use Selen\Schema\Validate\Model\ValidateResult;
12use Selen\Schema\Validate\ValueValidateInterface;
13
14/**
15 * 値の最大範囲値をバリデーションするクラス
16 */
17class Max implements ValueValidateInterface
18{
19    /** @var string */
20    protected $messageFormat = 'Invalid value. Specify a value of %s or less.';
21
22    /** @var float */
23    protected $threshold;
24
25    /**
26     * 新しいインスタンスを構築します
27     *
28     * @param int|float $threshold 最大範囲値を指定します
29     */
30    public function __construct(int|float $threshold)
31    {
32        $this->threshold = $threshold;
33    }
34
35    /**
36     * {@inheritDoc}
37     */
38    public function execute($value, ValidateResult $result): ValidateResult
39    {
40        $allowType = \is_int($value) || \is_float($value);
41
42        if (!$allowType) {
43            $mes = 'Skip validation. Executed only when the value is of type int or float';
44            return $result->setResult(true)->setMessage($mes);
45        }
46
47        if ($this->threshold < $value) {
48            $mes = \sprintf($this->messageFormat, $this->threshold);
49            return $result->setResult(false)->setMessage($mes);
50        }
51        return $result->setResult(true);
52    }
53}