How to lowercase first letter of Textmate snippet input? - textmate

I have shortcut for creating getter method and here is my snippet code right now:
public function get${1:PropertyName}() {
return \$this->${1:propertyName};
}
$0
Output I'm looking for:
public function getAreaCode() {
return $this->areaCode;
}
So the question is, how to automatically transform input's first letter into lowercase, but only on the second line?

You can do a regexp match that matches the first character and modifies it like this:
public function get${1/./\u/}() {
return \$this->${1:propertyName};
}
$0
I've used this, to also add the property and setting the scope with dropdown:
${2|private,protected,public|} \$${1};
${3|public,protected,private|} function get${1/./\u$0/}() {
return \$this->${1:propertyName};
}
${3} function set${1/./\u$0/}(\$value) {
\$this->${1} = \$value;
return \$this;
}
$0
See Transformation section on Macromates for more.

Related

Which rule can I use to change variable name?

I need to replace all occurencies of $connection with $link?
I know I could do with a regexp replacement using my IDE, but I need to be able to re-run the sostitution automatically.
So I want to use rector.
Is there a way to replace a var name ? Which is the rule name?
There are a couple of potentially suitable rules & sets:
RenamePropertyRector
There is also the '\Rector\Set\ValueObject\SetList::NAMING' set that can be enabled in rector.php that will perform some similar rules, like renaming variables according to the type.
The complete set of basic rules (not including framework or library-specific) are at https://github.com/rectorphp/rector/blob/main/docs/rector_rules_overview.md
I create a custom rule, very specific for my needs
<?php
namespace Rules;
use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use PhpParser\Node\Expr\Variable;
class ReplaceConnectionVarNameWithLink extends AbstractRector
{
public function getNodeTypes(): array
{
return [
Variable::class
];
}
public function getRuleDefinition(): \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new RuleDefinition(
'rename $connect into $link',
[]
);
}
public function refactor(\PhpParser\Node $node)
{
if (!$this->isName( $node, 'connection')) {
// return null to skip it
return null;
}
$node->name = "link";
return $node;
}
}

Golang mock for all arguments except

mock.on("FunctionName", "someStringArgument").Return(...)
Suppose if someStringArgument is "hello" then I want to return "1". But if someStringArgument is any other string I want to return "2".
How is this achieve able with GoMock?
What you want to do is write a custom function which will return your desired output.
Here is a simple example of what I do.
Define a custom response function
func FunctionNameResponse(arg String) string{
if arg == "hellp" {
// I used quotes because you mentioned "1" and not 1
return "1"
}
return "2"
}
Call custom function anywhere needed.
mock.on("FunctionName", mock.Anything).Return(FunctionNameResponse("someStringArgument"))

view does not appear, only data appears

i want to passing data to view
my controller
public function create()
{
$maxcode=Product::selectRaw('MAX(RIGHT(product_code, 3)) as max')->first();
$prx='P2018-';
if($maxcode->count()>0)
{
$tmp = ((int)$maxcode->max)+1;
$newcode = $prx.sprintf("%03s", $tmp);
}
else
{
$newcode = $prx."P2018-001";
}
return $newcode;
return view('product.create', $newcode);
}
my view
...other code...
#foreach($newcode as $nc)
{{$nc->newcode}}
#endforeach
---other code...
the code works well, but the only results appear, the other code on the view does not work.
someone might be able to help me
remove return $newcode;
return view('product.create')->with('newcode',$newcode);
$newcode variable is not an array, you can not use foreach.
use {{$newcode}} in blade to display value of variable.
remove return $newcode; line and change your return function like this,
return view('product.create', compact('newcode'));

How can i place validations for fields in groovy for specific format

I have Domain class and in that for a particular field of type String, it accepts alphanumeric values,i need a validation in the format it should accept only AB12-QW-1 (or) XY-12 values. how can i validate the field.
Please suggest the solution.
Thanks.
Assume your domain class looks like this
class Foo {
String bar
}
If you can define a regex that matches only the legal values, you can apply the constraint using:
class Foo {
String bar
constraints = {
bar(matches:"PUT-YOUR-REGEX-HERE")
}
}
Alternatively, if you can easily list all the legal values, you can use:
class Foo {
String bar
constraints = {
bar(inList:['AB12-QW-1', 'XY-12'])
}
}
If neither of these solutions will work, then you'll likely need to write a custom validator method
You could use a custom validator:
class YourDomain {
String code
static constraints = {
code( validator: {
if( !( it in [ 'AB12-QW-1', 'XY-12' ] ) ) return ['invalid.code']
})
}
}
However, your explanation of what codes are valid is a bit vague, so you probably want something else in place of the in call
[edit]
Assuming your two strings just showed placeholders for letters or numbers, the following regexp validator should work:
constraints = {
code( matches:'[A-Z]{2}[0-9]{2}-[A-Z]{2}-[0-9]|[A-Z]{2}-[0-9]{2}' )
}
And that will return the error yourDomain.code.matches.invalid if it fails

Flex 4 create validator that allows only letters and dashes?

How can I create a validator that allows only letters as dashes as input?
Thanks in advance
EDIT
This is what I have so far..
If I write test it passes and if I write 123 it fails but if I write test123 it passes which I don't want
EDIT
The validator now works as I wanted. :)
override protected function doValidation(value:Object):Array
{
results = [];
var regEx:RegExp = /^[a-zA-Z _-]*[a-zA-Z][a-zA-Z _-]*$/;
if(regEx.test(value as String)) {
trace("passed")
return results;
} else {
var err:ValidationResult = new ValidationResult(true,"","","Only letters are allowed");
results.push(err);
trace("error")
}
return results;
}
OK the correct RE is ^[a-zA-Z _-]*[a-zA-Z][a-zA-Z _-]*$

Resources