Git Product home page Git Product logo

php-stack-util's Introduction

PHP Stack Util

Build Status Test Coverage Dependency Status SensioLabs Insight Code Climate Latest Stable Version Total Downloads License

A PHP library providing common Stack implementation.

Installation

{
   	"require": {
        "chroma-x/stack-util": "~1.0"
    }
}

Usage

Autoloading and namesapce

require_once('path/to/vendor/autoload.php');

Handling a stack

Pushing to the stack

use ChromaX\StackUtil\Stack;

$stack = new Stack();

$stack
	->push('Item')
	->push('Another item')
	->push(12)
	->push(null)
	->push(8.12);

$stackSize = $stack->size();
echo 'Stack size: ' . $stackSize . PHP_EOL;
Output
Stack size: 5

Reading from the stack

Getting the last item

$lastItem = $stack->get();
echo 'Last item: ' . $lastItem . PHP_EOL;
Output
Last item: 8.12

Getting an item by index

If the index is not available null is returned

$secondItem = $stack->get(1);
echo 'Second item: ' . $secondItem . PHP_EOL;
Output
Second item: Another item

Popping from the stack

$poppedItem = $stack->pop();
echo 'Popped item: ' . $poppedItem . PHP_EOL;

$stackSize = $stack->size();
echo 'Stack size: ' . $stackSize . PHP_EOL;
Output
Popped item: 8.12
Stack size: 4

Updating stacked values

Updating the last item

$lastItem = $stack
	->set('9.12')
	->get();
echo 'Updated last item: ' . $lastItem . PHP_EOL;
Output
Updated last item: 9.12

Updating an item by index

$thirdItem = $stack
	->set('Third item', 2)
	->get(2);
echo 'Updated third item: ' . $thirdItem . PHP_EOL;
Output
Updated third item: Third item

Removing from the stack

echo 'Stack size: ' . $stack->size() . PHP_EOL;
$stack->delete(1);
echo 'Stack size: ' . $stack->size() . PHP_EOL;
Output
Stack size: 4
Stack size: 3

Iterating over the stack

Using foreach

foreach ($stack as $stackItemKey => $stackItemValue) {
	echo 'Stack item ' . $stackItemKey . ': ' . $stackItemValue . PHP_EOL;
}
Output
Stack item 0: Item
Stack item 1: Third item
Stack item 2: 9.12

Using for

for ($i = 0, $n = $stack->size(); $i < $n; $i++) {
	echo 'Stack index ' . $i . ': ' . $stack->get($i) . PHP_EOL;
}
Output
Stack index 0: Item
Stack index 1: Third item
Stack index 2: 9.12

Contribution

Contributing to our projects is always very appreciated.
But: please follow the contribution guidelines written down in the CONTRIBUTING.md document.

License

PHP Stack Util is under the MIT license.

php-stack-util's People

Watchers

 avatar  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.