-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,15 @@ | |
|
||
namespace CrudView\View\Helper; | ||
|
||
use BackedEnum; | ||
use Cake\Database\Type\EnumLabelInterface; | ||
use Cake\Datasource\EntityInterface; | ||
use Cake\Datasource\SchemaInterface; | ||
use Cake\Utility\Inflector; | ||
use Cake\Utility\Text; | ||
use Cake\View\Helper; | ||
use Cake\View\Helper\FormHelper; | ||
use UnitEnum; | ||
use function Cake\Core\h; | ||
use function Cake\I18n\__d; | ||
|
||
|
@@ -156,6 +159,10 @@ public function introspect(string $field, mixed $value, array $options = []): ar | |
return $this->formatTime($field, $value, $options); | ||
} | ||
|
||
if (str_starts_with($type, 'enum-')) { | ||
Check failure on line 162 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static AnalysisPossiblyNullArgument
Check failure on line 162 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static AnalysisPossiblyNullArgument
|
||
return $this->formatEnum($field, $value, $options); | ||
} | ||
|
||
$value = $this->formatString($field, $value); | ||
|
||
if ($field === $this->getViewVar('displayField')) { | ||
|
@@ -230,6 +237,23 @@ public function formatTime(string $field, mixed $value, array $options): string | |
return $value; | ||
} | ||
|
||
/** | ||
* Format an enum for display | ||
* | ||
* @param string $field Name of field. | ||
* @param int|string|\UnitEnum|\BackedEnum $value Value of field. | ||
Check warning on line 244 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static Analysis
Check warning on line 244 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static Analysis
|
||
* @return string | ||
Check failure on line 245 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static AnalysisInvalidReturnType
Check failure on line 245 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static AnalysisInvalidReturnType
|
||
*/ | ||
public function formatEnum(string $field, int|string|UnitEnum|BackedEnum $value, array $options): string | ||
{ | ||
if (is_scalar($value)) { | ||
return $value; | ||
Check failure on line 250 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static Analysis
Check failure on line 250 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static AnalysisInvalidReturnStatement
Check failure on line 250 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static Analysis
Check failure on line 250 in src/View/Helper/CrudViewHelper.php GitHub Actions / Coding Standard & Static AnalysisInvalidReturnStatement
|
||
} | ||
|
||
return $value instanceof EnumLabelInterface ? | ||
$value->label() : Inflector::humanize(Inflector::underscore($value->name)); | ||
} | ||
|
||
/** | ||
* Format a string for display | ||
* | ||
|