Skip to content

Commit 4d9e3f8

Browse files
committed
revert toClass changes
1 parent d9cfe69 commit 4d9e3f8

2 files changed

Lines changed: 10 additions & 33 deletions

File tree

LibreNMS/Util/StringHelpers.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626

2727
namespace LibreNMS\Util;
2828

29-
use Illuminate\Support\Str;
30-
3129
class StringHelpers
3230
{
3331
public static function niceCase($string)
@@ -132,13 +130,13 @@ public static function inferEncoding(?string $string): ?string
132130
*/
133131
public static function toClass(string $name, ?string $namespace = null): string
134132
{
135-
$numbers = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
133+
$pre_format = str_replace(['-', '_'], ' ', $name);
134+
$class = str_replace(' ', '', ucwords(strtolower($pre_format)));
135+
$class = preg_replace_callback('/^(\d)(.)/', function ($matches) {
136+
$numbers = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine'];
136137

137-
$class = Str::of($name)
138-
->replaceMatches('/^(\d)(.)/', fn (array $m) => $numbers[$m[1]] . strtoupper((string) $m[2]))
139-
->headline()
140-
->replace(' ', '')
141-
->toString();
138+
return $numbers[$matches[1]] . strtoupper($matches[2]);
139+
}, $class);
142140

143141
return $namespace . $class;
144142
}

tests/CommonFunctionsTest.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -112,32 +112,11 @@ public function testDisplay(): void
112112

113113
public function testStringToClass(): void
114114
{
115-
// namespace
116-
$this->assertSame('LibreNMS\OS\Os', StringHelpers::toClass('os', 'LibreNMS\\OS\\'));
115+
$this->assertSame('LibreNMS\OS\Os', StringHelpers::toClass('OS', 'LibreNMS\\OS\\'));
116+
$this->assertSame('SpacesName', StringHelpers::toClass('spaces name', null));
117+
$this->assertSame('DashName', StringHelpers::toClass('dash-name', null));
118+
$this->assertSame('UnderscoreName', StringHelpers::toClass('underscore_name', null));
117119
$this->assertSame('LibreNMS\\AllOfThemName', StringHelpers::toClass('all OF-thEm_NaMe', 'LibreNMS\\'));
118-
$this->assertSame('App\\Models\\FooBar', StringHelpers::toClass('foo_bar', 'App\\Models\\'));
119-
120-
// delimiters
121-
$this->assertSame('SpacesName', StringHelpers::toClass('spaces name'));
122-
$this->assertSame('DashName', StringHelpers::toClass('dash-name'));
123-
$this->assertSame('UnderscoreName', StringHelpers::toClass('underscore_name'));
124-
$this->assertSame('FooBarBaz', StringHelpers::toClass('foo-bar_baz'));
125-
126-
// camel/pascal input
127-
$this->assertSame('FooBar', StringHelpers::toClass('fooBar'));
128-
$this->assertSame('FooBar', StringHelpers::toClass('FooBar'));
129-
$this->assertSame('MyHTTPRequest', StringHelpers::toClass('myHTTPRequest'));
130-
131-
// numeric prefix
132-
$this->assertSame('ZeroClass', StringHelpers::toClass('0_class'));
133-
$this->assertSame('ZeroName', StringHelpers::toClass('0name'));
134-
$this->assertSame('OneClass', StringHelpers::toClass('1_class'));
135-
$this->assertSame('NineClass', StringHelpers::toClass('9_class'));
136-
$this->assertSame('Foo2Bar', StringHelpers::toClass('foo_2_bar'));
137-
138-
// single word / edge cases
139-
$this->assertSame('Foo', StringHelpers::toClass('foo'));
140-
$this->assertSame('FOOBAR', StringHelpers::toClass('FOOBAR'));
141120
}
142121

143122
public function testIsValidHostname(): void

0 commit comments

Comments
 (0)