Cast the required field properly and add helper #146

This commit is contained in:
Nabeel Shahzad 2018-01-21 13:43:32 -05:00
parent e86fd64235
commit f09b3b888a
3 changed files with 29 additions and 11 deletions

View File

@ -18,13 +18,6 @@ class AirportController extends BaseController
/** @var AirportRepository */ /** @var AirportRepository */
private $airportRepository; private $airportRepository;
public static $enabledStates = [
'on',
'true',
'1',
true,
];
public function __construct(AirportRepository $airportRepo) public function __construct(AirportRepository $airportRepo)
{ {
$this->airportRepository = $airportRepo; $this->airportRepository = $airportRepo;
@ -73,7 +66,7 @@ class AirportController extends BaseController
public function store(CreateAirportRequest $request) public function store(CreateAirportRequest $request)
{ {
$input = $request->all(); $input = $request->all();
$input['hub'] = \in_array($input['hub'], self::$enabledStates); $input['hub'] = get_truth_state($input['hub']);
$this->airportRepository->create($input); $this->airportRepository->create($input);
@ -137,7 +130,7 @@ class AirportController extends BaseController
} }
$attrs = $request->all(); $attrs = $request->all();
$attrs['hub'] = \in_array($attrs['hub'], self::$enabledStates); $attrs['hub'] = get_truth_state($attrs['hub']);
$this->airportRepository->update($attrs, $id); $this->airportRepository->update($attrs, $id);

View File

@ -52,12 +52,14 @@ class PirepFieldController extends BaseController
* @param CreatePirepFieldRequest $request * @param CreatePirepFieldRequest $request
* *
* @return Response * @return Response
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/ */
public function store(CreatePirepFieldRequest $request) public function store(CreatePirepFieldRequest $request)
{ {
$input = $request->all(); $input = $request->all();
$input['required'] = get_truth_state($input['required']);
$field = $this->pirepFieldRepo->create($input); $this->pirepFieldRepo->create($input);
Flash::success('PirepField saved successfully.'); Flash::success('PirepField saved successfully.');
return redirect(route('admin.pirepfields.index')); return redirect(route('admin.pirepfields.index'));
@ -107,6 +109,7 @@ class PirepFieldController extends BaseController
/** /**
* Update the specified PirepField in storage. * Update the specified PirepField in storage.
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/ */
public function update($id, UpdatePirepFieldRequest $request) public function update($id, UpdatePirepFieldRequest $request)
{ {
@ -117,7 +120,9 @@ class PirepFieldController extends BaseController
return redirect(route('admin.pirepfields.index')); return redirect(route('admin.pirepfields.index'));
} }
$field = $this->pirepFieldRepo->update($request->all(), $id); $attrs = $request->all();
$attrs['required'] = get_truth_state($attrs['required']);
$this->pirepFieldRepo->update($attrs, $id);
Flash::success('PirepField updated successfully.'); Flash::success('PirepField updated successfully.');
return redirect(route('admin.pirepfields.index')); return redirect(route('admin.pirepfields.index'));

View File

@ -1,5 +1,25 @@
<?php <?php
if(!function_exists('get_truth_state')) {
/**
* Check if the passed state matches any of the states that
* we regard as being true or false
* @param $state
* @return bool
*/
function get_truth_state($state)
{
$enabledStates = [
'on',
'true',
'1',
true,
];
return \in_array($state, $enabledStates, false);
}
}
if(!function_exists('list_to_assoc')) { if(!function_exists('list_to_assoc')) {
/** /**
* Converts a straight list into an assoc array with * Converts a straight list into an assoc array with