定制 mvccore/ext-tool-collections 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mvccore/ext-tool-collections

Composer 安装命令:

composer require mvccore/ext-tool-collections

包简介

MvcCore - Extension - Tool - Collections - extension with collection classes for typed arrays in PHP.

README 文档

README

Latest Stable Version License PHP Version

Collection classes for typed arrays in PHP.

Be careful, in places, where is necessary to process large arrays very fast,
use arrays instead. This extensions are only for comfortable coding where
you have enough execution time or where time is not so critical.

Installation

composer require mvccore/ext-tool-collections

Usage

First, le'ts say we have defined collection item class like this:

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

To have automatically typed collection items
in for or foreach loops like this, without PHPDocs code:

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

For sequence collection, you need to create:

  • empty class extended from \MvcCore\Ext\Tools\Collections\Set
  • go to extended class and copy paste commented template methods into empty class
  • uncomment all template methods and replace all mixed types with final type (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;
	}
}

For associative collection, you need to create:

  • empty class extended from \MvcCore\Ext\Tools\Collections\Map
  • go to extended class and copy paste commented template methods into empty class
  • uncomment all template methods and replace all mixed types with final type (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;
	}
}

统计信息

  • 总下载量: 332
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2022-05-12

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固