Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pascalboucher committed Nov 4, 2018
0 parents commit 4ac87e0
Show file tree
Hide file tree
Showing 62 changed files with 3,837 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vendor/
.idea/
.vscode/
node_modules/
.DS_Store
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018-present Pascal Boucher <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Laravel Form Maker

Simple package to generate laravel form easily.

## Installation

Requires php >=7.2.0 and laravel 5.5+

```
composer require chess/laravel-form-maker
php artisan vendor:publish --provider="Chess\FormMaker\FormMakerServiceProvider"
```

This is all there is to do.

## Documentation

To do.

## Credits

- [Pascal Boucher](https://github.com/pascalboucher)

## License

laravel-form-maker is open-sourced software licensed under the [MIT license](https://github.com/pascalboucher/laravel-form-maker/blob/master/LICENSE.md)
34 changes: 34 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "chess/laravel-form-maker",
"description": "Simple form maker module for Laravel",
"keywords": ["laravel", "form maker", "form builder", "form generator"],
"type": "library",
"support": {
"issues": "https://github.com/pascalboucher/laravel-form-maker/issues",
"source": "https://github.com/pascalboucher/laravel-form-maker"
},
"require": {
"php": ">=7.2.0",
"ext-fileinfo": "*",
"laravel/framework": "5.5.*|5.6.*|5.7.*"
},
"license": "MIT",
"authors": [
{
"name": "Pascal Boucher",
"email": "[email protected]"
}
],
"autoload": {
"psr-4": {
"Chess\\FormMaker\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Chess\\FormMaker\\FormMakerServiceProvider"
]
}
}
}
42 changes: 42 additions & 0 deletions database/migrations/create_form_builder_tables.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateFormBuilderTables extends Migration
{
public function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('description')->nullable();
$table->json('html_properties')->nullable();
$table->timestamps();
});

Schema::create('inputs', function (Blueprint $table) {
$table->increments('id');
$table->morphs('inputable');
$table->string('type');
$table->json('html_properties')->nullable();
$table->json('rules')->nullable();
$table->timestamps();
});

Schema::create('rankings', function (Blueprint $table) {
$table->increments('id');
$table->morphs('rankable');
$table->json('ranks');
$table->timestamps();
});
}

public function down()
{
Schema::drop('rankings');
Schema::drop('inputs');
Schema::drop('forms');
}
}
35 changes: 35 additions & 0 deletions src/FormMakerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Chess\FormMaker;

use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Support\ServiceProvider;

class FormMakerServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot(): void
{
$this->publishMigration();

Resource::withoutWrapping();
}

/**
* Publish Form Maker migrations
*
* @return void
*/
protected function publishMigration(): void
{
$timestamp = date('Y_m_d_His', time());

$this->publishes([
__DIR__ . '/../database/migrations/create_form_maker_tables.php' => database_path('migrations/' . $timestamp . '_create_form_maker_tables.php'),
], 'migrations');
}
}
21 changes: 21 additions & 0 deletions src/Http/Resources/FormCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Chess\FormMaker\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class FormCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request): array
{
return $this->collection->transform(function ($form) {
return new FormResource($form);
})->toArray();
}
}
35 changes: 35 additions & 0 deletions src/Http/Resources/FormResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Chess\FormMaker\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class FormResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request): array
{
$inputs = new InputCollection($this->inputs()->sortBy('rank'));

return [
'id' => $this->id,
$this->mergeWhen($this->description, [
'description' => $this->description,
]),
$this->mergeWhen($inputs->collection->isNotEmpty(), [
'inputs' => $inputs,
]),
$this->mergeWhen($this->html_properties, [
'properties' => $this->html_properties,
]),
'title' => $this->title,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
21 changes: 21 additions & 0 deletions src/Http/Resources/InputCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Chess\FormMaker\Http\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class InputCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request): array
{
return $this->collection->transform(function ($input) {
return new InputResource($input);
})->toArray();
}
}
34 changes: 34 additions & 0 deletions src/Http/Resources/InputResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Chess\FormMaker\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class InputResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request): array
{
if ($inputs = method_exists($this->resource, 'inputs')) {
$inputs = new InputCollection($this->inputs()->sortBy('rank'));
}

return [
'id' => $this->id,
$this->mergeWhen($inputs && $inputs->collection->isNotEmpty(), [
'inputs' => $inputs,
]),
$this->mergeWhen($this->html_properties, [
'properties' => $this->html_properties,
]),
'type' => $this->type,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}
38 changes: 38 additions & 0 deletions src/Listeners/AddInRanking.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Chess\FormMaker\Listeners;

use Illuminate\Database\Eloquent\Model;

class AddInRanking
{
/**
* The model with assigned properties.
*
* @var \Illuminate\Database\Eloquent\Model $model
*/
protected $model;

/**
* Create the event listener.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function __construct(Model $model)
{
$this->model = $model;

$this->handle();
}

/**
* Add the new input in the rankings.
*
* @return void
*/
protected function handle(): void
{
$this->model->inputable->ranking->add($this->model->id);
}
}
55 changes: 55 additions & 0 deletions src/Listeners/AssignProperties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Chess\FormMaker\Listeners;

use Illuminate\Database\Eloquent\Model;

class AssignProperties
{
/**
* The model with assigned properties.
*
* @var \Illuminate\Database\Eloquent\Model $model
*/
protected $model;

/**
* Create the event listener.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function __construct(Model $model)
{
$this->model = $model;

$this->handle();
}

/**
* Handle the event.
*
* @return void
*/
protected function handle(): void
{
$this->model->type = $this->model->getClassName();

foreach ($this->model->assignedProperties as $property) {
if (!isset($this->model->html_properties[$property])) {
$this->setProperty($property);
}
}
}

/**
* Set the assigned property.
*
* @param string $property
* @return void
*/
protected function setProperty(string $property): void
{
$this->model->html_properties = [$property => uniqid(sprintf('%s_', $this->model->type))];
}
}
Loading

0 comments on commit 4ac87e0

Please sign in to comment.