File templates: make a PHP variable with template variable case conversion

Answered

Hi,

i'm trying to make a file template for a generating a model. I would like to use the ${NAME} variable and in some places convert it to camel case.

I would like to create something like this, where the protected variable has the same name as the model but in camel case:

<?php
declare(strict_types=1);

namespace App\Model;

/**
* Class TestModel
* @package App\Model
*
* @property int TestModelID
*
*/
class TestModel extends AbstractModel
{
protected $testModel = null;
}

I didn't find anything online, the closest to my problem is this post but I don't understand the "answer". I installed the String manipulation plugin and tried to set a $CAMEL_CASE variable as David Rees has written in the post I linked but with no success. I also tried to use `uncapitalize` string manipulation function but it doesn't work:

My file template:

<?php
declare(strict_types=1);

namespace App\Model;

#set($CAMEL_NAME = ${StringUtils.uncapitalize(${NAME})})

/**
* Class ${NAME}
* @package App\Model
*
* @property int ${NAME}ID
*
*/
class ${NAME} extends AbstractModel
{
/** @var ${NAME} */
protected ${CAMEL_NAME} = null;
}

 

Generated output:

<?php
declare(strict_types=1);

namespace App\Model;


/**
* Class TestModel
* @package App\Model
*
* @property int TestModelID
*
*/
class TestModel extends AbstractModel
{
/** @var TestModel */
protected ${
CAMEL_NAME
} = null;
}


Can you please tell me what I'm doing wrong and how to solve my problem.

Recap: I would like to make a PHP variable with camel case from a template variable ${NAME}.

 

Thanks

0
2 comments

Hi there,

  1. String Manipulation plugin has NOTHING to do here -- it's for string manipulations in the actual Editor via GUI (context menu/shortcuts).
  2. It's possible that StringUtils is not available in PhpStorm and only in IntelliJ IDEA...
  3. You can use String instead -- a bit more work but it works:
<?php
declare(strict_types=1);

namespace App\Model;

#set($class = ${NAME})
#set($class_start = $class.substring(0,1).toLowerCase())
#set($class_rest = $class.substring(1))
/**
 * Class ${NAME}
 * @package App\Model
 *
 * @property int ${NAME}ID
 *
 */
class ${NAME} extends AbstractModel
{
 /** @var ${NAME} */
 protected ${DS}${class_start}${class_rest} = null;
}

Here we have separate variables for 1st letter (which we need to manipulate: make it lower case) and the rest of the name.

2
Avatar
Permanently deleted user

Thank you, this is what I was looking for.

I created another variable from concatenated first letter and the rest of the word so it's a little less writing :)

#set($camel_case = $class_start.concat($class_rest))
0

Please sign in to leave a comment.