Module: Ionize::Php::Translate::RailsForPhp

Included in:
Rewrites
Defined in:
lib/ionize/translate/rails_for_php.rb

Instance Method Summary collapse

Instance Method Details

#rewrite_fcall_array_combine(args) ⇒ Object



70
71
72
# File 'lib/ionize/translate/rails_for_php.rb', line 70

def rewrite_fcall_array_combine(args)
  [:call, args.second, :combine, [:array, args.third]]
end

#rewrite_fcall_array_merge(args) ⇒ Object



74
75
76
# File 'lib/ionize/translate/rails_for_php.rb', line 74

def rewrite_fcall_array_merge(args)
  [:call, args.second, :merge, [:array, args.third]]
end

#rewrite_fcall_count(args) ⇒ Object

arrays



66
67
68
# File 'lib/ionize/translate/rails_for_php.rb', line 66

def rewrite_fcall_count(args)
  [:call, args.second, :length]
end

#rewrite_fcall_explode(args) ⇒ Object



28
29
30
# File 'lib/ionize/translate/rails_for_php.rb', line 28

def rewrite_fcall_explode(args)
  [:call, args.third, :split, [:array, args.second]]
end

#rewrite_fcall_file(args) ⇒ Object

files



42
43
44
# File 'lib/ionize/translate/rails_for_php.rb', line 42

def rewrite_fcall_file(args)
  [:call, [:const, :File], :readlines, args]
end

#rewrite_fcall_implode(args) ⇒ Object

fcall rewrites..



24
25
26
# File 'lib/ionize/translate/rails_for_php.rb', line 24

def rewrite_fcall_implode(args)
  [:call, args.third, :join, [:array, args.second]]
end

#rewrite_fcall_in_array(args) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/ionize/translate/rails_for_php.rb', line 32

def rewrite_fcall_in_array(args)
  if args.last == [:true]
    [:call, args.third, :include?, [:array, args.second]]
  else
    raise "Cannot handle non-strict version of in_array!"
  end
end

#rewrite_fcall_ltrim(args) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/ionize/translate/rails_for_php.rb', line 101

def rewrite_fcall_ltrim(args)
  if args.length == 2
    [:call, args.second, :lstrip]
  elsif args.length == 3
    regex = [:lit, eval(Regexp.new("^[#{args.third.last}]+").inspect)]
    [:call, args.second, :gsub, [:array, regex, [:str, '']]]
  end
end

#rewrite_fcall_md5(args) ⇒ Object

hashing algorithms..



48
49
50
# File 'lib/ionize/translate/rails_for_php.rb', line 48

def rewrite_fcall_md5(args)
  [:call, [:colon2, [:const, :Digest], :MD5], :hexdigest, [:array, args.second]]
end

#rewrite_fcall_md5_file(args) ⇒ Object



52
53
54
# File 'lib/ionize/translate/rails_for_php.rb', line 52

def rewrite_fcall_md5_file(args)
  [:call, [:colon2, [:const, :Digest], :MD5], :hexdigest, [:array, [:call, [:const, :File], :read, [:array, args.second]]]]
end

#rewrite_fcall_preg_replace(args) ⇒ Object



179
180
181
182
# File 'lib/ionize/translate/rails_for_php.rb', line 179

def rewrite_fcall_preg_replace(args)
  regex = [:lit, eval(args.second.last)]
  [:call, args.fourth, :gsub, [:array, regex, args.third]]
end

#rewrite_fcall_preg_split(args) ⇒ Object



184
185
186
187
# File 'lib/ionize/translate/rails_for_php.rb', line 184

def rewrite_fcall_preg_split(args)
  regex = [:lit, eval(args.second.last)]
  [:call, args.third, :split, [:array, regex]]
end

#rewrite_fcall_rtrim(args) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/ionize/translate/rails_for_php.rb', line 92

def rewrite_fcall_rtrim(args)
  if args.length == 2
    [:call, args.second, :rstrip]
  elsif args.length == 3
    regex = [:lit, eval(Regexp.new("[#{args.third.last}]+$").inspect)]
    [:call, args.second, :gsub, [:array, regex, [:str, '']]]
  end
end

#rewrite_fcall_sha1(args) ⇒ Object



56
57
58
# File 'lib/ionize/translate/rails_for_php.rb', line 56

def rewrite_fcall_sha1(args)
  [:call, [:colon2, [:const, :Digest], :SHA1], :hexdigest, [:array, args.second]]
end

#rewrite_fcall_sha1_file(args) ⇒ Object



60
61
62
# File 'lib/ionize/translate/rails_for_php.rb', line 60

def rewrite_fcall_sha1_file(args)
  [:call, [:colon2, [:const, :Digest], :SHA1], :hexdigest, [:array, [:call, [:const, :File], :read, [:array, args.second]]]]
end

#rewrite_fcall_str_ireplace(args) ⇒ Object



174
175
176
177
# File 'lib/ionize/translate/rails_for_php.rb', line 174

def rewrite_fcall_str_ireplace(args)
  regex = [:lit, eval(Regexp.new(args.second.last, Regexp::IGNORECASE).inspect)]
  [:call, args.fourth, :gsub, [:array, regex, args.third]]
end

#rewrite_fcall_str_replace(args) ⇒ Object



170
171
172
# File 'lib/ionize/translate/rails_for_php.rb', line 170

def rewrite_fcall_str_replace(args)
  [:call, args.fourth, :gsub, [:array, args.second, args.third]]
end

#rewrite_fcall_stripslashes(args) ⇒ Object



84
85
86
# File 'lib/ionize/translate/rails_for_php.rb', line 84

def rewrite_fcall_stripslashes(args)
  [:call, args.second, :gsub, [:array, [:str, "\\"], [:str, ""]]]
end

#rewrite_fcall_strlen(args) ⇒ Object



88
89
90
# File 'lib/ionize/translate/rails_for_php.rb', line 88

def rewrite_fcall_strlen(args)
  [:call, args.second, :length]
end

#rewrite_fcall_strpos(args) ⇒ Object



135
136
137
# File 'lib/ionize/translate/rails_for_php.rb', line 135

def rewrite_fcall_strpos(args)
  [:call, args.second, :index, [:array, args.third]]
end

#rewrite_fcall_strrev(args) ⇒ Object

strings..



80
81
82
# File 'lib/ionize/translate/rails_for_php.rb', line 80

def rewrite_fcall_strrev(args)
  [:call, args.second, :reverse]
end

#rewrite_fcall_strrpos(args) ⇒ Object



139
140
141
# File 'lib/ionize/translate/rails_for_php.rb', line 139

def rewrite_fcall_strrpos(args)
  [:call, args.second, :rindex, [:array, args.third]]
end

#rewrite_fcall_strtolower(args) ⇒ Object



123
124
125
# File 'lib/ionize/translate/rails_for_php.rb', line 123

def rewrite_fcall_strtolower(args)
  [:call, args.second, :downcase]
end

#rewrite_fcall_strtoupper(args) ⇒ Object



127
128
129
# File 'lib/ionize/translate/rails_for_php.rb', line 127

def rewrite_fcall_strtoupper(args)
  [:call, args.second, :upcase]
end

#rewrite_fcall_strtr(args) ⇒ Object



131
132
133
# File 'lib/ionize/translate/rails_for_php.rb', line 131

def rewrite_fcall_strtr(args)
  [:call, args.second, :tr, [:array, args.third, args.fourth]]
end

#rewrite_fcall_substr(args) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/ionize/translate/rails_for_php.rb', line 143

def rewrite_fcall_substr(args)
  if args.length == 4
    [:call, args.second, :slice, [:array, args.third, args.fourth]]
  elsif args.length == 3
    single_arg = args.third.last
    if single_arg < 0
      positive_arg = [:lit, (-single_arg)]
      [:call, args.second, :slice, [:array, args.third, positive_arg]]
    else
      range = [:lit, eval(single_arg.to_s + "..-1")]
      [:call, args.second, :slice, [:array, range]]
    end
  end
end

#rewrite_fcall_substr_replace(args) ⇒ Object

string replacement



160
161
162
163
164
165
166
167
168
# File 'lib/ionize/translate/rails_for_php.rb', line 160

def rewrite_fcall_substr_replace(args)
  if args.length == 5
    [:attrasgn, args.second, :[]=, [:array, args.fourth, args.fifth, args.third]]
  elsif args.length == 4
    single_arg = args.fourth.last
    range = [:lit, eval(single_arg.to_s + "..-1")]
    [:attrasgn, args.second, :[]=, [:array, range, args.third]]
  end
end

#rewrite_fcall_trim(args) ⇒ Object



110
111
112
113
114
115
116
117
# File 'lib/ionize/translate/rails_for_php.rb', line 110

def rewrite_fcall_trim(args)
  if args.length == 2
    [:call, args.second, :strip]
  elsif args.length == 3
    regex = [:lit, eval(Regexp.new("^[#{args.third.last}]+|[#{args.third.last}]+$").inspect)]
    [:call, args.second, :gsub, [:array, regex, [:str, '']]]
  end          
end

#rewrite_fcall_ucfirst(args) ⇒ Object



119
120
121
# File 'lib/ionize/translate/rails_for_php.rb', line 119

def rewrite_fcall_ucfirst(args)
  [:call, args.second, :capitalize]
end

#rewrite_lasgn_preg_match(var, expr) ⇒ Object

lasgn rewrites



6
7
8
9
10
11
12
# File 'lib/ionize/translate/rails_for_php.rb', line 6

def rewrite_lasgn_preg_match(var, expr)
  regex, object, matches = *expr.last.rest

  [:masgn, 
   [:array, [:lasgn, var], [:lasgn, matches.last]], 
   [:to_ary, [:call, object, :preg_match, [:array, [:lit, eval(regex.last)]]]]]
end

#rewrite_lasgn_preg_match_all(var, expr) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/ionize/translate/rails_for_php.rb', line 14

def rewrite_lasgn_preg_match_all(var, expr)
  regex, object, matches = *expr.last.rest
  
  [:masgn, 
   [:array, [:lasgn, var], [:lasgn, matches.last]], 
   [:to_ary, [:call, object, :preg_match_all, [:array, [:lit, eval(regex.last)]]]]]          
end