Skip to content

Commit 4baf84d

Browse files
committed
add Constant; class constants can have declared visibility and comment
1 parent 9127f1f commit 4baf84d

File tree

4 files changed

+96
-12
lines changed

4 files changed

+96
-12
lines changed

src/PhpGenerator/ClassType.php

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ClassType
5454
/** @var string|NULL */
5555
private $comment;
5656

57-
/** @var array name => value */
57+
/** @var Constant[] name => Constant */
5858
private $consts = [];
5959

6060
/** @var Property[] name => Property */
@@ -112,8 +112,10 @@ public function __construct($name = '', PhpNamespace $namespace = NULL)
112112
public function __toString()
113113
{
114114
$consts = [];
115-
foreach ($this->consts as $name => $value) {
116-
$consts[] = "const $name = " . Helpers::dump($value) . ";\n";
115+
foreach ($this->consts as $const) {
116+
$consts[] = Helpers::formatDocComment($const->getComment())
117+
. ($const->getVisibility() ? $const->getVisibility() . ' ' : '')
118+
. 'const ' . $const->getName() . ' = ' . Helpers::dump($const->getValue()) . ";\n";
117119
}
118120

119121
$properties = [];
@@ -392,36 +394,72 @@ public function addDocument($s)
392394

393395

394396
/**
397+
* @deprecated use setConstants()
395398
* @return static
396399
*/
397400
public function setConsts(array $consts)
398401
{
399-
$this->consts = $consts;
400-
return $this;
402+
return $this->setConstants($consts);
401403
}
402404

403405

404406
/**
407+
* @deprecated use getConstants()
405408
* @return array
406409
*/
407410
public function getConsts()
408411
{
409-
return $this->consts;
412+
return array_map(function ($const) { return $const->getValue(); }, $this->consts);
410413
}
411414

412415

413416
/**
417+
* @deprecated use addConstant()
414418
* @param string
415419
* @param mixed
416420
* @return static
417421
*/
418422
public function addConst($name, $value)
419423
{
420-
$this->consts[$name] = $value;
424+
$this->addConstant($name, $value);
421425
return $this;
422426
}
423427

424428

429+
/**
430+
* @return static
431+
*/
432+
public function setConstants(array $consts)
433+
{
434+
$this->consts = [];
435+
foreach ($consts as $k => $v) {
436+
$const = $v instanceof Constant ? $v : (new Constant($k))->setValue($v);
437+
$this->consts[$const->getName()] = $const;
438+
}
439+
return $this;
440+
}
441+
442+
443+
/**
444+
* @return Constant[]
445+
*/
446+
public function getConstants()
447+
{
448+
return $this->consts;
449+
}
450+
451+
452+
/**
453+
* @param string
454+
* @param mixed
455+
* @return Constant
456+
*/
457+
public function addConstant($name, $value)
458+
{
459+
return $this->consts[$name] = (new Constant($name))->setValue($value);
460+
}
461+
462+
425463
/**
426464
* @param Property[]
427465
* @return static

src/PhpGenerator/Constant.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Nette Framework (https://nette.org)
5+
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6+
*/
7+
8+
namespace Nette\PhpGenerator;
9+
10+
use Nette;
11+
12+
13+
/**
14+
* Class constant.
15+
*/
16+
class Constant extends Member
17+
{
18+
/** @var mixed */
19+
private $value;
20+
21+
22+
/**
23+
* @return static
24+
*/
25+
public function setValue($val)
26+
{
27+
$this->value = $val;
28+
return $this;
29+
}
30+
31+
32+
/**
33+
* @return mixed
34+
*/
35+
public function getValue()
36+
{
37+
return $this->value;
38+
}
39+
40+
}

tests/PhpGenerator/ClassType.expect

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ abstract final class Example extends ParentClass implements IExample, IOne
99
use ObjectTrait;
1010

1111
const ROLE = 'admin';
12-
const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
12+
const ACTIVE = FALSE;
13+
/** Commented */
14+
private const FORCE_ARRAY = Nette\Utils\Json::FORCE_ARRAY;
1315

1416
/** @var resource orignal file handle */
1517
private $handle;

tests/PhpGenerator/ClassType.phpt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ $class
2121
->addImplement('IOne')
2222
->addTrait('ObjectTrait')
2323
->addComment("Description of class.\nThis is example\n")
24-
->addComment('@property-read Nette\Forms\Form $form');
24+
->addComment('@property-read Nette\Forms\Form $form')
25+
->setConsts(['ROLE' => 'admin'])
26+
->addConst('ACTIVE', FALSE);
2527

26-
$class
27-
->addConst('ROLE', 'admin')
28-
->addConst('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY'));
28+
Assert::same(['ROLE' => 'admin', 'ACTIVE' => FALSE], $class->getConsts());
29+
30+
$class->addConstant('FORCE_ARRAY', new PhpLiteral('Nette\Utils\Json::FORCE_ARRAY'))
31+
->setVisibility('private')
32+
->addComment('Commented');
2933

3034
$class->addProperty('handle')
3135
->setVisibility('private')

0 commit comments

Comments
 (0)