mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
Add clarifying comments
This commit is contained in:
parent
1a1f417633
commit
b24d80680e
|
@ -61,38 +61,44 @@ class ReportTemplate extends Model
|
||||||
return $this->options[$property] ?? '0';
|
return $this->options[$property] ?? '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function radioValue(string $property, $value, $isDefault = false): bool
|
public function radioValue(string $property, string $value, bool $isDefault = false): bool
|
||||||
{
|
{
|
||||||
// @todo: this method feels more like "radioShouldBeChecked" or something...
|
|
||||||
// @todo: improve the variable names...
|
|
||||||
|
|
||||||
$propertyExists = array_has($this->options, $property);
|
$propertyExists = array_has($this->options, $property);
|
||||||
|
|
||||||
|
// If the property doesn't exist but the radio input
|
||||||
|
// being checked is the default then return true.
|
||||||
if (!$propertyExists && $isDefault) {
|
if (!$propertyExists && $isDefault) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// If the property exists and matches what we're checking then return true.
|
||||||
if ($propertyExists && $this->options[$property] === $value) {
|
if ($propertyExists && $this->options[$property] === $value) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Otherwise return false.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function selectValue(string $property, string $model = null)
|
public function selectValue(string $property, string $model = null)
|
||||||
{
|
{
|
||||||
|
// If the property does not exist then return null.
|
||||||
if (!isset($this->options[$property])) {
|
if (!isset($this->options[$property])) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
$value = $this->options[$property];
|
$value = $this->options[$property];
|
||||||
|
|
||||||
|
// If the value was stored as an array, most likely
|
||||||
|
// due to a previously being a multi-select,
|
||||||
|
// then return the first value.
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
$value = $value[0];
|
$value = $value[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a model is provided then we should ensure we only return
|
// If a model is provided then we should ensure we only return
|
||||||
// the value if the model still exists.
|
// the value if the model still exists.
|
||||||
|
// Note: It is possible $value is an id that no longer exists and this will return null.
|
||||||
if ($model) {
|
if ($model) {
|
||||||
$foundModel = $model::find($value);
|
$foundModel = $model::find($value);
|
||||||
|
|
||||||
|
@ -104,23 +110,20 @@ class ReportTemplate extends Model
|
||||||
|
|
||||||
public function selectValues(string $property, string $model = null): iterable
|
public function selectValues(string $property, string $model = null): iterable
|
||||||
{
|
{
|
||||||
|
// If the property does not exist then return an empty array.
|
||||||
if (!isset($this->options[$property])) {
|
if (!isset($this->options[$property])) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// @todo: I think this was added to support the null object pattern
|
|
||||||
// @todo: Check if this is still needed and if so, add a test for it (testParsingSelectValues()).
|
|
||||||
// if ($this->options[$property] === [null]) {
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// If a model is provided then we should ensure we only return
|
// If a model is provided then we should ensure we only return
|
||||||
// the ids of models that exist and are not deleted.
|
// the ids of models that exist and are not deleted.
|
||||||
if ($model) {
|
if ($model) {
|
||||||
return $model::findMany($this->options[$property])->pluck('id');
|
return $model::findMany($this->options[$property])->pluck('id');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wrap the value in an array if needed.
|
// Wrap the value in an array if needed. This is to ensure
|
||||||
|
// values previously stored as a single value,
|
||||||
|
// most likely from a single select, are returned as an array.
|
||||||
if (!is_array($this->options[$property])) {
|
if (!is_array($this->options[$property])) {
|
||||||
return [$this->options[$property]];
|
return [$this->options[$property]];
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,11 +60,15 @@ class ReportTemplateTest extends TestCase
|
||||||
$template = ReportTemplate::factory()->create([
|
$template = ReportTemplate::factory()->create([
|
||||||
'options' => [
|
'options' => [
|
||||||
'is_a_checkbox_field' => '1',
|
'is_a_checkbox_field' => '1',
|
||||||
|
// This shouldn't happen since unchecked inputs are
|
||||||
|
// not submitted, but we should handle it anyway
|
||||||
|
'is_checkbox_field_with_zero' => '0',
|
||||||
],
|
],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->assertEquals('1', $template->checkmarkValue('is_a_checkbox_field'));
|
$this->assertEquals('1', $template->checkmarkValue('is_a_checkbox_field'));
|
||||||
$this->assertEquals('0', $template->checkmarkValue('non_existent_key'));
|
$this->assertEquals('0', $template->checkmarkValue('non_existent_key'));
|
||||||
|
$this->assertEquals('0', $template->checkmarkValue('is_checkbox_field_with_zero'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testParsingTextValue()
|
public function testParsingTextValue()
|
||||||
|
|
Loading…
Reference in a new issue