{"id":19188777,"url":"https://github.com/miquido/observable","last_synced_at":"2025-10-18T18:56:35.552Z","repository":{"id":57017332,"uuid":"144590078","full_name":"miquido/observable","owner":"miquido","description":"The project was made by Miquido. https://www.miquido.com/","archived":false,"fork":false,"pushed_at":"2018-10-05T09:27:15.000Z","size":32,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-05-08T02:47:38.767Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/miquido.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":"2018-08-13T14:20:15.000Z","updated_at":"2023-09-25T07:17:16.000Z","dependencies_parsed_at":"2022-08-22T11:31:36.289Z","dependency_job_id":null,"html_url":"https://github.com/miquido/observable","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/miquido/observable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Fobservable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Fobservable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Fobservable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Fobservable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/miquido","download_url":"https://codeload.github.com/miquido/observable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/miquido%2Fobservable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":268009800,"owners_count":24180458,"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","status":"online","status_checked_at":"2025-07-31T02:00:08.723Z","response_time":66,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-09T11:26:00.247Z","updated_at":"2025-10-18T18:56:30.503Z","avatar_url":"https://github.com/miquido.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build](https://travis-ci.org/miquido/observable.svg?branch=master)](https://travis-ci.org/miquido/observable)\n[![Maintainability](https://api.codeclimate.com/v1/badges/e60a93e53ddde8a3875c/maintainability)](https://codeclimate.com/github/miquido/observable/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/e60a93e53ddde8a3875c/test_coverage)](https://codeclimate.com/github/miquido/observable/test_coverage)\n[![MIT Licence](https://badges.frapsoft.com/os/mit/mit.svg?v=103)](https://opensource.org/licenses/mit-license.php)\n\n# Observable\n\nSet of classes for data streams.\n\n- [Installation guide](#installation)\n- [Code Samples](#code-samples)\n- [Contributing](#contributing)\n\n## Installation \nUse [Composer](https://getcomposer.org) to install the package:\n\n```shell\ncomposer require miquido/observable\n```\n\n## Code Samples\n- [Create and subscribe to a data stream](#create-and-subscribe-to-a-data-stream)\n- [Manipulate data in a stream with *Operators*](#manipulate-data-in-a-stream-with-operators)\n- [Using a *Subject*](#using-a-subject)\n- [List of build-in operators](#list-of-build-in-operators)\n\nPlease also check [miquido/csv-file-reader](https://github.com/miquido/csv-file-reader) library for more real-life examples.\n\n### Create and subscribe to a data stream\n\nYou can start with simple *Miquido\\Observable\\Stream\\FromArray::create* method.\n\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Observer;\n\n// create an observable stream from an array:\n// $stream is an objects that implements Miquido\\Observable\\ObservableInterface\n$stream = FromArray::create([1, 2, 3, 4, 5]);\n\n// then you can subscribe to the stream using Observer (both parameters are optional):\n$stream-\u003esubscribe(new Observer(\n    function (int $i): void { /* this callback will be called 5 times with consecutive 1, 2, 3, 4, 5*/ }, \n    function (): void { /* this callback will be called once after every items in the stream will be emitted */ }\n));\n\n// alternatively - if you are only interested in items in the stream you can pass just a callback \n$stream-\u003esubscribe(function (int $i): void {\n    // do something with numbers\n});\n\n```\n\n### Manipulate data in a stream with *Operators*\n\nOperators can be useful when you want to process the data in the stream before notifying observers.\nOperators do not interfere with source stream, every operator returns new stream that can be subscribed independently.\n\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 2, 3, 4, 5]);\n\n$squareStream = $stream-\u003epipe(new Operator\\Map(function (int $i): int {\n    return $i * $i;\n}));\n\n$sumStream = $stream-\u003epipe(new Operator\\Sum());\n$squareSumStream = $squareStream-\u003epipe(new Operator\\Sum());\n$tripleSumStream = $stream\n    -\u003epipe(new Operator\\Map(function (int $i): int {\n        return $i ** 3;\n    }))\n    -\u003epipe(new Operator\\Filter(function (int $i): bool {\n        return $i % 3 \u003e 0;\n    }));\n\n$squareStream-\u003esubscribe(function ($i) {}); // called 5 times with consecutive: 1, 4, 9, 16, 25\n$squareSumStream-\u003esubscribe(function ($i) {}); // called once with a number 55\n$sumStream-\u003esubscribe(function ($i) {}); // called once with a number 15\n```\n\nYou can also add multiple pipes:\n\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n\n$stream = FromArray::create([1, 2, 3, 4, 5, 6]);\n$stream\n    // first pipe raises each number to a power 3,\n    -\u003epipe(new Operator\\Map(function (int $i): int {\n        return $i ** 3;\n    }))\n    // then BufferCount(3) receives stream of numbers: 1, 8, 25, 64, 125, 216\n    // holds the stream until it receives 3 values, then releases array with three values\n    -\u003epipe(new Operator\\BufferCount(3))\n    // next pipes receives two arrays of three numbers: [1, 8, 27], [64, 125, 216] and returns sum of each group\n    -\u003epipe(new Operator\\Map(function (array $numbers): int {\n        return array_sum($numbers);\n    }))\n    -\u003esubscribe(function (int $i): void {\n        // subscribe() is called twice with numbers: 36, 405\n    });\n```\n\n### Using a *Subject*\nSubject acts both as an observer and as an observable. See an example below:\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Subject\\Subject;\nuse Miquido\\Observable\\Operator;\n\n// lets create a Subject\n$words = new Subject();\n\n// because it is an observable, you can pipe and subscribe to the data\n$words\n    -\u003epipe(new Operator\\Map(function (string $word): string {\n        return \\strtoupper($word);\n    }))\n    -\u003esubscribe(function (string $word): void {\n        // receives upper cased words\n    });\n\n$words\n    -\u003epipe(new Operator\\Map('ucfirst'))\n    -\u003epipe(new Operator\\Reduce(\n        function (string $sentenceInProgress, string $word) {\n            return \\sprintf('%s %s', $sentenceInProgress, $word);\n        },\n        ''\n    ))\n    -\u003epipe(new Operator\\Map('trim'))\n    -\u003esubscribe(function (string $sentence): void {\n        // receives a sentence of all words int the stream\n    });\n\n// And because a Subject is also an observer, you can push new items to the stream.\n$words-\u003enext('lorem');\n$words-\u003enext('ipsum');\n$words-\u003enext('dolor');\n$words-\u003enext('sit');\n$words-\u003enext('amet');\n\n// complete will send a \"complete\" notification to all observers and will remove observers from the subject\n$words-\u003ecomplete();\n\n/**\n * In this example:\n * - first subscriber will receive 5 items: 'LOREM', 'IPSUM', 'DOLOR', 'SIT' and 'AMET'\n * - second subscriber will recive one item: 'Lorem Ipsum Dolor Sit Amet'\n */\n\n```\n\n### List of build-in operators\n- [ArrayCount](#arraycount-operator)\n- [BufferCount](#buffercount-operator)\n- [BufferUniqueCount](#bufferuniquecount-operator)\n- [Count](#count-operator)\n- [Filter](#filter-operator)\n- [Flat](#flat-operator)\n- [Flat](#let-operator)\n- [Reduce](#reduce-operator)\n- [Scan](#scan-operator)\n- [Sum](#sum-operator)\n\n### *ArrayCount* operator\nTransforms array item to number with count value.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([\n    [1, 2],\n    [3, 4, 5] \n]);\n$stream\n    -\u003epipe(new Operator\\ArrayCount())\n    -\u003esubscribe(function (int $count): void {\n        // called twice with values: 2 and 3\n    });\n```\n\n### *BufferCount* operator\nGroups individual items into an array of provided size.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 2, 3, 4, 5, 6]);\n$stream\n    -\u003epipe(new Operator\\BufferCount(3))\n    -\u003esubscribe(function (array $values): void {\n        // called twice with values: [1, 2, 3] and [4, 5, 6]\n    });\n```\n\n### *BufferUniqueCount* operator\nSimilar to *BufferCount*, but removes duplications.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 1, 2, 1, 3, 4, 5, 5, 6]);\n$stream\n    -\u003epipe(new Operator\\BufferUniqueCount(3))\n    -\u003esubscribe(function (array $values): void {\n        // called twice with values: [1, 2, 3] and [4, 5, 6]\n    });\n```\n\n### *Count* operator\nCount all items emitted into the stream.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 1, 2, 1, 3, 4, 5, 5, 6]);\n$stream\n    -\u003epipe(new Operator\\Count())\n    -\u003esubscribe(function (int $count): void {\n        // called once with value: 9\n    });\n```\n\n### *Filter* operator\nRemoves all values for which provided callback returns false.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 2, 3, 4, 5]);\n$stream\n    -\u003epipe(new Operator\\Filter(function (int $number): bool {\n        return $number % 2 === 0;\n    }))\n    -\u003esubscribe(function (int $number): void {\n        // called twice with values: 2, 4\n    });\n```\n\n### *Flat* operator\nIf item in a stream is an array, *Flat* converts this array into set of individual items. \n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([\n    [1, 2],\n    [3, 4, 5]\n]);\n$stream\n    -\u003epipe(new Operator\\Flat())\n    -\u003esubscribe(function (int $number): void {\n        // called 5 times with values: 1, 2, 3, 4, 5\n        var_dump($number);\n    });\n```\n\n### *Let* operator\nDoes nothing, do the stream, just fires provided callback for every item in the stream and returns unchanged value.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 2, 3, 4, 5]);\n$stream\n    -\u003epipe(new Operator\\Let(function (int $number): void {\n        // do something with this number, no need to return anything\n    }))\n    -\u003esubscribe(function (int $number): void {\n        // called 5 times with values: 1, 2, 3, 4, 5\n        var_dump($number);\n    });\n```\n\n### *Map* operator\nTransform each item in the stream into new value.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create(['lorem', 'ipsum', 'dolor', 'sit', 'amet']);\n$stream\n    -\u003epipe(new Operator\\Map(function (string $word): int {\n        return \\strlen($word);\n    }))\n    -\u003esubscribe(function (int $length): void {\n        // called 5 times with values: 5, 5, 5, 3, 5\n    });\n```\n### *Reduce* operator\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 2, 3, 4, 5]);\n$stream\n    -\u003epipe(new Operator\\Reduce(\n        function (int $sum, int $number): int {\n            return $sum + $number;\n        },\n        0\n    ))\n    -\u003esubscribe(function (int $sum): void {\n        // called once with value 15\n    });\n```\n### *Scan* operator\nLike *Reduce*, but observer receives a value after each Scan call.\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 2, 3, 4, 5]);\n$stream\n    -\u003epipe(new Operator\\Scan(\n        function (int $sum, int $number): int {\n            return $sum + $number;\n        },\n        0\n    ))\n    -\u003esubscribe(function (int $sum): void {\n        // called 5 times with values: 1, 3, 6, 10, 15\n    });\n```\n### *Sum* operator\nSums all items in the stream.\n\n```php\n\u003c?php\n\nuse Miquido\\Observable\\Stream\\FromArray;\nuse Miquido\\Observable\\Operator;\n\n$stream = FromArray::create([1, 2, 3, 4, 5]);\n$stream\n    -\u003epipe(new Operator\\Sum())\n    -\u003esubscribe(function (int $sum): void {\n        // called once with value 15\n    });\n```\n\n## Contributing\n\nPull requests, bug fixes and issue reports are welcome.\nBefore proposing a change, please discuss your change by raising an issue.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiquido%2Fobservable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmiquido%2Fobservable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmiquido%2Fobservable/lists"}