Skip to content

Commit 0feaba7

Browse files
stephanniewerthvearutop
authored andcommitted
Render defaults of class properties (#29)
1 parent 4535bc2 commit 0feaba7

File tree

4 files changed

+218
-1
lines changed

4 files changed

+218
-1
lines changed

src/JsonSchema/PhpBuilder.php

+10
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ public function __construct()
6767
/** @var PhpBuilderClassHook */
6868
public $classPreparedHook;
6969

70+
/**
71+
* Use default values to initialize properties
72+
* @var bool
73+
*/
74+
public $declarePropertyDefaults = false;
75+
7076
/**
7177
* @param SchemaContract $schema
7278
* @param string $path
@@ -176,6 +182,10 @@ private function makeClass($schema, $path)
176182
$phpProperty->addMeta($property, self::SCHEMA);
177183
$phpProperty->addMeta($name, self::PROPERTY_NAME);
178184

185+
if (!is_null($property->default) && $this->declarePropertyDefaults) {
186+
$phpProperty->setDefault($property->default);
187+
}
188+
179189
if ($this->schemaIsNullable($property)) {
180190
$phpProperty->setIsMagical(true);
181191
}

src/PhpFunction.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class PhpFunction extends PhpTemplate
2525
private $throws;
2626

2727
private $body;
28+
protected $outputArgumentsWithDefaults = true;
2829
public $skipCodeCoverage = false;
2930

3031

@@ -99,7 +100,8 @@ private function renderArguments()
99100
{
100101
$result = '';
101102
foreach ($this->arguments as $argument) {
102-
$result .= "{$argument->renderArgumentType()}\${$argument->getName()}{$argument->renderDefault()}, ";
103+
$default = $this->outputArgumentsWithDefaults ? $argument->renderDefault() : '';
104+
$result .= "{$argument->renderArgumentType()}\${$argument->getName()}{$default}, ";
103105
}
104106
if ($result) {
105107
$result = substr($result, 0, -2);

src/Property/Setter.php

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public function __construct(PhpClassProperty $property, $fluent = true)
2323
);
2424

2525
$this->skipCodeCoverage = true;
26+
$this->outputArgumentsWithDefaults = false;
2627

2728
$this->addArgument($property->getNamedVar());
2829

tests/src/PHPUnit/JsonSchema/AdvancedTest.php

+204
Original file line numberDiff line numberDiff line change
@@ -529,4 +529,208 @@ public function setZedValue($name, $value)
529529
$this->assertSame($expected, $result);
530530

531531
}
532+
533+
534+
public function testClassPropertyDefaultsActivated()
535+
{
536+
$schemaData = json_decode(<<<'JSON'
537+
{
538+
"properties": {
539+
"stringDefault": {
540+
"type": "string",
541+
"default": "list"
542+
},
543+
"booleanDefault": {
544+
"type": "boolean",
545+
"default": false
546+
},
547+
"integerDefault": {
548+
"type": "integer",
549+
"default": 1
550+
},
551+
"arrayDefault": {
552+
"type": "array",
553+
"default": []
554+
},
555+
"noDefault": {
556+
"type": "string"
557+
}
558+
}
559+
}
560+
JSON
561+
);
562+
563+
$schema = Schema::import($schemaData);
564+
$builder = new PhpBuilder();
565+
$builder->buildSetters = true;
566+
$builder->declarePropertyDefaults = true;
567+
$class = $builder->getClass($schema, 'Root');
568+
569+
$result = '';
570+
foreach ($builder->getGeneratedClasses() as $class) {
571+
$result .= $class->class . "\n\n";
572+
}
573+
574+
$expected = <<<'PHP'
575+
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
576+
{
577+
/** @var string */
578+
public $stringDefault = 'list';
579+
580+
/** @var bool */
581+
public $booleanDefault = false;
582+
583+
/** @var int */
584+
public $integerDefault = 1;
585+
586+
/** @var array */
587+
public $arrayDefault = array (
588+
);
589+
590+
/** @var string */
591+
public $noDefault;
592+
593+
/**
594+
* @param Swaggest\JsonSchema\Constraint\Properties|static $properties
595+
* @param Swaggest\JsonSchema\Schema $ownerSchema
596+
*/
597+
public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $ownerSchema)
598+
{
599+
$properties->stringDefault = Swaggest\JsonSchema\Schema::string();
600+
$properties->stringDefault->default = "list";
601+
$properties->booleanDefault = Swaggest\JsonSchema\Schema::boolean();
602+
$properties->booleanDefault->default = false;
603+
$properties->integerDefault = Swaggest\JsonSchema\Schema::integer();
604+
$properties->integerDefault->default = 1;
605+
$properties->arrayDefault = Swaggest\JsonSchema\Schema::arr();
606+
$properties->arrayDefault->default = array();
607+
$properties->noDefault = Swaggest\JsonSchema\Schema::string();
608+
}
609+
610+
/**
611+
* @param string $stringDefault
612+
* @return $this
613+
* @codeCoverageIgnoreStart
614+
*/
615+
public function setStringDefault($stringDefault)
616+
{
617+
$this->stringDefault = $stringDefault;
618+
return $this;
619+
}
620+
/** @codeCoverageIgnoreEnd */
621+
622+
/**
623+
* @param bool $booleanDefault
624+
* @return $this
625+
* @codeCoverageIgnoreStart
626+
*/
627+
public function setBooleanDefault($booleanDefault)
628+
{
629+
$this->booleanDefault = $booleanDefault;
630+
return $this;
631+
}
632+
/** @codeCoverageIgnoreEnd */
633+
634+
/**
635+
* @param int $integerDefault
636+
* @return $this
637+
* @codeCoverageIgnoreStart
638+
*/
639+
public function setIntegerDefault($integerDefault)
640+
{
641+
$this->integerDefault = $integerDefault;
642+
return $this;
643+
}
644+
/** @codeCoverageIgnoreEnd */
645+
646+
/**
647+
* @param array $arrayDefault
648+
* @return $this
649+
* @codeCoverageIgnoreStart
650+
*/
651+
public function setArrayDefault($arrayDefault)
652+
{
653+
$this->arrayDefault = $arrayDefault;
654+
return $this;
655+
}
656+
/** @codeCoverageIgnoreEnd */
657+
658+
/**
659+
* @param string $noDefault
660+
* @return $this
661+
* @codeCoverageIgnoreStart
662+
*/
663+
public function setNoDefault($noDefault)
664+
{
665+
$this->noDefault = $noDefault;
666+
return $this;
667+
}
668+
/** @codeCoverageIgnoreEnd */
669+
}
670+
671+
672+
PHP;
673+
674+
$this->assertSame($expected, $result);
675+
}
676+
677+
public function testClassPropertyDefaultsDeactivated()
678+
{
679+
$schemaData = json_decode(<<<'JSON'
680+
{
681+
"properties": {
682+
"stringDefault": {
683+
"type": "string",
684+
"default": "list"
685+
},
686+
"booleanDefault": {
687+
"type": "boolean",
688+
"default": false
689+
},
690+
"integerDefault": {
691+
"type": "integer",
692+
"default": 1
693+
},
694+
"arrayDefault": {
695+
"type": "array",
696+
"default": []
697+
},
698+
"noDefault": {
699+
"type": "string"
700+
}
701+
}
702+
}
703+
JSON
704+
);
705+
706+
$schema = Schema::import($schemaData);
707+
$builder = new PhpBuilder();
708+
$builder->buildSetters = true;
709+
$class = $builder->getClass($schema, 'Root');
710+
711+
$result = '';
712+
foreach ($builder->getGeneratedClasses() as $class) {
713+
$result .= $class->class . "\n\n";
714+
}
715+
716+
$expected = <<<'PHP'
717+
/** @var string */
718+
public $stringDefault;
719+
720+
/** @var bool */
721+
public $booleanDefault;
722+
723+
/** @var int */
724+
public $integerDefault;
725+
726+
/** @var array */
727+
public $arrayDefault;
728+
729+
/** @var string */
730+
public $noDefault;
731+
PHP;
732+
733+
$this->assertContains($expected, $result);
734+
}
735+
532736
}

0 commit comments

Comments
 (0)