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.



11
12
13
14
15
16
17
18
19
# File 'lib/php_session/decoder.rb', line 11

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
# File 'lib/php_session/decoder.rb', line 7

def self.decode(string, encoding = nil, encoding_option = {})
  self.new(string, encoding, encoding_option).decode
end

Instance Method Details

#consume_arrayObject



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

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

#decodeObject



21
22
23
24
25
26
27
# File 'lib/php_session/decoder.rb', line 21

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

#elements_countObject



37
38
39
# File 'lib/php_session/decoder.rb', line 37

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

#extract_stack(count) ⇒ Object



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

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

#finished_arrayObject



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

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

#in_arrayObject



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

def in_array
  @array.size > 0
end

#process_empty_array_valueObject



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/php_session/decoder.rb', line 56

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



68
69
70
71
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
# File 'lib/php_session/decoder.rb', line 68

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



29
30
31
32
33
34
35
36
# File 'lib/php_session/decoder.rb', line 29

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