diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..eca2e30 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,7 @@ +# +# Exclude files from exporting +# + +/.gitattributes export-ignore +/.github export-ignore +/.gitignore export-ignore diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index d73cbbd..b1c4618 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -14,7 +14,7 @@ jobs: strategy: fail-fast: true matrix: - php: [ 8.0, 7.4, 7.3, 7.2 ] + php: [ 8.0, 7.4 ] stability: [ prefer-lowest, prefer-stable ] name: PHP ${{ matrix.php }} - ${{ matrix.stability }} diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5c1f858 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,15 @@ +# Changelog +All notable changes to this project will be documented in this file. + +The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), +and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +## [2.1.0] - 2022-03-05 + +### Changes + +- Changed minimum required PHP version to 7.4. +- Add .gitattributes file. +- Update composer version constraints for composer/installers package. +- Change config.php code style. +- Add version constraint for october/system. diff --git a/Plugin.php b/Plugin.php index 8acfde4..1f7450c 100644 --- a/Plugin.php +++ b/Plugin.php @@ -1,14 +1,12 @@ Updates & Plugins > Install plugins and search for 'Hashids'. - ## Configuration To configure this plugin execute the following command: diff --git a/classes/Hashids.php b/classes/Hashids.php index dd89078..d905340 100644 --- a/classes/Hashids.php +++ b/classes/Hashids.php @@ -9,21 +9,28 @@ class Hashids implements HashidsInterface { - private $hashids; + private HashidsHelper $hashids; public function __construct(HashidsHelper $hashids) { $this->hashids = $hashids; } + /** + * @inheritDoc + */ public function encode(...$numbers): string { return $this->hashids->encode($numbers); } + /** + * @inheritDoc + */ public function decode($hash): int { $result = $this->hashids->decode($hash); + if (array_key_exists(0, $result)) { return (int) $result[0]; } @@ -31,11 +38,17 @@ public function decode($hash): int return 0; } + /** + * @inheritDoc + */ public function encodeHex($str): string { return $this->hashids->encodeHex($str); } + /** + * @inheritDoc + */ public function decodeHex($hash): string { return $this->hashids->decodeHex($hash); diff --git a/classes/HashidsManager.php b/classes/HashidsManager.php index 4861c72..adfe8ed 100644 --- a/classes/HashidsManager.php +++ b/classes/HashidsManager.php @@ -13,14 +13,9 @@ */ class HashidsManager { - protected $config; - - protected $factory; - - /** - * @var Hashids[] - */ - protected $instances = []; + protected array $instances = []; + protected Repository $config; + protected HashidsFactory $factory; public function __construct(Repository $config, HashidsFactory $factory) { @@ -31,7 +26,7 @@ public function __construct(Repository $config, HashidsFactory $factory) /** * @throws InvalidArgumentException */ - public function instance(string $name = null): Hashids + public function instance(?string $name = null): Hashids { $name = $name ?: $this->getDefaultInstance(); @@ -45,7 +40,7 @@ public function instance(string $name = null): Hashids /** * @throws InvalidArgumentException */ - public function reloadInstance(string $name = null): Hashids + public function reloadInstance(?string $name = null): Hashids { $name = $name ?: $this->getDefaultInstance(); @@ -54,7 +49,7 @@ public function reloadInstance(string $name = null): Hashids return $this->instance($name); } - public function removeInstance(string $name = null): void + public function removeInstance(?string $name = null): void { $name = $name ?: $this->getDefaultInstance(); unset($this->instances[$name]); @@ -73,13 +68,14 @@ public function setDefaultInstance(string $name): void /** * @throws InvalidArgumentException */ - public function getInstanceConfig(string $name = null): array + public function getInstanceConfig(?string $name = null): array { $name = $name ?: $this->getDefaultInstance(); $configurations = $this->config->get('hashids.configurations'); + if (!isset($configurations[$name]) || !is_array($configurations[$name])) { - throw new InvalidArgumentException("Configuration $name is not properly configured."); + throw new InvalidArgumentException(sprintf('Configuration %s is not properly configured.', $name)); } $config = $configurations[$name]; @@ -90,6 +86,7 @@ public function getInstanceConfig(string $name = null): array /** * @return mixed + * * @throws InvalidArgumentException */ public function __call(string $method, array $parameters) @@ -103,6 +100,7 @@ public function __call(string $method, array $parameters) protected function makeInstance(string $name): Hashids { $config = $this->getInstanceConfig($name); + return $this->createInstance($config); } diff --git a/composer.json b/composer.json index e8410d9..d9861cf 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,8 @@ { "name": "vdlp/oc-hashids-plugin", "description": "Allows developers to use secure hashed ID's in October CMS powered websites.", - "type": "october-plugin", "license": "GPL-2.0-only", + "type": "october-plugin", "authors": [ { "name": "Van der Let & Partners", @@ -10,13 +10,15 @@ } ], "require": { - "php": "^7.2||^8.0", + "php": "^7.4 || ^8.0", + "composer/installers": "^1.0 || ^2.0", "hashids/hashids": "^4.0", - "composer/installers": "^1.0" + "october/system": "^1.0 || ^2.0" }, "archive": { "exclude": [ ".gitignore", + ".github", ".idea/" ] } diff --git a/config.php b/config.php index b4b6379..17d8d35 100644 --- a/config.php +++ b/config.php @@ -28,6 +28,7 @@ */ 'configurations' => [ + 'main' => [ 'salt' => 'insert-salt-string-here', 'length' => 10, @@ -36,7 +37,9 @@ 'different-configuration' => [ 'salt' => 'insert-different-salt-string-here', 'length' => 100, - 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', # Default alphabet + 'alphabet' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890', ], + ], + ]; diff --git a/updates/version.yaml b/updates/version.yaml index 288be5e..0ef3202 100644 --- a/updates/version.yaml +++ b/updates/version.yaml @@ -1,7 +1,8 @@ -1.0.0: First version of Vdlp.Hashids -1.0.1: Add PHP >= 7.1 as dependency -1.1.0: +v1.0.0: First version of Vdlp.Hashids +v1.0.1: Add PHP >= 7.1 as dependency +v1.1.0: - Code optimizations - Moved service provider -2.0.0: Drop PHP 7.1 support -2.0.1: Update plugin dependencies +v2.0.0: Drop PHP 7.1 support +v2.0.1: Update plugin dependencies +v2.1.0: Maintenance release