Coder Social home page Coder Social logo

colloection's Introduction

PHP Collection Class (POC)

This Collection class is a POC inspired by Laravel's collection. It offers various methods for working with arrays of data in PHP.

Functionality

The Collection class provides the following functionalities:

  • First: Get the first element of the collection.
  • Last: Get the last element of the collection.
  • Merge: Merge the collection with another array.
  • Pop: Remove and return the last element from the collection.
  • Count: Count the number of elements in the collection.
  • Map: Apply a callback to each element of the collection.
  • Sort: Sort the collection either in ascending order.
  • SortBy: Sort the collection by a specified direction and key.
  • All: Get all elements of the collection.

Please note that the SortBy method is not fully working yet, as indicated by the provided comments in the code.

Example Usage

<?php

use Davids\Collection\Collection;

// Create a new collection
$collection = new Collection([1, 2, 3]);

// Get the first element
$result = $collection->first();
// Returns: 1

// Get the last element
$result = $collection->last();
// Returns: 3

// Merge with another array
$result = $collection->merge([4, 5, 6]);
// Returns: Collection object with elements [1, 2, 3, 4, 5, 6]

// Remove and return the last element
$result = $collection->pop();
// Returns: Collection object with elements [1, 2]

// Count the number of elements
$result = $collection->count();
// Returns: 2

// Apply a callback to each element
$result = $collection->map(function($item) { return $item * 2; });
// Returns: Collection object with elements [2, 4]

// Sort the collection
$result = $collection->sort();
// Returns: Collection object with elements [1, 2]

// Get all elements
$result = $collection->all();
// Returns: [1, 2]

Additional Examples

<?php

use Davids\Collection\Collection;

// Create an associative collection
$assocCollection = new Collection(['Timisoara' => 200000, 'Cluj' => 400000]);

// Merge with another array and then map
$mappedAssocCollection = $assocCollection
    ->merge(['Arad' => 30000])
    ->sort()
    ->map(function ($key, $item) {
        return $key . ' has ' . $item . ' habitats';
    })
    ->last();

print_r($mappedAssocCollection);
// Output: Cluj has 400000 habitats

In this example, we create an associative collection with population data for two cities. Then, we merge it with another array containing population data for a new city ('Arad'). Next, we sort the collection by the population (values), map over the collection to create a new array with formatted strings indicating the number of habitats for each city, and finally, retrieve the last element of the resulting collection, which is 'Cluj has 400000 habitats'.

Note

This Collection class is a proof of concept (POC) and is not complete or fully functional. Further testing and development are needed. Contributions are welcome!

colloection's People

Contributors

davseve avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.