Skip to content

Commit 49eeaa7

Browse files
authored
Merge pull request #28 from givebutter/develop
Develop
2 parents bfb23c0 + 2de2fba commit 49eeaa7

15 files changed

+128
-67
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
"autoload-dev": {
3636
"psr-4": {
37-
"Givebutter\\Tests\\": "tests/"
37+
"Givebutter\\Tests\\": "tests/",
38+
"Database\\": "database/"
3839
}
3940
},
4041
"scripts": {

config/custom-fields.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
return [
44
'form_name' => env('CUSTOM_FIELDS_FORM_NAME', 'custom_fields'),
5+
56
'tables' => [
67
'fields' => env('CUSTOM_FIELDS_TABLE', 'custom_fields'),
78
'field-responses' => env('CUSTOM_FIELD_RESPONSES_TABLE', 'custom_field_responses'),

database/factories/CustomFieldFactory.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Database\Factories;
44

5+
use Exception;
56
use Faker\Provider\Lorem;
67
use Givebutter\LaravelCustomFields\Models\CustomField;
78
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -15,81 +16,113 @@ class CustomFieldFactory extends Factory
1516
*/
1617
protected $model = CustomField::class;
1718

19+
/**
20+
* Define the model's default state.
21+
*
22+
* @return array
23+
*/
1824
public function definition()
1925
{
2026
$typesRequireAnswers = [
21-
CustomField::TYPE_CHECKBOX => false,
22-
CustomField::TYPE_NUMBER => false,
27+
CustomField::TYPE_TEXT => false,
2328
CustomField::TYPE_RADIO => true,
2429
CustomField::TYPE_SELECT => true,
25-
CustomField::TYPE_TEXT => false,
30+
CustomField::TYPE_NUMBER => false,
31+
CustomField::TYPE_CHECKBOX => false,
2632
CustomField::TYPE_TEXTAREA => false,
2733
];
2834

29-
$type = array_keys($typesRequireAnswers)[rand(0, count($typesRequireAnswers))]; // Pick a random type
30-
$answers = [];
31-
if ($typesRequireAnswers) {
32-
$answers = Lorem::words();
33-
}
35+
$type = array_keys($typesRequireAnswers)[rand(0, count($typesRequireAnswers) - 1)]; // Pick a random type
3436

3537
return [
3638
'type' => $type,
39+
'required' => false,
3740
'title' => Lorem::sentence(3),
3841
'description' => Lorem::sentence(3),
39-
'answers' => $answers,
40-
'required' => false,
42+
'answers' => $typesRequireAnswers ? Lorem::words() : [],
4143
];
4244
}
4345

46+
/**
47+
* @return $this
48+
*/
4449
public function withTypeCheckbox()
4550
{
4651
$this->model->type = CustomField::TYPE_CHECKBOX;
4752

4853
return $this;
4954
}
5055

56+
/**
57+
* @return $this
58+
*/
5159
public function withTypeNumber()
5260
{
5361
$this->model->type = CustomField::TYPE_NUMBER;
5462

5563
return $this;
5664
}
5765

66+
/**
67+
* @param mixed $answerCount
68+
* @return $this
69+
* @throws Exception
70+
*/
5871
public function withTypeRadio($answerCount = 3)
5972
{
6073
$this->model->type = CustomField::TYPE_RADIO;
6174

6275
return $this->withAnswers($answerCount);
6376
}
6477

78+
/**
79+
* @param mixed $optionCount
80+
* @return $this
81+
* @throws Exception
82+
*/
6583
public function withTypeSelect($optionCount = 3)
6684
{
6785
$this->model->type = CustomField::TYPE_SELECT;
6886

6987
return $this->withAnswers($optionCount);
7088
}
7189

90+
/**
91+
* @return $this
92+
*/
7293
public function withTypeText()
7394
{
7495
$this->model->type = CustomField::TYPE_TEXT;
7596

7697
return $this;
7798
}
7899

100+
/**
101+
* @return $this
102+
*/
79103
public function withTypeTextArea()
80104
{
81105
$this->model->type = CustomField::TYPE_TEXTAREA;
82106

83107
return $this;
84108
}
85109

110+
/**
111+
* @param $defaultValue
112+
* @return $this
113+
*/
86114
public function withDefaultValue($defaultValue)
87115
{
88116
$this->model->default_value = $defaultValue;
89117

90118
return $this;
91119
}
92120

121+
/**
122+
* @param mixed $answers
123+
* @return $this
124+
* @throws Exception
125+
*/
93126
public function withAnswers($answers = 3)
94127
{
95128
if (is_numeric($answers)) {
@@ -104,6 +137,6 @@ public function withAnswers($answers = 3)
104137
return $this;
105138
}
106139

107-
throw new \Exception("withAnswers only accepts a number or an array");
140+
throw new Exception("withAnswers only accepts a number or an array");
108141
}
109142
}

database/migrations/create_custom_fields_tables.php.stub

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ use Illuminate\Database\Migrations\Migration;
66

77
class CreateCustomFieldsTables extends Migration
88
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
914
public function up()
1015
{
1116
Schema::create(config('custom-fields.tables.fields', 'custom_fields'), function (Blueprint $table) {
@@ -36,6 +41,11 @@ class CreateCustomFieldsTables extends Migration
3641
});
3742
}
3843

44+
/**
45+
* Reverse the migrations.
46+
*
47+
* @return void
48+
*/
3949
public function down()
4050
{
4151
Schema::dropIfExists(config('custom-fields.tables.fields', 'custom_fields'));

src/Models/CustomField.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ class CustomField extends Model
4646
*
4747
* @var string[]|bool
4848
*/
49-
protected $guarded = ['id'];
49+
protected $guarded = [
50+
'id',
51+
];
5052

5153
/**
5254
* The attributes that are mass assignable.
@@ -127,6 +129,7 @@ public function responses()
127129
public function getValidationRulesAttribute()
128130
{
129131
$typeRules = $this->getFieldValidationRules($this->required)[$this->type];
132+
130133
array_unshift($typeRules, $this->required ? 'required' : 'nullable');
131134

132135
return $typeRules;
@@ -141,24 +144,31 @@ public function getValidationRulesAttribute()
141144
protected function getFieldValidationRules($required)
142145
{
143146
return [
144-
self::TYPE_CHECKBOX => $required ? ['accepted', 'in:0,1'] : ['in:0,1'],
147+
self::TYPE_CHECKBOX => $required
148+
? ['accepted', 'in:0,1']
149+
: ['in:0,1'],
150+
145151
self::TYPE_NUMBER => [
146152
'integer',
147153
],
154+
148155
self::TYPE_SELECT => [
149156
'string',
150157
'max:255',
151158
Rule::in($this->answers),
152159
],
160+
153161
self::TYPE_RADIO => [
154162
'string',
155163
'max:255',
156164
Rule::in($this->answers),
157165
],
166+
158167
self::TYPE_TEXT => [
159168
'string',
160169
'max:255',
161170
],
171+
162172
self::TYPE_TEXTAREA => [
163173
'string',
164174
],

src/Models/CustomFieldResponse.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class CustomFieldResponse extends Model
1111
*
1212
* @var string[]|bool
1313
*/
14-
protected $guarded = ['id'];
14+
protected $guarded = [
15+
'id',
16+
];
1517

1618
/**
1719
* The attributes that are mass assignable.
@@ -26,11 +28,11 @@ class CustomFieldResponse extends Model
2628
* @var string[]
2729
*/
2830
const VALUE_FIELDS = [
29-
CustomField::TYPE_NUMBER => 'value_int',
30-
CustomField::TYPE_CHECKBOX => 'value_int',
31+
CustomField::TYPE_TEXT => 'value_str',
3132
CustomField::TYPE_RADIO => 'value_str',
3233
CustomField::TYPE_SELECT => 'value_str',
33-
CustomField::TYPE_TEXT => 'value_str',
34+
CustomField::TYPE_NUMBER => 'value_int',
35+
CustomField::TYPE_CHECKBOX => 'value_int',
3436
CustomField::TYPE_TEXTAREA => 'value_text',
3537
];
3638

@@ -95,7 +97,7 @@ public function scopeHasValue($query, $value)
9597
*/
9698
public function formatValue($value)
9799
{
98-
// checkboxes send a default value of `on` so we need to booleanize it.
100+
// Checkboxes send a default value of "on", so we need to booleanize the value
99101
if ($this->field->type === 'checkbox') {
100102
$value = ! ! $value;
101103
}

src/Traits/HasCustomFieldResponses.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public function saveCustomFields($fields)
3333

3434
CustomFieldResponse::create([
3535
'value' => $value,
36-
'field_id' => $customField->id,
3736
'model_id' => $this->id,
37+
'field_id' => $customField->id,
3838
'model_type' => get_class($this),
3939
]);
4040
}
@@ -49,10 +49,12 @@ public function saveCustomFields($fields)
4949
*/
5050
public function scopeWhereField($query, CustomField $field, $value)
5151
{
52-
$query->whereHas('customFieldResponses', function ($subQuery) use ($field, $value) {
53-
$subQuery
52+
$query->whereHas('customFieldResponses', function ($query) use ($field, $value) {
53+
$query
5454
->where('field_id', $field->id)
55-
->hasValue($value);
55+
->where(function ($subQuery) use ($value) {
56+
$subQuery->hasValue($value);
57+
});
5658
});
5759
}
5860
}

src/Traits/HasCustomFields.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public function order($fields)
6363
$fields = collect($fields);
6464

6565
if ($fields->count() !== $this->customFields()->count()) {
66-
throw new WrongNumberOfFieldsForOrderingException(
67-
$fields->count(),
68-
$this->customFields()->count()
69-
);
66+
throw new WrongNumberOfFieldsForOrderingException($fields->count(), $this->customFields()->count());
7067
}
7168

7269
$fields->each(function ($id, $index) {

src/Validators/CustomFieldValidator.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88

99
class CustomFieldValidator extends Validator
1010
{
11-
public function __construct($data, $rules)
11+
/**
12+
* Create a new Validator instance.
13+
*
14+
* @param array $data
15+
* @param array $rules
16+
*/
17+
public function __construct(array $data, array $rules)
1218
{
1319
parent::__construct(
1420
app('translator'),
@@ -17,12 +23,19 @@ public function __construct($data, $rules)
1723
);
1824
}
1925

26+
/**
27+
* Replace the :attribute placeholder in the given message.
28+
*
29+
* @param string $message
30+
* @param string $value
31+
* @return string
32+
*/
2033
protected function replaceAttributePlaceholder($message, $value)
2134
{
2235
$fieldId = (int) Str::after($value, 'field ');
2336
$fieldTitle = CustomField::find($fieldId)->title;
2437
$replacementString = "`{$fieldTitle}` field";
25-
38+
2639
return str_replace(
2740
[':attribute', ':ATTRIBUTE', ':Attribute'],
2841
[$replacementString, Str::upper($replacementString), Str::ucfirst($replacementString)],

tests/Feature/CustomFieldControllerTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function valid_data_passes_controller_validation()
1919
{
2020
$survey = Survey::create();
2121
$survey->customfields()->save(
22-
factory(CustomField::class)->make([
22+
CustomField::factory()->make([
2323
'title' => 'email',
2424
'type' => 'text',
2525
])
@@ -45,15 +45,15 @@ public function invalid_data_throws_validation_exception()
4545
{
4646
$survey = Survey::create();
4747
$survey->customfields()->save(
48-
factory(CustomField::class)->make([
48+
CustomField::factory()->make([
4949
'title' => 'favorite_album',
5050
'type' => 'select',
5151
'answers' => ['Tha Carter', 'Tha Carter II', 'Tha Carter III'],
5252
])
5353
);
5454

5555
Route::post("/surveys/{$survey->id}/responses", function (Request $request) use ($survey) {
56-
$validator = $survey->validateCustomFields($request);
56+
$validator = $survey->validateCustomFields($request->custom_fields);
5757

5858
if ($validator->fails()) {
5959
return ['errors' => $validator->errors()];
@@ -77,7 +77,7 @@ public function non_required_fields_can_be_left_null_for_validation()
7777
{
7878
$survey = Survey::create();
7979
$survey->customfields()->save(
80-
factory(CustomField::class)->make([
80+
CustomField::factory()->make([
8181
'title' => 'favorite_album',
8282
'type' => 'select',
8383
'answers' => ['Tha Carter', 'Tha Carter II', 'Tha Carter III'],
@@ -111,7 +111,7 @@ public function checkbox_can_pass_validation()
111111
$survey = Survey::create();
112112
$surveyResponse = SurveyResponse::create();
113113
$survey->customfields()->save(
114-
factory(CustomField::class)->make([
114+
CustomField::factory()->make([
115115
'title' => 'favorite_album',
116116
'type' => 'checkbox',
117117
])
@@ -147,7 +147,7 @@ public function fields_can_be_saved_from_request_with_convenience_method()
147147
$survey = Survey::create();
148148
$surveyResponse = SurveyResponse::create();
149149
$survey->customfields()->save(
150-
factory(CustomField::class)->make([
150+
CustomField::factory()->make([
151151
'title' => 'favorite_album',
152152
'type' => 'select',
153153
'answers' => ['Tha Carter', 'Tha Carter II', 'Tha Carter III'],

0 commit comments

Comments
 (0)