22
33namespace Database \Factories ;
44
5+ use Exception ;
56use Faker \Provider \Lorem ;
67use Givebutter \LaravelCustomFields \Models \CustomField ;
78use 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}
0 commit comments