Skip to content

Commit

Permalink
updated enum plugin to CakePHP 5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
arusinowski committed Feb 8, 2023
1 parent 120273d commit 1bcc10a
Show file tree
Hide file tree
Showing 25 changed files with 170 additions and 155 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,8 @@
Changelog
=========

* 3.0.0-dev CakePHP 5.x support

* 2.0.0 CakePHP 4.x support

* 1.2.0
Expand Down
10 changes: 3 additions & 7 deletions Docs/Documentation/Installation.md
Expand Up @@ -3,7 +3,7 @@
## Composer

```
composer require cakedc/enum:dev-master
composer require cakedc/enum:3.0.0-dev
```

## Creating Required Tables
Expand All @@ -16,12 +16,8 @@ bin/cake migrations migrate -p CakeDC/Enum

## Load the Plugin

Ensure the Enum Plugin is loaded in your config/bootstrap.php file
Ensure the Enum Plugin is loaded in your `Application::bootstrap()` method:

```php
Plugin::load('CakeDC/Enum');
$this->addPlugin('CakeDC/Enum');
```
or
```php
Plugin::loadAll();
```
4 changes: 2 additions & 2 deletions Docs/Home.md
Expand Up @@ -4,8 +4,8 @@ Home
Requirements
------------

* CakePHP 3.1+
* PHP 5.4.16+ Note CakePHP 3.2 requires PHP 5.5 so 5.4 compatibility would be dropped sooner than later...
* CakePHP 5.0+
* PHP 8.1+

Documentation
-------------
Expand Down
29 changes: 15 additions & 14 deletions README.md
Expand Up @@ -6,18 +6,19 @@ CakeDC Enum Plugin
[![Total Downloads](https://img.shields.io/packagist/dt/cakedc/enum.svg?style=flat-square)](https://packagist.org/packages/cakedc/enum)
[![License](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](LICENSE)

Enumeration list for [CakePHP 4](http://cakephp.org).
Enumeration list for [CakePHP 5](http://cakephp.org).

Versions and branches
---------------------

| CakePHP | CakeDC Enum Plugin | Tag | Notes |
| :-------------: | :------------------------: | :--: | :---- |
| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.4 | stable |
| ^3.7 | [master](https://github.com/cakedc/enum/tree/master) | 1.5.0 | stable |
| ^3.7 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable |
| ^3.6 | [master](https://github.com/cakedc/enum/tree/1.4.0) | 1.4.0 | stable |
| 3.1 - 3.5 | | 1.3.0 | stable |
| CakePHP | CakeDC Enum Plugin | Tag | Notes |
|:-----------|:--------------------------------------------------------|:----------|:---------|
| ^5.0.0-dev | [3.next](https://github.com/cakedc/enum/tree/3next) | 3.0.0-dev | unstable |
| ^4.0 | [2.next](https://github.com/cakedc/enum/tree/2.next) | 2.0.4 | stable |
| ^3.7 | [master](https://github.com/cakedc/enum/tree/master) | 1.5.0 | stable |
| ^3.7 | [develop](https://github.com/cakedc/enum/tree/develop) | - | unstable |
| ^3.6 | [master](https://github.com/cakedc/enum/tree/1.4.0) | 1.4.0 | stable |
| 3.1 - 3.5 | | 1.3.0 | stable |


Install
Expand All @@ -26,7 +27,7 @@ Install
Using [Composer](http://getcomposer.org):

```
composer require cakedc/enum:2.0.0
composer require cakedc/enum:3.0.0-dev
```

You then need to load the plugin. You can use the shell command:
Expand All @@ -35,17 +36,17 @@ You then need to load the plugin. You can use the shell command:
bin/cake plugin load CakeDC/Enum
```

or by manually adding statement shown below to `bootstrap.php`:
or by manually adding statement shown below to `Application::bootstrap()` method:

```php
Plugin::load('CakeDC/Enum');
$this->addPlugin('CakeDC/Enum');
```

Requirements
------------

* CakePHP 4.0+
* PHP 7.3+
* CakePHP 5.0+
* PHP 8.1+

Documentation
-------------
Expand All @@ -67,6 +68,6 @@ This repository follows the [CakeDC Plugin Standard](http://cakedc.com/plugin-st
License
-------

Copyright 2015 - 2021 Cake Development Corporation (CakeDC). All rights reserved.
Copyright 2015 - 2023 Cake Development Corporation (CakeDC). All rights reserved.

Licensed under the [MIT](http://www.opensource.org/licenses/mit-license.php) License. Redistributions of the source code included in this repository must retain the copyright notice found in each file.
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -21,7 +21,8 @@
"phpunit/phpunit": "^9.5.19",
"cakephp/cakephp-codesniffer": "^5.0",
"cakephp/bake": "3.x-dev",
"cakephp/migrations": "4.x-dev"
"cakephp/migrations": "4.x-dev",
"aura/intl": "^3.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion config/Migrations/001_create_enum_lookups.php
Expand Up @@ -8,7 +8,7 @@ class CreateEnumLookups extends AbstractMigration
*
* @return void
*/
public function change()
public function change(): void
{
$table = $this->table('enum_lookups');

Expand Down
13 changes: 4 additions & 9 deletions src/Plugin.php → src/EnumPlugin.php
Expand Up @@ -2,25 +2,20 @@
declare(strict_types=1);

/**
* Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace CakeDC\Enum;

use Cake\Core\BasePlugin;

class Plugin extends BasePlugin
class EnumPlugin extends BasePlugin
{
/**
* Plugin name.
*
* @var string
*/
protected $name = 'BootstrapUI';

}
53 changes: 27 additions & 26 deletions src/Model/Behavior/EnumBehavior.php
Expand Up @@ -2,20 +2,21 @@
declare(strict_types=1);

/**
* Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

namespace CakeDC\Enum\Model\Behavior;

use ArrayObject;
use BadMethodCallException;
use Cake\Event\EventInterface;use Cake\ORM\Behavior;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\Utility\Hash;
Expand Down Expand Up @@ -64,7 +65,7 @@ class EnumBehavior extends Behavior
*
* @var array
*/
protected $_defaultConfig = [
protected array $_defaultConfig = [
'defaultStrategy' => 'lookup',
'translate' => false,
'translationDomain' => 'default',
Expand All @@ -80,7 +81,7 @@ class EnumBehavior extends Behavior
*
* @var array
*/
protected $_classMap = [
protected array $classMap = [
'lookup' => LookupStrategy::class,
'const' => ConstStrategy::class,
'config' => ConfigStrategy::class,
Expand All @@ -91,7 +92,7 @@ class EnumBehavior extends Behavior
*
* @var array
*/
protected $_strategies = [];
protected array $strategies = [];

/**
* Initializes the behavior.
Expand All @@ -102,7 +103,7 @@ class EnumBehavior extends Behavior
public function initialize(array $config): void
{
parent::initialize($config);
$this->_normalizeConfig();
$this->normalizeConfig();
}

/**
Expand All @@ -113,39 +114,39 @@ public function initialize(array $config): void
* @return \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface
* @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumStrategyException
*/
public function strategy(string $alias, $strategy): \CakeDC\Enum\Model\Behavior\Strategy\StrategyInterface
public function strategy(string $alias, mixed $strategy): StrategyInterface
{
if (!empty($this->_strategies[$alias])) {
return $this->_strategies[$alias];
if (!empty($this->strategies[$alias])) {
return $this->strategies[$alias];
}

$this->_strategies[$alias] = $strategy;
$this->strategies[$alias] = $strategy;

if ($strategy instanceof StrategyInterface) {
return $strategy;
}

$class = null;
if (isset($this->_classMap[$strategy])) {
$class = $this->_classMap[$strategy];
if (isset($this->classMap[$strategy])) {
$class = $this->classMap[$strategy];
}

if ($class === null || !class_exists($class)) {
throw new MissingEnumStrategyException([$class]);
}

return $this->_strategies[$alias] = new $class($alias, $this->_table);
return $this->strategies[$alias] = new $class($alias, $this->_table);
}

/**
* Normalizes the strategies configuration and initializes the strategies.
*
* @return void
*/
protected function _normalizeConfig(): void
protected function normalizeConfig(): void
{
$classMap = $this->getConfig('classMap');
$this->_classMap = array_merge($this->_classMap, $classMap);
$this->classMap = array_merge($this->classMap, $classMap);

$lists = $this->getConfig('lists');
$defaultStrategy = $this->getConfig('defaultStrategy');
Expand Down Expand Up @@ -175,19 +176,19 @@ protected function _normalizeConfig(): void
}

/**
* @param string|array|null $alias Defined list's alias/name.
* @param array|string|null $alias Defined list's alias/name.
* @return array
* @throws \CakeDC\Enum\Model\Behavior\Exception\MissingEnumConfigurationException
*/
public function enum($alias = null): array
public function enum(array|string|null $alias = null): array
{
if (is_string($alias)) {
$config = $this->getConfig('lists.' . $alias);
if (empty($config)) {
throw new MissingEnumConfigurationException([$alias]);
}

return $this->_enumList($alias, $config);
return $this->enumList($alias, $config);
}

$lists = $this->getConfig('lists');
Expand All @@ -197,7 +198,7 @@ public function enum($alias = null): array

$return = [];
foreach ($lists as $alias => $config) {
$return[$alias] = $this->_enumList($alias, $config);
$return[$alias] = $this->enumList($alias, $config);
}

return $return;
Expand All @@ -208,11 +209,11 @@ public function enum($alias = null): array
* @param array $config Config
* @return array
*/
protected function _enumList(string $alias, array $config): array
protected function enumList(string $alias, array $config): array
{
$return = $this->strategy($alias, $config['strategy'])->enum($config);
if ($this->getConfig('translate')) {
$return = $this->_translate($return);
$return = $this->translate($return);
}

if ($this->getConfig('nested')) {
Expand All @@ -235,7 +236,7 @@ function (&$item, $val) {
* @param array $list List.
* @return array
*/
protected function _translate(array $list): array
protected function translate(array $list): array
{
$domain = $this->getConfig('translationDomain');

Expand All @@ -249,7 +250,7 @@ protected function _translate(array $list): array
* @param \Cake\ORM\RulesChecker $rules Rules checker.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(EventInterface $event, RulesChecker $rules): \Cake\ORM\RulesChecker
public function buildRules(EventInterface $event, RulesChecker $rules): RulesChecker
{
foreach ($this->getConfig('lists') as $alias => $config) {
if (Hash::get($config, 'applicationRules') === false) {
Expand Down Expand Up @@ -277,7 +278,7 @@ public function buildRules(EventInterface $event, RulesChecker $rules): \Cake\OR
*/
public function __call(string $method, array $args): bool
{
if (strpos($method, 'isValid') !== 0) {
if (!str_starts_with($method, 'isValid')) {
throw new BadMethodCallException(sprintf('Call to undefined method (%s)', $method));
}

Expand All @@ -302,7 +303,7 @@ public function __call(string $method, array $args): bool
* @param \ArrayObject $options The options for the query
* @return void
*/
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options)
public function beforeFind(EventInterface $event, Query $query, ArrayObject $options): void
{
foreach ($this->getConfig('lists') as $alias => $config) {
$strategy = $this->strategy($alias, $config['strategy']);
Expand Down
6 changes: 3 additions & 3 deletions src/Model/Behavior/Exception/InvalidAliasListException.php
Expand Up @@ -2,12 +2,12 @@
declare(strict_types=1);

/**
* Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

Expand All @@ -17,5 +17,5 @@

class InvalidAliasListException extends CakeException
{
protected $_messageTemplate = 'Invalid alias %s. %s defined association %s.';
protected string $_messageTemplate = 'Invalid alias %s. %s defined association %s.';
}
Expand Up @@ -2,12 +2,12 @@
declare(strict_types=1);

/**
* Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2015 - 2019, Cake Development Corporation (http://cakedc.com)
* @copyright Copyright 2015 - 2023, Cake Development Corporation (http://cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/

Expand All @@ -17,5 +17,5 @@

class MissingEnumConfigurationException extends CakeException
{
protected $_templateMessage = 'Missing enum configuration (%s)';
protected string $_messageTemplate = 'Missing enum configuration (%s)';
}

0 comments on commit 1bcc10a

Please sign in to comment.