承接 coshi/variator 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

coshi/variator

Composer 安装命令:

composer require coshi/variator

包简介

Utils for generating arrays of data combinations

README 文档

README

Usage:

    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        foreach($values as $value) {
            print($value); // displays first, second, third            
        }
    }

More complex:

    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
        'number' => [
            'type' => 'int',
            'min' => 0,
            'max' => 2
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        echo sprintf('text: %s, number: %d', $values['text'], $values['number']);
        echo PHP_EOL;
    }

Output will be:

    text: first, number: 0
    text: second, number: 0
    text: third, number: 0
    text: first, number: 1
    text: second, number: 1
    text: third, number: 1
    text: first, number: 2
    text: second, number: 2
    text: third, number: 2

It can call methods and functions in order to get values:

    class DummyClass
    {
        public function someValues()
        {
            return [0, 1, 2];
        }
    }
    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
        'number' => [
            'type' => 'callback',
            'callback' => [DummyClass::class, 'someValues']
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        echo sprintf('text: %s, number: %d', $values['text'], $values['number']);
        echo PHP_EOL;
    }

Result will be exactly the same as from the previous one. Result will be the same for callback defined as '['range', [0, 2]]'.

Supports cross-referencing between variations:

    class DummyClass
    {
        public function someValues($text)
        {
            $values = [
                'first' => [0, 1],
                'second' => [2, 3],
                'third' => [4, 5],
            ];
    
            return $values[$text];
        }
    }
    $factory = new VariationFactory();
    $builder = new VariationsTreeBuilder($factory);
    
    $config = [
        'text' => [
            'type' => 'enum',
            'values' => ['first', 'second', 'third'],
        ],
        'number' => [
            'type' => 'callback',
            'callback' => [DummyClass::class, 'someValues', ['@text']],
            'context_dependent' => true
        ],
    ];
    $variations = $builder->build($config);
    
    foreach ($variations as $values) {
        echo sprintf('text: %s, number: %d', $values['text'], $values['number']);
        echo PHP_EOL;
    }

Output will be:

    text: first, number: 0
    text: first, number: 1
    text: second, number: 2
    text: second, number: 3
    text: third, number: 4
    text: third, number: 5

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-08-23

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固