Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Accepted
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 evaluate
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 generateDefaultMessage
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Validator;
16
17/**
18 * Accepted validator class
19 *
20 * @category   Pop
21 * @package    Pop\Validator
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    5.0.0
26 */
27class Accepted extends AbstractValidator
28{
29
30    /**
31     * Method to evaluate the validator
32     *
33     * @param  mixed $input
34     * @return bool
35     */
36    public function evaluate(mixed $input = null): bool
37    {
38        // Set the input, if passed
39        if ($input !== null) {
40            $this->input = $input;
41        }
42
43        if (is_string($this->input)) {
44            $this->input = strtolower($this->input);
45        }
46
47        // Set the default message
48        if (!$this->hasMessage()) {
49            $this->generateDefaultMessage();
50        }
51
52        $inputValue = ($this->hasKeyField()) ? $this->getKeyFieldValue() : $this->input;
53
54        return in_array($inputValue, [1, '1', true, 'true', 'yes'], true);
55
56    }
57
58    /**
59     * Generate default message
60
61     * @param  mixed $name
62     * @param  mixed $value
63     * @return string
64     */
65    public function generateDefaultMessage(mixed $name = null, mixed $value = null): string
66    {
67        $this->message = "The " . (($name !== null) ? "'" . $name . "'" : "value") .
68            " must a value of either 'yes', '1', 1, 'true' or true.";
69
70        return $this->message;
71    }
72
73}