mvccore/ext-tool-collections

MvcCore - 扩展 - 工具 - 集合 - PHP中类型化数组的集合类扩展。

v5.0.2 2022-10-21 15:39 UTC

This package is auto-updated.

Last update: 2024-09-26 14:29:12 UTC


README

Latest Stable Version License PHP Version

PHP中类型化数组的集合类。

请注意,在需要快速处理大数组的场合,
请使用数组。此扩展仅在您有足够执行时间或时间不是那么关键的地方
提供舒适编码。

安装

composer require mvccore/ext-tool-collections

用法

首先,让我们假设我们已经定义了一个集合项目类,如下所示

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

以便在for或foreach循环中自动具有类型化集合项目
如下所示,无需PHPDocs代码

$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();
}

对于顺序集合,您需要创建

  • \MvcCore\Ext\Tools\Collections\Set 扩展的空类
  • 进入扩展类并将注释模板方法复制粘贴到空类中
  • 取消注释所有模板方法,并将所有混合类型替换为最终类型(MyItem
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;
	}
}

对于关联集合,您需要创建

  • \MvcCore\Ext\Tools\Collections\Map 扩展的空类
  • 进入扩展类并将注释模板方法复制粘贴到空类中
  • 取消注释所有模板方法,并将所有混合类型替换为最终类型(MyItem
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;
	}
}