Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DateTimeTrait
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
9
100.00% covered (success)
100.00%
1 / 1
 getValue
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
4
 setValue
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
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
17use Pop\Utils;
18
19/**
20 * Date-time trait
21 *
22 * @category   Pop
23 * @package    Pop\Validator
24 * @author     Nick Sagona, III <nick@popphp.org>
25 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
26 * @license    https://www.popphp.org/license     New BSD License
27 * @version    5.0.0
28 */
29trait DateTimeTrait
30{
31
32    use Utils\DateTimeTrait;
33
34    /**
35     * Get the validator value
36     *
37     * @return mixed
38     */
39    public function getValue(): mixed
40    {
41        if (!empty($this->dateTimeFormat)) {
42            if (is_array($this->value)) {
43                $values = $this->value;
44                foreach ($values as $key => $value) {
45                    $values[$key] = date($this->dateTimeFormat, $value);
46                }
47                return $values;
48            } else {
49                return date($this->dateTimeFormat, $this->value);
50            }
51        } else {
52            return $this->value;
53        }
54    }
55
56    /**
57     * Set the validator value
58     *
59     * @param  mixed $value
60     * @return static
61     */
62    public function setValue(mixed $value): static
63    {
64        if ($value !== null) {
65            if (is_array($value)) {
66                $this->detectFormat($value[0]);
67                $value = array_map('strtotime', $value);
68            } else {
69                $this->detectFormat($value);
70                if ($this->dateTimeFormat !== null) {
71                    $value = strtotime($value);
72                } else if (strtotime($value) !== false) {
73                    $value = strtotime($value);
74                }
75            }
76        }
77        return parent::setValue($value);
78    }
79
80}