Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
node_values
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
6
100.00% covered (success)
100.00%
1 / 1
 id
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2// This file is part of Moodle - https://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle.  If not, see <https://www.gnu.org/licenses/>.
16
17namespace tool_autoflow\local\nodes;
18
19use tool_autoflow\local\engine\node_failed;
20
21/**
22 * Checks shared by the Moodle nodes for values that arrive as text from placeholders.
23 *
24 * @package    tool_autoflow
25 * @copyright  2026 DevLMS <contact@devlms.com>
26 * @license    https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
28final class node_values {
29    /**
30     * A record id: a positive whole number, given as a number or as digits.
31     *
32     * @param mixed $value
33     * @param string $what What the id is for, such as "course id", used in the error.
34     * @return int
35     */
36    public static function id(mixed $value, string $what): int {
37        if (is_int($value) && $value > 0) {
38            return $value;
39        }
40        if (is_string($value) && preg_match('/^[1-9][0-9]*$/', trim($value))) {
41            return (int) $value;
42        }
43        $shown = is_scalar($value) ? (string) $value : gettype($value);
44        throw new node_failed("The $what must be a positive whole number, not \"$shown\".");
45    }
46}