Skip to content

Commit

Permalink
feat: add isCLI() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
BernhardBaumrock committed Feb 11, 2024
1 parent 49af349 commit 5f637e5
Showing 1 changed file with 53 additions and 48 deletions.
101 changes: 53 additions & 48 deletions App/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,57 @@ public function backtrace()
echo "------------------\n";
}

/**
* Convert string to colonCase
* Converts FooBar to foo-bar
* Taken from PW Sanitizer
* @return string
*/
public static function colonCase($value, array $options = array())
{

$defaults = array(
'hyphen' => ':',
'allow' => 'a-z0-9',
'allowUnderscore' => false,
);

$options = array_merge($defaults, $options);
$value = (string)$value;
$hyphen = $options['hyphen'];

// if value is empty then exit now
if (!strlen($value)) return '';

if ($options['allowUnderscore']) $options['allow'] .= '_';

// check if value is already in the right format, and return it if so
if (strtolower($value) === $value) {
if ($options['allow'] === $defaults['allow']) {
if (ctype_alnum(str_replace($hyphen, '', $value))) return $value;
} else {
if (preg_match('/^[' . $hyphen . $options['allow'] . ']+$/', $value)) return $value;
}
}

// don’t allow apostrophes to be separators
$value = str_replace(array("'", ""), '', $value);
// some initial whitespace conversions to reduce workload on preg_replace
$value = str_replace(array(" ", "\r", "\n", "\t"), $hyphen, $value);
// convert everything not allowed to hyphens
$value = preg_replace('/[^' . $options['allow'] . ']+/i', $hyphen, $value);
// convert camel case to hyphenated
$value = preg_replace('/([[:lower:]])([[:upper:]])/', '$1' . $hyphen . '$2', $value);
// prevent doubled hyphens
$value = preg_replace('/' . $hyphen . $hyphen . '+/', $hyphen, $value);

if ($options['allowUnderscore']) {
$value = str_replace(array('-_', '_-'), '_', $value);
}

return strtolower(trim($value, $hyphen));
}

/**
* Check if a .ddev folder exists in pw root
* @return bool
Expand Down Expand Up @@ -167,55 +218,9 @@ public function html()
echo $this->browser->getInternalResponse()->getContent();
}

/**
* Convert string to colonCase
* Converts FooBar to foo-bar
* Taken from PW Sanitizer
* @return string
*/
public static function colonCase($value, array $options = array())
public function isCLI(): bool
{

$defaults = array(
'hyphen' => ':',
'allow' => 'a-z0-9',
'allowUnderscore' => false,
);

$options = array_merge($defaults, $options);
$value = (string)$value;
$hyphen = $options['hyphen'];

// if value is empty then exit now
if (!strlen($value)) return '';

if ($options['allowUnderscore']) $options['allow'] .= '_';

// check if value is already in the right format, and return it if so
if (strtolower($value) === $value) {
if ($options['allow'] === $defaults['allow']) {
if (ctype_alnum(str_replace($hyphen, '', $value))) return $value;
} else {
if (preg_match('/^[' . $hyphen . $options['allow'] . ']+$/', $value)) return $value;
}
}

// don’t allow apostrophes to be separators
$value = str_replace(array("'", ""), '', $value);
// some initial whitespace conversions to reduce workload on preg_replace
$value = str_replace(array(" ", "\r", "\n", "\t"), $hyphen, $value);
// convert everything not allowed to hyphens
$value = preg_replace('/[^' . $options['allow'] . ']+/i', $hyphen, $value);
// convert camel case to hyphenated
$value = preg_replace('/([[:lower:]])([[:upper:]])/', '$1' . $hyphen . '$2', $value);
// prevent doubled hyphens
$value = preg_replace('/' . $hyphen . $hyphen . '+/', $hyphen, $value);

if ($options['allowUnderscore']) {
$value = str_replace(array('-_', '_-'), '_', $value);
}

return strtolower(trim($value, $hyphen));
return php_sapi_name() === 'cli';
}

/**
Expand Down

0 comments on commit 5f637e5

Please sign in to comment.