getTimestamp(); } if ( ! is_int( $time ) ) { return null; } return gmdate( 'Y-m-d\TH:i:s\Z', $time ); } /** * A decimal string the hub can parse exactly. Never a float, never empty. * * WooCommerce hands amounts back as floats or strings depending on where they * came from; both are normalised to a plain decimal with at most six places. * * @param mixed $amount Amount as WooCommerce returns it. */ public static function money( $amount ): string { if ( is_int( $amount ) ) { return (string) $amount; } if ( is_float( $amount ) ) { $amount = sprintf( '%.6F', $amount ); } $amount = is_string( $amount ) ? trim( $amount ) : ''; if ( ! preg_match( '/^-?\d+(\.\d+)?$/', $amount ) ) { return '0'; } if ( str_contains( $amount, '.' ) ) { [ $whole, $fraction ] = explode( '.', $amount, 2 ); $fraction = rtrim( substr( $fraction, 0, 6 ), '0' ); $amount = '' === $fraction ? $whole : $whole . '.' . $fraction; } return '-0' === $amount ? '0' : $amount; } /** * Money, or null when there is no value (an unset sale price). * * @param mixed $amount Amount as WooCommerce returns it. */ public static function moneyOrNull( $amount ): ?string { return null === $amount || '' === $amount ? null : self::money( $amount ); } /** * A canonical Iranian mobile, or null. The raw input is never sent. * * @param mixed $raw Number as typed. */ public static function phone( $raw ): ?string { $phone = is_string( $raw ) && '' !== $raw ? PhoneNumber::parse( $raw ) : null; return null === $phone ? null : $phone->canonical(); } /** * A lower-cased email the contract accepts, or null. * * @param mixed $raw Address as stored. */ public static function email( $raw ): ?string { $email = is_string( $raw ) ? strtolower( trim( $raw ) ) : ''; return '' !== $email && is_email( $email ) && ! preg_match( '/\s/', $email ) ? $email : null; } /** * A non-empty string, or null. * * @param mixed $value Value. */ public static function text( $value ): ?string { return is_string( $value ) && '' !== trim( $value ) ? $value : null; } /** * A platform id as a string, or null for zero and empty. * * @param mixed $value Id. */ public static function id( $value ): ?string { if ( is_int( $value ) ) { return $value > 0 ? (string) $value : null; } return is_string( $value ) && '' !== $value && '0' !== $value ? $value : null; } /** * A WooCommerce status slug without the `wc-` prefix. * * @param string $status Status. */ public static function status( string $status ): string { $status = str_starts_with( $status, 'wc-' ) ? substr( $status, 3 ) : $status; $status = strtolower( (string) preg_replace( '/[^a-z0-9_-]/i', '', $status ) ); return '' === $status ? 'unknown' : $status; } }