Module: DPay::Internal::PHP

Defined in:
lib/dpay/internal/php.rb,
sig/dpay/internal/php.rbs

Constant Summary collapse

SAFE_INTEGRAL_FLOAT =

Returns:

  • (Integer)
2**53
NON_UNRESERVED =

Returns:

  • (Regexp)
/[^A-Za-z0-9\-_.~]/n

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.float_to_string(value) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/dpay/internal/php.rb', line 36

def float_to_string(value)
  return "NAN" if value.nan?
  return value.positive? ? "INF" : "-INF" if value.infinite?

  formatted = format("%.14G", value)
  return formatted unless formatted.include?("E")

  parts = formatted.split("E")
  mantissa = parts.fetch(0)
  exponent = parts.fetch(1)
  mantissa += ".0" unless mantissa.include?(".")
  sign = exponent.start_with?("-") ? "-" : "+"
  digits = exponent.sub(/\A[+-]/, "").sub(/\A0+/, "")
  digits = "0" if digits.empty?
  "#{mantissa}E#{sign}#{digits}"
end

.integral_float?(value) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/dpay/internal/php.rb', line 66

def integral_float?(value)
  value.finite? && value.abs < SAFE_INTEGRAL_FLOAT && value.to_i == value
end

.json_encode(data, escape_slashes: false) ⇒ Object



27
28
29
30
# File 'lib/dpay/internal/php.rb', line 27

def json_encode(data, escape_slashes: false)
  encoded = JSON.generate(normalize_floats(data))
  escape_slashes ? encoded.gsub("/") { "\\/" } : encoded
end

.normalize_floats(value) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dpay/internal/php.rb', line 53

def normalize_floats(value)
  case value
  when Float
    integral_float?(value) ? value.to_i : value
  when Hash
    value.each_with_object({}) { |(key, item), result| result[key] = normalize_floats(item) }
  when Array
    value.map { |item| normalize_floats(item) }
  else
    value
  end
end

.raw_url_encode(value) ⇒ Object



32
33
34
# File 'lib/dpay/internal/php.rb', line 32

def raw_url_encode(value)
  value.to_s.b.gsub(NON_UNRESERVED) { |byte| format("%%%02X", byte.ord) }
end

.round(value) ⇒ Object



23
24
25
# File 'lib/dpay/internal/php.rb', line 23

def round(value)
  value >= 0 ? (value + 0.5).floor : (value - 0.5).ceil
end

.strval(value) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/dpay/internal/php.rb', line 13

def strval(value)
  case value
  when nil, false then ""
  when true then "1"
  when Float then float_to_string(value)
  when String then value
  else value.to_s
  end
end

Instance Method Details

#self?.float_to_stringString

Parameters:

  • value (Float)

Returns:

  • (String)


11
# File 'sig/dpay/internal/php.rbs', line 11

def self?.float_to_string: (Float value) -> String

#self?.integral_float?Boolean

Parameters:

  • value (Float)

Returns:

  • (Boolean)


13
# File 'sig/dpay/internal/php.rbs', line 13

def self?.integral_float?: (Float value) -> bool

#self?.json_encodeString

Parameters:

  • data (Object)
  • escape_slashes: (Boolean)

Returns:

  • (String)


9
# File 'sig/dpay/internal/php.rbs', line 9

def self?.json_encode: (untyped data, ?escape_slashes: bool) -> String

#self?.normalize_floatsObject

Parameters:

  • value (Object)

Returns:

  • (Object)


12
# File 'sig/dpay/internal/php.rbs', line 12

def self?.normalize_floats: (untyped value) -> untyped

#self?.raw_url_encodeString

Parameters:

  • value (Object)

Returns:

  • (String)


10
# File 'sig/dpay/internal/php.rbs', line 10

def self?.raw_url_encode: (untyped value) -> String

#self?.roundInteger

Parameters:

  • value (Float)

Returns:

  • (Integer)


8
# File 'sig/dpay/internal/php.rbs', line 8

def self?.round: (Float value) -> Integer

#self?.strvalString

Parameters:

  • value (Object)

Returns:

  • (String)


7
# File 'sig/dpay/internal/php.rbs', line 7

def self?.strval: (untyped value) -> String