PHP code example of mvccore / ext-tool-collections

1. Go to this page and download the library: Download mvccore/ext-tool-collections library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mvccore / ext-tool-collections example snippets


class MyItem {
	public function __construct (protected string $name) {}
	public function GetName (): string {
		return $this->name;
	}
}

$myItemsSet = new MyItemsSet([
	new MyItem('John'),
	new MyItem('Joe'),
]);
$myItemsMap = new MyItemsMap([
	'john'	=> new MyItem('John'),
	'joe'	=> new MyItem('Joe'),
]);
for ($i = 0, $l = count($myItemsSet); $i < $l; $i++) {
	$myItem = $myItemsSet[$i];
	// now `$myItem` local variable is automatically 
	// typed by your IDE as `MyItem`, the method 
	// `GetName()` is always autocompleted by your IDE:
	echo $myItem->GetName();
}
foreach ($myItemsMap as $myItem) {
	// now `$myItem` local variable is automatically 
	// typed by your IDE as `MyItem`, the method 
	// `GetName()` is always autocompleted by your IDE:
	echo $myItem->GetName();
}

class MyItemsSet extends \MvcCore\Ext\Tools\Collections\Set {
	// all methods are copy pasted from extended class:
	public function current (): MyItem {
		$offsetInt = $this->keys[$this->position];
		return $this->array[$offsetInt];
	}
	/**
	 * @param  int    $offset 
	 * @param  MyItem $value 
	 */
	public function offsetSet ($offset, $value): void {
		if ($offset === NULL) {
			$this->array[] = $value;
		} else {
			$offsetInt = intval($offset);
			$this->array[$offsetInt] = $value;
		}
		$this->keys = array_keys($this->array);
		$this->count = count($this->array);
	}
	/** @param int $offset */
	public function offsetGet ($offset): MyItem {
		$offsetInt = intval($offset);
		return array_key_exists($offsetInt, $this->array)
			? $this->array[$offsetInt] 
			: null;
	}
	public function shift (): MyItem {
		$offsetInt = array_shift($this->keys);
		$value = $this->array[$offsetInt];
		unset($this->array[$offsetInt]);
		$this->count -= 1;
		if ($this->position === $this->count)
			$this->position--;
		return $value;
	}
	/**
	 * @param MyItem $values,...
	 */
	public function unshift (): int {
		$args = func_get_args();
		$argsCnt = count($args);
		array_unshift($args, $this->array);
		$this->count = call_user_func_array('array_unshift', $args);
		$this->keys = array_keys($this->array);
		if ($this->position > 0)
			$this->position += $argsCnt;
		return $this->count;
	}
}

class MyItemsMap extends \MvcCore\Ext\Tools\Collections\Map {
	// all methods are copy pasted from extended class:
		public function current (): MyItem {
		$key = $this->keys[$this->position];
		return $this->array[$key];
	}
	/**
	 * @param string $offset 
	 * @param MyItem  $value 
	 */
	public function offsetSet ($offset, $value): void {
		if ($offset === NULL) {
			$this->array[] = $value;
		} else {
			$offsetStr = (string) $offset;
			$this->array[$offsetStr] = $value;
		}
		$this->keys = array_keys($this->array);
		$this->count = count($this->array);
	}
	/** @param string $offset */
	public function offsetGet ($offset): MyItem {
		$offsetStr = (string) $offset;
		return array_key_exists($offsetStr, $this->array)
			? $this->array[$offsetStr] 
			: null;
	}
	public function shift (): MyItem {
		$offsetStr = array_shift($this->keys);
		$value = $this->array[$offsetStr];
		unset($this->array[$offsetStr]);
		$this->count -= 1;
		if ($this->position === $this->count)
			$this->position--;
		return $value;
	}
}