-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a22d98c
commit f56348f
Showing
9 changed files
with
122 additions
and
346 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,7 @@ | |
|
||
namespace nguyenanhung\Classes\Helper; | ||
|
||
use InvalidArgumentException; | ||
use BadMethodCallException; | ||
use nguyenanhung\Libraries\BoolType\BoolType as BaseBoolType; | ||
|
||
if (!class_exists('nguyenanhung\Classes\Helper\BoolType')) { | ||
/** | ||
|
@@ -20,192 +19,8 @@ | |
* @author 713uk13m <[email protected]> | ||
* @copyright 713uk13m <[email protected]> | ||
*/ | ||
class BoolType | ||
class BoolType extends BaseBoolType | ||
{ | ||
/** | ||
* Returns $bool value in the string $format | ||
* | ||
* I'll return a bool value as a true-false, yes-no, or on-off string. | ||
* | ||
* For example: | ||
* | ||
* BoolType::boolToString(true); // returns (string) 'true' | ||
* BoolType::boolToString(true, 'yes-no'); // returns (string) 'true' | ||
* BoolType::boolToString(false, 'on-off'); // returns (string) 'off' | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param bool $bool the boolean value to convert | ||
* @param string $format the string format to convert to (possible values are | ||
* 't[/-]f', true[/-]false', 'y[/-]n', 'yes[/-]no', 'o[/-o]', and 'on[/-]off') | ||
* (case-insensitive) (optional; if omitted, defaults to 'true-false') | ||
* | ||
* @return string the string value | ||
* | ||
* @throws \BadMethodCallException if $bool is null | ||
* @throws \InvalidArgumentException if $bool is not a (bool) value | ||
* @throws \InvalidArgumentException if $format is not a string | ||
* @throws \InvalidArgumentException if $format is not a valid format | ||
*/ | ||
public static function boolToString($bool, $format = 'true-false'): string | ||
{ | ||
$string = false; | ||
|
||
// if $bool and format are not null | ||
if ($bool !== null && $format !== null) { | ||
// if $bool is actually a bool | ||
if (is_bool($bool)) { | ||
// if $format is a string | ||
if (is_string($format)) { | ||
// switch on the lower-case $format | ||
switch (strtolower($format)) { | ||
|
||
case 'oo': | ||
case 'o/o': | ||
case 'o-o': | ||
case 'onoff': | ||
case 'on/off': | ||
case 'on-off': | ||
$string = $bool ? 'on' : 'off'; | ||
break; | ||
|
||
case 'tf': | ||
case 't/f': | ||
case 't-f': | ||
case 'truefalse': | ||
case 'true/false': | ||
case 'true-false': | ||
$string = $bool ? 'true' : 'false'; | ||
break; | ||
|
||
case 'yn': | ||
case 'y/n': | ||
case 'y-n': | ||
case 'yesno': | ||
case 'yes/no': | ||
case 'yes-no': | ||
$string = $bool ? 'yes' : 'no'; | ||
break; | ||
|
||
default: | ||
throw new InvalidArgumentException(__METHOD__ . "() expects parameter two, format, to be one of the following: " . "'t[/-]f', 'true[/-]false', 'y[/-]s', 'yes[/-]no', 'o[/-]o', or " . "'on[/-]off', '$format' given"); | ||
} | ||
} else { | ||
throw new InvalidArgumentException(__METHOD__ . "() expects parameter two, format, to be a string"); | ||
} | ||
} else { | ||
throw new InvalidArgumentException(__METHOD__ . "() expects parameter one, bool, to be a bool value given"); | ||
} | ||
} else { | ||
throw new BadMethodCallException(__METHOD__ . "() expects one or two parameters, a bool value and a string format"); | ||
} | ||
|
||
return $string; | ||
} | ||
|
||
/** | ||
* Returns the boolean value of $var | ||
* | ||
* PHP's native boolval() function is not available before PHP 5.5, and it does not | ||
* support the strings 'yes', 'no', 'on', or 'off'. | ||
* | ||
* I follow the following rules: | ||
* | ||
* Strings | ||
* The strings "yes", "true", "on", or "1" are considered true, and | ||
* the strings "no", "false", "off", or "0" are considered false | ||
* (case-insensitive). Any other non-empty string is true. | ||
* | ||
* Numbers | ||
* The numbers 0 or 0.0 are considered false. Any other number is | ||
* considered true. | ||
* | ||
* Array | ||
* An empty array is considered false. Any other array (even an | ||
* associative array with no values) is considered true. | ||
* | ||
* Object | ||
* Any object is considered true. | ||
* | ||
* For example... | ||
* | ||
* BoolType::val(""); // returns (bool) false | ||
* BoolType::val(true); // returns (bool) true | ||
* BoolType::val(0); // returns (bool) false | ||
* BoolType::val(0.0); // returns (bool) false | ||
* BoolType::val('0'); // returns (bool) false | ||
* BoolType::val('abc'); // returns (bool) true | ||
* BoolType::val('true'); // returns (bool) true | ||
* BoolType::val('on'); // returns (bool) true | ||
* BoolType::val('yes'); // returns (bool) true | ||
* BoolType::val('off'); // returns (bool) false | ||
* BoolType::val([]); // returns (bool) false | ||
* BoolType::val([1, 2]); // returns (bool) true | ||
* BoolType::val(new StdClass()); // returns (bool) true | ||
* | ||
* @since 0.1.0 | ||
* | ||
* @param mixed $var the variable to test | ||
* | ||
* @return bool|null the bool value | ||
* | ||
* @see http://www.php.net/manual/en/function.boolval.php boolval() man page | ||
*/ | ||
public static function val($var) | ||
{ | ||
$value = null; | ||
|
||
// if $var is not empty | ||
// any value considered empty by empty() is considered false | ||
// for example, "0", array(), "", etc | ||
// | ||
if (!empty($var)) { | ||
// if $var is not already a bool type | ||
if (!is_bool($var)) { | ||
// if $var is a string | ||
if (is_string($var)) { | ||
// switch on the string | ||
// the strings '1', 'on', 'yes', and 'true' are considered true | ||
// the strings '0', 'no', 'off', and 'false' are considered false | ||
// any other non-empty string is true | ||
// | ||
switch (strtolower($var)) { | ||
|
||
case '1': | ||
case 'on': | ||
case 'yes': | ||
case 'true': | ||
$value = true; | ||
break; | ||
|
||
case '0': | ||
case 'no': | ||
case 'off': | ||
case 'false': | ||
$value = false; | ||
break; | ||
|
||
default: | ||
$value = !empty($var); | ||
} | ||
} elseif (is_numeric($var)) { | ||
// any non-zero integer or float is considered true | ||
$value = ($var !== 0 && $var !== 0.0); | ||
} elseif (is_object($var)) { | ||
// any object is considered true | ||
$value = true; | ||
} elseif (is_array($var)) { | ||
// any non-empty array is considered true | ||
$value = !empty($var); | ||
} | ||
} else { | ||
$value = $var; | ||
} | ||
} else { | ||
$value = false; | ||
} | ||
|
||
return $value; | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -27,37 +27,14 @@ class Common implements ProjectInterface | |
|
||
use Version; | ||
|
||
/** | ||
* Function dump | ||
* | ||
* @param $str | ||
* | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
* @time : 30/12/2022 12:51 | ||
*/ | ||
public function dump($str) | ||
{ | ||
echo "<pre>"; | ||
var_dump($str); | ||
echo "</pre>"; | ||
return dump($str); | ||
} | ||
|
||
/** | ||
* Function dump_die | ||
* | ||
* @param $str | ||
* | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
* @time : 30/12/2022 13:17 | ||
*/ | ||
public function dump_die($str) | ||
{ | ||
echo "<pre>"; | ||
var_dump($str); | ||
echo "</pre>"; | ||
die; | ||
dump_die($str); | ||
} | ||
|
||
/** | ||
|
@@ -220,9 +197,7 @@ public function directoryMap($source_dir, $directory_depth = 0, $hidden = false) | |
*/ | ||
public function newFolder($pathname = '', $mode = 0777): bool | ||
{ | ||
$file = new File(); | ||
|
||
return $file->createNewFolder($pathname, $mode); | ||
return (new File())->createNewFolder($pathname, $mode); | ||
} | ||
|
||
/** | ||
|
@@ -237,9 +212,7 @@ public function newFolder($pathname = '', $mode = 0777): bool | |
*/ | ||
public function formatSizeUnits($bytes = 0): string | ||
{ | ||
$file = new File(); | ||
|
||
return $file->formatSizeUnits($bytes); | ||
return (new File())->formatSizeUnits($bytes); | ||
} | ||
|
||
/** | ||
|
@@ -684,6 +657,23 @@ public function highlightPhrase($str = '', $phrase = '', $tag_open = '<mark>', $ | |
return TextProcessor::highlightPhrase($str, $phrase, $tag_open, $tag_close); | ||
} | ||
|
||
/** | ||
* Keyword Highlighter | ||
* | ||
* Highlights a keyword within a text string | ||
* | ||
* @param string $str the text string | ||
* @param string $phrase the phrase you'd like to highlight | ||
* @param string $tag_open the opening tag to precede the phrase with | ||
* @param string $tag_close the closing tag to end the phrase with | ||
* | ||
* @return string | ||
*/ | ||
public function highlightKeyword($str = '', $phrase = '', $tag_open = '<mark>', $tag_close = '</mark>'): string | ||
{ | ||
return TextProcessor::highlightKeyword($str, $phrase, $tag_open, $tag_close); | ||
} | ||
|
||
/** | ||
* Function convertStrToEn | ||
* | ||
|
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 |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
|
||
namespace nguyenanhung\Classes\Helper; | ||
|
||
use nguyenanhung\Libraries\Email\Email as BaseEmail; | ||
|
||
if (!class_exists('nguyenanhung\Classes\Helper\Email')) { | ||
/** | ||
* Class Email | ||
|
@@ -18,30 +20,8 @@ | |
* @author 713uk13m <[email protected]> | ||
* @copyright 713uk13m <[email protected]> | ||
*/ | ||
class Email implements ProjectInterface | ||
class Email extends BaseEmail implements ProjectInterface | ||
{ | ||
use Version; | ||
|
||
/** | ||
* Function validateEmail | ||
* | ||
* @param $email | ||
* | ||
* @return bool | ||
* @author : 713uk13m <[email protected]> | ||
* @copyright: 713uk13m <[email protected]> | ||
* @time : 07/28/2021 59:25 | ||
*/ | ||
public static function validateEmail($email): bool | ||
{ | ||
// Remove all illegal characters from email | ||
$email = filter_var($email, FILTER_SANITIZE_EMAIL); | ||
|
||
if (filter_var($email, FILTER_VALIDATE_EMAIL)) { | ||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.