PluginProbe
Packeta / 2.0.9
Packeta v2.0.9
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 / CoreHelper.php

CoreHelper.php in Packeta 2.0.9, at src/Packetery/Core/CoreHelper.php

130 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Class CoreHelper
4 *
5 * @package Packetery
6 */
7
8 declare( strict_types=1 );
9
10 namespace Packetery\Core;
11
12 use DateTimeImmutable;
13
14 /**
15 * Class CoreHelper
16 *
17 * @package Packetery
18 */
19 class CoreHelper {
20 public const MYSQL_DATETIME_FORMAT = 'Y-m-d H:i:s';
21 public const MYSQL_DATE_FORMAT = 'Y-m-d';
22 public const DATEPICKER_FORMAT = 'Y-m-d';
23 public const DATEPICKER_FORMAT_JS = 'yy-mm-dd';
24
25 /**
26 * @var string
27 */
28 private $trackingUrl;
29
30 public function __construct( string $trackingUrl ) {
31 $this->trackingUrl = $trackingUrl;
32 }
33
34 /**
35 * Simplifies weight.
36 *
37 * @param float|null $weight Weight.
38 *
39 * @return float|null
40 */
41 public static function simplifyWeight( ?float $weight ): ?float {
42 return self::simplifyFloat( $weight, 3 );
43 }
44
45 /**
46 * Simplifies float value to have max decimal places.
47 *
48 * @param float|null $value Value.
49 * @param int $maxDecimalPlaces Max decimal places.
50 *
51 * @return float|null
52 */
53 public static function simplifyFloat( ?float $value, int $maxDecimalPlaces ): ?float {
54 if ( $value === null ) {
55 return null;
56 }
57
58 return (float) number_format( $value, $maxDecimalPlaces, '.', '' );
59 }
60
61 public static function trimDecimalPlaces( ?float $value, int $decimals ): ?string {
62 if ( $value === null ) {
63 return null;
64 }
65
66 $formattedValue = number_format( $value, $decimals, '.', '' );
67
68 if ( $decimals > 0 ) {
69 $valueWithoutTrailingZeros = rtrim( $formattedValue, '0' );
70
71 return rtrim( $valueWithoutTrailingZeros, '.' );
72 }
73
74 return $formattedValue;
75 }
76
77 public function getTrackingUrl( ?string $packetId ): ?string {
78 if ( $packetId === null ) {
79 return null;
80 }
81
82 return sprintf( $this->trackingUrl, rawurlencode( $packetId ) );
83 }
84
85 public static function now(): DateTimeImmutable {
86 return new DateTimeImmutable( 'now', new \DateTimeZone( 'UTC' ) );
87 }
88
89 /**
90 * Creates string in given format from DateTimeImmutable object
91 *
92 * @param DateTimeImmutable|null $date Datetime.
93 * @param string $format Datetime format.
94 *
95 * @return string|null
96 */
97 public function getStringFromDateTime( ?DateTimeImmutable $date, string $format ): ?string {
98 return $date !== null ? $date->format( $format ) : null;
99 }
100
101 /**
102 * Creates DateTimeImmutable object from string
103 *
104 * @param string $date Date.
105 *
106 * @return \DateTimeImmutable
107 * @throws \Exception From DateTimeImmutable.
108 */
109 public function getDateTimeFromString( ?string $date ): ?DateTimeImmutable {
110 return $date !== null && $date !== '' ? new DateTimeImmutable( $date ) : null;
111 }
112
113 public static function convertToCentimeters( float $value, string $fromUnit ): float {
114 $conversionFactors = [
115 'cm' => 1,
116 'm' => 100,
117 'mm' => 0.1,
118 'in' => 2.54,
119 'yd' => 91.44,
120 ];
121
122 if ( ! isset( $conversionFactors[ $fromUnit ] ) ) {
123 // Do not convert unknown values.
124 return $value;
125 }
126
127 return $value * $conversionFactors[ $fromUnit ];
128 }
129 }
130