-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
36afd65
commit f5a3766
Showing
11 changed files
with
187 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Bake\Utility\Model; | ||
|
||
enum EnumParser | ||
{ | ||
/** | ||
* @param string|null $casesString | ||
* @param bool $int | ||
* @return array<string, int|string> | ||
*/ | ||
public static function parseCases(?string $casesString, bool $int): array | ||
{ | ||
if ($casesString === null || $casesString === '') { | ||
return []; | ||
} | ||
|
||
$enumCases = explode(',', $casesString); | ||
|
||
$definition = []; | ||
foreach ($enumCases as $k => $enumCase) { | ||
$case = $value = trim($enumCase); | ||
if (str_contains($case, ':')) { | ||
$value = trim(mb_substr($case, strpos($case, ':') + 1)); | ||
$case = mb_substr($case, 0, strpos($case, ':')); | ||
} elseif ($int) { | ||
$value = $k; | ||
} | ||
|
||
$definition[$case] = $int ? (int)$value : $value; | ||
} | ||
|
||
return $definition; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org) | ||
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* | ||
* Licensed under The MIT License | ||
* For full copyright and license information, please see the LICENSE.txt | ||
* Redistributions of files must retain the above copyright notice | ||
* | ||
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) | ||
* @link https://cakephp.org CakePHP(tm) Project | ||
* @since 3.1.0 | ||
* @license https://www.opensource.org/licenses/mit-license.php MIT License | ||
*/ | ||
namespace Bake\Test\TestCase\Utility\Model; | ||
|
||
use Bake\Utility\Model\EnumParser; | ||
use Cake\TestSuite\TestCase; | ||
|
||
/** | ||
* BakeViewTest class | ||
*/ | ||
class EnumParserTest extends TestCase | ||
{ | ||
/** | ||
* @return void | ||
*/ | ||
public function testParseCases(): void | ||
{ | ||
$cases = EnumParser::parseCases('', false); | ||
$this->assertSame([], $cases); | ||
|
||
$cases = EnumParser::parseCases('foo, bar', false); | ||
$this->assertSame(['foo' => 'foo', 'bar' => 'bar'], $cases); | ||
|
||
$cases = EnumParser::parseCases('foo:f, bar:b', false); | ||
$this->assertSame(['foo' => 'f', 'bar' => 'b'], $cases); | ||
|
||
$cases = EnumParser::parseCases('foo:0, bar:1', true); | ||
$this->assertSame(['foo' => 0, 'bar' => 1], $cases); | ||
|
||
$cases = EnumParser::parseCases('foo, bar', true); | ||
$this->assertSame(['foo' => 0, 'bar' => 1], $cases); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
tests/comparisons/Model/testBakeEnumBackedIntWithCases.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Bake\Test\App\Model\Enum; | ||
|
||
use Cake\Database\Type\EnumLabelInterface; | ||
use Cake\Utility\Inflector; | ||
|
||
/** | ||
* FooBar Enum | ||
*/ | ||
enum FooBar: int implements EnumLabelInterface | ||
{ | ||
case Foo = 0; | ||
case Bar = 1; | ||
case BarBaz = 9; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function label(): string | ||
{ | ||
return Inflector::humanize(Inflector::underscore($this->name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Bake\Test\App\Model\Enum; | ||
|
||
use Cake\Database\Type\EnumLabelInterface; | ||
use Cake\Utility\Inflector; | ||
|
||
/** | ||
* FooBar Enum | ||
*/ | ||
enum FooBar: string implements EnumLabelInterface | ||
{ | ||
case Foo = 'foo'; | ||
case Bar = 'b'; | ||
case BarBaz = 'bar_baz'; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function label(): string | ||
{ | ||
return Inflector::humanize(Inflector::underscore($this->name)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters