• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 12673986434

08 Jan 2025 03:42PM UTC coverage: 84.455% (+0.001%) from 84.454%
12673986434

Pull #9385

github

web-flow
Merge 06e47f0ee into e475fd8fa
Pull Request #9385: refactor: Fix phpstan expr.resultUnused

20699 of 24509 relevant lines covered (84.45%)

190.57 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.0
/system/DataCaster/Cast/DatetimeCast.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\DataCaster\Cast;
15

16
use CodeIgniter\Database\BaseConnection;
17
use CodeIgniter\Exceptions\InvalidArgumentException;
18
use CodeIgniter\I18n\Time;
19

20
/**
21
 * Class DatetimeCast
22
 *
23
 * (PHP) [Time --> string] --> (DB driver) --> (DB column) datetime
24
 *       [     <-- string] <-- (DB driver) <-- (DB column) datetime
25
 */
26
class DatetimeCast extends BaseCast
27
{
28
    public static function get(
29
        mixed $value,
30
        array $params = [],
31
        ?object $helper = null
32
    ): Time {
33
        if (! is_string($value)) {
29✔
34
            self::invalidTypeValueError($value);
×
35
        }
36

37
        if (! $helper instanceof BaseConnection) {
29✔
38
            $message = 'The parameter $helper must be BaseConnection.';
×
39

40
            throw new InvalidArgumentException($message);
×
41
        }
42

43
        /**
44
         * @see https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters
45
         */
46
        $format = self::getDateTimeFormat($params, $helper);
29✔
47

48
        return Time::createFromFormat($format, $value);
29✔
49
    }
50

51
    public static function set(
52
        mixed $value,
53
        array $params = [],
54
        ?object $helper = null
55
    ): string {
56
        if (! $value instanceof Time) {
11✔
57
            self::invalidTypeValueError($value);
×
58
        }
59

60
        if (! $helper instanceof BaseConnection) {
11✔
61
            $message = 'The parameter $helper must be BaseConnection.';
×
62

63
            throw new InvalidArgumentException($message);
×
64
        }
65

66
        $format = self::getDateTimeFormat($params, $helper);
11✔
67

68
        return $value->format($format);
11✔
69
    }
70

71
    /**
72
     * Gets DateTime format from the DB connection.
73
     *
74
     * @param list<string> $params Additional param
75
     */
76
    protected static function getDateTimeFormat(array $params, BaseConnection $db): string
77
    {
78
        return match ($params[0] ?? '') {
34✔
79
            ''      => $db->dateFormat['datetime'],
32✔
80
            'ms'    => $db->dateFormat['datetime-ms'],
25✔
81
            'us'    => $db->dateFormat['datetime-us'],
2✔
82
            default => throw new InvalidArgumentException('Invalid parameter: ' . $params[0]),
34✔
83
        };
34✔
84
    }
85
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc