PluginProbe
Packeta / 1.5.4
Packeta v1.5.4
2.3.2 2.3.1 trunk 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.3.0 1.3.1 1.3.2 1.4 1.4.1 1.4.2 1.4.3 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 All 56 releases
packeta / src / Packetery / Core / Helper.php

Helper.php in Packeta 1.5.4, at src/Packetery/Core/Helper.php

97 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class Helper
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10
11 namespace Packetery\Core;
12
13 use DateTimeImmutable;
14
15 /**
16 * Class Helper
17 *
18 * @package Packetery
19 */
20 class Helper {
21 public const TRACKING_URL = 'https://tracking.packeta.com/?id=%s';
22 public const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s';
23 public const DATEPICKER_FORMAT = 'Y-m-d';
24
25 /**
26 * Simplifies weight.
27 *
28 * @param float|null $weight Weight.
29 *
30 * @return float|null
31 */
32 public static function simplifyWeight( ?float $weight ): ?float {
33 return self::simplifyFloat( $weight, 3 );
34 }
35
36 /**
37 * Simplifies float value to have max decimal places.
38 *
39 * @param float|null $value Value.
40 * @param int $maxDecimalPlaces Max decimal places.
41 *
42 * @return float|null
43 */
44 public static function simplifyFloat( ?float $value, int $maxDecimalPlaces ): ?float {
45 if ( null === $value ) {
46 return null;
47 }
48
49 return (float) number_format( $value, $maxDecimalPlaces, '.', '' );
50 }
51
52 /**
53 * Returns tracking URL.
54 *
55 * @param string $packet_id Packet ID.
56 *
57 * @return string
58 */
59 public function get_tracking_url( string $packet_id ): string {
60 return sprintf( self::TRACKING_URL, rawurlencode( $packet_id ) );
61 }
62
63 /**
64 * Creates UTC DateTime.
65 *
66 * @return DateTimeImmutable
67 * @throws \Exception From DateTimeImmutable.
68 */
69 public static function now(): DateTimeImmutable {
70 return new DateTimeImmutable( 'now', new \DateTimeZone( 'UTC' ) );
71 }
72
73 /**
74 * Creates string in given format from DateTimeImmutable object
75 *
76 * @param DateTimeImmutable|null $date Datetime.
77 * @param string $format Datetime format.
78 *
79 * @return string|null
80 */
81 public function getStringFromDateTime( ?DateTimeImmutable $date, string $format ): ?string {
82 return $date ? $date->format( $format ) : null;
83 }
84
85 /**
86 * Creates DateTimeImmutable object from string
87 *
88 * @param string $date Date.
89 *
90 * @return \DateTimeImmutable
91 * @throws \Exception From DateTimeImmutable.
92 */
93 public function getDateTimeFromString( ?string $date ): ?DateTimeImmutable {
94 return $date ? new DateTimeImmutable( $date ) : null;
95 }
96 }
97