承接 krak/coll 相关项目开发

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

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

krak/coll

Composer 安装命令:

composer require krak/coll

包简介

PHP Collections Library

README 文档

README

The Collections library is yet another attempt to generate OO collections in php.

Currently, the only collection in here is the Set because that's the only collection PHP actually is missing.

Installation

compose require krak/coll

Usage

Set

A set represents a set of unique values.

There are two interfaces for the Set type: ConstSet and Set.

Const sets allow read access only whereas Sets allow read write access.

// const methods
public function get($value);
public function has($value);
public function count();
public function getIterator();

// non-const methods
public function add($value);
public function remove($value);
<?php

use Krak\Coll\Set;

$s1 = new Set\ArraySet();
// or
$s1 = Set\ArraySet::create([1,2,3]);

$s1->add(4);
$s2 = set\union($s1, Set\ArraySet::create([3,4]));

set\is_subset($s2, $s1); // returns true because s1 is a subset of s2

// if you need to store object values

// internally uses SplObjectStorage
$oset = new Set\ObjectSet();
$oset->add(new stdClass());

// if you need to store values that work as array keys
$hset = new Set\HashSet();
$hset->add(['a' => 1]);

// if you need to store any of those types of values
$aset = new Set\AnySet();
set\fill($aset, [1, [1], new stdClass()]);

Each is set does also work as an iterable and is countable

<?php

count($s1); // returns the count
foreach ($s1 as $val) {
    // iterate over the set values
}

For type hinting use, Set or ConstSet

<?php

use Krak\Coll\Set;

function operate_on_mutable(Set\Set $s1) {
    // ...
}

function operate_on_immutable(Set\ConstSet $s1) {
    // ...
}

As seen earlier, we do have several set operation functions.

// similar to union, but can work with different typed set (AnySet and ArraySet)
function join(ConstSet $s1, ConstSet $s2, $factory = null);
// s1 + s2
function union(ConstSet $s1, ConstSet $s2);
// s1 * s2
function intersect(ConstSet $s1, ConstSet $s2);
// s1 - s2
function difference(ConstSet $s1, ConstSet $s2);
// compares to sets returns true if s2 is subset of s1
function is_subset(ConstSet $s1, ConstSet $s2);
// compares to sets returns true on equal
function equals(ConstSet $s1, ConstSet $s2);
// fills a set with values and returns it
function fill(Set $s1, $values);

In addition to the functional methods, there is an OO interface like so

<?php
$s1->union($s2)
    ->intersect($s3)
    ->difference($s4)
    ->equals($s5);

统计信息

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

GitHub 信息

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

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固