Skip to content
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

Added support for data structures #37

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/Annotation/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Attribute
public $identifier;

/**
* @var string
* @var mixed
*/
public $type = 'string';

Expand Down
14 changes: 14 additions & 0 deletions src/Annotation/DataStructures.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Dingo\Blueprint\Annotation;

/**
* @Annotation
*/
class DataStructures
{
/**
* @array<Type>
*/
public $value;
}
29 changes: 29 additions & 0 deletions src/Annotation/Property.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Dingo\Blueprint\Annotation;

/**
* @Annotation
*/
class Property
{
/**
* @var string
*/
public $identifier;

/**
* @var mixed
*/
public $type = 'string';

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

/**
* @var mixed
*/
public $sample;
}
5 changes: 5 additions & 0 deletions src/Annotation/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ class Request
* @var array<Dingo\Blueprint\Annotation\Attribute>
*/
public $attributes;

/**
* @var Dingo\Blueprint\Annotation\Type
*/
public $type;
}
5 changes: 5 additions & 0 deletions src/Annotation/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ class Response
* @var array<Dingo\Blueprint\Annotation\Attribute>
*/
public $attributes;

/**
* @var Dingo\Blueprint\Annotation\Type
*/
public $type;
}
24 changes: 24 additions & 0 deletions src/Annotation/Type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Dingo\Blueprint\Annotation;

/**
* @Annotation
*/
class Type
{
/**
* @var string
*/
public $identifier;

/**
* @var mixed
*/
public $type;

/**
* @array<Property>
*/
public $properties;
}
108 changes: 104 additions & 4 deletions src/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dingo\Blueprint;

use Dingo\Blueprint\Annotation\Type;
use ReflectionClass;
use RuntimeException;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -135,7 +136,9 @@ protected function generateContentsFromResources(Collection $resources, $name)
$contents .= sprintf('# %s', $name);
$contents .= $this->line(2);

$resources->each(function ($resource) use (&$contents) {
$globalDataStructures = new Collection();

$resources->each(function ($resource) use (&$contents, $globalDataStructures) {
if ($resource->getActions()->isEmpty()) {
return;
}
Expand All @@ -151,6 +154,10 @@ protected function generateContentsFromResources(Collection $resources, $name)
$this->appendParameters($contents, $parameters);
}

if (($dataStructures = $resource->getDataStructures()) && ! $dataStructures->isEmpty()) {
$globalDataStructures->push($dataStructures);
}

$resource->getActions()->each(function ($action) use (&$contents, $resource) {
$contents .= $this->line(2);
$contents .= $action->getDefinition();
Expand Down Expand Up @@ -192,6 +199,15 @@ protected function generateContentsFromResources(Collection $resources, $name)
$contents .= $this->line(2);
});

if (! $globalDataStructures->isEmpty()) {
$contents .= $this->line(1);
$contents .= '# Data Structures';

$globalDataStructures->each(function ($dataStructure) use (&$contents) {
$this->appendDataStructures($contents, $dataStructure);
});
}

return stripslashes(trim($contents));
}

Expand All @@ -217,15 +233,35 @@ protected function appendAttributes(&$contents, Collection $attributes, $indent
$contents .= sprintf(': %s', $attribute->sample);
}

$arrayType = false;
if (is_array($attribute->type)) {
$arrayType = true;
$attribute->type = array_shift($attribute->type);
}

$type = $this->resolveType($attribute->type);

$contents .= sprintf(
' (%s, %s) - %s',
$attribute->type,
$arrayType ? "array[${type}]" : $type,
$attribute->required ? 'required' : 'optional',
$attribute->description
);
});
}

/**
* Append a type attribute to a resource or action.
*
* @param string $contents
* @param Type $type
* @param int $indent
*/
protected function appendType(&$contents, Type $type, $indent = 0)
{
$this->appendSection($contents, sprintf('Attributes (%s)', $type->identifier), $indent);
}

/**
* Append the parameters subsection to a resource or action.
*
Expand Down Expand Up @@ -284,7 +320,9 @@ protected function appendResponse(&$contents, Annotation\Response $response, Res
$this->appendHeaders($contents, array_merge($resource->getResponseHeaders(), $response->headers));
}

if (isset($response->attributes)) {
if (isset($response->type)) {
$this->appendType($contents, $response->type, 1);
} elseif (isset($response->attributes)) {
$this->appendAttributes($contents, collect($response->attributes), 1);
}

Expand Down Expand Up @@ -316,7 +354,9 @@ protected function appendRequest(&$contents, $request, Resource $resource)
$this->appendHeaders($contents, array_merge($resource->getRequestHeaders(), $request->headers));
}

if (isset($request->attributes)) {
if (isset($request->type)) {
$this->appendType($contents, $request->type, 1);
} elseif (isset($request->attributes)) {
$this->appendAttributes($contents, collect($request->attributes), 1);
}

Expand Down Expand Up @@ -388,6 +428,66 @@ protected function appendSection(&$contents, $name, $indent = 0, $lines = 2)
$contents .= '+ '.$name;
}

/**
* Append data structures.
*
* @param $contents
* @param $allStructures
*/
protected function appendDataStructures(&$contents, $allStructures)
{
$allStructures->each(function ($dataStructures) use (&$contents) {
collect($dataStructures->value)->each(function ($type) use (&$contents) {

$contents .= $this->line(2);
$contents .= sprintf('## %s (%s)', $type->identifier, $this->resolveType($type->type));
$contents .= $this->line(1);

if (isset($type->properties)) {
$this->appendProperties($contents, collect($type->properties), -1, false);
}
});
});
}

/**
* Resolves the name of a type from either a string or a Type class.
*
* @param string|Type $type
*
* @return string
*/
protected function resolveType($type)
{
return $type instanceof Type ? $type->identifier : $type;
}

/**
* Append the properties to a type.
*
* @param string $contents
* @param \Illuminate\Support\Collection $properties
*
* @return void
*/
protected function appendProperties(&$contents, Collection $properties)
{
$properties->each(function ($property) use (&$contents) {
$contents .= $this->line();
$contents .= sprintf('+ %s', $property->identifier);

if ($property->sample) {
$contents .= sprintf(': %s', $property->sample);
}

$contents .= sprintf(
' (%s) - %s',
$this->resolveType($property->type),
$property->description
);
});
}

/**
* Prepare a body.
*
Expand Down
13 changes: 13 additions & 0 deletions src/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Dingo\Blueprint;

use Dingo\Blueprint\Annotation\DataStructures;
use Illuminate\Support\Collection;
use ReflectionClass;

Expand Down Expand Up @@ -107,6 +108,18 @@ public function getActions()
return $this->actions;
}

/**
* Get the data structures belonging to the resource.
*
* @return \Illuminate\Support\Collection
*/
public function getDataStructures()
{
return $this->annotations->filter(function ($annotation) {
return $annotation instanceof DataStructures;
});
}

/**
* get the resource URI.
*
Expand Down
56 changes: 56 additions & 0 deletions tests/BlueprintTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,4 +487,60 @@ public function testGeneratingSimpleBlueprints()

$this->assertEquals(trim($expected), $blueprint->generate($resources, 'testing', 'v1', null));
}

public function testGeneratingBlueprintsWithDataStructures()
{
$resources = new Collection([new Stubs\AccountController]);

$blueprint = new Blueprint(new SimpleAnnotationReader, new Filesystem);

$expected = <<<'EOT'
FORMAT: 1A

# testing

# Account [/accounts]

## Show all accounts. [GET /accounts]


+ Response 200 (application/json)

+ Attributes
+ success: true (boolean, required) - Status of the request
+ data (array[Account], required) - The account's data

## Show a specific account. [GET /accounts/1]


+ Response 200 (application/json)

+ Attributes
+ success: true (boolean, required) - Status of the request
+ data (Deposit, required) - The account's data

## Create a new account. [POST /accounts]


+ Request (application/json)

+ Attributes (Account)

+ Response 200 (application/json)

+ Attributes (Account)


# Data Structures

## Account (object)

+ name: Savings (string) - The account name
+ balance: 1200 (number) - The account balance

## Deposit (Account)

EOT;
$this->assertEquals(trim($expected), $blueprint->generate($resources, 'testing', 'v1', null));
}
}
Loading