-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improving manifest middlewares, refactoring
- Loading branch information
1 parent
5df720d
commit 3d70839
Showing
14 changed files
with
191 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Dealroadshow\K8S\Framework\Util; | ||
|
||
class Str | ||
{ | ||
public static function asClassName(string $str): string | ||
{ | ||
$str = ucwords($str, " \t\r\n\f\v-_"); | ||
|
||
return preg_replace('/[\s\-_]+/', '', $str); | ||
} | ||
|
||
public static function camelCased(string $str): string | ||
{ | ||
return lcfirst(static::asClassName($str)); | ||
} | ||
|
||
public static function withoutSuffix(string $str, string $suffix): string | ||
{ | ||
return str_ends_with($str, $suffix) | ||
? substr($str, 0, -strlen($suffix)) | ||
: $str; | ||
} | ||
|
||
public static function withSuffix(string $className, string $suffix): string | ||
{ | ||
return static::withoutSuffix($className, $suffix).$suffix; | ||
} | ||
|
||
public static function asDirName(string $str, string $suffix = null): string | ||
{ | ||
$className = self::asClassName($str); | ||
|
||
return $suffix ? self::withoutSuffix($className, $suffix) : $className; | ||
} | ||
|
||
public static function asNamespace(object $object): string | ||
{ | ||
$reflection = new \ReflectionObject($object); | ||
$namespace = $reflection->getNamespaceName(); | ||
|
||
return trim($namespace, '\\'); | ||
} | ||
|
||
public static function asDir(object $object): string | ||
{ | ||
$reflection = new \ReflectionObject($object); | ||
|
||
return dirname($reflection->getFileName()); | ||
} | ||
|
||
public static function asDNSSubdomain(string $str): string | ||
{ | ||
$camel2dash = strtolower( | ||
preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $str) | ||
); | ||
$valid = preg_replace('/([^\w\-]|[_])+/', '', $camel2dash); | ||
|
||
if (0 === strlen($valid)) { | ||
throw new \InvalidArgumentException( | ||
sprintf('String "%s" cannot be converted DNS subdomain representation', $str) | ||
); | ||
} | ||
if (253 < strlen($valid)) { | ||
throw new \InvalidArgumentException( | ||
sprintf( | ||
'String "%s" DNS subdomain representation is too long (must be less than 253 characters)', | ||
$str | ||
) | ||
); | ||
} | ||
|
||
return $valid; | ||
} | ||
|
||
public static function isValidDNSSubdomain(string $str): bool | ||
{ | ||
return 253 > strlen($str) && 0 !== preg_match('/^[a-z0-9]+[a-z0-9\-.]+[a-z0-9]$/', $str); | ||
} | ||
} |