Aptana PHP Snippet(在ruby中):如何创建getter / setter片段并将变量名称的首字母大写?

I like snippets in Aptana. I want to create a snippet that creates me a private class variable of type String. I got it to work like this:

# code from php bunle in Aptana 'snippets.rb'
# private member with getter setter and type checking for STRING
  snippet 'private String MEMBER' do |s|
  s.trigger = 'myps'
  s.expansion = '
private \$${variableName}; /* string */

function set_${variableName}(/* string */ \$${variableName}) {
    if(!is_string(\$${variableName})) {
        throw new \Exception(get_class(\$this)."->set_${variableName}(): Parameter must be string!");
    }
    \$this->${variableName} = \$${variableName};
}

function get_${variableName}() {
    return \$this->${variableName};
}

'

Unfortunately I do not know Ruby and I would like to know if it is possible to get getter/setter function names like this (CamelCase):

private $member;
function setMember($member);
function getMember();

instead of:

private $member;
function set_member($member);
function get_member();

I must use Camel case in my company and I really would like to get it to work.

Thanks for you help.

end

I found a working solution for my question. I managed to generate getter and setter methods in a convenient way in Aptana Studio 3. I did not realize this solution via snippets but as a command in the php 'ruble' bundle. I will post the source for this command even i think it is very ugly code, but i really do not know ruby and just managed to get it to work somehow. Beautification and better style are very welcome.

I found this link interesting: https://wiki.appcelerator.org/display/tis/Ruble+Specification

howto get the command:

  1. In Aptana click on Commands > PHP > Edit this bundle
  2. in your project explorer you'll find a new "* PHP" bundle. Open "PHP -> commands" folder here
  3. create a new file "getter_setter.rb"
  4. past ugly ruby code below into it
  5. save that file and restart the IDE

code:

require 'ruble'

command "getter_setter" do |cmd|
    cmd.key_binding = "CONTROL+G"

    cmd.input = :selection
    cmd.output = :insert_as_text

    cmd.invoke do |context|

        input = STDIN.read

        splittedInput = input.split(";")

        type = nil
        if splittedInput[1] != nil
            type = splittedInput[1].gsub(/\s+/, "")
            type = type.sub("//", "")
        end

        varName = splittedInput.first
        varName = varName.sub("private", "")
        varName = varName.gsub(/\s+/, "")
        varName = varName.sub("$", "")

        capitalizedName = varName.slice(0,1).capitalize + varName.slice(1..-1)

        templateTypeless = "

    function set#{capitalizedName}($#{varName}) {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->set#{capitalizedName}()');}
        $this->#{varName} = $#{varName};
    }

    function get#{capitalizedName}() {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->get#{capitalizedName}()');}
        return $this->#{varName};
    }

    "

        templateBasicType = "

    function set#{capitalizedName}(/* #{type} */ $#{varName}) {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->set#{capitalizedName}()');}
        if(!is_#{type}(#{varName})) {
            throw new \Exception(get_class($this).'->set#{capitalizedName}(): Parameter must be #{type}!');
        }
        $this->#{varName} = $#{varName};
    }

    function get#{capitalizedName}() {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->get#{capitalizedName}()');}
        return $this->#{varName};
    }

    "
        templateComplexType = "

    function set#{capitalizedName}(#{type} $#{varName}) {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->set#{capitalizedName}()');}
        $this->#{varName} = $#{varName};
    }

    function get#{capitalizedName}() {
        global $MY_CLIENT_LOGGER;
        if($MY_CLIENT_LOGGER) { $MY_CLIENT_LOGGER->log(get_class($this).'->get#{capitalizedName}()');}
        return $this->#{varName};
    }

    "
        outputString = ""
        if type == nil
            outputString = templateTypeless
        elsif type == "string" or type == "int" or type == "float" or type == "double" or type == "array" or type == "object"
            outputString = templateBasicType
        else
            outputString = templateComplexType
        end

        context.output = outputString
    end
end

Usage:

  1. create a php file containing a class and declare a private $var
  2. select the line in which the var was declared and use the keyboard shortcut "CONTROL+G" to create the getter/setter methods

Hint: If you append a comment to the line of private member declaration note a basic type (string/int/float/double/array/bool/object) then you can get type checking in the setter. e.g.: private $var //string

Hint: If you append a comment to the line of private member declaration note a complex type then you can get type checking in the setter. e.g.: private $var //my amespace\CComplexTypeClass