PHP code example of darkdarin / php-entity-renderer
1. Go to this page and download the library: Download darkdarin/php-entity-renderer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
darkdarin / php-entity-renderer example snippets
// Return new type renderer with nullable type
$nullableTypeRenderer = $typeRenderer->setNullable();
// Check current type is nullable
$typeRenderer->isNullable();
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = BuiltinTypeRenderer::Float;
echo $renderer->render(new EntityAliases());
echo $renderer->renderDocBlock(new EntityAliases());
use DarkDarin\PhpEntityRenderer\Renderers\Types\ClassTypeRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new ClassTypeRenderer('\\App\\MyClass');
echo $renderer->render(new EntityAliases());
echo $renderer->renderDocBlock(new EntityAliases());
use DarkDarin\PhpEntityRenderer\Renderers\Types\ArrayTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new ArrayTypeRenderer(BuiltinTypeRenderer::String);
echo $renderer->render(new EntityAliases());
echo $renderer->renderDocBlock(new EntityAliases());
use DarkDarin\PhpEntityRenderer\Renderers\Types\NullableTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new NullableTypeRenderer(BuiltinTypeRenderer::String);
echo $renderer->render(new EntityAliases());
echo $renderer->renderDocBlock(new EntityAliases());
use DarkDarin\PhpEntityRenderer\Renderers\Types\IntersectTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\ClassTypeRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new IntersectTypeRenderer(
new ClassTypeRenderer('\\App\\MyInterfaceOne'),
new ClassTypeRenderer('\\App\\MyInterfaceTwo')
);
echo $renderer->render(new EntityAliases());
echo $renderer->renderDocBlock(new EntityAliases());
use DarkDarin\PhpEntityRenderer\Renderers\Types\UnionTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\ClassTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new UnionTypeRenderer(
new ClassTypeRenderer('\\App\\MyClass'),
BuiltinTypeRenderer::Int
);
echo $renderer->render(new EntityAliases());
echo $renderer->renderDocBlock(new EntityAliases());
public function setInheritanceModifier(InheritanceModifierEnum $inheritanceModifier): self
public function getInheritanceModifier(): ?InheritanceModifierEnum
InheritanceModifierEnum::Abstract
InheritanceModifierEnum::Final
public function setReadonly(bool $isReadonly = true): self
public function isReadonly(): bool
public function setVisibilityModifier(?VisibilityModifierEnum $visibilityModifier = null): self
public function getVisibilityModifier(): ?VisibilityModifierEnum
VisibilityModifierEnum::Public
VisibilityModifierEnum::Protected
VisibilityModifierEnum::Private
public function setStatic(bool $isStatic = true): self
public function isStatic(): bool
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$myValue = [
'some' => 'foo',
'different' => 'bar',
];
$renderer = new ValueRenderer($myValue);
echo $renderer->render(new EntityAliases());
[
'some' => 'foo',
'different' => 'bar',
]
use DarkDarin\PhpEntityRenderer\Renderers\ConstantRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new ConstantRenderer('MY_CONST', new ValueRenderer(2345));
echo $renderer->render(new EntityAliases());
const MY_CONST = 2345;
use DarkDarin\PhpEntityRenderer\Renderers\DocBlockRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new DocBlockRenderer();
$renderer->addLine('My comment');
$renderer->addLine('@param int $foo My param');
echo $renderer->render(new EntityAliases());
/**
* My comment
* @param int $foo My param
*/
use DarkDarin\PhpEntityRenderer\Renderers\AttributeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new AttributeRenderer('\\App\\MyAttribute', ['foo' => new ValueRenderer('bar')]);
echo $renderer->render(new EntityAliases());
#[MyAttribute(foo: 'bar')]
use DarkDarin\PhpEntityRenderer\Renderers\PropertyRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new PropertyRenderer('myProperty', BuiltinTypeRenderer::Int);
$renderer->setDefault(new ValueRenderer(567));
echo $renderer->render(new EntityAliases());
public int $myProperty = 567;
use DarkDarin\PhpEntityRenderer\Renderers\ParameterRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new ParameterRenderer('myParameter', BuiltinTypeRenderer::Int);
$renderer->setDefault(new ValueRenderer(567));
echo $renderer->render(new EntityAliases());
int $myParameter = 567
use DarkDarin\PhpEntityRenderer\Renderers\MethodRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ParameterRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
use DarkDarin\PhpEntityRenderer\Enums\VisibilityModifierEnum;
$parameterRenderer = new ParameterRenderer('myParameter', BuiltinTypeRenderer::Int);
$parameterRenderer->setDefault(new ValueRenderer(567));
$renderer = new MethodRenderer('myMethod');
$renderer->setVisibilityModifier(VisibilityModifierEnum::Public);
$renderer->addParameter($parameterRenderer);
echo $renderer->render(new EntityAliases());
/**
* @param int $myParameter
*/
public function myMethod(int $myParameter = 567)
{}
use DarkDarin\PhpEntityRenderer\Renderers\ClassRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\MethodRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ParameterRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
use DarkDarin\PhpEntityRenderer\Enums\VisibilityModifierEnum;
$parameterRenderer = new ParameterRenderer('myParameter', BuiltinTypeRenderer::Int);
$parameterRenderer->setDefault(new ValueRenderer(567));
$methodRenderer = new MethodRenderer('myMethod');
$methodRenderer->setVisibilityModifier(VisibilityModifierEnum::Public);
$methodRenderer->addParameter($parameterRenderer);
$renderer = new ClassRenderer('\\App\\MyClassName');
$renderer->setReadonly();
$renderer->addMethod($methodRenderer);
echo $renderer->render(new EntityAliases());
namespace App;
readonly class MyClassName
{
/**
* @param int $myParameter
*/
public function myMethod(int $myParameter = 567)
{}
}
use DarkDarin\PhpEntityRenderer\Renderers\TraitRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\MethodRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ParameterRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
use DarkDarin\PhpEntityRenderer\Enums\VisibilityModifierEnum;
$parameterRenderer = new ParameterRenderer('myParameter', BuiltinTypeRenderer::Int);
$parameterRenderer->setDefault(new ValueRenderer(567));
$methodRenderer = new MethodRenderer('myMethod');
$methodRenderer->setVisibilityModifier(VisibilityModifierEnum::Public);
$methodRenderer->addParameter($parameterRenderer);
$renderer = new TraitRenderer('\\App\\MyTraitName');
$renderer->addMethod($methodRenderer);
echo $renderer->render(new EntityAliases());
namespace App;
trait MyTraitName
{
/**
* @param int $myParameter
*/
public function myMethod(int $myParameter = 567)
{}
}
use DarkDarin\PhpEntityRenderer\Renderers\InterfaceRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\MethodRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ParameterRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
use DarkDarin\PhpEntityRenderer\Enums\VisibilityModifierEnum;
$parameterRenderer = new ParameterRenderer('myParameter', BuiltinTypeRenderer::Int);
$parameterRenderer->setDefault(new ValueRenderer(567));
$methodRenderer = new MethodRenderer('myMethod');
$methodRenderer->setVisibilityModifier(VisibilityModifierEnum::Public);
$methodRenderer->addParameter($parameterRenderer);
$renderer = new InterfaceRenderer('\\App\\MyInterfaceName');
$renderer->addMethod($methodRenderer);
echo $renderer->render(new EntityAliases());
namespace App;
interface MyInterfaceName
{
/**
* @param int $myParameter
*/
public function myMethod(int $myParameter = 567);
}
use DarkDarin\PhpEntityRenderer\Renderers\EnumCaseRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
$renderer = new EnumCaseRenderer('MyCase', new ValueRenderer('case_value'));
echo $renderer->render(new EntityAliases());
case MyCase = 'case_value';
use DarkDarin\PhpEntityRenderer\Renderers\EnumRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\EnumCaseRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\MethodRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ParameterRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\Types\BuiltinTypeRenderer;
use DarkDarin\PhpEntityRenderer\Renderers\ValueRenderer;
use DarkDarin\PhpEntityRenderer\EntityAliases;
use DarkDarin\PhpEntityRenderer\Enums\VisibilityModifierEnum;
$parameterRenderer = new ParameterRenderer('myParameter', BuiltinTypeRenderer::Int);
$parameterRenderer->setDefault(new ValueRenderer(567));
$methodRenderer = new MethodRenderer('myMethod');
$methodRenderer->setVisibilityModifier(VisibilityModifierEnum::Public);
$methodRenderer->addParameter($parameterRenderer);
$enumFooCase = new EnumCaseRenderer('Foo', new ValueRenderer('foo'));
$enumBarCase = new EnumCaseRenderer('Bar', new ValueRenderer('bar'));
$renderer = new EnumRenderer('\\App\\MyEnumName');
$renderer->addMethod($methodRenderer);
$renderer->addCase($enumFooCase);
$renderer->addCase($enumBarCase);
echo $renderer->render(new EntityAliases());
namespace App;
enum MyEnumName
{
case Foo = 'foo';
case Bar = 'bar';
/**
* @param int $myParameter
*/
public function myMethod(int $myParameter = 567)
{}
}
bash
composer
array
list<string>
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.