-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 4ac87e0
Showing
62 changed files
with
3,837 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
vendor/ | ||
.idea/ | ||
.vscode/ | ||
node_modules/ | ||
.DS_Store | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))]; | ||
} | ||
} |
Oops, something went wrong.