Skip to content

Commit

Permalink
Add visibility property when generating a document. (#784)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
fattouchsquall authored and saimaz committed Apr 14, 2017
1 parent c3c53f1 commit 1bb0c31
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
40 changes: 39 additions & 1 deletion Command/DocumentGenerateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class DocumentGenerateCommand extends AbstractManagerAwareCommand
*/
private $propertyAnnotations;

/**
* @var string[]
*/
private $propertyVisibilities;

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -165,6 +170,19 @@ protected function interact(InputInterface $input, OutputInterface $output)
continue;
}

$this->propertyVisibilities = ['private', 'protected', 'public'];
$output->writeln($this->getOptionsLabel($this->propertyVisibilities, 'Available visibilities'));
$property['visibility'] = $this->questionHelper->ask(
$input,
$output,
$this->getQuestion(
'Property visibility',
'private',
[$this, 'validatePropertyVisibility'],
$this->propertyVisibilities
)
);

$output->writeln($this->getOptionsLabel($this->propertyAnnotations, 'Available annotations'));
$property['annotation'] = $this->questionHelper->ask(
$input,
Expand Down Expand Up @@ -210,7 +228,7 @@ protected function interact(InputInterface $input, OutputInterface $output)
$output,
$this->getQuestion(
'Property type',
'string',
'text',
[$this, 'validatePropertyType'],
$this->getPropertyTypes()
)
Expand Down Expand Up @@ -515,6 +533,26 @@ public function validatePropertyAnnotation($annotation)
return $annotation;
}

/**
* Validates property visibility
*
* @param string $visibility
*
* @return string
* @throws \InvalidArgumentException When the visibility is not found in the list of allowed ones.
*/
public function validatePropertyVisibility($visibility)
{
if (!in_array($visibility, $this->propertyVisibilities)) {
throw $this->getException(
'The property visibility isn\'t valid ("%s" given, expecting one of following: %s)',
[$visibility, implode(', ', $this->propertyVisibilities)]
);
}

return $visibility;
}

/**
* Returns formatted question
*
Expand Down
5 changes: 4 additions & 1 deletion Generator/DocumentGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ private function generateDocumentProperties(array $metadata)

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

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

foreach ($metadata['properties'] as $property) {
if (isset($property['visibility']) && $property['visibility'] === 'public') {
continue;
}
$lines[] = $this->generateDocumentMethod($property, $this->setMethodTemplate) . "\n";
if (isset($property['property_type']) && $property['property_type'] === 'boolean') {
$lines[] = $this->generateDocumentMethod($property, $this->isMethodTemplate) . "\n";
Expand Down
2 changes: 2 additions & 0 deletions Tests/Unit/Service/GenerateServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@ public function testGenerate()
[
'field_name' => 'test',
'annotation' => 'property',
'visibility' => 'private',
'property_type' => 'string',
'property_name' => 'testProperty',
'property_options' => 'test',
],
[
'field_name' => 'embedded',
'visibility' => 'protected',
'annotation' => 'embedded',
'property_class' => 'TestBundle:Product',
'property_multiple' => true,
Expand Down

0 comments on commit 1bb0c31

Please sign in to comment.