Skip to content

Commit

Permalink
Merge pull request #143 from greg0ire/new-inflector-api
Browse files Browse the repository at this point in the history
Use new Inflector API
  • Loading branch information
greg0ire authored Jul 1, 2020
2 parents 6065a12 + a931dee commit ffa3a89
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
branches:
only:
- legacy-stable
- master
- unstable

language: php

php:
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"require": {
"php": "^7.1",
"doctrine/inflector": "^1.0"
"doctrine/inflector": "^1.4 || ^2.0"
},
"conflict": {
"symfony/form": "<3.2 || >=6.0 <999",
Expand Down
23 changes: 21 additions & 2 deletions src/AbstractEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Greg0ire\Enum;

use Doctrine\Common\Inflector\Inflector;
use Doctrine\Inflector\Inflector;
use Doctrine\Inflector\InflectorFactory;
use Greg0ire\Enum\Exception\InvalidEnumName;
use Greg0ire\Enum\Exception\InvalidEnumValue;

Expand All @@ -19,6 +20,11 @@ abstract class AbstractEnum

private static $constCache = [];

/**
* @var Inflector
*/
private static $inflector;

/**
* Uses reflection to find the constants defined in the class and cache
* them in a local property for performance, before returning them.
Expand Down Expand Up @@ -98,7 +104,11 @@ final public static function getClassPrefixedKeys(
?string $namespaceSeparator = null
): array {
$namespaceSeparator = $namespaceSeparator ?: static::$defaultNamespaceSeparator;
$classKey = str_replace('\\', $namespaceSeparator, Inflector::tableize(static::class));
$classKey = str_replace(
'\\',
$namespaceSeparator,
self::inflector()->tableize(static::class)
);

$keys = static::getKeys(function ($key) use ($namespaceSeparator, $classKey) {
return $classKey.$namespaceSeparator.$key;
Expand All @@ -111,6 +121,15 @@ final public static function getClassPrefixedKeys(
return $keys;
}

private static function inflector(): Inflector
{
if (!isset(self::$inflector)) {
self::$inflector = InflectorFactory::create()->build();
}

return self::$inflector;
}

/**
* Checks whether a constant with this name is defined.
*/
Expand Down

0 comments on commit ffa3a89

Please sign in to comment.