Skip to content

Commit

Permalink
Add SumOfTwoNumbersToWords mode
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed Sep 21, 2019
1 parent 9bbb5de commit f3bc4fe
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/DNTCaptcha.Core/CaptchaServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static void AddDNTCaptcha(
services.TryAddSingleton<HumanReadableIntegerProvider>();
services.TryAddSingleton<ShowDigitsProvider>();
services.TryAddSingleton<SumOfTwoNumbersProvider>();
services.TryAddSingleton<SumOfTwoNumbersToWordsProvider>();
services.TryAddSingleton<Func<DisplayMode, ICaptchaTextProvider>>(serviceProvider => key =>
{
switch (key)
Expand All @@ -35,6 +36,8 @@ public static void AddDNTCaptcha(
return serviceProvider.GetRequiredService<ShowDigitsProvider>();
case DisplayMode.SumOfTwoNumbers:
return serviceProvider.GetRequiredService<SumOfTwoNumbersProvider>();
case DisplayMode.SumOfTwoNumbersToWords:
return serviceProvider.GetRequiredService<SumOfTwoNumbersToWordsProvider>();
default:
throw new NotImplementedException($"Service of type {key} is not implemented.");
}
Expand Down
5 changes: 5 additions & 0 deletions src/DNTCaptcha.Core/DNTCaptchaImageController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,12 @@ public IActionResult Show(string data)
{
return BadRequest();
}

var model = _serializationProvider.Deserialize<CaptchaImageParams>(decryptedModel);
if (model == null)
{
return BadRequest();
}

var decryptedText = _captchaProtectionProvider.Decrypt(model.Text);
if (decryptedText == null)
Expand Down
3 changes: 1 addition & 2 deletions src/DNTCaptcha.Core/DNTCaptchaTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,7 @@ private TagBuilder getValidationMessageTagBuilder()

if (!ViewContext.ModelState.IsValid)
{
ModelStateEntry captchaInputNameValidationState;
if (ViewContext.ModelState.TryGetValue(CaptchaInputName, out captchaInputNameValidationState))
if (ViewContext.ModelState.TryGetValue(CaptchaInputName, out var captchaInputNameValidationState))
{
if (captchaInputNameValidationState.ValidationState == ModelValidationState.Invalid)
{
Expand Down
7 changes: 6 additions & 1 deletion src/DNTCaptcha.Core/Providers/DisplayMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public enum DisplayMode
/// <summary>
/// Display a numeric value as a sum of 2 numbers
/// </summary>
SumOfTwoNumbers
SumOfTwoNumbers,

/// <summary>
/// Display a numeric value as a sum of 2 numbers using the equivalent text
/// </summary>
SumOfTwoNumbersToWords
}
}
38 changes: 38 additions & 0 deletions src/DNTCaptcha.Core/Providers/SumOfTwoNumbersToWordsProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using DNTCaptcha.Core.Contracts;

namespace DNTCaptcha.Core.Providers
{
/// <summary>
/// SumOfTwoNumbersToWords Provider
/// </summary>
public class SumOfTwoNumbersToWordsProvider : ICaptchaTextProvider
{
private readonly int _randomNumber;
private readonly HumanReadableIntegerProvider _humanReadableIntegerProvider;

/// <summary>
/// SumOfTwoNumbersToWords Provider
/// </summary>
public SumOfTwoNumbersToWordsProvider(
IRandomNumberProvider randomNumberProvider,
HumanReadableIntegerProvider humanReadableIntegerProvider)
{
_randomNumber = randomNumberProvider.Next(1, 7);
_humanReadableIntegerProvider = humanReadableIntegerProvider;
}

/// <summary>
/// display a numeric value using the equivalent text
/// </summary>
/// <param name="number">input number</param>
/// <param name="language">local language</param>
/// <returns>the equivalent text</returns>
public string GetText(long number, Language language)
{
var text = number > _randomNumber ?
$"{_humanReadableIntegerProvider.NumberToText(number - _randomNumber, language)} + {_humanReadableIntegerProvider.NumberToText(_randomNumber, language)}" :
$"{_humanReadableIntegerProvider.NumberToText(0, language)} + {_humanReadableIntegerProvider.NumberToText(number, language)}";
return language == Language.Persian ? text.ToPersianNumbers() : text;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<dnt-captcha asp-captcha-generator-max="9000"
asp-captcha-generator-min="1"
asp-captcha-generator-language="English"
asp-captcha-generator-display-mode="NumberToWord"
asp-captcha-generator-display-mode="NumberToWord"
asp-use-relative-urls="true"
asp-placeholder="Security code as a number"
asp-validation-error-message="Please enter the security code as a number."
Expand Down
6 changes: 3 additions & 3 deletions src/DNTCaptcha.TestWebApp.V3x/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public IActionResult Index()
[ValidateDNTCaptcha(ErrorMessage = "Please enter the security code as a number.",
IsNumericErrorMessage = "The input value should be a number.",
CaptchaGeneratorLanguage = Language.English,
CaptchaGeneratorDisplayMode = DisplayMode.SumOfTwoNumbers)]
CaptchaGeneratorDisplayMode = DisplayMode.SumOfTwoNumbersToWords)]
public IActionResult Index([FromForm]AccountViewModel data)
{
if (ModelState.IsValid) // If `ValidateDNTCaptcha` fails, it will set a `ModelState.AddModelError`.
Expand All @@ -32,7 +32,7 @@ public IActionResult Index([FromForm]AccountViewModel data)
[ValidateDNTCaptcha(ErrorMessage = "Please enter the security code as a number.",
IsNumericErrorMessage = "The input value should be a number.",
CaptchaGeneratorLanguage = Language.English,
CaptchaGeneratorDisplayMode = DisplayMode.SumOfTwoNumbers)]
CaptchaGeneratorDisplayMode = DisplayMode.SumOfTwoNumbersToWords)]
public IActionResult Login2([FromForm]AccountViewModel data)
{
if (ModelState.IsValid) // If `ValidateDNTCaptcha` fails, it will set a `ModelState.AddModelError`.
Expand All @@ -47,7 +47,7 @@ public IActionResult Login2([FromForm]AccountViewModel data)
[ValidateDNTCaptcha(ErrorMessage = "Please enter the security code as a number.",
IsNumericErrorMessage = "The input value should be a number.",
CaptchaGeneratorLanguage = Language.English,
CaptchaGeneratorDisplayMode = DisplayMode.SumOfTwoNumbers)]
CaptchaGeneratorDisplayMode = DisplayMode.SumOfTwoNumbersToWords)]
public IActionResult Login3([FromForm]AccountViewModel data) // For Ajax Forms
{
if (ModelState.IsValid) // If `ValidateDNTCaptcha` fails, it will set a `ModelState.AddModelError`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
<dnt-captcha asp-captcha-generator-max="30"
asp-captcha-generator-min="1"
asp-captcha-generator-language="English"
asp-captcha-generator-display-mode="SumOfTwoNumbers"
asp-captcha-generator-display-mode="SumOfTwoNumbersToWords"
asp-use-relative-urls="true"
asp-placeholder="Security code as a number"
asp-validation-error-message="Please enter the security code as a number."
Expand Down

0 comments on commit f3bc4fe

Please sign in to comment.