commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
MapperFactory.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Base;
11
12use DateTime;
13use DateTimeImmutable;
14
16{
17 public const TIME_FORMAT = "H:i:s.u";
18 public const DATE_FORMAT = "Y-m-d";
19 public const DATETIME_FORMAT = "Y-m-d?H:i:s.uT";
20
24 public static function stringMapper()
25 {
26 return
28 function ($data): ?string {
29 if (is_null($data)) {
30 return null;
31 }
32 return (string)$data;
33 };
34 }
35
39 public static function numberMapper()
40 {
41 return
43 function ($data): ?float {
44 if (is_null($data)) {
45 return null;
46 }
47 return (float)$data;
48 };
49 }
50
54 public static function integerMapper()
55 {
56 return
58 function ($data): ?int {
59 if (is_null($data)) {
60 return null;
61 }
62 return (int)$data;
63 };
64 }
65
69 public static function dateTimeMapper(string $format = self::DATETIME_FORMAT)
70 {
71 return
73 function ($data) use ($format): ?DateTimeImmutable {
74 if (is_null($data)) {
75 return null;
76 }
77 $date = DateTimeImmutable::createFromFormat($format, $data);
78 if ($date === false) {
79 return null;
80 }
81 return $date;
82 };
83 }
84}
static dateTimeMapper(string $format=self::DATETIME_FORMAT)