2017 © Pedro Peláez
 

library nitria

PHP 7 Code Generator

image

gm314/nitria

PHP 7 Code Generator

  • Thursday, May 3, 2018
  • by gperler
  • Repository
  • 1 Watchers
  • 1 Stars
  • 61 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 10 Versions
  • 11 % Grown

The README.md

Nitria PHP7 Code Generator

Build Status License Latest Stable Version Total Downloads Latest Unstable Version, (*1)

Class generator for PHP7 code. It will take care of indentation, use statements, php doc creation., (*2)

Installation

{
    "require": {
        "gm314/nitria": "*"
    }
}

Usage

Create a class and set extends and implements, (*3)


$classGenerator = new ClassGenerator("Generated\\MyClass", true); // add extends statement, a use statement will be added automatically $classGenerator->setExtends("BaseClass\\ClassName"); // add implement statement $classGenerator->addImplements("\\Serializable"); // add a constant $classGenerator->addConstant("CONSTANT_STRING", '"hello"');

Properties

Properties have a name, a type, a modifier and an optional default value. The example will generate a static private property with the name myName and the default value 19.08., (*4)

 $classGenerator->addProperty("myName","float","private", false, '19.08', 'doc bloc comment');

This will result in the following code, (*5)

    /**
     * @var float doc bloc comment
     */
    private $myName = 19.08;

Or you can use the short version for non static properties., (*6)

// add property (for classes use statement will be added - unless the class is in the same namespace)
$classGenerator->addPrivateProperty("iAmPrivat", 'MyPackage\MyClass');
$classGenerator->addProtectedProperty("iAmProtected", "array", []);
$classGenerator->addPublicProperty("iAmPublic", "float");

And for the static properties., (*7)

// add static property
$classGenerator->addPrivateStaticProperty("iAmPrivatStatic", "int");
$classGenerator->addProtectedStaticProperty("iAmProtectedStatic", "bool");
$classGenerator->addPublicStaticProperty("iAmPublicStatic", "array");

Methods

$method = $classGenerator->addPublicMethod("myFunction");
$method->addParameter("string", "parameterName", '"defaultValue!"');
$method->addParameter("\\DateTime", "datetime");

// the method will have a return type string that is not nullable
$method->setReturnType("string", false);
$method->addCodeLine('return $parameterName;');

the above code will generate the following method, (*8)


/** * @param string $parameterName * @param \DateTime $datetime * @return string */ public function myFunction(string $parameterName = "defaultValue!", \DateTime $datetime) : string { return $parameterName; }
// method generation
$classGenerator->addPrivateMethod("iAmPrivate");
$classGenerator->addProtectedMethod("iAmProtected");
$classGenerator->addPublicMethod("iAmPublic");

// static method generation
$classGenerator->addPrivateStaticMethod("iAmPrivateStatic");
$classGenerator->addProtectedStaticMethod("iAmProtectedStatic");
$classGenerator->addPublicStaticMethod("iAmPublicStatic");

Method Content generation

Code

$method = $classGenerator->addPublicMethod("sayIf");
$method->addParameter("int", "intParam");
$method->setReturnType("int", false);

// add a simple line of code
$method->addCodeLine('return $intParam * $intParam;');

If Statement

$method = $classGenerator->addPublicMethod("sayIf");
$method->addParameter("int", "int");
$method->setReturnType("int", false);

// start if statement >> if ($int ===1) {
$method->addIfStart('$int === 1');
$method->addCodeLine('return 1;');

// add if else statement >> } else if ($int === 2) {
$method->addIfElseIf('$int === 2');
$method->addCodeLine('return 2;');

// add else statement >> } else {
$method->addIfElse();
$method->addCodeLine('return 3;');

// close if statement >> }
$method->addIfEnd();

While Statement

$method = $classGenerator->addPublicMethod("sayWhile");
$method->addParameter("int", "int");
$method->setReturnType("string", false);

$method->addCodeLine('$string = "";');

// start while statement >> while($int++ < 10) {
$method->addWhileStart('$int++ < 10');
$method->addCodeLine('$string .= "x";');

// end while statement >> }
$method->addWhileEnd();
$method->addCodeLine('return $string;');

Foreach Statement

$method = $classGenerator->addPublicMethod("sayForeach");
$method->addParameter("array", "list");
$method->setReturnType("string", false);

$method->addCodeLine('$string = "";');

// start foreach >> foreach($list as $item) {
$method->addForeachStart('$list as $item');
$method->addCodeLine('$string .= $item;');

// end foreach >> }
$method->addForeachEnd();
$method->addCodeLine('return $string;');

Switch Statement

$method = $classGenerator->addPublicMethod("saySwitch");
$method->addParameter("string", "value");
$method->setReturnType("string", false);

// start switch statement >> switch($value) {
$method->addSwitch('$value');

// case statement >> case "a":
$method->addSwitchCase('"a"');
$method->addCodeLine('return "a";');

// case break >> break;
$method->addSwitchBreak();

// default >> default:
$method->addSwitchDefault();
$method->addCodeLine('return "c";');
$method->addSwitchBreak();

$method->addSwitchEnd();

Tests / More examples

See under tests/End2End * GeneratorTest * CodeTest, (*9)

License

MIT, (*10)

The Versions

03/05 2018

dev-master

9999999-dev

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregor Müller

03/05 2018

0.1.8

0.1.8.0

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregor Müller

29/01 2018

0.1.7

0.1.7.0

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregor Müller

29/03 2017

0.1.6

0.1.6.0

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregor Müller

28/03 2017

0.1.5

0.1.5.0

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregor Müller

22/03 2017

0.1.4

0.1.4.0

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregor Müller

02/03 2017

0.1.3

0.1.3.0

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

 

The Development Requires

by Gregor Müller

28/02 2017

0.1.2

0.1.2.0

PHP 7 Code Generator

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Gregor Müller

28/02 2017

0.1.1

0.1.1.0

php7 class generator

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Gregor Müller

27/02 2017

0.1.0

0.1.0.0

php7 class generator

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Gregor Müller