Coder Social home page Coder Social logo

oved-php / typed-collections Goto Github PK

View Code? Open in Web Editor NEW
10.0 4.0 3.0 62 KB

A simple library to create type hinted collections in PHP- build on Laravals Collection class

PHP 100.00%
php7 php-library collections type-safe type-hints laravel laravel-collections

typed-collections's Introduction

Build Status

What is this?

This is a very simple way to make arrays type hinted!

$list = new UserCollection(new User());

OK

$list = new UserCollection('User');

Will throw:

Vistik\Exception\InvalidTypeException: Item (string) 'User' is not a Vistik\Example\User object!

Install

Run composer require vistik/typed-collections

Use build in Collections for primitives

Out-of-the-box collections for:

  • Booleans
  • Integers
  • Floats
  • Strings
  • Emails
  • Numbers (float or int)

They can be created with out-of-the-box laraval style helpers:

  • bCollect([true, false]) - To create a BoolCollection
  • iCollect([1, 2, 3, 4]) - To create a IntegerCollection
  • fCollect([3.14, 22/7, 6.66]) - To create a FloatCollection
  • sCollect(['hello', 'world'']) - To create a StringCollection
  • eCollect(['[email protected]', '[email protected]']) - To create a EmailCollection
  • nCollect([1, 2, 3, 4, 3.14, INF]) - To create a NumberCollection

Do I have to create a type for each list? Yes, but

Look how easy it is:

use Phpsafari\Example\User;
class UserCollection extends TypedCollection{
    protected $type = User::class;
}

2 simple steps

  1. Create a Class eg. AccountCollection extend TypedCollection
  2. Just replace protected $type = 'Vistik\Example\User'; with your class

or

If the check is more then just a is_a check, you can overwrite the function: isValidItem($item) in your new Collection class

Features

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.