$val) { if (\is_string($key)) { $key = '\'' . \str_replace('\'', '\\\'', $key) . '\''; } $stringify .= \str_repeat(' ', $depth * 4) . $key . ' => ' . self::parseVariable($val, $depth + 1) . ',' . "\n"; } return $stringify . \str_repeat(' ', ($depth - 1) * 4) . ']'; } /** * Serialize value. * * @param mixed $value Value to serialzie * @param int $depth Array depth * * @return string Returns the parsed value as string representation * * @throws \UnexpectedValueException Throws this exception if the value cannot be parsed (invalid data type) * * @since 1.0.0 */ public static function parseVariable(mixed $value, int $depth = 1) : string { if (\is_array($value)) { return self::serializeArray($value, $depth); } elseif (\is_string($value)) { return '\'' . \str_replace('\'', '\\\'', $value) . '\''; } elseif (\is_bool($value)) { return $value ? 'true' : 'false'; } elseif ($value === null) { return 'null'; } elseif (\is_float($value)) { return \rtrim(\rtrim(\number_format($value, 5, '.', ''), '0'), '.'); } elseif (\is_scalar($value)) { return (string) $value; } elseif ($value instanceof SerializableInterface) { return self::parseVariable($value->serialize()); } elseif ($value instanceof \JsonSerializable) { return self::parseVariable($value->jsonSerialize()); } else { throw new \UnexpectedValueException(); } } }