Skip to content

Commit

Permalink
Merge pull request #331 from FriendsOfCake/cake-5-enum-introspect
Browse files Browse the repository at this point in the history
Add support for introspecting enum
  • Loading branch information
ADmad authored Mar 28, 2024
2 parents 59e3fb9 + 13712af commit c003ceb
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 37 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-22.04

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<bootstrap class="Cake\TestSuite\Fixture\Extension\PHPUnitExtension"/>
</extensions>
<php>
<!-- <env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/> -->
<env name="FIXTURE_SCHEMA_METADATA" value="./tests/schema.php"/>
</php>
<source>
<include>
Expand Down
16 changes: 4 additions & 12 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="dev-master@">
<file src="src/Listener/ViewSearchListener.php">
<PossiblyUndefinedArrayOffset occurrences="1">
<code>$input['type']</code>
</PossiblyUndefinedArrayOffset>
</file>
<files psalm-version="5.23.1@8471a896ccea3526b26d082f4461eeea467f10a4">
<file src="src/View/Widget/DateTimeWidget.php">
<PossiblyNullArrayAccess occurrences="2">
<code>$data['name']</code>
<code>$data['templateVars']</code>
<PossiblyNullArrayAccess>
<code><![CDATA[$data['name']]]></code>
<code><![CDATA[$data['templateVars']]]></code>
</PossiblyNullArrayAccess>
<PossiblyUndefinedArrayOffset occurrences="1">
<code>$data['type']</code>
</PossiblyUndefinedArrayOffset>
</file>
</files>
8 changes: 2 additions & 6 deletions src/Dashboard/Module/LinkItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function __construct(string|array $title, string|array|null $url, array $
*/
protected function _setTitle(string|array|null $title): string|array
{
if (empty($title)) {
if ($title === null || $title === '') {
throw new InvalidArgumentException('Missing title for LinkItem action');
}

Expand All @@ -72,7 +72,7 @@ protected function _setTitle(string|array|null $title): string|array
*/
protected function _setUrl(string|array|null $url): string|array
{
if ($url === null || empty($url)) {
if ($url === null || $url === '') {
throw new InvalidArgumentException('Invalid url specified for LinkItem');
}

Expand All @@ -87,10 +87,6 @@ protected function _setUrl(string|array|null $url): string|array
*/
protected function _setOptions(array $options): string|array
{
if (empty($options)) {
$options = [];
}

$url = $this->get('url');
if (!is_array($url)) {
$isHttp = substr($url, 0, 7) === 'http://';
Expand Down
4 changes: 2 additions & 2 deletions src/Listener/ViewListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function _getPageTitle(): string
}

$primaryKeyValue = $this->_primaryKeyValue();
if (empty($primaryKeyValue)) {
if ($primaryKeyValue === null) {
return sprintf('%s %s', $actionName, $controllerName);
}

Expand Down Expand Up @@ -612,7 +612,7 @@ protected function _deriveFieldFromContext(string $field): mixed
$request = $this->_request();
$value = $entity->get($field);

if ($value) {
if ($value !== null) {
return $value;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Listener/ViewSearchListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function fields(): array
$input['options'][$input['value']] = $input['value'];
}

/** @psalm-suppress PossiblyInvalidOperand */
$input += [
'data-input-type' => 'text',
'data-tags' => 'true',
Expand Down Expand Up @@ -188,6 +189,12 @@ public function fields(): array
return $fields;
}

/**
* Get placeholder text for a field.
*
* @param string $field Field name.
* @return string
*/
protected function getPlaceholder(string $field): string
{
if (str_contains($field, '.')) {
Expand Down
3 changes: 2 additions & 1 deletion src/View/Cell/TablesListCell.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ class TablesListCell extends Cell
*/
public function display(?array $tables = null, ?array $blacklist = null): void
{
if (empty($tables)) {
if ($tables === null) {
/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get('default');
$schema = $connection->getSchemaCollection();
$tables = $schema->listTables();
ksort($tables);

/** @psalm-suppress RiskyTruthyFalsyComparison */
if (!empty($blacklist)) {
$tables = array_diff($tables, $blacklist);
}
Expand Down
24 changes: 24 additions & 0 deletions src/View/Helper/CrudViewHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -156,6 +159,10 @@ public function introspect(string $field, mixed $value, array $options = []): ar
return $this->formatTime($field, $value, $options);
}

if ($type !== null && str_starts_with($type, 'enum-')) {
return $this->formatEnum($field, $value, $options);
}

$value = $this->formatString($field, $value);

if ($field === $this->getViewVar('displayField')) {
Expand Down Expand Up @@ -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 \UnitEnum|\BackedEnum|string|int $value Value of field.
* @return string
*/
public function formatEnum(string $field, UnitEnum|BackedEnum|string|int $value, array $options): string
{
if (is_scalar($value)) {
return (string)$value;
}

return $value instanceof EnumLabelInterface ?
$value->label() : Inflector::humanize(Inflector::underscore($value->name));
}

/**
* Format a string for display
*
Expand Down
11 changes: 1 addition & 10 deletions tests/Fixture/BlogsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,7 @@

class BlogsFixture extends TestFixture
{
public $fields = [
'id' => ['type' => 'integer'],
'is_active' => ['type' => 'boolean', 'default' => true, 'null' => false],
'name' => ['type' => 'string', 'length' => 255, 'null' => false],
'body' => ['type' => 'text', 'null' => false],
'user_id' => ['type' => 'integer'],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
];

public $records = [
public array $records = [
['name' => '1st post', 'body' => '1st post body', 'user_id' => 1],
['name' => '2nd post', 'body' => '2nd post body', 'user_id' => 1],
['name' => '3rd post', 'body' => '3rd post body', 'user_id' => 1],
Expand Down
10 changes: 6 additions & 4 deletions tests/TestCase/Listener/ViewSearchListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@
*/
class ViewSearchListenerTest extends TestCase
{
protected $fixtures = ['plugin.CrudView.Blogs'];
protected array $fixtures = ['plugin.CrudView.Blogs'];

/**
* @var \Cake\Controller\Controller;
*/
protected $controller;
protected Controller $controller;

/**
* @var \CrudView\Listener\ViewSearchListener
*/
protected $listener;
protected ViewSearchListener $listener;

public function setUp(): void
{
Expand All @@ -39,7 +39,8 @@ public function setUp(): void
'params' => ['controller' => 'Blogs', 'action' => 'index', 'plugin' => null, '_ext' => null],
]);

$this->controller = new Controller($request, null, 'Blogs');
$this->controller = new Controller($request, 'Blogs');
$this->controller->loadComponent('Crud.Crud');

$this->listener = new ViewSearchListener($this->controller);

Expand Down Expand Up @@ -67,6 +68,7 @@ public function testFields()
'data-tags' => 'true',
'data-allow-clear' => 'true',
'data-placeholder' => '',
'empty' => 'Name',
],
'is_active' => [
'required' => false,
Expand Down
16 changes: 16 additions & 0 deletions tests/schema.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

return [
[
'table' => 'blogs',
'columns' => [
'id' => ['type' => 'integer'],
'is_active' => ['type' => 'boolean', 'default' => true, 'null' => false],
'name' => ['type' => 'string', 'length' => 255, 'null' => false],
'body' => ['type' => 'text', 'null' => false],
'user_id' => ['type' => 'integer'],
],
'constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
],
];

0 comments on commit c003ceb

Please sign in to comment.