Skip to content

Commit

Permalink
Add StaticInflector
Browse files Browse the repository at this point in the history
  • Loading branch information
olvlvl committed Nov 18, 2024
1 parent 6005be9 commit efd39be
Show file tree
Hide file tree
Showing 8 changed files with 371 additions and 60 deletions.
8 changes: 4 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ None

### New features

None
Added `StaticInflector`, that can be used instead of the helper functions.

### Backward Incompatible Changes

The file with the helper functions is no longer included in the autoload. You
need to include the file `vendor/icanboogie/inflector/lib/helpers.php` in your
`composer.json` if you want to continue using these functions.
The file with the helper functions is no longer included in the autoload. You need to include the
file `vendor/icanboogie/inflector/lib/helpers.php` in your `composer.json` if you want to continue
using these functions. Better use the new `StaticInflector` instead.

### Deprecated Features

Expand Down
8 changes: 8 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ FROM php:${PHP_TAG}

RUN <<-EOF
docker-php-ext-enable opcache

if [ "$PHP_VERSION" \< "7.4" ]; then
apt-get update
apt-get install -y autoconf pkg-config
pecl channel-update pecl.php.net
pecl install xdebug-2.9.0
docker-php-ext-enable xdebug
fi
EOF

RUN <<-EOF
Expand Down
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,20 @@ $inflector->is_uncountable("weather"); // true
$inflector->is_uncountable("cat"); // false
```

Helpers make it easy to use default locale inflections.
Static interfaces are also available:

```php
<?php

namespace ICanBoogie;

# Static inflector
echo StaticInflector::pluralize('child'); // "children"
echo StaticInflector::pluralize('genou', 'fr'); // "genoux"
echo StaticInflector::singularize('lærere', 'nb'); // "lærer"
echo StaticInflector::pluralize('üçgen', 'tr'); // "üçgenler"

# Helper functions
echo pluralize('child'); // "children"
echo pluralize('genou', 'fr'); // "genoux"
echo singularize('lærere', 'nb'); // "lærer"
Expand Down
103 changes: 103 additions & 0 deletions lib/StaticInflector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

namespace ICanBoogie;

/**
* A static inflector that uses {@see Inflector::get()} to get inflectors for the specified locale.
*/
final class StaticInflector
{
/**
* Returns a lowercase string.
*/
public static function downcase(string $str): string
{
return mb_strtolower($str);
}

/**
* Returns an uppercase string.
*/
public static function upcase(string $str): string
{
return mb_strtoupper($str);
}

/**
* Returns a copy of str with the first character converted to uppercase and the
* remainder to lowercase.
*
* @param bool $preserve_str_end Whether the string end should be preserved or downcased.
*/
public static function capitalize(string $str, bool $preserve_str_end = false): string
{
$end = mb_substr($str, 1);

if (!$preserve_str_end) {
$end = downcase($end);
}

return upcase(mb_substr($str, 0, 1)) . $end;
}

/**
* @see Inflector::pluralize()
*/
public static function pluralize(string $word, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->pluralize($word);
}

/**
* @see Inflector::singularize()
*/
public static function singularize(string $word, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->singularize($word);
}

/**
* @see Inflector::camelize()
*/
public static function camelize(
string $str,
bool $uppercase_first_letter = false,
string $locale = Inflector::DEFAULT_LOCALE
): string {
return Inflector::get($locale)->camelize($str, $uppercase_first_letter);
}

/**
* @see Inflector::underscore()
*/
public static function underscore(string $camel_cased_word, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->underscore($camel_cased_word);
}

/**
* @see Inflector::hyphenate()
*/
public static function hyphenate(string $str, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->hyphenate($str);
}

/**
* @see Inflector::humanize()
*/
public static function humanize(
string $lower_case_and_underscored_word,
string $locale = Inflector::DEFAULT_LOCALE
): string {
return Inflector::get($locale)->humanize($lower_case_and_underscored_word);
}

/**
* @see Inflector::titleize()
*/
public static function titleize(string $str, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->titleize($str);
}
}
49 changes: 20 additions & 29 deletions lib/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,100 +5,91 @@
// Avoid conflicts with ICanBoogie/Common
if (!function_exists(__NAMESPACE__ . '\downcase')) {
/**
* Returns a lowercase string.
* @see StaticInflector::downcase()
*/
function downcase(string $str): string
{
return mb_strtolower($str);
return StaticInflector::downcase($str);
}
}

// Avoid conflicts with ICanBoogie/Common
if (!function_exists(__NAMESPACE__ . '\upcase')) {
/**
* Returns an uppercase string.
* @see StaticInflector::upcase()
*/
function upcase(string $str): string
{
return mb_strtoupper($str);
return StaticInflector::upcase($str);
}
}

// Avoid conflicts with ICanBoogie/Common
if (!function_exists(__NAMESPACE__ . '\capitalize')) {
/**
* Returns a copy of str with the first character converted to uppercase and the
* remainder to lowercase.
*
* @param bool $preserve_str_end Whether the string end should be preserved or downcased.
* @see StaticInflector::capitalize()
*/
function capitalize(string $str, bool $preserve_str_end = false): string
{
$end = mb_substr($str, 1);

if (!$preserve_str_end) {
$end = downcase($end);
}

return upcase(mb_substr($str, 0, 1)) . $end;
return StaticInflector::capitalize($str, $preserve_str_end);
}
}

/**
* Forwards calls to `Inflector::get()->pluralize()`.
* @see StaticInflector::pluralize()
*/
function pluralize(string $word, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->pluralize($word);
return StaticInflector::pluralize($word, $locale);
}

/**
* Forwards calls to `Inflector::get()->singularize()`.
* @see StaticInflector::singularize()
*/
function singularize(string $word, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->singularize($word);
return StaticInflector::singularize($word, $locale);
}

/**
* Forwards calls to `Inflector::get()->camelize()`.
* @see StaticInflector::camelize()
*/
function camelize(
string $str,
bool $uppercase_first_letter = false,
string $locale = Inflector::DEFAULT_LOCALE
): string {
return Inflector::get($locale)->camelize($str, $uppercase_first_letter);
return StaticInflector::camelize($str, $uppercase_first_letter, $locale);
}

/**
* Forwards calls to `Inflector::get()->underscore()`.
* @see StaticInflector::underscore()
*/
function underscore(string $camel_cased_word, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->underscore($camel_cased_word);
return StaticInflector::underscore($camel_cased_word, $locale);
}

/**
* Forwards calls to `Inflector::get()->hyphenate()`.
* @see StaticInflector::hyphenate()
*/
function hyphenate(string $str, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->hyphenate($str);
return StaticInflector::hyphenate($str, $locale);
}

/**
* Forwards calls to `Inflector::get()->humanize()`.
* @see StaticInflector::humanize()
*/
function humanize(string $lower_case_and_underscored_word, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->humanize($lower_case_and_underscored_word);
return StaticInflector::humanize($lower_case_and_underscored_word, $locale);
}

/**
* Forwards calls to `Inflector::get()->titleize()`.
* @see StaticInflector::titleize()
*/
function titleize(string $str, string $locale = Inflector::DEFAULT_LOCALE): string
{
return Inflector::get($locale)->titleize($str);
return StaticInflector::titleize($str, $locale);
}
11 changes: 7 additions & 4 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# $schema: https://phpstan.olvlvl.com/schema.json
parameters:
level: max
paths:
- lib
- tests
level: max
paths:
- lib
- tests
ignoreErrors:
- "#::provide_.+ type array#"
Loading

0 comments on commit efd39be

Please sign in to comment.