承接 speelpenning/binary-search-collection 相关项目开发

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

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

speelpenning/binary-search-collection

Composer 安装命令:

composer require speelpenning/binary-search-collection

包简介

An extension for Laravel's collection with support for binary search.

README 文档

README

Build Status codecov.io Latest Stable Version Latest Unstable Version License

This package is useful when working with large data sets that contain objects with an unique identifier.

Getting started

Requirements

  • illuminate/support: >= 5.0

Installation

Pull in the package by Composer:

composer require speelpenning/binary-search-collection

Usage

If you have structured data stored in different formats like csv and XML and you need to combine the data for a report. The only thing you need to do is to define some models and create a reader which returns a collection with the models.

Example use case

Say we have the the following files:

  • An XML file with 200.000 products (product number, description, etc.)
  • A csv price list with 70.000 prices (product number, gross price, net price, list price)

For sending a price list to "our customers" we need to make a csv file with product number, description, list price and gross price.

First we need two models, say Product and Price, which hold the attributes as defined in the files. Next, we create a reader for the XML (ie. ProductsReader) file and the csv file (ie. PriceReader). The readers accept a binary sort collection in the constructor and fills the collection with models from the file. The collections is returned by the readers.

Now we come near our goal: generating a csv price list for our customers. For this, we create a writer (say CustomerPriceListWriter), which accepts both readers in the constructor. The writer iterates over the collection with Price models, while finding the Product records in the other collection using binary search. Then the data from both models is combined and written to the csv file with customer prices.

<?php

class CustomerPriceListWriter
{
    /**
     * @var ProductReader
     */
    protected $productReader;

    /**
     * @var PriceReader
     */
    protected $priceReader;

    /**
     * Create a new customer price list writer instance.
     *
     * @param ProductReader $productReader
     * @param PriceReader $priceReader
     */
    public function __construct(ProductReader $productReader, PriceReader $priceReader)
    {
        $this->productReader = $productReader;
        $this->priceReader = $priceReader;
    }


    protected function write($productsFilename, $pricesFilename, $targetFilename = null)
    {
        // First we read the products and prices file, which both
        // return a collection that supports binary search.
        $products = $this->productReader->read($productsFilename);
        $prices = $this->priceReader->read($pricesFilename);

        // Prepare the array with CSV lines.
        $csv = [$this->toCsv([
            'Product Number',
            'Description',
            'List price',
            'Your price'
        ])];
        foreach ($prices as $price) {
            $product = $products->find($price->product_number);

            $csv[] = $this->toCsv([
                $product->product_number,
                $product->description,
                $price->list_price,
                $price->gross_price
            ]);
        }

        $targetFilename = is_null($targetFilename) ? 'customer-prices-'.date('YmdHis').'.csv' : $targetFilename;
        file_put_contents($targetFilename, implode("\n", $csv));
    }

    protected function toCsv(array $values)
    {
        foreach ($values as $key => $value) {
            $value[$key] = is_numeric($value) ? $value : "\"{$value}\"";
        }
        return implode(',', $values);
    }
}

speelpenning/binary-search-collection 适用场景与选型建议

speelpenning/binary-search-collection 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13 次下载、GitHub Stars 达 1, 最近一次更新时间为 2016 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 speelpenning/binary-search-collection 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 speelpenning/binary-search-collection 我们能提供哪些服务?
定制开发 / 二次开发

基于 speelpenning/binary-search-collection 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-04-14