Skip to content

Commit

Permalink
Merge pull request #25 from givebutter/Laravel8-Factory
Browse files Browse the repository at this point in the history
Laravel 8 Factories
  • Loading branch information
liran-co authored Apr 15, 2021
2 parents e4c6be7 + ff3d091 commit db4fc06
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 23 deletions.
116 changes: 116 additions & 0 deletions database/factories/CustomFieldFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php


namespace Database\Factories;


use Faker\Provider\Lorem;
use Faker\Provider\Text;
use Givebutter\LaravelCustomFields\Models\CustomField;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class CustomFieldFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = CustomField::class;

public function definition()
{

$typesRequireAnswers = [
CustomField::TYPE_CHECKBOX => false,
CustomField::TYPE_NUMBER => false,
CustomField::TYPE_RADIO => true,
CustomField::TYPE_SELECT => true,
CustomField::TYPE_TEXT => false,
CustomField::TYPE_TEXTAREA => false,
];

$type = array_keys($typesRequireAnswers)[rand(0, count($typesRequireAnswers))]; // Pick a random type
$answers = [];
if ($typesRequireAnswers) {
$answers = Lorem::words();
}

return [
'type' => $type,
'title' => Lorem::sentence(3),
'description' => Lorem::sentence(3),
'answers' => $answers,
'required' => false,
];
}

public function withTypeCheckbox()
{
$this->model->type = CustomField::TYPE_CHECKBOX;

return $this;
}

public function withTypeNumber()
{
$this->model->type = CustomField::TYPE_NUMBER;

return $this;
}

public function withTypeRadio($answerCount = 3)
{
$this->model->type = CustomField::TYPE_RADIO;

return $this->withAnswers($answerCount);
}

public function withTypeSelect($optionCount = 3)
{
$this->model->type = CustomField::TYPE_SELECT;

return $this->withAnswers($optionCount);
}

public function withTypeText()
{
$this->model->type = CustomField::TYPE_TEXT;

return $this;
}

public function withTypeTextArea()
{
$this->model->type = CustomField::TYPE_TEXTAREA;

return $this;
}

public function withDefaultValue($defaultValue)
{
$this->model->default_value = $defaultValue;

return $this;
}

public function withAnswers($answers = 3)
{
if (is_numeric($answers)) {
$this->model->answers = Lorem::words($answers);

return $this;
}

if (is_array($answers)) {
$this->model->answers = $answers;

return $this;
}

throw new \Exception("withAnswers only accepts a number or an array");

}
}
38 changes: 21 additions & 17 deletions src/Models/CustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@

namespace Givebutter\LaravelCustomFields\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Validation\Rule;

class CustomField extends Model
{

use SoftDeletes;
const TYPE_CHECKBOX = 'checkbox';
const TYPE_NUMBER = 'number';
const TYPE_RADIO = 'radio';
const TYPE_SELECT = 'select';
const TYPE_TEXT = 'text';
const TYPE_TEXTAREA = 'textarea';

use SoftDeletes, HasFactory;

protected $guarded = ['id'];

Expand All @@ -29,37 +36,34 @@ class CustomField extends Model

public function __construct(array $attributes = [])
{
$this->bootIfNotBooted();
$this->initializeTraits();
$this->syncOriginal();
$this->fill($attributes);
parent::__construct($attributes);

$this->table = config('custom-fields.tables.fields', 'custom_fields');
}

private function fieldValidationRules($required)
{
return [
'text' => [
'string',
'max:255',
self::TYPE_CHECKBOX => $required ? ['accepted','in:0,1'] : ['in:0,1'],
self::TYPE_NUMBER => [
'integer',
],
'textarea' => [
self::TYPE_SELECT => [
'string',
'max:255',
Rule::in($this->answers),
],
'select' => [
self::TYPE_RADIO => [
'string',
'max:255',
Rule::in($this->answers),
],
'number' => [
'integer',
],
'checkbox' => $required ? ['accepted','in:0,1'] : ['in:0,1'],
'radio' => [
self::TYPE_TEXT => [
'string',
'max:255',
Rule::in($this->answers),
],
self::TYPE_TEXTAREA => [
'string',
],
];
}
Expand Down
12 changes: 6 additions & 6 deletions src/Models/CustomFieldResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class CustomFieldResponse extends Model
];

const VALUE_FIELDS = [
'number' => 'value_int',
'checkbox' => 'value_int',
'radio' => 'value_str',
'select' => 'value_str',
'text' => 'value_str',
'textarea' => 'value_text',
CustomField::TYPE_NUMBER => 'value_int',
CustomField::TYPE_CHECKBOX => 'value_int',
CustomField::TYPE_RADIO => 'value_str',
CustomField::TYPE_SELECT => 'value_str',
CustomField::TYPE_TEXT => 'value_str',
CustomField::TYPE_TEXTAREA => 'value_text',
];

public function __construct(array $attributes = [])
Expand Down

0 comments on commit db4fc06

Please sign in to comment.