{"id":25711330,"url":"https://github.com/simondeeley/tuple","last_synced_at":"2025-06-11T22:37:54.610Z","repository":{"id":57051126,"uuid":"112079808","full_name":"simondeeley/tuple","owner":"simondeeley","description":"Extendable, reusable PHP tuple object","archived":false,"fork":false,"pushed_at":"2017-12-04T17:34:10.000Z","size":41,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-11-22T17:49:08.287Z","etag":null,"topics":["immutable","php","php7","tuple"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simondeeley.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-11-26T12:14:52.000Z","updated_at":"2022-02-12T12:48:06.000Z","dependencies_parsed_at":"2022-08-24T05:11:10.136Z","dependency_job_id":null,"html_url":"https://github.com/simondeeley/tuple","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simondeeley%2Ftuple","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simondeeley%2Ftuple/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simondeeley%2Ftuple/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simondeeley%2Ftuple/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simondeeley","download_url":"https://codeload.github.com/simondeeley/tuple/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240649300,"owners_count":19835129,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["immutable","php","php7","tuple"],"created_at":"2025-02-25T10:33:30.216Z","updated_at":"2025-02-25T10:33:30.749Z","avatar_url":"https://github.com/simondeeley.png","language":"PHP","readme":"Reusable tuple object for PHP\n==================================\n\n[![License](https://poser.pugx.org/simondeeley/tuple/license)](https://packagist.org/packages/simondeeley/tuple) [![Latest Stable Version](https://poser.pugx.org/simondeeley/tuple/v/stable)](https://packagist.org/packages/simondeeley/tuple) [![Latest Unstable Version](https://poser.pugx.org/simondeeley/tuple/v/unstable)](https://packagist.org/packages/simondeeley/tuple) [![Total Downloads](https://poser.pugx.org/simondeeley/tuple/downloads)](https://packagist.org/packages/simondeeley/tuple) [![Build Status](https://travis-ci.org/simondeeley/tuple.svg?branch=master)](https://travis-ci.org/simondeeley/tuple) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/simondeeley/tuple/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/simondeeley/tuple/?branch=master) [![Code Coverage](https://scrutinizer-ci.com/g/simondeeley/tuple/badges/coverage.png?b=master)](https://scrutinizer-ci.com/g/simondeeley/tuple/?branch=master)\n\nThis package provides a tuple type object for PHP. It is based off [simondeeley\\Type](https://github.com/simondeeley/type) package which provides immutable objects.\n\n\u003e In [mathematics](https://en.wikipedia.org/wiki/Mathematics) a **tuple** is a finite ordered list (sequence) of [elements](https://en.wikipedia.org/wiki/Element_(mathematics)). An **_n_-tuple** is a [sequence](https://en.wikipedia.org/wiki/Sequence) (or ordered list) of _n_ elements, where _n_ is a non-negative integer.\n\u003e\n\u003e –_[Wikipedia](https://en.wikipedia.org/wiki/Tuple)_\n\nAn example of a tuple could be `[1, 2, 3, 4, 5]`. The order of the items in a tuple is important, so given a set of 'A' being `[1, 2, 3]` and 'B' also being `[1, 2, 3]` then it's true that A equals B. However if we have another set 'C' which is `[3, 2, 1]` then this is not equal to A (or B).\n\nThis package was born out of a love creating beautifully crafted 'boilerplate' code which can be reused again and again in any number of both simple and complex objects. Most of the ideas used in this package revolve around the need to create the most basic of PHP objects - one that is immutable and unchanging. Trouble is, PHP has a fair few 'magic methods' which, although great for method overriding, are less great for ensuring an immutable state. This package aims to solve that problem and provide a number of base classes to allow you to build easy-to-use immutable classes of your own.\n\nRequirements\n============\n\n* PHP \u003e= 7.1.0\n\nInstallation\n============\n\n```\ncomposer require simondeeley/tuple\n```\n\nUsage\n=====\n\nCreate your own tuple object\n\n```php\nuse simondeeley\\Tuple;\n\nclass MyTuple extends Tuple\n{\n    //...\n}\n```\n\nThis is the starting point to creating a tuple. By default you can have any number of items in your tuple object (up to the limit of PHP's [PHP_INT_MAX](http://php.net/manual/en/reserved.constants.php) value). You can override this and create your own maximum length tuples.\n\nLet's go ahead and create a simple tuple called a pair. Unsurprisingly this has a maximum of two items.\n\n```php\nuse simondeeley\\Tuple;\n\nclass Pair extends Tuple\n{\n    const MAX_LENGTH = 2;\n}\n```\n\nNow we have our new class we can create pair-type objects until out hearts content...\n```php\n$numeric = new Pair(1, 2);\n$strings = new Pair('A', 'B');\n$mixed = new Pair(1024, 'FooBar');\n$objects = new Pair($numeric, $strings);\n```\n\nAs well as a maximum length value, you can also specify minimum length values. By setting the two values as the same, you effectively impose an exact requirement for the number of arguments a tuple can have. This is very useful for creating tuples that are true to their mathematical form, which brings us on to...\n\nExample Tuples\n==============\nThis package provides two pre-built tuples out-of-the box, [`Single`](https://github.com/simondeeley/type/blob/master/src/Tuples/Single.php) and [`Pair`](https://github.com/simondeeley/type/blob/master/src/Tuples/Pair.php) these provide functionality for _1_-tuple and _2_-tuple objects, respectively. Using them is very simple\n```php\nuse simondeeley\\Tuples\\Single;\nuse simondeeley\\Tuples\\Pair;\n\n$single = new Single('foo'); // Can only ever have one argument\n$pair = new Pair(10, 20); // Can only ever have exactly two arguments\n```\n\nTesting Equality\n================\n\nThe base tuple implements the  [`TypeEquality`](https://github.com/simondeeley/type/blob/master/src/Type/TypeEquality.php) interface from the [simondeeley\\Type](https://github.com/simondeeley/type) package. Using this equality check we can compare two tuples. Following on from our Pair example above we can do the following\n\n```php\n$foo = new Pair(1, 2);\n$bar = new Pair(1, 2);\n$baz = new Pair(2, 1);\n\n$foo-\u003eequals($bar); // Returns true\n$foo-\u003eequals($baz); // Returns false\n```\n\nExplore the source code to see how the comparisons are made or to see how the base tuple class is built.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimondeeley%2Ftuple","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimondeeley%2Ftuple","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimondeeley%2Ftuple/lists"}