PHPackages                             phpextra/common - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpextra/common

AbandonedArchivedLibrary[Utility &amp; Helpers](/categories/utility)

phpextra/common
===============

Common interfaces and classes

1.1.1(11y ago)28.5k[1 issues](https://github.com/phpextra/common/issues)BSD-3-ClausePHP

Since Mar 14Pushed 11y ago1 watchersCompare

[ Source](https://github.com/phpextra/common)[ Packagist](https://packagist.org/packages/phpextra/common)[ RSS](/packages/phpextra-common/feed)WikiDiscussions master Synced 3d ago

READMEChangelogDependencies (1)Versions (12)Used By (0)

\#Common classes and interfaces for PHP [![Latest Stable Version](https://camo.githubusercontent.com/4f018133e6729ae3483dba300b1c0a6f8a31ee859eeccf91f02bcf2dfaf13628/68747470733a2f2f706f7365722e707567782e6f72672f70687065787472612f736f727465722f762f737461626c652e737667)](https://packagist.org/packages/phpextra/common)[![Total Downloads](https://camo.githubusercontent.com/37fc07acfcc37ebf7017360f6d36f440c86eaeb326be42df433be0c3c8cf542b/68747470733a2f2f706f7365722e707567782e6f72672f70687065787472612f636f6d6d6f6e2f646f776e6c6f6164732e737667)](https://packagist.org/packages/phpextra/common)[![License](https://camo.githubusercontent.com/1debcebbf5e7f0409e2b21cebbec73e1b8733d9e073c86f635798bca31a7f2da/68747470733a2f2f706f7365722e707567782e6f72672f70687065787472612f636f6d6d6f6e2f6c6963656e73652e737667)](https://packagist.org/packages/phpextra/common)[![Build Status](https://camo.githubusercontent.com/16e61c66ec5775de45241dbb62afec9a0c615267589dd10c36353696def82003/687474703a2f2f696d672e736869656c64732e696f2f7472617669732f70687065787472612f636f6d6d6f6e2e737667)](https://travis-ci.org/phpextra/common)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/be95d57d989d456f6ee9c0307974dc36ce12c389082607121de8a49b0389188b/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f70687065787472612f636f6d6d6f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/phpextra/common/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/44aea8d339e0263062eeb6bdddc3965f73f6cebf9779bc0a9438faa69318faa4/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f70687065787472612f636f6d6d6f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/phpextra/common/?branch=master)[![GitTip](https://camo.githubusercontent.com/0ec766cd3550cb42f0db394a3dc81c594379aba38bdca4d1b31a56d9baae78c3/687474703a2f2f696d672e736869656c64732e696f2f6769747469702f6a6b6f6275732e737667)](https://www.gittip.com/jkobus)

\##Usage

\###Enum (PHPExtra\\Type\\Enum)

Create your first enum type by creating a new class:

```
class TheGuy extends AbstractEnum
{
    const _default = self::NICE_GUY;
    const SMART_GUY = 'Mike';
    const NICE_GUY = 'Rick';
}
```

Thats all.

Now you can use it:

```
$guy = new TheGuy();
echo $guy->getValue(); // returns Rick

$mike = new TheGuy(TheGuy::MIKE);
echo $mike->getValue(); // returns Mike

echo $guy->equals($mike); // returns false
```

If no default value will be specified, you must set it as a constructor argument. If given constructor value will be invalid, `\UnexpectedValueException` will be thrown.

\###Collection (PHPExtra\\Type\\Collection)

Collections solve few things by implementing following interfaces: `\Countable`, `\ArrayAccess`, `\Iterator`, and `\SortableInterface`. This gives you an ability to `count()` collection, use a `foreach` on it, access it like an array `$a[1]` and sort its contents `$a->sort($sorter)`. Apart from regular collections there are also `LazyCollection`'s that allow you to specify a closure that will initialize collection contents only if and when it's needed.

Create your first collection:

```
$collection = new Collection();

$collection->add('item1');
$collection->add('item2');
$collection->add('item3);
```

Use it:

```
echo count($collection); // returns 3
echo $collection[0]; // returns "item1"
echo $collection->slice(1, 2); // returns Collection with a length of 2 containing item2 and item3.
echo $collection->filter(function($element, $offset){ return $offset % 2 == 0; }); // returns sub-collection with all elements with even offset number
$collection->sort(SorterInterface $sorter); // sorts collection
```

Lazy collection example:

```
$lazy = new LazyCollection(function(){
    return new Collection(array(1, 2, 3));
});

echo $lazy[2]; // initializes the closure and returns "3"
```

\###UnknownType (PHPExtra\\Type\\UnknownType)

It should not happen but sometimes does - you have a method with many different response types, but want to handle it like a pro:

```
$messedUpResponse = $api->getMeSomeChickens(); // returns "Chicken" **or** "Collection" **of** "Chickens" **or** "no" as an error response :-)

$result = new UnknownType($messedUpResponse);

if($result->isCollection()){
    $result->getAsCollection()->sort($sorter);
    ...
}elseif($result->isException){
    throw $result->getAsException();
    ...
}
```

UnknownType can be extended and customized :-)

\###Paginator (PHPExtra\\Paginator)

Paginator is fully compatible with CollectionInterface. It's task is to split large collections into pages.

```
$page = 2;
$itemsPerPage = 10;
$products = new Collection(...);
$paginator = new Paginator($products, $page, $itemsPerPage);

echo $paginator->getPage(); // returns a collection with size of 10 for current page
echo $paginator->getNextPageNumber(); // returns "3"
echo $paginator->hasNextPage(); // returns bool true or false
```

\##Changelog

\###1.2.x

- added **CollectionInterface::exists(Closure $c)** method to Collection
- removed Serializable interface from Collection
- added Serializable interface to LazyCollection
- added deprecation mark to LazyCollection which **will became** **final** in 1.3
- added **CollectionProxy** class
- added CollectionInterface::sort(SorterInterface $sorter) for collections
- added EnumInterface::equals(EnumInterface $enum) for enums
- added UnknownType::isSortable() for unknown type
- re-worked Enum type:
    - added deprecation mark to **Enum class** as it will be changed to **abstract** in 1.3
    - added default value for enums
    - AbstractEnum::isValid($val) is now static
    - added AbstractEnum:equals(EnumInterface $enum)
- updated README

\###1.1.1

- Collection::current() now returns null on empty collection

\###1.1.0

- added PaginatorInterface
- added default, optional, value holders for page number getters
- added Collection::forAll(Closure $c) method in collections

\###1.0.3 (cannot be downgraded)

- changed paginator behaviour - will return closest matching page ig page number is out of range or empty collection
- added getters for last page and its number

\###1.0.2 (cannot be downgraded)

- fixed paginator page hasser that returned false positives
- fixed slice() to not use array\_slice on Collections
- paginator changes; added hassers and getters for pages, toString method (returns current page number), changed constructor

\###1.0.1 (cannot be downgraded)

- added paginator that can handle large collections and split them between pages of given length
- reset internal pointer after filter() in collections
- added first() and last() method in collections
- fixed exception message in UnknownType for getAsCollection() method

\###1.0.0

First release

Installation (Composer)
-----------------------

[](#installation-composer)

```
{
    "require": {
        "phpextra/common":"~1.2"
    }
}
```

\##Running tests

```
// Windows
composer install & call ./vendor/bin/phpunit.bat ./tests

```

\##Contributing

All code contributions must go through a pull request. Fork the project, create a feature branch, and send me a pull request. To ensure a consistent code base, you should make sure the code follows the [coding standards](http://symfony.com/doc/2.0/contributing/code/standards.html). If you would like to help take a look at the [list of issues](https://github.com/phpextra/common/issues).

\##Requirements

See **composer.json** for a full list of dependencies.

\##Authors

Jacek Kobus -

License information
-------------------

[](#license-information)

```
See the file LICENSE.txt for copying permission.

```

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance10

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity70

Established project with proven stability

 Bus Factor1

Top contributor holds 77.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~33 days

Recently: every ~56 days

Total

10

Last Release

4140d ago

Major Versions

0.0.2 → 1.0.02014-03-18

1.2.x-dev → 2.0.x-dev2015-01-12

### Community

Maintainers

![](https://www.gravatar.com/avatar/85f3695da9474a49d00d00449442d8f8bcbc21abfcae1c9d51a2762d0cad87f7?d=identicon)[jkobus](/maintainers/jkobus)

---

Top Contributors

[![jkobus](https://avatars.githubusercontent.com/u/1527096?v=4)](https://github.com/jkobus "jkobus (14 commits)")[![ramonacat](https://avatars.githubusercontent.com/u/303398?v=4)](https://github.com/ramonacat "ramonacat (4 commits)")

### Embed Badge

![Health badge](/badges/phpextra-common/health.svg)

```
[![Health](https://phpackages.com/badges/phpextra-common/health.svg)](https://phpackages.com/packages/phpextra-common)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
