Skip to content

Repository files navigation

Classidy

Latest Version on PackagistSoftware LicenseBuild StatusCoverage StatusTotal Downloads

A package that creates PHP classes using PHP. That's it.

useRougin\Classidy\Classidy;
useRougin\Classidy\Generator;
useRougin\Classidy\Method;
$class = newClassidy;
$class->setName('Hello');
$method = newMethod('hi');
$method->setCodeEval(function ()
{
return'Hello world!';
});
$class->addMethod($method);
$maker = newGenerator;
echo$maker->make($class);

Installation

Install Classidy through Composer:

$ composer require rougin/classidy

Basic usage

Creating a simple class

Creating a PHP class only requires the Classidy and Generator classes:

// index.phpuseRougin\Classidy\Classidy;
useRougin\Classidy\Generator;
useRougin\Classidy\Method;
// ...// Create a new class definition ---$class = newClassidy;
// ---------------------------------// Define the details of the class ------------$class->setComment('Sample class for Acme.');
$class->setNamespace('Acme');
$class->setPackage('Acme');
$class->setAuthor('John Doe', 'jdoe@acme.com');
$class->setName('Greet');
// --------------------------------------------// Add a "greet" method in the class ---$method = newMethod('greet');
$method->setCodeEval(function ()
{
return'Hello world!';
});
$class->addMethod($method);
// -------------------------------------// Generate the class --------$generator = newGenerator;
echo$generator->make($class);
// ---------------------------
$ php index.php
<?php
namespace Acme;
/*** Sample class for Acme.
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet
{
public functiongreet()
{
return'Hello world!';
}
}

The setCodeLine method can also be used for specifying code of a method in a line based format. This may be useful in adding conditions in generating code of a method:

// index.php// ...$shout = false;
$method->setCodeLine(function ($lines) use ($shout)
{
if ($shout)
{
$lines[] = "return 'HELLO WORLD!';";
}
else
{
$lines[] = "return 'Hello world!';";
}
return$lines;
});
// ...

Adding parent class, interfaces

The class can be added with a parent class using extendsTo:

// index.phpuseAcme\Hello\Greeter;
// ...// Define the details of the class ---// ...$class->extendsTo(Greeter::class);
// ...// -----------------------------------// ...
$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/*** Sample class for Acme.
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet extends Greeter
{
public functiongreet()
{
return'Hello world!';
}
}

Note

If the added parent class or interface is not from the same namespace of the class to be generated, Classidy will automatically import the said parent class/interface.

For adding interfaces, the addInterface method can be used:

// index.phpuseAcme\Greetable;
useAcme\Helloable;
// ...// Define the details of the class ----// ...$class->addInterface(Greetable::class);
$class->addInterface(Helloable::class);
// ...// ------------------------------------// ...
$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/*** Sample class for Acme.
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet extends Greeter implements Greetable, Helloable
{
public functiongreet()
{
return'Hello world!';
}
}

Adding traits

Similar in defining class and interfaces, adding a trait is possible using addTrait:

// index.phpuseAcme\Hello\Traitable;
// ...// Define the details of the class ---// ...$class->addTrait(Traitable::class);
// ...// -----------------------------------// ...
$ php index.php
<?php
namespace Acme;
use Acme\Hello\Traitable;
/*** Sample class for Acme.
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet
{
use Traitable;
public functiongreet()
{
return'Hello world!';
}
}

Adding methods

Based from the first example, the addMethod can be used to add a method to the class:

// index.php// ...// Add a "greet" method in the class ---$method = newMethod('greet');
$method->setCodeEval(function ()
{
return'Hello world!';
});
$class->addMethod($method);
// -------------------------------------// ...

To add arguments in a specified method, kindy use the following methods below:

MethodDescription
addArrayArgumentAdds a property with a array as its data type.
addBooleanArgumentAdds an argument with a boolean as its data type.
addClassArgumentAdds an argument with the specified class.
addFloatArgumentAdds an argument with a float as its data type.
addIntegerArgumentAdds an argument with an integer as its data type.
addStringArgumentAdds an argument with a string as its data type.
// index.php// ...$method = newMethod('greet');
$method->addBooleanArgument('shout')
->withDefaultValue(false);
$method->setReturn('string');
$method->setCodeEval(function ()
{
return'Hello world!';
});
$class->addMethod($method);
// ...
$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/*** Sample class for Acme.
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet extends Greeter implements Greetable, Helloable
{
/*** @param boolean $shout** @return string
*/
public functiongreet($shout = false)
{
return'Hello world!';
}
}

To add a class argument without being its type declared, add withoutTypeDeclared after addClassArgument:

// index.php// ...$method = newMethod('greet');
$method->addClassArgument('test', 'Acme\Test')
->withoutTypeDeclared();
$method->setReturn('string');
$method->setCodeEval(function ($test)
{
return$test->hello();
});
$class->addMethod($method);
// ...
$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/*** Sample class for Acme.
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet extends Greeter implements Greetable, Helloable
{
/*** @param \Acme\Test $test** @return string
*/
public functiongreet($test)
{
return$test->hello();
}
}

A method can also be defined as protected or private:

// index.php// ...$method = newMethod('greet');
// Set the method as "protected" ---$method->asProtected();
// ---------------------------------// Set the method as "private" ---$method->asPrivate();
// -------------------------------// ...

Note

By default, all of the specified methods are in public visibility.

The method can be alternatively be specified as a @method tag in the class:

// index.php// ...$method = newMethod('greet');
// ...// Set as "@method" in the class ---$method->asTag();
// ---------------------------------// ...
$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/*** Sample class for Acme.
** @method string greet(boolean $shout = false)
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet extends Greeter implements Greetable, Helloable
{
}

Adding properties

Similiar to adding arguments in a method, adding properties to a class can be done by the following:

MethodDescription
addArrayPropertyAdds a property with a array as its data type.
addBooleanPropertyAdds a property with a boolean as its data type.
addClassPropertyAdds a property with the specified class.
addFloatPropertyAdds a property with a float as its data type.
addIntegerPropertyAdds a property with an integer as its data type.
addStringPropertyAdds a property with a string as its data type.
// index.php// ...$class->addStringProperty('text')
->withDefaultValue('Hello world!');
// ...// Modify "greet" method to access "text" property ---$method->setCodeEval(function ()
{
return$this->text;
});
// ---------------------------------------------------// ...
$ php index.php
<?php
namespace Acme;
useAcme\Hello\Greeter;
/** * Sample class for Acme. * * @package Acme * * @author John Doe <jdoe@acme.com> */class Greet extends Greeter implements Greetable, Helloable
{
/** * @var string */protected$text = 'Hello world!';
/** * @param boolean $shout * * @return string */publicfunctiongreet($shout = false)
{
return$this->text;
}
}

To change a visibility of a property, the methods asPublic and asPrivate can be used:

// index.php// ...$class->addStringProperty('text')
->asPrivate()
->withDefaultValue('Hello world!');
// ...

Note

By default, all of the specified properties are in protected visibility.

Alternatively, the property be specified as a @property tag in the class:

// index.php// ...$class->addStringProperty('text')->asTag();
// ...
$ php index.php
<?php
namespace Acme;
use Acme\Hello\Greeter;
/*** @property string $text** Sample class for Acme.
** @package Acme
** @author John Doe <jdoe@acme.com>*/
class Greet extends Greeter implements Greetable, Helloable
{
}

Setting an empty class

The setEmpty method can be used to clear any methods and properties previously specified. This maybe useful when generating empty classes without specifying a new Classidy class:

// index.php// ...$class->setEmpty();
// ...
$ php index.php
<?php
namespace Acme;
useAcme\Hello\Greeter;
/** * Sample class for Acme. * * @package Acme * * @author John Doe <jdoe@acme.com> */class Greet extends Greeter implements Greetable, Helloable
{
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

See CONTRIBUTING on how to contribute.

License

The MIT License (MIT). Please see LICENSE for more information.

About

Create PHP classes using PHP.

Topics

Resources

Contributing

Stars

0 stars

Watchers

1 watching

Forks

Releases

Used by

Contributors

Languages