Class: PHPSession::Decoder

Inherits:
Object
  • Object
show all
Defined in:
lib/php_session/decoder.rb

Defined Under Namespace

Modules: State

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(string, encoding, encoding_option) ⇒ Decoder

Returns a new instance of Decoder.



15
16
17
18
19
20
21
22
23
# File 'lib/php_session/decoder.rb', line 15

def initialize(string, encoding, encoding_option)
  @encoding = encoding
  @encoding_option = encoding_option
  @buffer = string
  @data = {}
  @state = State::VarName
  @stack = []
  @array = [] # array of array
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



4
5
6
# File 'lib/php_session/decoder.rb', line 4

def array
  @array
end

#bufferObject

Returns the value of attribute buffer.



4
5
6
# File 'lib/php_session/decoder.rb', line 4

def buffer
  @buffer
end

#encodingObject (readonly)

Returns the value of attribute encoding.



5
6
7
# File 'lib/php_session/decoder.rb', line 5

def encoding
  @encoding
end

#encoding_optionObject (readonly)

Returns the value of attribute encoding_option.



5
6
7
# File 'lib/php_session/decoder.rb', line 5

def encoding_option
  @encoding_option
end

#stackObject

Returns the value of attribute stack.



4
5
6
# File 'lib/php_session/decoder.rb', line 4

def stack
  @stack
end

#stateObject

Returns the value of attribute state.



4
5
6
# File 'lib/php_session/decoder.rb', line 4

def state
  @state
end

Class Method Details

.decode(string, encoding = nil, encoding_option = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/php_session/decoder.rb', line 7

def self.decode(string, encoding = nil, encoding_option = {})
  if string.nil?
    string = ""
  end
  string.chomp!
  self.new(string, encoding, encoding_option).decode
end

Instance Method Details

#consume_arrayObject



47
48
49
# File 'lib/php_session/decoder.rb', line 47

def consume_array
  @array[0][:consumed_count] += 1
end

#decodeObject



25
26
27
28
29
30
31
# File 'lib/php_session/decoder.rb', line 25

def decode
  loop do
    break if @buffer.size == 0
    @state.parse(self)
  end
  @data
end

#elements_countObject



41
42
43
# File 'lib/php_session/decoder.rb', line 41

def elements_count
  @array[0][:length]
end

#extract_stack(count) ⇒ Object



54
55
56
57
58
# File 'lib/php_session/decoder.rb', line 54

def extract_stack(count)
  poped = @stack[(@stack.size - count) .. -1]
  @stack = @stack.slice(0, @stack.size - count)
  poped
end

#finished_arrayObject



50
51
52
# File 'lib/php_session/decoder.rb', line 50

def finished_array
  @array[0][:length] * 2 == @array[0][:consumed_count]
end

#in_arrayObject



44
45
46
# File 'lib/php_session/decoder.rb', line 44

def in_array
  @array.size > 0
end

#process_empty_array_valueObject



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/php_session/decoder.rb', line 60

def process_empty_array_value
  array_which_finished  = @array.shift

  klass  = array_which_finished[:klass];
  if klass
    struct = define_or_find_struct(klass, [])
    process_value(struct.new)
  else
    process_value({})
  end
  @state = State::ArrayEnd
end

#process_value(value) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/php_session/decoder.rb', line 72

def process_value(value)
  if in_array
    @stack.push(value)
    consume_array

    if finished_array
      array_which_finished  = @array.shift
      key_values_array = extract_stack(array_which_finished[:length] * 2)
      key_values = key_values_array.group_by.with_index{|el, i| i%2 == 0 ? :key : :value}

      klass  = array_which_finished[:klass];
      if klass
        struct = define_or_find_struct(klass, key_values[:key])
        process_value(struct.new(*key_values[:value]))
        @state = State::ArrayEnd
        @state.parse(self)
      else
        hash = {}
        key_values_array.each_slice(2) do |kv|
          hash[kv[0]] = kv[1]
        end
        process_value(hash)

        @state = State::ArrayEnd
        @state.parse(self)
      end
    else
      @state = State::VarType
    end
  else
    varname = @stack.pop;
    @data[varname] = value;
    @state = State::VarName
  end
end

#start_array(length, klass = nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/php_session/decoder.rb', line 33

def start_array(length, klass = nil)
  # [length, comsumed?, class]
  @array.unshift({
    :length => length,
    :consumed_count => 0,
    :klass => klass
  })
end