Skip to content

Commit

Permalink
chore: update Readme with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
ductiletoaster committed Apr 16, 2021
1 parent c483409 commit 34851d3
Showing 1 changed file with 171 additions and 2 deletions.
173 changes: 171 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ This will create a php-7.2 docker image and container with everything we need to
```
docker-compose run php-7.2 composer install
```

## Code Quality Testing
There are two simple steps for verifying your changes.

Expand All @@ -24,9 +23,179 @@ This compors alias can be found in the composer.json file under scripts. We util
docker-compose run php-7.2 composer test
```


#### Linting
This compors alias can be found in the composer.json file under scripts. We utilizes PHPCodesniffer for all liniting needs.
```
docker-compose run php-7.2 composer lint
```

## Usage
The Http model is meant to help abstract the request into an input model. The example below shows how it might be used in production.

First we have our Address model which defines how our API accepts address.

```php
<?php

declare(strict_types=1);

namespace App\Http\Models;

use PixelOven\Http\Model;

/**
* @SWG\Definition(
* type="object",
* required={"street", "postal", "region", "country"},
* @SWG\Xml(
* name="AddressBody"
* )
* )
*
* Class AddressBody
* @package App\Http\Models
*/
class AddressBody extends Model
{
/**
* @SWG\Property(
* description="Street address",
* )
* @var string
*/
public $street;

/**
* @SWG\Property(
* description="Postal code",
* )
* @var string
*/
public $postal;

/**
* @SWG\Property(
* description="Region, Province or State",
* )
* @var string
*/
public $region;

/**
* @SWG\Property(
* description="Country",
* )
* @var string
*/
public $country;

}
```

Then building off of this we define our User model which has a relationship with Address.
```php
<?php

declare(strict_types=1);

namespace App\Http\Models;

use PixelOven\Http\Model;

/**
* @SWG\Definition(
* type="object",
* required={"email", "name"},
* @SWG\Xml(
* name="UserBody"
* )
* )
*
* Class UserBody
* @package App\Http\Models
*/
class UserBody extends Model
{
protected $relations = [
'address' => AddressBody::class
];

/**
* @SWG\Property(
* description="User's email",
* )
* @var string
*/
public $email;

/**
* @SWG\Property(
* description="User's full name",
* )
* @var string
*/
public $name;

/**
* @SWG\Property(
* description="Address body",
* )
* @var AddressBody
*/
public $address;

}
```

As we can see from the above User now is related to address where address is a nested property. There is nothing stoping us from also extending a model if we wish for a flatter structure.

Lastly we can use these models to replace the raw Request object.
```php
<?php

declare(strict_types=1);

/**
* @SWG\Post(
* path="/pixeloven/v1/user",
* summary="Create user",
* tags={"V1 User"},
* description="Create a new user",
* operationId="createUser",
* consumes={"application/json"},
* produces={"application/json"},
* @SWG\Parameter(
* name="UserBody",
* in="body",
* description="User object",
* required=true,
* @SWG\Schema(ref="#/definitions/UserBody")
* ),
* @SWG\Response(ref="#/responses/201", response=201),
* @SWG\Response(ref="#/responses/404", response=404),
* @SWG\Response(ref="#/responses/422", response=422),
* @SWG\Response(ref="#/responses/500", response=500)
* )
*
* @param UserBody $model
*/
public function createUser(
UserBody $model,
) {
$model->validate([
'email' => ['required', 'string', 'email'],
'name' => ['required', 'string'],
]);

$userEmail = $model->email;
$userName = $model->name;
$userAddress = $model->address;


// ...
```
In this particular example we are also leveraging:
```
darkaonline/swagger-lume
```
This package allows us to use swagger annotations along side our request model.

0 comments on commit 34851d3

Please sign in to comment.