{"id":16721088,"url":"https://github.com/khalyomede/reorder-before-after","last_synced_at":"2025-03-15T12:23:18.432Z","repository":{"id":65210346,"uuid":"587902211","full_name":"khalyomede/reorder-before-after","owner":"khalyomede","description":"Reorder an item in an array before or after another.","archived":false,"fork":false,"pushed_at":"2023-11-28T20:44:12.000Z","size":61,"stargazers_count":0,"open_issues_count":3,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-15T03:47:38.880Z","etag":null,"topics":["after","before","reorder","reordering"],"latest_commit_sha":null,"homepage":"https://packagist.org/packages/khalyomede/reorder-before-after","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/khalyomede.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-01-11T21:19:09.000Z","updated_at":"2023-11-28T20:45:43.000Z","dependencies_parsed_at":"2024-11-21T08:42:21.238Z","dependency_job_id":"c1fb4a07-47f7-42dd-b68e-d4761afeb6f5","html_url":"https://github.com/khalyomede/reorder-before-after","commit_stats":{"total_commits":22,"total_committers":2,"mean_commits":11.0,"dds":0.4545454545454546,"last_synced_commit":"d31867de3d509688285e3d04dfde0e0815ac3797"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Freorder-before-after","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Freorder-before-after/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Freorder-before-after/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/khalyomede%2Freorder-before-after/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/khalyomede","download_url":"https://codeload.github.com/khalyomede/reorder-before-after/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243727249,"owners_count":20337956,"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":["after","before","reorder","reordering"],"created_at":"2024-10-12T22:28:42.610Z","updated_at":"2025-03-15T12:23:18.406Z","avatar_url":"https://github.com/khalyomede.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# reorder-before-after\n\nReorder an item in an array before or after another.\n\n## Summary\n\n- [About](#about)\n- [Features](#features)\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Examples](#examples)\n- [Tests](#tests)\n\n## About\n\nI make a web app in which my users can reorder before or after another.\n\nI did not find any package to do what I search for, and since I use it at multiple place on my app I figured I would open source it in the hope it can help simplify the life of another developer out there!\n\n## Features\n\n- Can reorder an item before or after another in a list of items\n- Systematically \"self-heals\" the items orders by re-writing the index of all items starting from the minimum index\n\n## Requirements\n\n- PHP \u003e= 8.1\n- Composer\n\n## Installation\n\nIn your terminal, on the root of your project folder, run:\n\n```bash\ncomposer require khalyomede/reorder-before-after\n```\n\n## Examples\n\n- [1. Moving an item before another](#1-moving-an-item-before-another)\n- [2. Create a list of items out of any type](#2-create-a-list-of-items-out-of-any-type)\n- [3. Find an item by its value](#3-find-an-item-by-its-value)\n- [4. Create a listing from an array](#4-create-a-listing-from-an-array)\n- [5. Getting all items from a listing](#5-getting-all-items-from-a-listing)\n- [6. Use a callback to apply the order on your value](#6-use-a-callback-to-apply-the-order-on-your-value)\n- [7. Create a listing from values and specify how to retrieve the order using a callback](#7-create-a-listing-from-values-and-specify-how-to-retrieve-the-order-using-a-callback)\n- [8. Specify how to match items together](#8-specify-how-to-match-items-together)\n\n### 1. Moving an item before another\n\nIn this example, we will move our book before our table.\n\n```php\nuse Khalyomede\\ReorderBeforeAfter\\Item;\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\nuse Khalyomede\\ReorderBeforeAfter\\Placement;\n\n$listing = new Listing();\n\n$listing-\u003epush(new Item(\"bag\", 1));\n$listing-\u003epush(new Item(\"chair\", 2));\n$listing-\u003epush(new Item(\"table\", 3));\n$listing-\u003epush(new Item(\"book\", 4));\n\n$listing-\u003ereorder(\"book\", Placement::Before, \"table\");\n\nassert($listing-\u003efind(\"bag\")-\u003eorder === 1);\nassert($listing-\u003efind(\"chair\")-\u003eorder === 2);\nassert($listing-\u003efind(\"book\")-\u003eorder === 3);\nassert($listing-\u003efind(\"table\")-\u003eorder === 4);\n```\n\n### 2. Create a list of items out of any type\n\nIn this example, we will see that we can create a listing of anything, including objects.\n\n```php\nuse Khalyomede\\ReorderBeforeAfter\\Item;\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\n\nfinal readonly class Product\n{\n    public function __construct(\n        public string $name,\n        public int $quantity,\n        public float $unitPrice,\n    ) {}\n}\n\n$bags = new Product(\"bag\", 15, 149.99);\n$tables = new Product(\"table\", 4, 89.99);\n$chairs = new Product(\"chairs\", 1, 399.99);\n\n$listing = new Listing();\n\n$listing-\u003epush(new Item($bags, 1));\n$listing-\u003epush(new Item($tables, 2));\n$listing-\u003epush(new Item($chairs, 3));\n```\n\n### 3. Find an item by its value\n\nIn this example, we will see we can find an item by its \"value\" (the first parameter when creating a new instance of `Item`).\n\n```php\nuse Khalyomede\\ReorderBeforeAfter\\Item;\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\nuse Khalyomede\\ReorderBeforeAfter\\Placement;\n\n$listing = new Listing();\n\n$listing-\u003epush(new Item(\"bag\", 1));\n$listing-\u003epush(new Item(\"chair\", 2));\n$listing-\u003epush(new Item(\"table\", 3));\n$listing-\u003epush(new Item(\"book\", 4));\n\necho $listing-\u003efind(\"bag\"); // Item(value: \"bag\", order: 1)\n```\n\nIf the method finds no item matching the value, a `ItemNotFoundException` will be thrown.\n\nYou can also search by anything that can be checked using `===`, including objects.\n\n```php\nuse Khalyomede\\ReorderBeforeAfter\\Item;\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\nuse Khalyomede\\ReorderBeforeAfter\\Placement;\n\nfinal readonly class Product\n{\n    public function __construct(\n        public string $name,\n        public int $quantity,\n        public float $unitPrice,\n    ) {}\n}\n\n$bags = new Product(\"bag\", 15, 149.99);\n$tables = new Product(\"table\", 4, 89.99);\n$chairs = new Product(\"chairs\", 1, 399.99);\n\n$listing = new Listing();\n\n$listing-\u003epush(new Item($bags, 1));\n$listing-\u003epush(new Item($tables, 2));\n$listing-\u003epush(new Item($chairs, 3));\n\necho $listing-\u003efind($bags); // Item(value: Product(name: \"bag\", quantity: 15, unitPrice: 149.99), order: 1)\n```\n\n### 4. Create a listing from an array\n\nIn this example, we will create the listing from an array instead of manually pushing items.\n\n```php\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\n\n$listing = Listing::from([\n    new Item(\"bag\", 1),\n    new Item(\"chair\", 2),\n    new Item(\"book\", 3),\n    new Item(\"table\", 4),\n]);\n\n$listing-\u003ereorder(\"bag\", Placement::After, \"book\");\n\nassert($listing-\u003efind(\"bag\")-\u003eorder === 3);\n```\n\n### 5. Getting all items from a listing\n\nIn this example, we will get all items from a listing. Useful if you want to perform some task after reordering your items.\n\n```php\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\n\n$listing = Listing::from([\n    new Item(\"bag\", 1),\n    new Item(\"chair\", 2),\n    new Item(\"book\", 3),\n    new Item(\"table\", 4),\n]);\n\n$products = $listing-\u003eall();\n\nforeach ($products as $product) {\n    echo $product; // \"bag\" or \"chair\" or \"book\" or \"table\"\n}\n```\n\n### 6. Use a callback to apply the order on your value\n\nIn this example, we will instruct the listing how it should set the order on our values. Useful if our values are objects holding their order. This saves you from looping again on all your objects.\n\nThis examples features an hypothetical `Product` [Eloquent](https://laravel.com/docs/master/eloquent) model.\n\n```php\nuse App\\Models\\Product;\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\n\n$items = Product::all()-\u003emap(fn (Product $product): Item =\u003e new Item($product, $product-\u003eorder));\n$listing = Listing::from($items);\n\n$listing-\u003eapplyWith(function (Item $item): void {\n    $item-\u003evalue-\u003eorder = $item-\u003eorder;\n    $item-\u003evalue-\u003esave();\n});\n```\n\n### 7. Create a listing from values and specify how to retrieve the order using a callback\n\nIn this example, we will see how to create a listing out of an array of values, and using the second parameter to specify how to get the order from these values.\n\nUseful if you have objects that already hold their own order, and you do not want to loop from them and create the `Item` by hand.\n\n```php\nuse App\\Models\\Product;\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\n\n$products = Product::all();\n$listing = Listing::outOf($products, fn (Product $product): Item =\u003e new Item($product, $product-\u003eorder));\n```\n\n### 8. Specify how to match items together\n\nIn this example, we will instruct the listing how it should match our objects. Useful if your objects can contain different set of data, making the whole object different, when in reality both are equal (matching by id).\n\nThis examples features an hypothetical `Product` [Eloquent](https://laravel.com/docs/master/eloquent) model, where one of the two can have more attributes (like eager loaded relationships), making the triple equal check invalid.\n\n```php\nuse App\\Models\\Product;\nuse Khalyomede\\ReorderBeforeAfter\\Listing;\n\n$listing = Listing::outOf(Product::all(), fn (Product $product): Item =\u003e new Item($product, $product-\u003eorder));\n\n$listing-\u003ematchWith(fn (Product $left, Product $right): bool =\u003e $left-\u003eid === $right-\u003eid);\n\n// Or with the shorter Eloquent::is() method\n$listing-\u003ematchWith(fn (Product $left, Product $right): bool =\u003e $left-\u003eis($right));\n```\n\n## Tests\n\n```bash\ncomposer run test\ncomposer run analyse\ncomposer run lint\ncomposer run check\ncomposer run updates\ncomposer run scan\n```\n\nOr\n\n```bash\ncomposer run all\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Freorder-before-after","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkhalyomede%2Freorder-before-after","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkhalyomede%2Freorder-before-after/lists"}