thewebsolver/luhn-algorithm 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thewebsolver/luhn-algorithm

Composer 安装命令:

composer require thewebsolver/luhn-algorithm

包简介

PHP library to validate digits such as Credit Card Number, IMEI number, etc. with Luhn Algorithm

README 文档

README

📦 This package is a sub-repo split of a PHP Validator which is currently under development.

✨ This can also be installed as a dependency for Payment Card library to validate Debit/Credit Cards.

Introduction

Luhn Algorithm to validate Payment Cards, IMEI numbers and other digits that needs Luhn validation.

Usage

Install

composer require thewebsolver/luhn-algorithm

Validate

Validation can be performed using any of the three OPTIONS presented below:

use TheWebSolver\Codegarage\Validator\LuhnAlgorithm;

// OPTION 1: Value passing via constructor.
$luhn     = new LuhnAlgorithm(79927398713);
$isValid  = $luhn->isValid();  // true
$checksum = $luhn->checksum(); // 70

// OPTION 2: Value passing via invocable class.
$luhn     = new LuhnAlgorithm();
$isValid  = $luhn(79927398713); // true
$checksum = $luhn->checksum();  // 70

// OPTION 3: Value passing via static method. In this case, There is no
// instance stored in memory & can only be used for one-off validation.
$isValid = LuhnAlgorithm::validate(79927398713); // true

Debug

From above Validation example, we can debug more details about the validation status and various state of each number when checkum was calculated.

var_dump( $luhn ); // Input value was: 79927398713

// The debug output.
array(4) {
  'isValid' =>
  bool(true)
  'digits' =>
  int(79947697723)
  'checksum' =>
  int(70)
  'state' =>
  array(11) {
    [0] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(7)
    }
    [1] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(9)
    }
    [2] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(9)
    }
    [3] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(4)
    }
    [4] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(7)
    }
    [5] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(6)
    }
    [6] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(9)
    }
    [7] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(7)
    }
    [8] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(7)
    }
    [9] =>
    array(2) {
      'doubled' =>
      bool(true)
      'result' =>
      int(2)
    }
    [10] =>
    array(2) {
      'doubled' =>
      bool(false)
      'result' =>
      int(3)
    }
  }
}

Composition

The Luhn Algorithm class is actually composed by using Luhn trait. This trait can be used elsewhere in separate class defined in your project or modify validation methods as per the project's requirement (latter of which seem unnecessary though).

统计信息

  • 总下载量: 3
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0-or-later
  • 更新时间: 2024-09-12