Skip to content
Snippets Groups Projects
Commit 89bb587e authored by mrweiner's avatar mrweiner
Browse files

Issue #3287346: Automated Drupal 10 compatibility fixes

parent 0e22a649
No related branches found
No related tags found
1 merge request!1up ^D10 and remove dep on external strutils
......@@ -5,8 +5,7 @@
"homepage": "http://drupal.org/project/enum_generator",
"license": "GPL-2.0+",
"require": {
"nette/php-generator": "^3",
"codeartery/string-utils": "dev-master"
"nette/php-generator": "^3"
},
"minimum-stability": "dev"
}
......@@ -3,6 +3,6 @@ type: module
description: Helps to generate enum files
package: Custom
core: 8.x
core_version_requirement: ^8 || ^9
core_version_requirement: ^8 || ^9 || ^10
dependencies:
- taxonomy
......@@ -2,12 +2,12 @@
namespace Drupal\enum_generator\Form;
use CodeArtery\String\StrTo;
use Drupal;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\enum_generator\StrTo;
use Drupal\taxonomy\TermInterface;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpFile;
......
<?php
namespace Drupal\enum_generator;
/**
* String utils.
*
* Copied from https://github.com/rajeshmk/string-utils/blob/main/src/StrTo.php
* since the repo composer.json php version req is stale.
*/
class StrTo {
protected static array $real_words = [];
protected static array $slugable_cache = [];
/**
* Convert string to `TitleCase`.
*/
public static function title(string $string): string {
return mb_convert_case(static::realWords($string), MB_CASE_TITLE_SIMPLE, 'UTF-8');
}
/**
* Smart version of ucwords()
* "DB settings" => "DB Settings" (Not "Db Settings").
*/
public static function words(string $string): string {
$parts = explode(' ', static::realWords($string));
$uc_words = array_map(fn($value) => static::ucfirst($value), $parts);
return implode(' ', $uc_words);
}
/**
* Convert string to snake case.
*/
public static function snake(string $string): string {
return static::slugable($string, '_');
}
public static function kebab(string $string): string {
return static::slugable($string, '-');
}
public static function dotted(string $string): string {
return static::slugable($string, '.');
}
/**
* Convert string to StudlyCase.
*/
public static function studly(string $string): string {
return str_replace(' ', '', static::title($string));
}
/**
* Convert string to camelCase.
*/
public static function camel(string $string): string {
return static::lcfirst(static::studly($string));
}
/**
* Convert "admin/ModuleName/TestNamespace" to "admin.module-name.test-namespace".
*/
public static function dotPath(string $path): string {
$parts = explode('/', str_replace('\\', '/', $path));
$parts = array_map(fn($value) => static::slugable($value, '-'), $parts);
return implode('.', $parts);
}
/**
* Laravel's Str::limit() won't preserve words. Use this function in such cases.
*/
public static function slug(string $string, int $max_length = 120, string $lang = 'en'): string {
// Replace @ with the word 'at'
$string = str_replace('@', '-at-', $string);
// Convert Unicode characters to English text
if ('en' === $lang && extension_loaded('intl')) {
$string = transliterator_transliterate('Any-Latin; Latin-ASCII;', $string);
}
// Keep lower case words separated by SPACE itself
$string = static::slugable($string, ' ');
// Keep `$max_length` - Wordwrap separated by '@'
$wrapped_text = wordwrap($string, $max_length, '@', TRUE);
$string = str_replace(' ', '-', current(explode('@', $wrapped_text)));
// Remove non-alphanumeric characters
if ('en' === $lang) {
$string = preg_replace('/[^a-zA-Z0-9]+/', '-', $string);
}
return $string;
}
/**
* Convert the given string to upper case.
*/
public static function upper(string $string): string {
return mb_convert_case($string, MB_CASE_UPPER_SIMPLE, 'UTF-8');
}
/**
* Convert the given string to lower case.
*/
public static function lower(string $string): string {
return mb_convert_case($string, MB_CASE_LOWER_SIMPLE, 'UTF-8');
}
public static function ucfirst(string $string): string {
return static::upper(static::substr($string, 0, 1)) . static::substr($string, 1);
}
public static function lcfirst(string $string): string {
return static::lower(static::substr($string, 0, 1)) . static::substr($string, 1);
}
/**
* Returns the portion of the string specified by the start and length parameters.
*/
public static function substr(string $string, int $start, ?int $length = NULL): string {
return mb_substr($string, $start, $length, 'UTF-8');
}
// -------------------------------------------------------------------------
// Private functions
// -------------------------------------------------------------------------
private static function realWords(string $string): string {
$key = $string;
if (isset(static::$real_words[$key])) {
return static::$real_words[$key];
}
// Replace all characters (except letters, numbers and underscores) with space
$string = preg_replace('/[\W|_]+/u', ' ', $string);
// Convert camelCaseString to space separated words, without touching UPPER CASE WORDS
// $string = preg_replace('/([a-z])([A-Z])/', '$1 $2', $string);
// https://stackoverflow.com/a/7729790
/*
* $re_explained = '/(?#! splitCamelCase Rev:20140412)
* # Split camelCase "words". Two global alternatives. Either g1of2:
* (?<=[a-z]) # Position is after a lowercase,
* (?=[A-Z]) # and before an uppercase letter.
* | (?<=[A-Z]) # Or g2of2; Position is after uppercase,
* (?=[A-Z][a-z]) # and before upper-then-lower case.
* /x';
*/
// Smart conversion of upper case words
// Split "WebERP" to ['Web', 'ERP']
// "HDDCapacity" to ['HDD', 'Capacity']
$optimal_words = preg_split('/(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])/x', $string);
return static::$real_words[$key] = trim(implode(' ', $optimal_words));
}
private static function slugable(string $string, string $separator = '-'): string {
if (ctype_lower($string)) {
return $string;
}
if (!isset(static::$slugable_cache[$string])) {
static::$slugable_cache[$string] = static::lower(static::realWords($string));
}
return str_replace(' ', $separator, static::$slugable_cache[$string]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment