Git Product home page Git Product logo

php-collection's Introduction

PHP Collection

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Latest Unstable Version License

Data Structure based on SplDoublyLinkedList.

Numerical keys, consequentially increasing, no gaps possible. Quick sequential iterating.

Advantages:

  • Using Lamda Modifiers (see addModifier method)
  • Regular array compatiable (ArrayAccess interface implemented)

Installation

Add the package to your composer.json and run composer update.

{
    "require": {
        "kozz/collection": "*"
    }
}

Basic Usage

Initializing

    use Kozz\Components\Collection;
    $collection = new Collection();

Initializing from any Traversable or Iterator

  1. You can initiate collection as SplDoublyLinkedList-based structure with Collection::from($traversable)

        $traversable = new \ArrayIterator(range(1,1000));
        $collection = Collection::from($traversable);
  2. You also able to use your Iterator as Collection's data container with new Collection($iterator). Your iterator will converts to SplDoublyLinkedList once you try use any method from ArrayAccess or Countable interfaces implemented in Collection. This is good solution if your iterator is cursor in big DB Data Set and you need just add some modifiers with addModifier

        $mongo = new \MongoClient();
        $cursor = $mongo->selectDB('testDB')->selectCollection('testCollection')->find();
        $collection = new Collection($cursor);

Modifiers

Sometimes you should modify your data in collection

With Collection

Modifiers are quite helpful to process DB Data Sets. And with this Collection you are able simply add modifier in just one line:

    use Kozz\Components\Collection;
    
    $mongo = new \MongoClient();
    $cursor = $mongo->selectDB('testDB')->selectCollection('testCollection')->find();
    //[0=>['_id'=>MongoId(...), 'value'=>123], ...]
    
    
    $collection = new Collection($cursor);
    $collection->addModifier(function(&$item){
        $item['id'] = (string)$item['_id'];
    });
    $collection->addModifier(function(&$item){
        unset($item['_id']);
    });

So now Modifiers are stored in Collection and you have two ways to apply it:

  1. use getFilterIterator() method to get an Iterator with all applied modifiers:

        foreach($collection->getFilterIterator() as $item)
        {
            // $item = ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123]
        }
  2. Call ->toArray() that calls getFilterIterator() :

        $array = $collection->toArray();
        //$item = [ 0=> ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123], ...]
        foreach($array as $item)
        {
            //do stuff
        }

Without Collection

You actually can modify your data with plain SPL:

    $mongo = new \MongoClient();
    $cursor = $mongo->selectDB('testDB')->selectCollection('testCollection')->find();
    
    $it = new CallbackFilterIterator($cursor, function(&$item){
        $item['id'] = (string)$item['_id'];
        return true;
    });
    $it = new CallbackFilterIterator($it, function(&$item){
        unset($item['_id']);
        return true;
    });
    
    foreach($array as $item)
    {
        // $item = ['id'=>'4af9f23d8ead0e1d32000000', 'value'=>123]
    }

ArrayAccess

Adding element

    $element = 'string';
    $collection->push($element);
    //or
    $collection[] = $element;

Replacing element

    $element2 = new stdClass();
    $collection->set(0, $element2);
    //or
    $collection[0] = $element2;
    // This throws Exception (offset 100 not exists)
    $collection->set(100, $element2);

Check offset

    $collection->exists(0); 
    //or
    isset($collection[0]);

Retrieve element

    $element = $collection->get(0); 
    //or
    $element = $collection[0];

Remove element

    $element = $collection->remove(0);
    //or
    $element = unset($collection[0]);

php-collection's People

Contributors

urakozz avatar

Watchers

 avatar  avatar

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.