Skip to content

Commit fa2dcc5

Browse files
Create new string to bool converter
1 parent ac3f7b2 commit fa2dcc5

2 files changed

Lines changed: 27 additions & 3 deletions

File tree

src/Console/IOUtil.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ public static function mapConfigVerbosity(string $verbosity): int
5050
return self::$verbosityMap[strtolower($verbosity)] ?? OutputInterface::VERBOSITY_NORMAL;
5151
}
5252

53+
/**
54+
* Convert a string to boolean
55+
*
56+
* @param string $string The string to convert
57+
* @param bool $default Either look for true or false values
58+
* @return bool
59+
*/
60+
public static function stringToBool(string $string, bool $default = false): bool
61+
{
62+
// convert to lowercase to make it easier to compare
63+
$string = strtolower($string);
64+
65+
// if the default is true, we only look for false values
66+
// if we can't find any, we return true
67+
if ($default) {
68+
return !in_array($string, ['false', 'n', 'no', '0', 'off']);
69+
}
70+
// if the default is false, we only look for true values,
71+
// and if we can't find any, we return false'
72+
return in_array($string, ['true', 'y', 'yes', 'ok', '1', 'on']);
73+
}
74+
5375
/**
5476
* Convert a user answer to boolean
5577
*
@@ -58,11 +80,11 @@ public static function mapConfigVerbosity(string $verbosity): int
5880
*/
5981
public static function answerToBool(string $answer): bool
6082
{
61-
return in_array(strtolower($answer), ['y', 'yes', 'ok']);
83+
return self::stringToBool($answer, false);
6284
}
6385

6486
/**
65-
* Create formatted cli headline
87+
* Create a formatted cli headline
6688
*
6789
* ">>>> HEADLINE <<<<"
6890
* "==== HEADLINE ===="

src/Runner/Action/Cli/Command/Placeholder.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace CaptainHook\App\Runner\Action\Cli\Command;
1313

14+
use CaptainHook\App\Console\IOUtil;
15+
1416
/**
1517
* Class Placeholder
1618
*
@@ -112,7 +114,7 @@ public function options(): array
112114
public function isCacheable(): bool
113115
{
114116
if ($this->hasOption('cache')) {
115-
return !in_array(strtolower($this->option('cache')), ['n', 'no', 'false', '0', 'off']);
117+
return IOUtil::stringToBool($this->option('cache'), true);
116118
}
117119
return true;
118120
}

0 commit comments

Comments
 (0)