Skip to content

Commit

Permalink
Merge pull request #1 from rmunate/analysis-22dbO9
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
rmunate authored Jul 31, 2023
2 parents f5126ac + 115740f commit 0ab65d6
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 57 deletions.
18 changes: 13 additions & 5 deletions src/Bases/BaseSpellNumber.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Rmunate\Utilities\Bases;

use Exception;
use NumberFormatter;
use Rmunate\Utilities\Exceptions\SpellNumberExceptions;
use Rmunate\Utilities\Langs\Langs;
Expand All @@ -16,40 +15,49 @@ abstract class BaseSpellNumber
/**
* Creates an instance of the child class with the provided value for conversion.
*
* @param mixed $value The numeric value to convert to words.
* @param mixed $value The numeric value to convert to words.
* @param string $locale The locale to use for conversion. Default is 'es_ES' (Spanish).
* @return static An instance of the child class.
*
* @throws SpellNumberExceptions When the Intl extension is not available or the value is not valid (integer or double).
*
* @return static An instance of the child class.
*/
public static function value($value)
{
$type = SpellNumberValidator::check('mixed', $value)->result();

return new static($value, $type);
}

/**
* Creates an instance of the child class with the provided integer value for conversion.
*
* @param int $value The integer value to convert to words.
* @return static An instance of the child class.
*
* @throws SpellNumberExceptions When the Intl extension is not available or the value is not a valid integer.
*
* @return static An instance of the child class.
*/
public static function integer($value)
{
SpellNumberValidator::check('integer', $value)->result();

return new static($value, 'integer');
}

/**
* Creates an instance of the child class with the provided float value for conversion.
*
* @param float $value The float value to convert to words.
* @return static An instance of the child class.
*
* @throws SpellNumberExceptions When the Intl extension is not available or the value is not a valid float.
*
* @return static An instance of the child class.
*/
public static function float($value)
{
SpellNumberValidator::check('double', $value)->result();

return new static($value, 'double');
}

Expand Down
1 change: 1 addition & 0 deletions src/Bases/BaseSpellNumberValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ abstract class BaseSpellNumberValidator
* Create a new instance of the BaseValidator class for the specified data type.
*
* @param string $type The data type to validate.
*
* @return BaseValidator
*/
public static function check(string $type, $value)
Expand Down
8 changes: 5 additions & 3 deletions src/Exceptions/SpellNumberExceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ final class SpellNumberExceptions extends Exception
/**
* Static method to throw the exception from the calling class.
*
* @param string $message The exception message.
* @param int $code The exception code (optional).
* @param string $message The exception message.
* @param int $code The exception code (optional).
* @param Throwable|null $previous The previous exception (optional).
*
* @throws SpellNumberExceptions Changed "CustomException" to "SpellNumberExceptions".
*
* @return SpellNumberExceptions An instance of SpellNumberExceptions.
*/
public static function create($message, $code = 0, Throwable $previous = null)
{
return new self("SpellNumber - " . $message, $code, $previous);
return new self('SpellNumber - '.$message, $code, $previous);
}
}
34 changes: 17 additions & 17 deletions src/Langs/Langs.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ final class Langs
{
/**
* Array containing supported locales in ISO 639-1 format.
*
*
* @var array
*/
public const LOCALES = [
Expand All @@ -22,30 +22,30 @@ final class Langs

/**
* Array containing connectors for each language.
*
*
* @var array
*/
public const LOCALES_CONNECTORS = [
'en' => "and", // English from the United States: "and"
'es' => "con", // Spanish from Spain: "con"
'pt' => "com", // Portuguese from Portugal: "com"
'fr' => "et", // French from France: "et"
'it' => "con", // Italian from Italy: "con"
'ro' => "cu", // Romanian from Romania: "cu"
'en' => 'and', // English from the United States: "and"
'es' => 'con', // Spanish from Spain: "con"
'pt' => 'com', // Portuguese from Portugal: "com"
'fr' => 'et', // French from France: "et"
'it' => 'con', // Italian from Italy: "con"
'ro' => 'cu', // Romanian from Romania: "cu"
];

/**
* Array containing connectors for each language used when representing money.
*
*
* @var array
*/
public const LOCALES_CONNECTORS_MONEY = [
'en' => "of", // English from the United States: "of"
'es' => "de", // Spanish from Spain: "de"
'pt' => "de", // Portuguese from Portugal: "de"
'fr' => "de", // French from France: "de"
'it' => "di", // Italian from Italy: "di"
'ro' => "de", // Romanian from Romania: "de"
'en' => 'of', // English from the United States: "of"
'es' => 'de', // Spanish from Spain: "de"
'pt' => 'de', // Portuguese from Portugal: "de"
'fr' => 'de', // French from France: "de"
'it' => 'di', // Italian from Italy: "di"
'ro' => 'de', // Romanian from Romania: "de"
];

/**
Expand All @@ -61,8 +61,8 @@ public static function getLocaleLaravel()
if (in_array($iso639_1, self::LOCALES)) {
return $iso639_1;
}

// Return the default locale if not found in supported locales.
return self::LOCALES[0];
return self::LOCALES[0];
}
}
32 changes: 17 additions & 15 deletions src/Miscellaneous/Replaces.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ final class Replaces
'illion' => 'illion Of',
],
'es' => [
'illón' => 'illón De',
'illones' => 'illones De'
'illón' => 'illón De',
'illones' => 'illones De',
],
'pt' => [
'ilhão' => 'ilhão De',
'ilhões' => 'ilhões De'
'ilhão' => 'ilhão De',
'ilhões' => 'ilhões De',
],
'fr' => [
'illion' => 'illion De',
'illions' => 'illions De'
'illion' => 'illion De',
'illions' => 'illions De',
],
'it' => [
'ilione' => 'ilione Di',
'ilioni' => 'ilioni Di'
'ilioni' => 'ilioni Di',
],
'ro' => [
'ilion' => 'ilion De',
'ilioane' => 'ilioane De'
'ilion' => 'ilion De',
'ilioane' => 'ilioane De',
],
];

Expand All @@ -49,25 +49,27 @@ final class Replaces
*/
private const GENERAL_CASE = [
'es' => [
'un pesos' => 'un peso',
'un pesos' => 'un peso',
'un centavos' => 'un centavo',
],
];

/**
* Perform replacements in the given text based on the language and current currency.
*
* @param string $value The original text string.
* @param string $locale The language of the text.
* @param string $value The original text string.
* @param string $locale The language of the text.
* @param string $current The current currency.
* @return string The adjusted text with the replacements performed.
*
* @throws \InvalidArgumentException If the parameters are invalid.
*
* @return string The adjusted text with the replacements performed.
*/
public static function locale(string $value, string $locale, string $current): string
{
// Parameter validation
if (empty($value) || empty($locale) || empty($current)) {
throw new \InvalidArgumentException("All parameters must have a value.");
throw new \InvalidArgumentException('All parameters must have a value.');
}

// Replace final strings for each language
Expand All @@ -88,7 +90,7 @@ public static function locale(string $value, string $locale, string $current): s
}

// Assign the currency value
$value = Str::lower($value . ' ' . $current);
$value = Str::lower($value.' '.$current);

foreach ($replacesGeneral as $search => $replace) {
if (substr_compare($value, $search, -strlen($search)) === 0) {
Expand Down
14 changes: 12 additions & 2 deletions src/Miscellaneous/Utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class Utilities
* Check if a specific PHP extension is loaded.
*
* @param string $name The name of the PHP extension to check.
*
* @return bool True if the extension is loaded, false otherwise.
*/
public static function validateExtension(string $name): bool
Expand All @@ -24,6 +25,7 @@ public static function validateExtension(string $name): bool
* Check if the given value is numeric.
*
* @param mixed $value The value to check.
*
* @return bool True if the value is numeric, false otherwise.
*/
public static function isValidNumber($value): bool
Expand All @@ -35,6 +37,7 @@ public static function isValidNumber($value): bool
* Check if the given value is a string.
*
* @param mixed $value The value to check.
*
* @return bool True if the value is a string, false otherwise.
*/
public static function isValidString($value): bool
Expand All @@ -46,6 +49,7 @@ public static function isValidString($value): bool
* Check if the given value is an integer.
*
* @param mixed $value The value to check.
*
* @return bool True if the value is an integer, false otherwise.
*/
public static function isValidInteger($value): bool
Expand All @@ -57,6 +61,7 @@ public static function isValidInteger($value): bool
* Check if the given value is a float (double).
*
* @param mixed $value The value to check.
*
* @return bool True if the value is a float, false otherwise.
*/
public static function isValidDouble($value): bool
Expand All @@ -68,18 +73,18 @@ public static function isValidDouble($value): bool
* Check if the given value does not exceed the maximum allowed value.
*
* @param mixed $value The value to check.
*
* @return bool True if the value does not exceed the maximum, false otherwise.
*/
public static function isNotExceedMax($value): bool
{
//Validate scientific notation.
if(preg_match('/[+\-E]/i', strval($value)) > 0){
if (preg_match('/[+\-E]/i', strval($value)) > 0) {
return false;
}

// Implementation for checking if the value does not exceed the maximum is provided here.
if (self::isValidString($value) || self::isValidDouble($value)) {

$parts = explode('.', $value);
$integerPart = intval($parts[0]);
$decimalPart = intval($parts[1]);
Expand All @@ -97,6 +102,7 @@ public static function isNotExceedMax($value): bool
* Check if the given locale is valid and supported.
*
* @param string $locale The locale code to check.
*
* @return bool True if the locale is valid and supported, false otherwise.
*/
public static function isValidLocale($locale): bool
Expand All @@ -108,23 +114,27 @@ public static function isValidLocale($locale): bool
* Get the connector for the given locale to use in regular sentences.
*
* @param string $locale The locale code to get the connector for.
*
* @return string The connector for the given locale.
*/
public static function connector($locale): string
{
$connector = Langs::LOCALES_CONNECTORS;

return $connector[$locale];
}

/**
* Get the connector for the given locale to use in sentences related to money.
*
* @param string $locale The locale code to get the money connector for.
*
* @return string The money connector for the given locale.
*/
public static function connectorMoney($locale): string
{
$connector = Langs::LOCALES_CONNECTORS_MONEY;

return $connector[$locale];
}
}
Loading

0 comments on commit 0ab65d6

Please sign in to comment.