Skip to content

Commit 2c6d374

Browse files
committed
Merge branch 'release/1.2'
2 parents efcbbae + 7b5adda commit 2c6d374

File tree

9 files changed

+126
-13
lines changed

9 files changed

+126
-13
lines changed

src/Application/Shared/Constant.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66

77
class Constant
88
{
9-
public const VERSION = '1.1.0';
9+
public const VERSION = '1.2.0';
1010
}

src/Domain/Definition/ObjectDefinition.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Morebec\ObjectGenerator\Domain\Definition;
44

55
use Morebec\ObjectGenerator\Domain\Definition\Schema\SchemaKey;
6+
use mysql_xdevapi\Schema;
67

78
class ObjectDefinition
89
{
@@ -231,7 +232,7 @@ public function setUse(array $use): void
231232
$this->use = $use;
232233
}
233234

234-
public function addUse(string $use): void
235+
public function addUse(UseDefinition $use): void
235236
{
236237
$this->use[] = $use;
237238
}
@@ -271,11 +272,17 @@ public static function createFromArray(string $objectName, array $data)
271272
$definition->setImplements($data[SchemaKey::IMPLEMENTS]);
272273
$definition->setTraits($data[SchemaKey::TRAITS]);
273274
$definition->setAnnotations($data[SchemaKey::ANNOTATIONS]);
274-
$definition->setUse($data[SchemaKey::USE]);
275275
$definition->setAbstract($data[SchemaKey::ABSTRACT]);
276276
$definition->setFinal($data[SchemaKey::FINAL]);
277277
$definition->setType($data[SchemaKey::TYPE]);
278278

279+
// Use
280+
foreach ($data[SchemaKey::USE] as $use) {
281+
$definition->addUse(
282+
new UseDefinition($use[SchemaKey::CLASS_NAME], $use[SchemaKey::AS])
283+
);
284+
}
285+
279286
// Properties
280287
foreach ($data[SchemaKey::PROPERTIES] as $propertyName => $property) {
281288
$definition->addProperty(

src/Domain/Definition/Schema/CodeDefinitionSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function getConfigTreeBuilder()
1515
$node = $treeBuilder->getRootNode();
1616
$node
1717
->info('You can specify some PHP code to be injected here.')
18-
->defaultValue(null)
18+
->defaultNull()
1919
->end()
2020
;
2121

src/Domain/Definition/Schema/ObjectDefinitionSchema.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getConfigTreeBuilder(): TreeBuilder
2828
$root
2929
->addDefaultsIfNotSet()
3030
->children()
31-
->scalarNode('namespace')->defaultValue(null)->end()
31+
->scalarNode('namespace')->defaultNull()->end()
3232
->append(FinalKeywordDefinitionSchema::create())
3333
->append(AbstractKeywordDefinitionSchema::create())
3434
->enumNode(SchemaKey::TYPE)
@@ -38,7 +38,7 @@ public function getConfigTreeBuilder(): TreeBuilder
3838
->scalarNode(SchemaKey::DESCRIPTION)
3939
->defaultValue('')
4040
->end()
41-
->scalarNode(SchemaKey::EXTENDS)->defaultValue(null)->end()
41+
->scalarNode(SchemaKey::EXTENDS)->defaultNull()->end()
4242
->arrayNode(SchemaKey::IMPLEMENTS)
4343
->beforeNormalization()->castToArray()->end()
4444
->scalarPrototype()->end()
@@ -50,10 +50,7 @@ public function getConfigTreeBuilder(): TreeBuilder
5050
->append(AnnotationsDefinitionSchema::create())
5151
->append(PropertiesDefinitionSchema::create())
5252
->append(MethodsDefinitionSchema::create())
53-
->arrayNode(SchemaKey::USE)
54-
->beforeNormalization()->castToArray()->end()
55-
->scalarPrototype()->end()
56-
->end()
53+
->append(UseKeywordDefinitionSchema::create())
5754
->end()
5855
;
5956

src/Domain/Definition/Schema/PropertiesDefinitionSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function getConfigTreeBuilder()
3737
->scalarNode(SchemaKey::NAME)
3838
->info('You can specify the name of your setter, here.
3939
If nothing is specified as a name, it will default to getPropertyName.')
40-
->defaultValue(null)->end()
40+
->defaultNull()->end()
4141
->scalarNode(SchemaKey::DESCRIPTION)->defaultValue('')->end()
4242
->append(VisibilityKeywordDefinitionSchema::create())
4343
->append(CodeDefinitionSchema::create())

src/Domain/Definition/Schema/SchemaKey.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ class SchemaKey
5555
public const CONSTANT = 'constant';
5656

5757
public const NAME = 'name';
58+
59+
const CLASS_NAME = 'classname';
60+
61+
const AS = 'as';
5862
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
4+
namespace Morebec\ObjectGenerator\Domain\Definition\Schema;
5+
6+
7+
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
8+
use function is_array;
9+
10+
class UseKeywordDefinitionSchema extends AbstractDefinitionSchema
11+
{
12+
13+
/**
14+
* @inheritDoc
15+
*/
16+
public function getConfigTreeBuilder()
17+
{
18+
$treeBuilder = new TreeBuilder(SchemaKey::USE);
19+
20+
21+
$node = $treeBuilder->getRootNode();
22+
23+
$node
24+
->beforeNormalization()
25+
->ifTrue(static function($v) {
26+
return !is_array($v);
27+
})
28+
->then(static function($v){
29+
return [[SchemaKey::CLASS_NAME => $v, SchemaKey::AS => null]];
30+
})
31+
->end()
32+
->arrayPrototype()
33+
->children()
34+
->scalarNode(SchemaKey::CLASS_NAME)->end()
35+
->scalarNode(SchemaKey::AS)->defaultNull()->end()
36+
->end()
37+
->end()
38+
;
39+
40+
return $node;
41+
}
42+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
4+
namespace Morebec\ObjectGenerator\Domain\Definition;
5+
6+
7+
class UseDefinition
8+
{
9+
/**
10+
* @var string
11+
*/
12+
private $className;
13+
/**
14+
* @var string|null
15+
*/
16+
private $as;
17+
18+
public function __construct(string $className, ?string $as = null)
19+
{
20+
$this->className = $className;
21+
$this->as = $as;
22+
}
23+
24+
public function __toString()
25+
{
26+
$use = $this->className;
27+
$as = $this->as;
28+
return $as ? "$use as $as" : $use;
29+
}
30+
31+
/**
32+
* @return string
33+
*/
34+
public function getClassName(): string
35+
{
36+
return $this->className;
37+
}
38+
39+
/**
40+
* @param string $className
41+
*/
42+
public function setClassName(string $className): void
43+
{
44+
$this->className = $className;
45+
}
46+
47+
/**
48+
* @return string|null
49+
*/
50+
public function getAs(): ?string
51+
{
52+
return $this->as;
53+
}
54+
55+
/**
56+
* @param string|null $as
57+
*/
58+
public function setAs(?string $as): void
59+
{
60+
$this->as = $as;
61+
}
62+
}

src/Domain/ObjectDumper.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Morebec\ObjectGenerator\Domain;
44

55
use Morebec\ObjectGenerator\Domain\Definition\ObjectDefinition;
6+
use Morebec\ObjectGenerator\Domain\Definition\UseDefinition;
67
use Nette\PhpGenerator\ClassType;
78

89
/**
@@ -22,8 +23,8 @@ public function dump(
2223
if ($namespace) {
2324
$namespace = "namespace $namespace;" . PHP_EOL . PHP_EOL;
2425
}
25-
26-
$use = join(PHP_EOL, array_map(static function ($n) {
26+
27+
$use = join(PHP_EOL, array_map(static function (UseDefinition $n) {
2728
return "use $n;";
2829
}, $definition->getUse()));
2930
if ($use) {

0 commit comments

Comments
 (0)