Skip to content

Commit

Permalink
Allow setting annotation's example property to null with "NULL" value
Browse files Browse the repository at this point in the history
  • Loading branch information
digitv committed Sep 29, 2021
1 parent 82acc30 commit 27dfaea
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 39 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Swagger yaml generator",
"keywords": ["api", "swagger", "open auth"],
"license": "MIT",
"version": "1.0.11",
"version": "1.0.12",
"authors": [
{
"name": "Digit",
Expand Down
60 changes: 51 additions & 9 deletions oa/BaseAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@

namespace OA;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Contracts\Support\Arrayable;

/**
* Basic annotation class to extend
*/
abstract class BaseAnnotation implements Arrayable
{
const NULL_VALUE = 'NULL';

/**
* BaseAnnotation constructor.
* @param array $values
*
* @param array $values
*/
public function __construct($values)
{
Expand All @@ -22,6 +25,7 @@ public function __construct($values)

/**
* Dumps object data as array
*
* @return array
*/
public function toArray()
Expand All @@ -32,32 +36,37 @@ public function toArray()
if ($property->isStatic()) {
continue;
}
$data[$property->name] = $this->{$property->name};
$value = $this->{$property->name};
$data[$property->name] = $value !== static::NULL_VALUE ? $value : null;
}

return $data;
}

/**
* Load data into object
* @param array $data
*
* @param array $data
* @return static
*/
public function fill(array $data)
{
$this->configureSelf($data);

return $this;
}

/**
* Configure object
* @param array $config
* @param string|null $defaultParam
*
* @param array $config
* @param string|null $defaultParam
* @return array
*/
protected function configureSelf($config, $defaultParam = null)
{
$setParams = [];
if (array_key_exists('value', $config) && !property_exists($this, 'value') && $defaultParam !== null) {
if (array_key_exists('value', $config) && ! property_exists($this, 'value') && $defaultParam !== null) {
$this->{$defaultParam} = Arr::pull($config, 'value');
$setParams[] = $defaultParam;
}
Expand All @@ -67,26 +76,59 @@ protected function configureSelf($config, $defaultParam = null)
$setParams[] = $key;
}
}

return $setParams;
}

/**
* Magic getter
*
* @param string $name
* @return mixed
* @throws \ErrorException
*/
public function __get($name)
{
$getter = 'get' . ucfirst(Str::camel($name));
if (!method_exists($this, $getter)) {
$getter = 'get' . Str::studly($name);
if (! method_exists($this, $getter)) {
throw new \ErrorException("Undefined property: " . __CLASS__ . "::\${$name}");
}

return $this->{$getter}();
}

/**
* Magic setter.
*
* @param string $name
* @param mixed $value
* @throws \ErrorException
*/
public function __set(string $name, $value): void
{
$setter = 'set' . Str::studly($name);
if (! method_exists($this, $setter)) {
throw new \ErrorException("Undefined property: " . __CLASS__ . "::\${$name}");
}
$this->{$setter}($value);
}

/**
* Magic `isset`.
*
* @param string $name
* @return bool
*/
public function __isset(string $name): bool
{
$getter = 'get' . Str::studly($name);

return method_exists($this, $getter) && $this->{$getter}() !== null;
}

/**
* Get object string representation
*
* @return string
*/
abstract public function __toString();
Expand Down
Loading

0 comments on commit 27dfaea

Please sign in to comment.