Skip to content

Commit 1bb0c31

Browse files
fattouchsquallsaimaz
authored andcommitted
Add visibility property when generating a document. (#784)
* Add visibility property when generating a document + fix default type for property. * Add public visibility for generation of documents. * Fix unit test by adding visibility in generate service test.
1 parent c3c53f1 commit 1bb0c31

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

Command/DocumentGenerateCommand.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ class DocumentGenerateCommand extends AbstractManagerAwareCommand
3232
*/
3333
private $propertyAnnotations;
3434

35+
/**
36+
* @var string[]
37+
*/
38+
private $propertyVisibilities;
39+
3540
/**
3641
* {@inheritdoc}
3742
*/
@@ -165,6 +170,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
165170
continue;
166171
}
167172

173+
$this->propertyVisibilities = ['private', 'protected', 'public'];
174+
$output->writeln($this->getOptionsLabel($this->propertyVisibilities, 'Available visibilities'));
175+
$property['visibility'] = $this->questionHelper->ask(
176+
$input,
177+
$output,
178+
$this->getQuestion(
179+
'Property visibility',
180+
'private',
181+
[$this, 'validatePropertyVisibility'],
182+
$this->propertyVisibilities
183+
)
184+
);
185+
168186
$output->writeln($this->getOptionsLabel($this->propertyAnnotations, 'Available annotations'));
169187
$property['annotation'] = $this->questionHelper->ask(
170188
$input,
@@ -210,7 +228,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
210228
$output,
211229
$this->getQuestion(
212230
'Property type',
213-
'string',
231+
'text',
214232
[$this, 'validatePropertyType'],
215233
$this->getPropertyTypes()
216234
)
@@ -515,6 +533,26 @@ public function validatePropertyAnnotation($annotation)
515533
return $annotation;
516534
}
517535

536+
/**
537+
* Validates property visibility
538+
*
539+
* @param string $visibility
540+
*
541+
* @return string
542+
* @throws \InvalidArgumentException When the visibility is not found in the list of allowed ones.
543+
*/
544+
public function validatePropertyVisibility($visibility)
545+
{
546+
if (!in_array($visibility, $this->propertyVisibilities)) {
547+
throw $this->getException(
548+
'The property visibility isn\'t valid ("%s" given, expecting one of following: %s)',
549+
[$visibility, implode(', ', $this->propertyVisibilities)]
550+
);
551+
}
552+
553+
return $visibility;
554+
}
555+
518556
/**
519557
* Returns formatted question
520558
*

Generator/DocumentGenerator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ private function generateDocumentProperties(array $metadata)
142142

143143
foreach ($metadata['properties'] as $property) {
144144
$lines[] = $this->generatePropertyDocBlock($property);
145-
$lines[] = $this->spaces . 'private $' . $property['field_name'] . ";\n";
145+
$lines[] = $this->spaces . $property['visibility'] . ' $' . $property['field_name'] . ";\n";
146146
}
147147

148148
return implode("\n", $lines);
@@ -160,6 +160,9 @@ private function generateDocumentMethods(array $metadata)
160160
$lines = [];
161161

162162
foreach ($metadata['properties'] as $property) {
163+
if (isset($property['visibility']) && $property['visibility'] === 'public') {
164+
continue;
165+
}
163166
$lines[] = $this->generateDocumentMethod($property, $this->setMethodTemplate) . "\n";
164167
if (isset($property['property_type']) && $property['property_type'] === 'boolean') {
165168
$lines[] = $this->generateDocumentMethod($property, $this->isMethodTemplate) . "\n";

Tests/Unit/Service/GenerateServiceTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ public function testGenerate()
5050
[
5151
'field_name' => 'test',
5252
'annotation' => 'property',
53+
'visibility' => 'private',
5354
'property_type' => 'string',
5455
'property_name' => 'testProperty',
5556
'property_options' => 'test',
5657
],
5758
[
5859
'field_name' => 'embedded',
60+
'visibility' => 'protected',
5961
'annotation' => 'embedded',
6062
'property_class' => 'TestBundle:Product',
6163
'property_multiple' => true,

0 commit comments

Comments
 (0)