guglielmopepe/callbackmapiterator

This iterator applies the callback to the elements of the given iterator.

1.0.1 2022-01-16 18:38 UTC

This package is auto-updated.

Last update: 2024-09-17 00:27:47 UTC


README

This iterator applies the callback to the elements of the given iterator.

Table of Contents

Installation

Use Composer

$ composer require guglielmopepe/callbackmapiterator

Usage

The callback should accept up to three arguments: the current item, the current key and the iterator, respectively.

<?php

/**
 * Callback for CallbackMapIterator
 *
 * @param $current   Current item's value
 * @param $key       Current item's key
 * @param $iterator  Iterator being filtered
 * @return mixed     The item after it has been applied the callback function
 */
function my_callback($current, $key, $iterator)
{
    // Your code here
}

?>

Any callable may be used; such as a string containing a function name, an array for a method, or an anonymous function.

<?php

$data = ['foo','bar'];

// A callback
function my_callback($current, $key, $iterator)
{
    if ($key % 2 == 0)
    {
        return $current;
    }

    return \strtoupper($current);
}

// Applies callback
$iterator = new \CallbackMapIterator\CallbackMapIterator(new \ArrayIterator($data), 'my_callback');

?>

Support

If you have a request, please create a GitHub issue.

If you discover a security vulnerability, please send an email to Guglielmo Pepe at info@guglielmopepe.com. All security vulnerabilities will be promptly addressed.

Contributing

If you want to say thank you and/or support the active development of CallbackMapIterator:

  1. Add a GitHub Star to the project.
  2. Share the project on social media.
  3. Write a review or tutorial on Medium, Dev.to or personal blog.

Contacts

If you need information please send an email to info@guglielmopepe.com.

Roadmap

See the list of open issues:

Change log

Please see Changelog file for more information on what has changed recently.

License

Distributed under the MIT License. Please see License File for more information.