147 lines
3.4 KiB
PHP
147 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace Laravel\Nova\Fields;
|
|
|
|
use Illuminate\Support\Arr;
|
|
use Laravel\Nova\Contracts\FilterableField;
|
|
use Laravel\Nova\Fields\Filters\BooleanFilter;
|
|
use Laravel\Nova\Http\Requests\NovaRequest;
|
|
|
|
class Boolean extends Field implements FilterableField
|
|
{
|
|
use FieldFilterable;
|
|
use SupportsDependentFields;
|
|
|
|
/**
|
|
* The field's component.
|
|
*
|
|
* @var string
|
|
*/
|
|
public $component = 'boolean-field';
|
|
|
|
/**
|
|
* The value to be used when the field is "true".
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $trueValue = true;
|
|
|
|
/**
|
|
* The value to be used when the field is "false".
|
|
*
|
|
* @var bool
|
|
*/
|
|
public $falseValue = false;
|
|
|
|
/**
|
|
* The text alignment for the field's text in tables.
|
|
*
|
|
* @var string
|
|
*/
|
|
public $textAlign = 'center';
|
|
|
|
/**
|
|
* Resolve the given attribute from the given resource.
|
|
*
|
|
* @param mixed $resource
|
|
* @param string $attribute
|
|
* @return bool|null
|
|
*/
|
|
protected function resolveAttribute($resource, $attribute)
|
|
{
|
|
$value = parent::resolveAttribute($resource, $attribute);
|
|
|
|
return ! is_null($value)
|
|
? ($value == $this->trueValue ? true : false)
|
|
: null;
|
|
}
|
|
|
|
/**
|
|
* Resolve the default value for the field.
|
|
*
|
|
* @return bool|null
|
|
*/
|
|
public function resolveDefaultValue(NovaRequest $request)
|
|
{
|
|
if ($request->isCreateOrAttachRequest() || $request->isActionRequest()) {
|
|
return parent::resolveDefaultValue($request) ?? false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hydrate the given attribute on the model based on the incoming request.
|
|
*
|
|
* @param string $requestAttribute
|
|
* @param \Illuminate\Database\Eloquent\Model|\Laravel\Nova\Support\Fluent $model
|
|
* @param string $attribute
|
|
* @return void
|
|
*/
|
|
protected function fillAttributeFromRequest(NovaRequest $request, $requestAttribute, $model, $attribute)
|
|
{
|
|
if (isset($request[$requestAttribute])) {
|
|
$model->{$attribute} = $request[$requestAttribute] == 1
|
|
? $this->trueValue : $this->falseValue;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Specify the values to store for the field.
|
|
*
|
|
* @param mixed $trueValue
|
|
* @param mixed $falseValue
|
|
* @return $this
|
|
*/
|
|
public function values($trueValue, $falseValue)
|
|
{
|
|
return $this->trueValue($trueValue)->falseValue($falseValue);
|
|
}
|
|
|
|
/**
|
|
* Specify the value to store when the field is "true".
|
|
*
|
|
* @param mixed $value
|
|
* @return $this
|
|
*/
|
|
public function trueValue($value)
|
|
{
|
|
$this->trueValue = $value;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Specify the value to store when the field is "false".
|
|
*
|
|
* @param mixed $value
|
|
* @return $this
|
|
*/
|
|
public function falseValue($value)
|
|
{
|
|
$this->falseValue = $value;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Make the field filter.
|
|
*
|
|
* @return \Laravel\Nova\Fields\Filters\Filter
|
|
*/
|
|
protected function makeFilter(NovaRequest $request)
|
|
{
|
|
return new BooleanFilter($this);
|
|
}
|
|
|
|
/**
|
|
* Prepare the field for JSON serialization.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function serializeForFilter()
|
|
{
|
|
return transform($this->jsonSerialize(), function ($field) {
|
|
return Arr::only($field, ['uniqueKey']);
|
|
});
|
|
}
|
|
}
|