Download the PHP package popcorn4dinner/collections without Composer

On this page you can find all versions of the php package popcorn4dinner/collections. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package collections

** Features

** Creating a collection Minimal Usage:

+BEGIN_SRC php

use Popcorn4dinner\Collection\AbstractCollection;

class MyCollection extends AbstractCollection {

protected function isCollectableInstance($item): bool { // This is the place where you can inforce typing, // but also any other convention that all items in your collection // have to follow.

     return true;
 }

}

+END_SRC

+BEGIN_SRC php

$item1 = new ExampleItem(); $item2 = new ExampleItem();

$collection = new MyCollection($item1, $item2);

foreach($collection as $item){ // Your logic goes here... }

+END_SRC

** Controlling storage mechanics

+BEGIN_SRC php

use Popcorn4dinner\Collection\AbstractCollection;

class MyCollection extends AbstractCollection {

// Now users are stored uniquly by their email address protected function store($user) { $this->items[$user->email] = $user; }

protected function isCollectableInstance($user): bool { return $user->email !== null; } }

+END_SRC

** Pagination and REST API endpoints Paginated Collections can be used so enable pagination for API Endpoints and Views.

+BEGIN_SRC php

use Popcorn4dinner\Collection\AbstractPaginatedCollection;

class ExampleCollection extends AbstractPaginatedCollection {

protected function isCollectableInstance($item): bool
{
    return is_a($item, ExampleItem::class);
}

}

+END_SRC

+BEGIN_SRC php

$offset = 10; $limit = 20; $totalAmountOfItems = 25;

$collection = new ExampleCollection($items, $offset, $limit, $totalAmountOfItems);

$collection->getPageSize() // 10

$collections->getAmountPages() // 3

$collection->getCurrentPage() // 2

$collection->hasPreviousPage() // true

$collection->hasNextPage() // true

+END_SRC

*** Paginated REST endpoints

+BEGIN_SRC php

use Popcorn4dinner\Collection\AbstractJsonSerializableCollection;

class ExampleJsonSerializableCollection extends AbstractJsonSerializableCollection { protected function isCollectableInstance($item): bool { return is_a($item, ExampleItem::class); }

protected static function serializeItem($item): array
{
    return [ 'name' => $item->name];
}

}

+END_SRC

+BEGIN_SRC php

$collection->jsonSerialize();

/*

*** Create collections from a request body

+BEGIN_SRC php

$users = PaginatedUserCollection::fromRequestBody($requestBody, function($userDataRow){ return new User($userDataRow['name']); } );

+END_SRC

** Utility functions

*** each Applies the callback to all items within the collection.

+BEGIN_SRC php

$users->each( function($user) { return $user->lock(); } );

+END_SRC

*** map Applies the callback to all items within the collection and returns a new collection.

+BEGIN_SRC php

$lockedUsers = $users->map( function($user) { return $user->lock(); } );

+END_SRC

*** first Returns the first item in the collection

+BEGIN_SRC php

$bird = $birds->first();

+END_SRC

*** last Returns the last item in the collection

+BEGIN_SRC php

$bird = $birds->last();

+END_SRC

*** get Return and item by key

+BEGIN_SRC php

$bird = $birds->get(3);

$user = $user->get("[email protected]");

+END_SRC

*** filter Returns a new collection based on the filter callback

+BEGIN_SRC php

$productsAddedToday = $products->filter( function($product) { return $product->createdAt() > new \DateTime('today'); } );

+END_SRC

*** reject Reverse effect of filter(). Return a new collection based on the filter callback

+BEGIN_SRC php

$delayedTrains = $trains->reject( function($tain) { return $train->isOnTime(); } );

+END_SRC

*** sort Sorts the collection based on a callback

+BEGIN_SRC php

$purchases->sort( function($a, $b){ return $a->createdAt > $b->createdAt; } );

+END_SRC

*** unique Return a new Collection with unique items.

+BEGIN_SRC php

$uniqueVisits = $visits->unique();

+END_SRC

*** split Maybe my personal favorite: Returns an array of collections, split based on the given callback.

+BEGIN_SRC php

$purchasesByDay = $purchases->split( functions($purchase){ return $purchase->createdAt->format('Y-m-d'); } );

// returns: // [ // '2017-08-01' => PurchaseCollection...

+END_SRC

*** reduce Returns an array of transformed items.

+BEGIN_SRC php

$userIds = $users->reduce( function($user){ $user->id } );

+END_SRC

*** toArray Returns an Array with all items in the collection.

+BEGIN_SRC php

SomeArrayFunction( $collection->toArray() );

+END_SRC

** License MIT


All versions of collections with dependencies

PHP Build Version
Package Version
Requires php Version >=7.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package popcorn4dinner/collections contains the following files

Loading the files please wait ....