Puppet Function: to_phpconfig

Defined in:
lib/puppet/functions/to_phpconfig.rb
Function type:
Ruby 4.x API

Overview

to_phpconfig(Hash $original_hash)Hash

Parameters:

  • original_hash (Hash)

Returns:

  • (Hash)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/puppet/functions/to_phpconfig.rb', line 3

Puppet::Functions.create_function(:to_phpconfig) do
  dispatch :convert do
    required_param 'Hash', :original_hash
    return_type 'Hash'
  end

  def convert(original_hash)
    new_hash = {}

    original_hash.each do |org_key, org_val|
      ## FORMAT KEY ##
      new_key = '$config'

      key_split = org_key.split('.')
      new_key  += format_key(key_split)

      ## FORMAT VALUE ##
      case org_val.to_s.strip
      when %r{^(\d+\.\d+|\d+|true|false)$}
        # These values do not need formatting
        new_hash[new_key] = org_val
      when %r{^(\[\{.*\}\]|\{.*\}\,(\{.*\}\,?)*)$}
        # For array of hashes, create an array key, and add a entry for each hash
        num = 0
        org_val.each do |hash|
          new_hash["#{new_key}[#{num}]"] = format_val_hash(hash)
          num += 1
        end
      when %r{^\[.*\]$}
        new_hash[new_key] = format_val_array(org_val)
      when %r{^\{.*\}$}
        new_hash[new_key] = format_val_hash(org_val)
      else
        # Strings need qoutes around it
        new_hash[new_key] = "'#{org_val}'"
      end
    end

    new_hash
  end

  # @param values - an array of values to format
  # @return [string]
  def format_key(values)
    formatted_vals = []

    values.each do |item|
      begin # rubocop:disable Style/RedundantBegin
        formatted_vals.push(Integer(item))
      rescue StandardError
        formatted_vals.push("'#{item}'")
      end
    end

    formatted_vals.map! { |item| "[#{item}]" }

    formatted_vals.join
  end

  # @param values - an array of values to format
  # @return [string]
  def format_val_array(values)
    # This is definitly not pretty; but it works fine enough
    joined_vals = values.join("', '")
    "array('#{joined_vals}')"
  end

  # @param hash - a hash of values to format
  # @return [string]
  def format_val_hash(hash)
    # Again, not pretty; works
    formatted_vals = []
    hash.each { |key, value| formatted_vals.push("'#{key}' => '#{value}'") }

    joined_vals = formatted_vals.join(', ')
    "array(#{joined_vals})"
  end
end