Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
93.75% covered (success)
93.75%
15 / 16
50.00% covered (warning)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CountNotEqual
93.75% covered (success)
93.75%
15 / 16
50.00% covered (warning)
50.00%
1 / 2
10.02
0.00% covered (danger)
0.00%
0 / 1
 evaluate
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
5
 generateDefaultMessage
87.50% covered (success)
87.50%
7 / 8
0.00% covered (danger)
0.00%
0 / 1
5.05
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 * Count not equal 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 CountNotEqual extends AbstractValidator
28{
29
30    /**
31     * Method to evaluate the validator
32     *
33     * @param  mixed $input
34     * @throws Exception
35     * @return bool
36     */
37    public function evaluate(mixed $input = null): bool
38    {
39        // Set the input, if passed
40        if ($input !== null) {
41            $this->input = $input;
42        }
43
44        // Set the default message
45        if (!$this->hasMessage()) {
46            $this->generateDefaultMessage();
47        }
48
49        if (!is_array($this->input)) {
50            throw new Exception('Error: The evaluated input must be an array.');
51        }
52
53        $inputValue = ($this->hasKeyField()) ? $this->getKeyFieldValue() : $this->input;
54
55        return (count($inputValue) != $this->value);
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        if ($value !== null) {
68            $valueString = $value;
69        } else if (!empty($this->value) && !is_array($this->value)) {
70            $valueString = $this->value;
71        } else {
72            $valueString = 'the value.';
73        }
74        $this->message = "The count of " . (($name !== null) ? "'" . $name . "'" : "the input") .
75            " must not be equal to '" . $valueString . "'.";
76
77        return $this->message;
78    }
79
80}