承接 jcrowe/type-safe-collection 相关项目开发

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

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

jcrowe/type-safe-collection

最新稳定版本:v7.1

Composer 安装命令:

composer require jcrowe/type-safe-collection

包简介

A thin wrapper around Illuminate/Collection that allows for enforcing type constraints on the collection elements

README 文档

README

Build Status Coverage Status

TypeSafeCollection is a thin wrapper around Illuminate's Collection object that allows for easy enforcement of type protection in your collections.

use JCrowe\TypeSafeCollection\TypeSafeCollection;

class MovieLibrary extends TypeSafeCollection {

    // list of classes that can be added to the collection
    protected $allowedClasses = [Watchable::class, Rentable::class];
}

$myLibrary = new MovieLibrary([
    new WatchableMovie(),
    new RentableDVD(),
    new ReadableBook() // throws \InvalidArgumentProvided exception
]);


$myLibrary = new MovieLibrary();

$myLibarry->push(new RentableDVD());

$myLibrary->push(new ReadableBook()); // throws \InvalidArgumentProvided exception 

Custom checks

class MovieLibrary extends TypeSafeCollection {

    // list of classes that can be added to the collection
    protected $allowedClasses = [Watchable::class, Rentable::class];
    
    
    // this function will be called whenever a new  
    // element is being added to the collection
    protected function onAddNewElement($element) 
    {
        if (!$element->isAvailable()) {
            
            return false; // or throw exception
        }
    }
}

Ignore invalid types and do not throw exception

class MovieLibrary extends TypeSafeCollection {

    // list of classes that can be added to the collection
    protected $allowedClasses = [Watchable::class, Rentable::class];
    
    // if set to true no exception will be thrown when
    // attempting to add an invalid value.
    protected $ignoreInvalidElements = true;
}

$myLibrary = new MovieLibrary();

$myLibrary->put('my_rentable', new RentableDVD());

$myLibrary->get('my_rentable'); // RentableDVD object

$myLibrary->put('my_readable', new ReadableBook()); // no exception is thrown

$myLibrary->get('my_readable'); // null

Installation

composer require jcrowe/type-safe-collection
{
    "require": {
        "jcrowe/type-safe-collection": "~1.0"
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-29