Class: Ionize::Php::Environment::PhpArray

Inherits:
Hash
  • Object
show all
Defined in:
lib/ionize/environment/php_array.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hash

#node_type

Constructor Details

#initialize(*args, &block) ⇒ PhpArray

Returns a new instance of PhpArray.



6
7
8
9
# File 'lib/ionize/environment/php_array.rb', line 6

def initialize(*args, &block)
  @index = 0
  super(*args, &block)
end

Class Method Details

.from_array(*args) ⇒ Object



83
84
85
86
87
# File 'lib/ionize/environment/php_array.rb', line 83

def self.from_array(*args)
  array = self.new
  args.each {|arg| array.add(arg)}
  array
end

.from_hash(hash) ⇒ Object



77
78
79
80
81
# File 'lib/ionize/environment/php_array.rb', line 77

def self.from_hash(hash)
  array = self.new
  hash.each {|key, value| array.put(key, value)}
  array
end

.from_mixed(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ionize/environment/php_array.rb', line 65

def self.from_mixed(*args)
  array = self.new
  args.each do |arg|
    if arg.is_a? Hash
      arg.each {|key, value| array.put(key, value)}
    else
      array.add(arg)
    end
  end
  array
end

Instance Method Details

#add(value) ⇒ Object



42
43
44
# File 'lib/ionize/environment/php_array.rb', line 42

def add(value)
  put(@index, value)
end

#array?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/ionize/environment/php_array.rb', line 19

def array?
  keys.all? {|k| k.class == Fixnum }
end

#combine(second) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/ionize/environment/php_array.rb', line 23

def combine(second)
  array = self.class.new
  second.values.each_with_index do |val, key| 
    array.put(self[key], val)
  end
  array
end

#hash?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/ionize/environment/php_array.rb', line 15

def hash?
  keys.any? {|k| k.class != Fixnum } 
end

#inspectObject



89
90
91
# File 'lib/ionize/environment/php_array.rb', line 89

def inspect
  "array(" + super.gsub("{", "").gsub("}", "") + ")"
end

#intersect(second) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ionize/environment/php_array.rb', line 31

def intersect(second)
  array = self.class.new
  second.values.each do |value|
    if self.values.include? value
      key = self.to_a.select {|k,v| v == value }.first.first
      array.put(key, value)
    end
  end
  array
end

#join(*args) ⇒ Object



11
12
13
# File 'lib/ionize/environment/php_array.rb', line 11

def join(*args)
  values.join(*args)
end

#merge(other) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/ionize/environment/php_array.rb', line 57

def merge(other)
  if self.array? and other.array?
    self.class.from_array(*(self.values + other.values))
  else
    self.class.from_hash(super(other))
  end
end

#put(key, value) ⇒ Object



46
47
48
49
50
51
# File 'lib/ionize/environment/php_array.rb', line 46

def put(key, value)
  if key.is_a? Fixnum and key >= @index
    @index = key+1
  end
  self[key] = value
end

#to_hashObject



53
54
55
# File 'lib/ionize/environment/php_array.rb', line 53

def to_hash
  Hash[*(self.to_a).flatten]
end