Skip to content

Commit

Permalink
feat: Bump minimum PHP version to 8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jawira committed Jun 13, 2022
1 parent 2f284c0 commit e04307b
Show file tree
Hide file tree
Showing 26 changed files with 141 additions and 269 deletions.
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ ij_visual_guides = 80, 120

[*.php]
indent_size = 4
max_line_length = 120
ij_php_align_multiline_chained_methods = true
ij_php_align_phpdoc_comments = true
ij_php_align_phpdoc_param_names = true
ij_php_align_assignments = true
ij_php_align_multiline_array_initializer_expression = true
ij_php_align_key_value_pairs = true
ij_php_align_match_arm_bodies = true
ij_php_phpdoc_use_fqcn = true

[*.{yml, yaml, neon, xml, svg, json, js}]
indent_size = 2
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/qa.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ jobs:
tests:
strategy:
matrix:
php: ['7.3','7.4','8.0','8.1']
runs-on: ubuntu-20.04
php: [ '7.4','8.0','8.1' ]
runs-on: ubuntu-22.04
steps:

- name: Setup PHP
Expand Down
39 changes: 20 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ Case converter

Use this library to convert string between:

| Name | Method | Output example |
| ------------- | --------------- | ----------------- |
| 🐪 Camel case | `toCamel()` | `myNameIsBond` |
| 👨‍🏫 Pascal case | `toPascal()` | `MyNameIsBond` |
| 🐍 Snake case | `toSnake()` | `my_name_is_bond` |
| 👩‍🏫 Ada case | `toAda()` | `My_Name_Is_Bond` |
| Ⓜ️ Macro case | `toMacro()` | `MY_NAME_IS_BOND` |
| 🥙 Kebab case | `toKebab()` | `my-name-is-bond` |
| 🚂 Train case | `toTrain()` | `My-Name-Is-Bond` |
| 🏦 Cobol case | `toCobol()` | `MY-NAME-IS-BOND` |
| 🔡 Lower case | `toLower()` | `my name is bond` |
| 🔠 Upper case | `toUpper()` | `MY NAME IS BOND` |
| 📰 Title case | `toTitle()` | `My Name Is Bond` |
| ✍️ Sentence case | `toSentence()` | `My name is bond` |
| ⚙️ Dot notation | `toDot()` | `my.name.is.bond` |
| Name | Method | Output example |
|-------------------|----------------|-------------------|
| 🐪 Camel case | `toCamel()` | `myNameIsBond` |
| 👨‍🏫 Pascal case | `toPascal()` | `MyNameIsBond` |
| 🐍 Snake case | `toSnake()` | `my_name_is_bond` |
| 👩‍🏫 Ada case | `toAda()` | `My_Name_Is_Bond` |
| Ⓜ️ Macro case | `toMacro()` | `MY_NAME_IS_BOND` |
| 🥙 Kebab case | `toKebab()` | `my-name-is-bond` |
| 🚂 Train case | `toTrain()` | `My-Name-Is-Bond` |
| 🏦 Cobol case | `toCobol()` | `MY-NAME-IS-BOND` |
| 🔡 Lower case | `toLower()` | `my name is bond` |
| 🔠 Upper case | `toUpper()` | `MY NAME IS BOND` |
| 📰 Title case | `toTitle()` | `My Name Is Bond` |
| ✍️ Sentence case | `toSentence()` | `My name is bond` |
| ⚙️ Dot notation | `toDot()` | `my.name.is.bond` |

Features:

Expand Down Expand Up @@ -52,8 +52,8 @@ Of course you can explicitly set the format of input string:
echo $hero->fromKebab()->toSnake(); // output: john_connor
```

You can also use the [provided factory][factory] to instantiate `Convert` class. A list of [all public methods] is also
available.
You can also use the [provided factory][factory] to instantiate `Convert` class.
A list of [all public methods] is also available.

i18n
----
Expand All @@ -74,7 +74,8 @@ $rus = new Convert('ОЧЕНЬ_ПРИЯТНО');
echo $rus->toCamel(); // output: оченьПриятно
```

`case-converter` is compatible with _Simple Case-Mapping_ and _Full Case-Mapping_.
`case-converter` is compatible with _Simple Case-Mapping_ and _Full
Case-Mapping_.
[Learn more about Case-Mapping][Case-Mapping].

Installation
Expand All @@ -94,7 +95,7 @@ Contributing

- If you liked this project, ⭐ star it on GitHub.
[![GitHub Repo stars](https://img.shields.io/github/stars/jawira/case-converter?style=social)](https://github.com/jawira/case-converter)
- Or follow me on Twitter.
- Or follow me on Twitter.
[![Twitter Follow](https://img.shields.io/twitter/follow/jawira?style=social)](https://twitter.com/jawira)

License
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
}
],
"require": {
"php": "^7.1 || ^8.0",
"php": ">=7.4",
"ext-mbstring": "*"
},
"require-dev": {
Expand Down
6 changes: 3 additions & 3 deletions docs/case-mapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Case-Mapping
Introduction
------------

> Case mapping or case conversion is a process whereby strings are converted
> to a particular form—uppercase, lowercase, or titlecase—possibly for display
> Case mapping or case conversion is a process whereby strings are converted
> to a particular form—uppercase, lowercase, or titlecase—possibly for display
> to the user.
PHP always performed _Simple Case-Mapping_, this is map one-to-one character
Expand All @@ -28,7 +28,7 @@ Please note that _Full Case-Mapping_ is locale dependent:

```php
// Turkish (requires appropriate locale)
$tur = new Convert('istambul');
$tur = new Convert('istambul');
echo $tur->toTrain(); // output: İstanbul
```

Expand Down
1 change: 0 additions & 1 deletion src/CaseConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*
* Factory class which returns a Convert object.
*
* @package Jawira\CaseConverter
* @author Jawira Portugal <dev@tugal.be>
*/
class CaseConverter implements CaseConverterInterface
Expand Down
5 changes: 2 additions & 3 deletions src/CaseConverterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

namespace Jawira\CaseConverter;

use Exception;
use RuntimeException;

class CaseConverterException extends Exception
class CaseConverterException extends RuntimeException
{

}
1 change: 0 additions & 1 deletion src/CaseConverterInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
/**
* Interface CaseConverterInterface
*
* @package Jawira\CaseConverter
* @author Jawira Portugal <dev@tugal.be>
*/
interface CaseConverterInterface
Expand Down
167 changes: 42 additions & 125 deletions src/Convert.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use Jawira\CaseConverter\Split\UnderscoreSplitter;
use Jawira\CaseConverter\Split\UppercaseSplitter;
use function is_subclass_of;
use function mb_strpos;
use function preg_match;

/**
Expand Down Expand Up @@ -77,25 +76,17 @@
*
* @see https://softwareengineering.stackexchange.com/questions/322413/bothered-by-an-unknown-letter-case-name
* @see http://www.unicode.org/charts/case/
* @package Jawira\CaseConverter
* @author Jawira Portugal <dev@tugal.be>
*/
class Convert
{
/**
* @var string Input string to convert
*/
protected $source;
/** @var string Input string to convert */
protected string $source;

/**
* @var string[] Words extracted from input string
*/
protected $words;
/** @var string[] Words extracted from input string */
protected array $words;

/**
* @var bool
*/
protected $forceSimpleCaseMapping;
protected bool $forceSimpleCaseMapping;

/**
* Constructor method
Expand All @@ -114,17 +105,17 @@ public function __construct(string $source)
/**
* Handle `to*` methods and `from*` methods
*
* @param string $methodName
* @param mixed[] $arguments
* @param string $methodName
* @param mixed[] $arguments
*
* @return string|\Jawira\CaseConverter\Convert
* @throws \Jawira\CaseConverter\CaseConverterException
*/
public function __call($methodName, $arguments)
{
if (0 === mb_strpos($methodName, 'from')) {
if (str_starts_with($methodName, 'from')) {
$result = $this->handleSplitterMethod($methodName);
} elseif (0 === mb_strpos($methodName, 'to')) {
} elseif (str_starts_with($methodName, 'to')) {
$result = $this->handleGluerMethod($methodName);
} else {
throw new CaseConverterException("Unknown method: $methodName");
Expand All @@ -136,7 +127,6 @@ public function __call($methodName, $arguments)
/**
* Auto-detect naming convention
*
* @return \Jawira\CaseConverter\Convert
* @throws \Jawira\CaseConverter\CaseConverterException
*/
public function fromAuto(): self
Expand Down Expand Up @@ -187,46 +177,19 @@ public function forceSimpleCaseMapping(): self
*
* @param string $input String to be analysed
*
* @throws \Jawira\CaseConverter\CaseConverterException
* @return \Jawira\CaseConverter\Split\Splitter
* @throws \Jawira\CaseConverter\CaseConverterException
*/
protected function analyse(string $input): Splitter
{
switch (true) {
case $this->contains($input, UnderscoreGluer::DELIMITER):
$splittingStrategy = new UnderscoreSplitter($input);
break;
case $this->contains($input, DashGluer::DELIMITER):
$splittingStrategy = new DashSplitter($input);
break;
case $this->contains($input, SpaceGluer::DELIMITER):
$splittingStrategy = new SpaceSplitter($input);
break;
case $this->contains($input, DotNotation::DELIMITER):
$splittingStrategy = new DotSplitter($input);
break;
case $this->isUppercaseWord($input):
$splittingStrategy = new UnderscoreSplitter($input);
break;
default:
$splittingStrategy = new UppercaseSplitter($input);
break;
}

return $splittingStrategy;
}

/**
* Return true if $needle is found in $input string
*
* @param string $input String where the search is performed
* @param string $needle Needle
*
* @return bool
*/
protected function contains(string $input, string $needle): bool
{
return is_int(mb_strpos($input, $needle));
return match (true) {
str_contains($input, UnderscoreGluer::DELIMITER) => new UnderscoreSplitter($input),
str_contains($input, DashGluer::DELIMITER) => new DashSplitter($input),
str_contains($input, SpaceGluer::DELIMITER) => new SpaceSplitter($input),
str_contains($input, DotNotation::DELIMITER) => new DotSplitter($input),
$this->isUppercaseWord($input) => new UnderscoreSplitter($input),
default => new UppercaseSplitter($input),
};
}

/**
Expand Down Expand Up @@ -279,33 +242,14 @@ protected function extractWords(Splitter $splitter): self
*/
protected function handleSplitterMethod(string $methodName): self
{
switch ($methodName) {
case 'fromCamel':
case 'fromPascal':
$splitterName = UppercaseSplitter::class;
break;
case 'fromSnake':
case 'fromAda':
case 'fromMacro':
$splitterName = UnderscoreSplitter::class;
break;
case 'fromKebab':
case 'fromTrain':
case 'fromCobol':
$splitterName = DashSplitter::class;
break;
case 'fromLower':
case 'fromUpper':
case 'fromTitle':
case 'fromSentence':
$splitterName = SpaceSplitter::class;
break;
case 'fromDot':
$splitterName = DotSplitter::class;
break;
default:
throw new CaseConverterException("Unknown method: $methodName");
}
$splitterName = match ($methodName) {
'fromCamel', 'fromPascal' => UppercaseSplitter::class,
'fromSnake', 'fromAda', 'fromMacro' => UnderscoreSplitter::class,
'fromKebab', 'fromTrain', 'fromCobol' => DashSplitter::class,
'fromLower', 'fromUpper', 'fromTitle', 'fromSentence' => SpaceSplitter::class,
'fromDot' => DotSplitter::class,
default => throw new CaseConverterException("Unknown method: $methodName"),
};

$splitter = $this->createSplitter($splitterName, $this->source);
$this->extractWords($splitter);
Expand Down Expand Up @@ -336,49 +280,22 @@ protected function createSplitter(string $className, string $source): Splitter
*/
protected function handleGluerMethod(string $methodName): string
{
switch ($methodName) {
case 'toAda':
$className = AdaCase::class;
break;
case 'toCamel':
$className = CamelCase::class;
break;
case 'toCobol':
$className = CobolCase::class;
break;
case 'toKebab':
$className = KebabCase::class;
break;
case 'toLower':
$className = LowerCase::class;
break;
case 'toMacro':
$className = MacroCase::class;
break;
case 'toPascal':
$className = PascalCase::class;
break;
case 'toSentence':
$className = SentenceCase::class;
break;
case 'toSnake':
$className = SnakeCase::class;
break;
case 'toTitle':
$className = TitleCase::class;
break;
case 'toTrain':
$className = TrainCase::class;
break;
case 'toUpper':
$className = UpperCase::class;
break;
case 'toDot':
$className = DotNotation::class;
break;
default:
throw new CaseConverterException("Unknown method: $methodName");
}
$className = match ($methodName) {
'toAda' => AdaCase::class,
'toCamel' => CamelCase::class,
'toCobol' => CobolCase::class,
'toKebab' => KebabCase::class,
'toLower' => LowerCase::class,
'toMacro' => MacroCase::class,
'toPascal' => PascalCase::class,
'toSentence' => SentenceCase::class,
'toSnake' => SnakeCase::class,
'toTitle' => TitleCase::class,
'toTrain' => TrainCase::class,
'toUpper' => UpperCase::class,
'toDot' => DotNotation::class,
default => throw new CaseConverterException("Unknown method: $methodName"),
};

$gluer = $this->createGluer($className, $this->words, $this->forceSimpleCaseMapping);

Expand Down
2 changes: 0 additions & 2 deletions src/Glue/AdaCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* Class AdaCase
*
* Outputs string in _Ada case_ format: This_Is_Ada_Case
*
* @package Jawira\CaseConverter\Glue
*/
class AdaCase extends UnderscoreGluer
{
Expand Down
Loading

0 comments on commit e04307b

Please sign in to comment.