Skip to content

Writing defaults of class properties #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/JsonSchema/PhpBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public function __construct()
/** @var PhpBuilderClassHook */
public $classPreparedHook;

/**
* Use default values to initialize properties
* @var bool
*/
public $declarePropertyDefaults = false;

/**
* @param SchemaContract $schema
* @param string $path
Expand Down Expand Up @@ -176,6 +182,10 @@ private function makeClass($schema, $path)
$phpProperty->addMeta($property, self::SCHEMA);
$phpProperty->addMeta($name, self::PROPERTY_NAME);

if (!is_null($property->default) && $this->declarePropertyDefaults) {
$phpProperty->setDefault($property->default);
}

if ($this->schemaIsNullable($property)) {
$phpProperty->setIsMagical(true);
}
Expand Down
4 changes: 3 additions & 1 deletion src/PhpFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PhpFunction extends PhpTemplate
private $throws;

private $body;
protected $outputArgumentsWithDefaults = true;
public $skipCodeCoverage = false;


Expand Down Expand Up @@ -99,7 +100,8 @@ private function renderArguments()
{
$result = '';
foreach ($this->arguments as $argument) {
$result .= "{$argument->renderArgumentType()}\${$argument->getName()}{$argument->renderDefault()}, ";
$default = $this->outputArgumentsWithDefaults ? $argument->renderDefault() : '';
$result .= "{$argument->renderArgumentType()}\${$argument->getName()}{$default}, ";
}
if ($result) {
$result = substr($result, 0, -2);
Expand Down
1 change: 1 addition & 0 deletions src/Property/Setter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function __construct(PhpClassProperty $property, $fluent = true)
);

$this->skipCodeCoverage = true;
$this->outputArgumentsWithDefaults = false;

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

Expand Down
204 changes: 204 additions & 0 deletions tests/src/PHPUnit/JsonSchema/AdvancedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,208 @@ public function setZedValue($name, $value)
$this->assertSame($expected, $result);

}


public function testClassPropertyDefaultsActivated()
{
$schemaData = json_decode(<<<'JSON'
{
"properties": {
"stringDefault": {
"type": "string",
"default": "list"
},
"booleanDefault": {
"type": "boolean",
"default": false
},
"integerDefault": {
"type": "integer",
"default": 1
},
"arrayDefault": {
"type": "array",
"default": []
},
"noDefault": {
"type": "string"
}
}
}
JSON
);

$schema = Schema::import($schemaData);
$builder = new PhpBuilder();
$builder->buildSetters = true;
$builder->declarePropertyDefaults = true;
$class = $builder->getClass($schema, 'Root');

$result = '';
foreach ($builder->getGeneratedClasses() as $class) {
$result .= $class->class . "\n\n";
}

$expected = <<<'PHP'
class Root extends Swaggest\JsonSchema\Structure\ClassStructure
{
/** @var string */
public $stringDefault = 'list';

/** @var bool */
public $booleanDefault = false;

/** @var int */
public $integerDefault = 1;

/** @var array */
public $arrayDefault = array (
);

/** @var string */
public $noDefault;

/**
* @param Swaggest\JsonSchema\Constraint\Properties|static $properties
* @param Swaggest\JsonSchema\Schema $ownerSchema
*/
public static function setUpProperties($properties, Swaggest\JsonSchema\Schema $ownerSchema)
{
$properties->stringDefault = Swaggest\JsonSchema\Schema::string();
$properties->stringDefault->default = "list";
$properties->booleanDefault = Swaggest\JsonSchema\Schema::boolean();
$properties->booleanDefault->default = false;
$properties->integerDefault = Swaggest\JsonSchema\Schema::integer();
$properties->integerDefault->default = 1;
$properties->arrayDefault = Swaggest\JsonSchema\Schema::arr();
$properties->arrayDefault->default = array();
$properties->noDefault = Swaggest\JsonSchema\Schema::string();
}

/**
* @param string $stringDefault
* @return $this
* @codeCoverageIgnoreStart
*/
public function setStringDefault($stringDefault)
{
$this->stringDefault = $stringDefault;
return $this;
}
/** @codeCoverageIgnoreEnd */

/**
* @param bool $booleanDefault
* @return $this
* @codeCoverageIgnoreStart
*/
public function setBooleanDefault($booleanDefault)
{
$this->booleanDefault = $booleanDefault;
return $this;
}
/** @codeCoverageIgnoreEnd */

/**
* @param int $integerDefault
* @return $this
* @codeCoverageIgnoreStart
*/
public function setIntegerDefault($integerDefault)
{
$this->integerDefault = $integerDefault;
return $this;
}
/** @codeCoverageIgnoreEnd */

/**
* @param array $arrayDefault
* @return $this
* @codeCoverageIgnoreStart
*/
public function setArrayDefault($arrayDefault)
{
$this->arrayDefault = $arrayDefault;
return $this;
}
/** @codeCoverageIgnoreEnd */

/**
* @param string $noDefault
* @return $this
* @codeCoverageIgnoreStart
*/
public function setNoDefault($noDefault)
{
$this->noDefault = $noDefault;
return $this;
}
/** @codeCoverageIgnoreEnd */
}


PHP;

$this->assertSame($expected, $result);
}

public function testClassPropertyDefaultsDeactivated()
{
$schemaData = json_decode(<<<'JSON'
{
"properties": {
"stringDefault": {
"type": "string",
"default": "list"
},
"booleanDefault": {
"type": "boolean",
"default": false
},
"integerDefault": {
"type": "integer",
"default": 1
},
"arrayDefault": {
"type": "array",
"default": []
},
"noDefault": {
"type": "string"
}
}
}
JSON
);

$schema = Schema::import($schemaData);
$builder = new PhpBuilder();
$builder->buildSetters = true;
$class = $builder->getClass($schema, 'Root');

$result = '';
foreach ($builder->getGeneratedClasses() as $class) {
$result .= $class->class . "\n\n";
}

$expected = <<<'PHP'
/** @var string */
public $stringDefault;

/** @var bool */
public $booleanDefault;

/** @var int */
public $integerDefault;

/** @var array */
public $arrayDefault;

/** @var string */
public $noDefault;
PHP;

$this->assertContains($expected, $result);
}

}