Skip to content

Commit ff3d091

Browse files
committed
Laravel 8 Factories
1 parent e4c6be7 commit ff3d091

File tree

3 files changed

+143
-23
lines changed

3 files changed

+143
-23
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
3+
4+
namespace Database\Factories;
5+
6+
7+
use Faker\Provider\Lorem;
8+
use Faker\Provider\Text;
9+
use Givebutter\LaravelCustomFields\Models\CustomField;
10+
use Illuminate\Database\Eloquent\Factories\Factory;
11+
use Illuminate\Support\Arr;
12+
use Illuminate\Support\Str;
13+
14+
class CustomFieldFactory extends Factory
15+
{
16+
/**
17+
* The name of the factory's corresponding model.
18+
*
19+
* @var string
20+
*/
21+
protected $model = CustomField::class;
22+
23+
public function definition()
24+
{
25+
26+
$typesRequireAnswers = [
27+
CustomField::TYPE_CHECKBOX => false,
28+
CustomField::TYPE_NUMBER => false,
29+
CustomField::TYPE_RADIO => true,
30+
CustomField::TYPE_SELECT => true,
31+
CustomField::TYPE_TEXT => false,
32+
CustomField::TYPE_TEXTAREA => false,
33+
];
34+
35+
$type = array_keys($typesRequireAnswers)[rand(0, count($typesRequireAnswers))]; // Pick a random type
36+
$answers = [];
37+
if ($typesRequireAnswers) {
38+
$answers = Lorem::words();
39+
}
40+
41+
return [
42+
'type' => $type,
43+
'title' => Lorem::sentence(3),
44+
'description' => Lorem::sentence(3),
45+
'answers' => $answers,
46+
'required' => false,
47+
];
48+
}
49+
50+
public function withTypeCheckbox()
51+
{
52+
$this->model->type = CustomField::TYPE_CHECKBOX;
53+
54+
return $this;
55+
}
56+
57+
public function withTypeNumber()
58+
{
59+
$this->model->type = CustomField::TYPE_NUMBER;
60+
61+
return $this;
62+
}
63+
64+
public function withTypeRadio($answerCount = 3)
65+
{
66+
$this->model->type = CustomField::TYPE_RADIO;
67+
68+
return $this->withAnswers($answerCount);
69+
}
70+
71+
public function withTypeSelect($optionCount = 3)
72+
{
73+
$this->model->type = CustomField::TYPE_SELECT;
74+
75+
return $this->withAnswers($optionCount);
76+
}
77+
78+
public function withTypeText()
79+
{
80+
$this->model->type = CustomField::TYPE_TEXT;
81+
82+
return $this;
83+
}
84+
85+
public function withTypeTextArea()
86+
{
87+
$this->model->type = CustomField::TYPE_TEXTAREA;
88+
89+
return $this;
90+
}
91+
92+
public function withDefaultValue($defaultValue)
93+
{
94+
$this->model->default_value = $defaultValue;
95+
96+
return $this;
97+
}
98+
99+
public function withAnswers($answers = 3)
100+
{
101+
if (is_numeric($answers)) {
102+
$this->model->answers = Lorem::words($answers);
103+
104+
return $this;
105+
}
106+
107+
if (is_array($answers)) {
108+
$this->model->answers = $answers;
109+
110+
return $this;
111+
}
112+
113+
throw new \Exception("withAnswers only accepts a number or an array");
114+
115+
}
116+
}

src/Models/CustomField.php

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@
22

33
namespace Givebutter\LaravelCustomFields\Models;
44

5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
56
use Illuminate\Database\Eloquent\SoftDeletes;
67
use Illuminate\Database\Eloquent\Model;
78
use Illuminate\Validation\Rule;
89

910
class CustomField extends Model
1011
{
11-
12-
use SoftDeletes;
12+
const TYPE_CHECKBOX = 'checkbox';
13+
const TYPE_NUMBER = 'number';
14+
const TYPE_RADIO = 'radio';
15+
const TYPE_SELECT = 'select';
16+
const TYPE_TEXT = 'text';
17+
const TYPE_TEXTAREA = 'textarea';
18+
19+
use SoftDeletes, HasFactory;
1320

1421
protected $guarded = ['id'];
1522

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

3037
public function __construct(array $attributes = [])
3138
{
32-
$this->bootIfNotBooted();
33-
$this->initializeTraits();
34-
$this->syncOriginal();
35-
$this->fill($attributes);
39+
parent::__construct($attributes);
3640

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

4044
private function fieldValidationRules($required)
4145
{
4246
return [
43-
'text' => [
44-
'string',
45-
'max:255',
47+
self::TYPE_CHECKBOX => $required ? ['accepted','in:0,1'] : ['in:0,1'],
48+
self::TYPE_NUMBER => [
49+
'integer',
4650
],
47-
'textarea' => [
51+
self::TYPE_SELECT => [
4852
'string',
53+
'max:255',
54+
Rule::in($this->answers),
4955
],
50-
'select' => [
56+
self::TYPE_RADIO => [
5157
'string',
5258
'max:255',
5359
Rule::in($this->answers),
5460
],
55-
'number' => [
56-
'integer',
57-
],
58-
'checkbox' => $required ? ['accepted','in:0,1'] : ['in:0,1'],
59-
'radio' => [
61+
self::TYPE_TEXT => [
6062
'string',
6163
'max:255',
62-
Rule::in($this->answers),
64+
],
65+
self::TYPE_TEXTAREA => [
66+
'string',
6367
],
6468
];
6569
}

src/Models/CustomFieldResponse.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ class CustomFieldResponse extends Model
1313
];
1414

1515
const VALUE_FIELDS = [
16-
'number' => 'value_int',
17-
'checkbox' => 'value_int',
18-
'radio' => 'value_str',
19-
'select' => 'value_str',
20-
'text' => 'value_str',
21-
'textarea' => 'value_text',
16+
CustomField::TYPE_NUMBER => 'value_int',
17+
CustomField::TYPE_CHECKBOX => 'value_int',
18+
CustomField::TYPE_RADIO => 'value_str',
19+
CustomField::TYPE_SELECT => 'value_str',
20+
CustomField::TYPE_TEXT => 'value_str',
21+
CustomField::TYPE_TEXTAREA => 'value_text',
2222
];
2323

2424
public function __construct(array $attributes = [])

0 commit comments

Comments
 (0)