Skip to content

Commit

Permalink
Moved installer utilities to traits and classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Dec 19, 2024
1 parent a63b55a commit b5f98b3
Show file tree
Hide file tree
Showing 13 changed files with 2,470 additions and 2,400 deletions.
2,413 changes: 147 additions & 2,266 deletions .vortex/installer/src/Command/InstallCommand.php

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions .vortex/installer/src/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace DrevOps\Installer;

/**
* Installer configuration.
*
* Installer config is a config of this installer script. For configs of the
* project being installed, @see get_answer().
*
* @package DrevOps\Installer
*/
class Config {

/**
* Installer configuration.
*
* @var array<string,mixed>
*/
protected array $config = [];

/**
* Get a configuration value or default.
*/
public function get(string $name, mixed $default = NULL): mixed {
return $this->config[$name] ?? $default;
}

/**
* Set a configuration value.
*/
public function set(string $name, mixed $value): void {
if (!is_null($value)) {
$this->config[$name] = $value;
}
}

}
72 changes: 72 additions & 0 deletions .vortex/installer/src/Converter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace DrevOps\Installer;

/**
* Converter.
*
* Convert strings to different formats.
*
* @package DrevOps\Installer
*/
class Converter {

public static function toCamelCase(string $value, bool $capitalise_first = FALSE): string {
$value = str_replace(' ', '', ucwords((string) preg_replace('/[^a-zA-Z0-9]/', ' ', $value)));

return $capitalise_first ? $value : lcfirst($value);
}

public static function toHumanName(string $value): ?string {
$value = preg_replace('/[^a-zA-Z0-9]/', ' ', $value);
$value = trim((string) $value);

return preg_replace('/\s{2,}/', ' ', $value);
}

/**
* Convert string to machine name.
*
* @param string $value
* Value to convert.
* @param array<int|string> $preserve_chars
* Array of characters to preserve.
*
* @return string
* Converted value.
*/
public static function toMachineName(string $value, array $preserve_chars = []): string {
$preserve = '';
foreach ($preserve_chars as $char) {
$preserve .= preg_quote(strval($char), '/');
}
$pattern = '/[^a-zA-Z0-9' . $preserve . ']/';

$value = preg_replace($pattern, '_', $value);

return strtolower($value);
}

public static function toAbbreviation(string $value, int $length = 2, string $word_delim = '_'): string {
$value = trim($value);
$value = str_replace(' ', '_', $value);
$parts = empty($word_delim) ? [$value] : explode($word_delim, $value);

if (count($parts) == 1) {
return strlen($parts[0]) > $length ? substr($parts[0], 0, $length) : $value;
}

$value = implode('', array_map(static function (string $word): string {
return substr($word, 0, 1);
}, $parts));

return substr($value, 0, $length);
}

public static function snakeToPascal(string $string): string {
return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
}

}
Loading

1 comment on commit b5f98b3

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.