1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author stev leibelt <[email protected]> |
4
|
|
|
* @since 2014-05-24 |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
require_once 'AbstractExample.php'; |
8
|
|
|
require_once __DIR__ . '/../vendor/autoload.php'; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class ClassExample |
12
|
|
|
* @package Net\Bazzline\Component\CodeGenerator\Example |
13
|
|
|
*/ |
14
|
|
|
class ClassExample extends AbstractExample |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @return string |
18
|
|
|
*/ |
19
|
|
|
function demonstrate() |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
$blockFactory = $this->getBlockGeneratorFactory(); |
22
|
|
|
$classFactory = $this->getClassGeneratorFactory(); |
23
|
|
|
$constantFactory = $this->getConstantGeneratorFactory(); |
24
|
|
|
$documentationFactory = $this->getDocumentationGeneratorFactory(); |
25
|
|
|
$methodFactory = $this->getMethodGeneratorFactory(); |
26
|
|
|
$propertyFactory = $this->getPropertyGeneratorFactory(); |
27
|
|
|
$traitFactory = $this->getTraitGeneratorFactory(); |
28
|
|
|
|
29
|
|
|
$myConstant = $constantFactory->create(); |
30
|
|
|
$myConstant->setName('MY_CONSTANT'); |
31
|
|
|
$myConstant->setValue('foobar'); |
32
|
|
|
|
33
|
|
|
$myProperty = $propertyFactory->create(); |
34
|
|
|
$myProperty->setDocumentation($documentationFactory->create()); |
35
|
|
|
$myProperty->markAsProtected(); |
36
|
|
|
$myProperty->setName('myProperty'); |
37
|
|
|
$myProperty->setValue(12345678.90); |
38
|
|
|
$myProperty->addTypeHint('float'); |
39
|
|
|
|
40
|
|
|
$myMethod = $methodFactory->create(); |
41
|
|
|
$myMethod->setDocumentation($documentationFactory->create()); |
42
|
|
|
$myMethod->markAsPublic(); |
43
|
|
|
$myMethod->markAsFinal(); |
44
|
|
|
$myMethod->setName('myMethod'); |
45
|
|
|
$myMethod->addParameter('foo', null, 'Foo'); |
46
|
|
|
$myMethod->addParameter('bar', 'null', 'Bar'); |
47
|
|
|
$myMethodBody = $blockFactory->create(); |
48
|
|
|
$myMethodBody |
49
|
|
|
->add('$foobar = $foo->toString();') |
50
|
|
|
->add('') |
51
|
|
|
->add('if (!is_null($bar)) {') |
52
|
|
|
->startIndention() |
53
|
|
|
->add('$foobar .= $bar->toString();') |
54
|
|
|
->stopIndention() |
55
|
|
|
->add('}') |
56
|
|
|
->add('') |
57
|
|
|
->add('return $foobar'); |
58
|
|
|
$myMethod->setBody($myMethodBody, 'string'); |
|
|
|
|
59
|
|
|
|
60
|
|
|
$myTrait = $traitFactory->create(); |
61
|
|
|
$myTrait->setDocumentation($documentationFactory->create()); |
62
|
|
|
$myTrait->setName('myTrait'); |
63
|
|
|
|
64
|
|
|
$myClass = $classFactory->create(); |
65
|
|
|
$myClass->setDocumentation($documentationFactory->create()); |
66
|
|
|
$myClass->setNamespace('My\Namespace'); |
67
|
|
|
$myClass->setName('MyClass'); |
68
|
|
|
$myClass->markAsFinal(); |
69
|
|
|
$myClass->setExtends('Foo\Bar', true); |
70
|
|
|
$myClass->addImplements('Bar\FooInterface', true); |
71
|
|
|
$myClass->addConstant($myConstant); |
72
|
|
|
$myClass->addMethod($myMethod); |
73
|
|
|
$myClass->addProperty($myProperty); |
74
|
|
|
$myClass->addTrait($myTrait); |
75
|
|
|
$myClass->getDocumentation()->setAuthor('stev leibelt', '[email protected]'); |
|
|
|
|
76
|
|
|
$myClass->getDocumentation()->setVersion('0.8.15', 'available since 2014-05-24'); |
|
|
|
|
77
|
|
|
|
78
|
|
|
echo $myClass->generate() . PHP_EOL; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$example = new ClassExample(); |
83
|
|
|
$example->demonstrate(); |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.