<?php


namespace App\Utils\Collections;

use InvalidArgumentException;

class IntegerCollection extends GeneralCollection
{
    public function push (int $element) : void {
        array_push($this->memory, $element);
        $this->length++;
    }
    public function unshift (int $element) {
        array_unshift($this->memory, $element);
        $this->length++;
    }
    public function get (int $index) : ?int {
        return $this->memory[$index];
    }
    public function pop () : ?int {
        $this->length--;
        return array_pop($this->memory);
    }
    public function shift () : ?int {
        $this->length--;
        return array_shift($this->memory);
    }
    public function find (object $callback) : ?int {
        if (is_callable($callback)) {
            for ($i = 0; $i < $this->length; $i++) {
                if ($callback($this->memory[$i], $i)) {
                    return $this->memory[$i];
                }
            }
            return null;
        } else {
            throw new InvalidArgumentException('some function only accepts function as argument.');
        }
    }
}