定制 krak/adt 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

krak/adt

Composer 安装命令:

composer require krak/adt

包简介

Algebraic Data Types in PHP

README 文档

README

Poor man's implementation of Algebraic Data Types in PHP.

Also known as an enum with an associated value in other languages like Swift or Rust.

Installation

Install with composer at krak/adt

Usage

<?php

use Krak\ADT\ADT;

/**
 * @method static Upc upc(int $numberSystem, int $manufacturer, int $product, int $check)
 * @method static QrCode qrCode(string $productCode)
 */
abstract class Barcode extends ADT {
    public static function types(): array {
        return [Upc::class, QrCode::class];
    }
}

final class Upc extends Barcode {
    public $numberSystem;
    public $manufacturer;
    public $product;
    public $check;

    public function __construct(int $numberSystem, int $manufacturer, int $product, int $check) {
        $this->numberSystem = $numberSystem;
        $this->manufacturer = $manufacturer;
        $this->product = $product;
        $this->check = $check;
    }
}

final class QrCode extends Barcode {
    public $productCode;

    public function __construct(string $productCode) {
        $this->productCode = $productCode;
    }
}

$barcode = new QrCode('abc123');

// requires that all cases are set or exception is thrown
$oneOrTwo = $barcode->match([
    Upc::class => function(Upc $upc) { return 1;},
    QrCode::class => function(QrCode $qrCode) { return 2; },
]);

// allow a default value
$oneOrNull = $barcode->matchWithDefault([
    Upc::class => function(Upc $upc) { return 1; }
]);

// return static values
$threeOrFour = $barcode->match([
    Upc::class => 3,
    QrCode::class => 4,
]);

// static constructors
$qrCode = Barcode::qrCode('abc123');

Autoloading Concerns

With the example above, if you try to create a QrCode or Upc before ever referencing the Barcode class, you'll likely get a file not found when using composer's psr-4 autoloader.

You can get around this a few ways:

  1. Utilize class mapping

  2. Include the enum class file in the composer autoload.files section (something like php-inc could make that easier)

  3. Utilize static constructors provided by the base ADT class:

    <?php
    
    $upc = Barcode::upc(1, 2, 3, 4);
    $qrCode = Barcode::qrCode('abc123');

Tests

Tests are run via make test and are stored in the test directory. We use peridot for testing.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-07-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固