woocommerce-pos
/
vendor_prefixed
/
sentry
/
sentry
/
src
/
Serializer
/
RepresentationSerializer.php
RepresentationSerializer.php in WCPOS – Point of Sale (POS) plugin for WooCommerce 1.10.23, at vendor_prefixed/sentry/sentry/src/Serializer/RepresentationSerializer.php
| 1 | <?php |
| 2 | |
| 3 | declare (strict_types=1); |
| 4 | namespace WCPOS\Vendor\Sentry\Serializer; |
| 5 | |
| 6 | /** |
| 7 | * Serializes a value into a representation that should reasonably suggest |
| 8 | * both the type and value, and be serializable into JSON. |
| 9 | */ |
| 10 | class RepresentationSerializer extends AbstractSerializer implements RepresentationSerializerInterface |
| 11 | { |
| 12 | /** |
| 13 | * {@inheritdoc} |
| 14 | */ |
| 15 | public function representationSerialize($value) |
| 16 | { |
| 17 | $value = $this->serializeRecursively($value); |
| 18 | if (\is_numeric($value)) { |
| 19 | return (string) $value; |
| 20 | } |
| 21 | if (\is_bool($value)) { |
| 22 | return $value ? 'true' : 'false'; |
| 23 | } |
| 24 | return $value; |
| 25 | } |
| 26 | /** |
| 27 | * This method is overridden to return even basic types as strings. |
| 28 | * |
| 29 | * @param mixed $value The value that needs to be serialized |
| 30 | * |
| 31 | * @return string |
| 32 | */ |
| 33 | protected function serializeValue($value) |
| 34 | { |
| 35 | if ($value === null) { |
| 36 | return 'null'; |
| 37 | } |
| 38 | if ($value === \false) { |
| 39 | return 'false'; |
| 40 | } |
| 41 | if ($value === \true) { |
| 42 | return 'true'; |
| 43 | } |
| 44 | if (\is_float($value)) { |
| 45 | if (\is_nan($value)) { |
| 46 | return 'NAN'; |
| 47 | } |
| 48 | if ($this->isCastableToInt($value) && (int) $value == $value) { |
| 49 | return $value . '.0'; |
| 50 | } |
| 51 | } |
| 52 | if (\is_numeric($value)) { |
| 53 | return (string) $value; |
| 54 | } |
| 55 | return (string) parent::serializeValue($value); |
| 56 | } |
| 57 | /** |
| 58 | * Casting a non-finite float, or one that falls outside of the integer |
| 59 | * range, raises a warning as of PHP 8.5. |
| 60 | * |
| 61 | * @param float $value The value to check |
| 62 | */ |
| 63 | private function isCastableToInt(float $value) : bool |
| 64 | { |
| 65 | return $value >= (float) \PHP_INT_MIN && $value < (float) \PHP_INT_MAX; |
| 66 | } |
| 67 | } |
| 68 |