PluginProbe
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO / 1.4.14
Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO v1.4.14
1.4.14 1.4.13 1.4.12 1.4.11 1.4.10 1.4.9 1.4.8 1.4.7 1.4.6 1.4.5 1.4.4 1.4.3 1.4.2 1.4.1 1.4.0 1.3.54 1.3.53 1.3.52 1.3.51 1.3.50 1.3.49 1.3.48 1.3.47 1.3.46 1.3.45 All 177 releases
disco / app / Utility / Value.php

Value.php in Discount Rules for WooCommerce – Disco | Dynamic Pricing, Conditions, Bulk, Bundle, BOGO 1.4.14, at app/Utility/Value.php

65 lines 1.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Scalar value casting.
4 *
5 * @package Disco
6 * @subpackage \App\Utility
7 */
8
9 namespace Disco\App\Utility;
10
11 /**
12 * Casts untyped campaign / cart values to scalars.
13 *
14 * Campaign rules come out of a JSON column and cart items out of WooCommerce
15 * session data, so quantities and ids arrive as strings, nulls or occasionally
16 * arrays. These casts keep calling code free of defensive `is_numeric()` noise
17 * and return a harmless zero for anything unusable.
18 *
19 * @package Disco
20 * @subpackage Disco\App\Utility
21 * @category Utility
22 */
23 class Value {
24
25 /**
26 * Cast a raw value to int, or 0 when it is not numeric.
27 *
28 * @param mixed $value Raw value.
29 */
30 public static function to_int( $value ): int {
31 if ( ! is_numeric( $value ) ) {
32 return 0;
33 }
34
35 return (int) $value;
36 }
37
38 /**
39 * Cast a raw value to float, or 0.0 when it is not numeric.
40 *
41 * @param mixed $value Raw value.
42 */
43 public static function to_float( $value ): float {
44 if ( ! is_numeric( $value ) ) {
45 return 0.0;
46 }
47
48 return (float) $value;
49 }
50
51 /**
52 * Cast a raw value to string, or '' when it is not scalar.
53 *
54 * @param mixed $value Raw value.
55 */
56 public static function to_string( $value ): string {
57 if ( ! is_scalar( $value ) ) {
58 return '';
59 }
60
61 return (string) $value;
62 }
63
64 }
65