提交的内容

This commit is contained in:
2025-05-12 15:45:02 +08:00
parent 629c4750da
commit b48c692775
3043 changed files with 34732 additions and 60810 deletions

168
vendor/topthink/think-helper/src/Collection.php vendored Executable file → Normal file
View File

@ -24,6 +24,12 @@ use Traversable;
/**
* 数据集管理类
*
* @template TKey of array-key
* @template-covariant TValue
*
* @implements ArrayAccess<TKey, TValue>
* @implements IteratorAggregate<TKey, TValue>
*/
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Arrayable, Jsonable
{
@ -33,11 +39,19 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
*/
protected $items = [];
/**
* 构造函数
* @param iterable<TKey, TValue>|Collection<TKey, TValue> $items 数据
*/
public function __construct($items = [])
{
$this->items = $this->convertToArray($items);
}
/**
* @param iterable<TKey, TValue>|Collection<TKey, TValue> $items
* @return static<TKey, TValue>
*/
public static function make($items = [])
{
return new static($items);
@ -45,7 +59,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 是否为空
* @access public
* @return bool
*/
public function isEmpty(): bool
@ -60,6 +73,9 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
}, $this->items);
}
/**
* @return array<TKey, TValue>
*/
public function all(): array
{
return $this->items;
@ -68,7 +84,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 合并数组
*
* @access public
* @param mixed $items 数据
* @return static
*/
@ -80,12 +95,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 按指定键整理数据
*
* @access public
* @param mixed $items 数据
* @param string $indexKey 键名
* @param string|null $indexKey 键名
* @return array
*/
public function dictionary($items = null, string &$indexKey = null)
public function dictionary($items = null, ?string &$indexKey = null)
{
if ($items instanceof self) {
$items = $items->all();
@ -107,12 +121,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 比较数组,返回差集
*
* @access public
* @param mixed $items 数据
* @param string $indexKey 指定比较的键名
* @return static
* @param string|null $indexKey 指定比较的键名
* @return static<TKey, TValue>
*/
public function diff($items, string $indexKey = null)
public function diff($items, ?string $indexKey = null)
{
if ($this->isEmpty() || is_scalar($this->items[0])) {
return new static(array_diff($this->items, $this->convertToArray($items)));
@ -135,12 +148,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 比较数组,返回交集
*
* @access public
* @param mixed $items 数据
* @param string $indexKey 指定比较的键名
* @return static
* @param string|null $indexKey 指定比较的键名
* @return static<TKey, TValue>
*/
public function intersect($items, string $indexKey = null)
public function intersect($items, ?string $indexKey = null)
{
if ($this->isEmpty() || is_scalar($this->items[0])) {
return new static(array_intersect($this->items, $this->convertToArray($items)));
@ -163,8 +175,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 交换数组中的键和值
*
* @access public
* @return static
* @return static<TValue, TKey>
*/
public function flip()
{
@ -174,8 +185,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 返回数组中所有的键名
*
* @access public
* @return static
* @return static<TKey, TValue>
*/
public function keys()
{
@ -184,8 +194,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 返回数组中所有的值组成的新 Collection 实例
* @access public
* @return static
* @return static<int, TValue>
*/
public function values()
{
@ -195,8 +204,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 删除数组的最后一个元素(出栈)
*
* @access public
* @return mixed
* @return TValue
*/
public function pop()
{
@ -206,7 +214,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 通过使用用户自定义函数,以字符串返回数组
*
* @access public
* @param callable $callback 调用方法
* @param mixed $initial
* @return mixed
@ -219,8 +226,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 以相反的顺序返回数组。
*
* @access public
* @return static
* @return static<TKey, TValue>
*/
public function reverse()
{
@ -230,8 +236,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 删除数组中首个元素,并返回被删除元素的值
*
* @access public
* @return mixed
* @return TValue
*/
public function shift()
{
@ -240,12 +245,12 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 在数组结尾插入一个元素
* @access public
*
* @param mixed $value 元素
* @param string $key KEY
* @param string|null $key KEY
* @return $this
*/
public function push($value, string $key = null)
public function push($value, ?string $key = null)
{
if (is_null($key)) {
$this->items[] = $value;
@ -259,7 +264,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 把一个数组分割为新的数组块.
*
* @access public
* @param int $size 块大小
* @param bool $preserveKeys
* @return static
@ -277,12 +281,12 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 在数组开头插入一个元素
* @access public
*
* @param mixed $value 元素
* @param string $key KEY
* @param string|null $key KEY
* @return $this
*/
public function unshift($value, string $key = null)
public function unshift($value, ?string $key = null)
{
if (is_null($key)) {
array_unshift($this->items, $value);
@ -296,7 +300,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 给每个元素执行个回调
*
* @access public
*
* @param callable $callback 回调
* @return $this
*/
@ -317,9 +321,9 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 用回调函数处理数组中的元素
* @access public
*
* @param callable|null $callback 回调
* @return static
* @return static<TKey, TValue>
*/
public function map(callable $callback)
{
@ -328,11 +332,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 用回调函数过滤数组中的元素
* @access public
*
* @param callable|null $callback 回调
* @return static
* @return static<TKey, TValue>
*/
public function filter(callable $callback = null)
public function filter(?callable $callback = null)
{
if ($callback) {
return new static(array_filter($this->items, $callback));
@ -343,11 +347,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 根据字段条件过滤数组中的元素
* @access public
*
* @param string $field 字段名
* @param mixed $operator 操作符
* @param mixed $value 数据
* @return static
* @return static<TKey, TValue>
*/
public function where(string $field, $operator, $value = null)
{
@ -405,10 +409,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* LIKE过滤
* @access public
*
* @param string $field 字段名
* @param string $value 数据
* @return static
* @return static<TKey, TValue>
*/
public function whereLike(string $field, string $value)
{
@ -417,10 +421,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* NOT LIKE过滤
* @access public
*
* @param string $field 字段名
* @param string $value 数据
* @return static
* @return static<TKey, TValue>
*/
public function whereNotLike(string $field, string $value)
{
@ -429,10 +433,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* IN过滤
* @access public
*
* @param string $field 字段名
* @param array $value 数据
* @return static
* @return static<TKey, TValue>
*/
public function whereIn(string $field, array $value)
{
@ -441,10 +445,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* NOT IN过滤
* @access public
*
* @param string $field 字段名
* @param array $value 数据
* @return static
* @return static<TKey, TValue>
*/
public function whereNotIn(string $field, array $value)
{
@ -453,10 +457,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* BETWEEN 过滤
* @access public
*
* @param string $field 字段名
* @param mixed $value 数据
* @return static
* @return static<TKey, TValue>
*/
public function whereBetween(string $field, $value)
{
@ -465,10 +469,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* NOT BETWEEN 过滤
* @access public
*
* @param string $field 字段名
* @param mixed $value 数据
* @return static
* @return static<TKey, TValue>
*/
public function whereNotBetween(string $field, $value)
{
@ -477,12 +481,12 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 返回数据中指定的一列
* @access public
*
* @param string|null $columnKey 键名
* @param string|null $indexKey 作为索引值的列
* @return array
*/
public function column( ? string $columnKey, string $indexKey = null)
public function column(?string $columnKey, ?string $indexKey = null)
{
return array_column($this->items, $columnKey, $indexKey);
}
@ -490,11 +494,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 对数组排序
*
* @access public
* @param callable|null $callback 回调
* @return static
* @return static<TKey, TValue>
*/
public function sort(callable $callback = null)
public function sort(?callable $callback = null)
{
$items = $this->items;
@ -509,7 +512,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 指定字段排序
* @access public
*
* @param string $field 排序字段
* @param string $order 排序
* @return $this
@ -520,15 +523,14 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
$fieldA = $a[$field] ?? null;
$fieldB = $b[$field] ?? null;
return 'desc' == strtolower($order) ? intval($fieldB > $fieldA) : intval($fieldA > $fieldB);
return 'desc' == strtolower($order) ? ($fieldB <=> $fieldA) : ($fieldA <=> $fieldB);
});
}
/**
* 将数组打乱
*
* @access public
* @return static
* @return static<TKey, TValue>
*/
public function shuffle()
{
@ -542,12 +544,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 获取第一个单元数据
*
* @access public
* @param callable|null $callback
* @param null $default
* @return mixed
* @return TValue
*/
public function first(callable $callback = null, $default = null)
public function first(?callable $callback = null, $default = null)
{
return Arr::first($this->items, $callback, $default);
}
@ -555,12 +556,11 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 获取最后一个单元数据
*
* @access public
* @param callable|null $callback
* @param null $default
* @return mixed
* @return TValue
*/
public function last(callable $callback = null, $default = null)
public function last(?callable $callback = null, $default = null)
{
return Arr::last($this->items, $callback, $default);
}
@ -568,30 +568,41 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 截取数组
*
* @access public
* @param int $offset 起始位置
* @param int $length 截取长度
* @param int|null $length 截取长度
* @param bool $preserveKeys preserveKeys
* @return static
* @return static<TKey, TValue>
*/
public function slice(int $offset, int $length = null, bool $preserveKeys = false)
public function slice(int $offset, ?int $length = null, bool $preserveKeys = false)
{
return new static(array_slice($this->items, $offset, $length, $preserveKeys));
}
// ArrayAccess
/**
* @param TKey $key
* @return bool
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset) : bool
{
return array_key_exists($offset, $this->items);
}
/**
* @param TKey $offset
* @return TValue
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->items[$offset];
}
/**
* @param TKey|null $offset
* @param TValue $value
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
@ -602,6 +613,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
}
}
/**
* @param TKey $offset
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
@ -614,7 +629,9 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
return count($this->items);
}
//IteratorAggregate
/**
* @return ArrayIterator<TKey, TValue>
*/
#[\ReturnTypeWillChange]
public function getIterator(): Traversable
{
@ -630,7 +647,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 转换当前数据集为JSON字符串
* @access public
*
* @param integer $options json参数
* @return string
*/
@ -647,7 +664,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
/**
* 转换成数组
*
* @access public
* @param mixed $items 数据
* @return array
*/

0
vendor/topthink/think-helper/src/contract/Arrayable.php vendored Executable file → Normal file
View File

0
vendor/topthink/think-helper/src/contract/Jsonable.php vendored Executable file → Normal file
View File

68
vendor/topthink/think-helper/src/helper.php vendored Executable file → Normal file
View File

@ -16,12 +16,15 @@ if (!function_exists('throw_if')) {
/**
* 按条件抛异常
*
* @param mixed $condition
* @param Throwable|string $exception
* @param array ...$parameters
* @return mixed
* @template TValue
* @template TException of \Throwable
*
* @throws Throwable
* @param TValue $condition
* @param TException|class-string<TException>|string $exception
* @param mixed ...$parameters
* @return TValue
*
* @throws TException
*/
function throw_if($condition, $exception, ...$parameters)
{
@ -37,11 +40,15 @@ if (!function_exists('throw_unless')) {
/**
* 按条件抛异常
*
* @param mixed $condition
* @param Throwable|string $exception
* @param array ...$parameters
* @return mixed
* @throws Throwable
* @template TValue
* @template TException of \Throwable
*
* @param TValue $condition
* @param TException|class-string<TException>|string $exception
* @param mixed ...$parameters
* @return TValue
*
* @throws TException
*/
function throw_unless($condition, $exception, ...$parameters)
{
@ -57,9 +64,11 @@ if (!function_exists('tap')) {
/**
* 对一个值调用给定的闭包,然后返回该值
*
* @param mixed $value
* @param callable|null $callback
* @return mixed
* @template TValue
*
* @param TValue $value
* @param (callable(TValue): mixed)|null $callback
* @return TValue
*/
function tap($value, $callback = null)
{
@ -77,8 +86,10 @@ if (!function_exists('value')) {
/**
* Return the default value of the given value.
*
* @param mixed $value
* @return mixed
* @template TValue
*
* @param TValue|\Closure(): TValue $value
* @return TValue
*/
function value($value)
{
@ -277,3 +288,30 @@ if (!function_exists('class_uses_recursive')) {
return array_unique($results);
}
}
if (!function_exists('array_is_list')) {
/**
* 判断数组是否为list
*
* @param array $array 数据
* @return bool
*/
function array_is_list(array $array): bool
{
return array_values($array) === $array;
}
}
if (!function_exists('json_validate')) {
/**
* 判断是否为有效json数据
*
* @param string $string 数据
* @return bool
*/
function json_validate(string $string): bool
{
json_decode($string);
return json_last_error() === JSON_ERROR_NONE;
}
}

99
vendor/topthink/think-helper/src/helper/Arr.php vendored Executable file → Normal file
View File

@ -32,9 +32,9 @@ class Arr
/**
* Add an element to an array using "dot" notation if it doesn't exist.
*
* @param array $array
* @param array $array
* @param string $key
* @param mixed $value
* @param mixed $value
* @return array
*/
public static function add($array, $key, $value)
@ -110,7 +110,7 @@ class Arr
/**
* Flatten a multi-dimensional associative array with dots.
*
* @param array $array
* @param array $array
* @param string $prepend
* @return array
*/
@ -132,7 +132,7 @@ class Arr
/**
* Get all of the given array except for a specified array of keys.
*
* @param array $array
* @param array $array
* @param array|string $keys
* @return array
*/
@ -147,7 +147,7 @@ class Arr
* Determine if the given key exists in the provided array.
*
* @param \ArrayAccess|array $array
* @param string|int $key
* @param string|int $key
* @return bool
*/
public static function exists($array, $key)
@ -162,12 +162,12 @@ class Arr
/**
* Return the first element in an array passing a given truth test.
*
* @param array $array
* @param array $array
* @param callable|null $callback
* @param mixed $default
* @param mixed $default
* @return mixed
*/
public static function first($array, callable $callback = null, $default = null)
public static function first($array, ?callable $callback = null, $default = null)
{
if (is_null($callback)) {
if (empty($array)) {
@ -191,12 +191,12 @@ class Arr
/**
* Return the last element in an array passing a given truth test.
*
* @param array $array
* @param array $array
* @param callable|null $callback
* @param mixed $default
* @param mixed $default
* @return mixed
*/
public static function last($array, callable $callback = null, $default = null)
public static function last($array, ?callable $callback = null, $default = null)
{
if (is_null($callback)) {
return empty($array) ? value($default) : end($array);
@ -209,7 +209,7 @@ class Arr
* Flatten a multi-dimensional array into a single level.
*
* @param array $array
* @param int $depth
* @param int $depth
* @return array
*/
public static function flatten($array, $depth = INF)
@ -234,7 +234,7 @@ class Arr
/**
* Remove one or many array items from a given array using "dot" notation.
*
* @param array $array
* @param array $array
* @param array|string $keys
* @return void
*/
@ -279,8 +279,8 @@ class Arr
* Get an item from an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string $key
* @param mixed $default
* @param string $key
* @param mixed $default
* @return mixed
*/
public static function get($array, $key, $default = null)
@ -316,7 +316,7 @@ class Arr
* Check if an item or items exist in an array using "dot" notation.
*
* @param \ArrayAccess|array $array
* @param string|array $keys
* @param string|array $keys
* @return bool
*/
public static function has($array, $keys)
@ -356,15 +356,13 @@ class Arr
*/
public static function isAssoc(array $array)
{
$keys = array_keys($array);
return array_keys($keys) !== $keys;
return !array_is_list($array);
}
/**
* Get a subset of the items from the given array.
*
* @param array $array
* @param array $array
* @param array|string $keys
* @return array
*/
@ -376,8 +374,8 @@ class Arr
/**
* Pluck an array of values from an array.
*
* @param array $array
* @param string|array $value
* @param array $array
* @param string|array $value
* @param string|array|null $key
* @return array
*/
@ -412,7 +410,7 @@ class Arr
/**
* Explode the "value" and "key" arguments passed to "pluck".
*
* @param string|array $value
* @param string|array $value
* @param string|array|null $key
* @return array
*/
@ -447,9 +445,9 @@ class Arr
/**
* Get a value from the array, and remove it.
*
* @param array $array
* @param array $array
* @param string $key
* @param mixed $default
* @param mixed $default
* @return mixed
*/
public static function pull(&$array, $key, $default = null)
@ -464,7 +462,7 @@ class Arr
/**
* Get one or a specified number of random values from an array.
*
* @param array $array
* @param array $array
* @param int|null $number
* @return mixed
*
@ -506,9 +504,9 @@ class Arr
*
* If no key is given to the method, the entire array will be replaced.
*
* @param array $array
* @param array $array
* @param string $key
* @param mixed $value
* @param mixed $value
* @return array
*/
public static function set(&$array, $key, $value)
@ -540,7 +538,7 @@ class Arr
/**
* Shuffle the given array and return the result.
*
* @param array $array
* @param array $array
* @param int|null $seed
* @return array
*/
@ -562,7 +560,7 @@ class Arr
/**
* Sort the array using the given callback or "dot" notation.
*
* @param array $array
* @param array $array
* @param callable|string|null $callback
* @return array
*/
@ -608,7 +606,7 @@ class Arr
/**
* Filter the array using the given callback.
*
* @param array $array
* @param array $array
* @param callable $callback
* @return array
*/
@ -631,4 +629,41 @@ class Arr
return is_array($value) ? $value : [$value];
}
}
/**
* Recursively merge arrays.
* If the value is an associative array, it will be merged recursively.
* If the value is an indexed array, it will be replaced entirely.
*
* @param array ...$arrays
* @return array
*/
public static function mergeDeep(array ...$arrays): array
{
$result = [];
foreach ($arrays as $array) {
foreach ($array as $key => $value) {
if (isset($result[$key]) && is_array($result[$key]) && is_array($value)) {
// 只有当两个数组都是关联数组时才递归合并
if (self::isAssoc($result[$key]) && self::isAssoc($value)) {
$result[$key] = self::mergeDeep(
$result[$key],
$value
);
} else {
// 如果任一数组是索引数组,则直接覆盖
$result[$key] = $value;
}
} else {
$result[$key] = $value;
}
}
}
return $result;
}
public static function flatMap(callable $fn, array $array): array
{
return array_merge(...array_map($fn, $array));
}
}

View File

@ -0,0 +1,66 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace think\helper;
use Closure;
use think\exception\FuncNotFoundException;
trait Macroable
{
/**
* 方法注入.
*
* @var Closure[]
*/
protected static $macro = [];
/**
* 设置方法注入.
*
* @param string $method
* @param Closure $closure
*
* @return void
*/
public static function macro(string $method, Closure $closure)
{
static::$macro[$method] = $closure;
}
/**
* 检查方法是否已经有注入
*
* @param string $name
* @return bool
*/
public static function hasMacro(string $method)
{
return isset(static::$macro[$method]);
}
public function __call($method, $args)
{
if (!isset(static::$macro[$method])) {
throw new FuncNotFoundException('method not exists: ' . static::class . '::' . $method . '()', "{static::class}::{$method}");
}
return call_user_func_array(static::$macro[$method]->bindTo($this, static::class), $args);
}
public static function __callStatic($method, $args)
{
if (!isset(static::$macro[$method])) {
throw new FuncNotFoundException('method not exists: ' . static::class . '::' . $method . '()', "{static::class}::{$method}");
}
return call_user_func_array(static::$macro[$method]->bindTo(null, static::class), $args);
}
}

4
vendor/topthink/think-helper/src/helper/Str.php vendored Executable file → Normal file
View File

@ -80,7 +80,7 @@ class Str
* @param string $addChars
* @return string
*/
public static function random(int $length = 6, int $type = null, string $addChars = ''): string
public static function random(int $length = 6, ?int $type = null, string $addChars = ''): string
{
$str = '';
switch ($type) {
@ -158,7 +158,7 @@ class Str
* @param int|null $length
* @return string
*/
public static function substr(string $string, int $start, int $length = null): string
public static function substr(string $string, int $start, ?int $length = null): string
{
return mb_substr($string, $start, $length, 'UTF-8');
}

View File

@ -1,203 +0,0 @@
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 刘志淳 <chun@engineer.com>
// +----------------------------------------------------------------------
namespace think\helper;
class Time
{
/**
* 返回今日开始和结束的时间戳
*
* @return array
*/
public static function today()
{
list($y, $m, $d) = explode('-', date('Y-m-d'));
return [
mktime(0, 0, 0, $m, $d, $y),
mktime(23, 59, 59, $m, $d, $y)
];
}
/**
* 返回昨日开始和结束的时间戳
*
* @return array
*/
public static function yesterday()
{
$yesterday = date('d') - 1;
return [
mktime(0, 0, 0, date('m'), $yesterday, date('Y')),
mktime(23, 59, 59, date('m'), $yesterday, date('Y'))
];
}
/**
* 返回本周开始和结束的时间戳
*
* @return array
*/
public static function week()
{
list($y, $m, $d, $w) = explode('-', date('Y-m-d-w'));
if($w == 0) $w = 7; //修正周日的问题
return [
mktime(0, 0, 0, $m, $d - $w + 1, $y), mktime(23, 59, 59, $m, $d - $w + 7, $y)
];
}
/**
* 返回上周开始和结束的时间戳
*
* @return array
*/
public static function lastWeek()
{
$timestamp = time();
return [
strtotime(date('Y-m-d', strtotime("last week Monday", $timestamp))),
strtotime(date('Y-m-d', strtotime("last week Sunday", $timestamp))) + 24 * 3600 - 1
];
}
/**
* 返回本月开始和结束的时间戳
*
* @return array
*/
public static function month($everyDay = false)
{
list($y, $m, $t) = explode('-', date('Y-m-t'));
return [
mktime(0, 0, 0, $m, 1, $y),
mktime(23, 59, 59, $m, $t, $y)
];
}
/**
* 返回上个月开始和结束的时间戳
*
* @return array
*/
public static function lastMonth()
{
$y = date('Y');
$m = date('m');
$begin = mktime(0, 0, 0, $m - 1, 1, $y);
$end = mktime(23, 59, 59, $m - 1, date('t', $begin), $y);
return [$begin, $end];
}
/**
* 返回今年开始和结束的时间戳
*
* @return array
*/
public static function year()
{
$y = date('Y');
return [
mktime(0, 0, 0, 1, 1, $y),
mktime(23, 59, 59, 12, 31, $y)
];
}
/**
* 返回去年开始和结束的时间戳
*
* @return array
*/
public static function lastYear()
{
$year = date('Y') - 1;
return [
mktime(0, 0, 0, 1, 1, $year),
mktime(23, 59, 59, 12, 31, $year)
];
}
public static function dayOf()
{
}
/**
* 获取几天前零点到现在/昨日结束的时间戳
*
* @param int $day 天数
* @param bool $now 返回现在或者昨天结束时间戳
* @return array
*/
public static function dayToNow($day = 1, $now = true)
{
$end = time();
if (!$now) {
list($foo, $end) = self::yesterday();
}
return [
mktime(0, 0, 0, date('m'), date('d') - $day, date('Y')),
$end
];
}
/**
* 返回几天前的时间戳
*
* @param int $day
* @return int
*/
public static function daysAgo($day = 1)
{
$nowTime = time();
return $nowTime - self::daysToSecond($day);
}
/**
* 返回几天后的时间戳
*
* @param int $day
* @return int
*/
public static function daysAfter($day = 1)
{
$nowTime = time();
return $nowTime + self::daysToSecond($day);
}
/**
* 天数转换成秒数
*
* @param int $day
* @return int
*/
public static function daysToSecond($day = 1)
{
return $day * 86400;
}
/**
* 周数转换成秒数
*
* @param int $week
* @return int
*/
public static function weekToSecond($week = 1)
{
return self::daysToSecond() * 7 * $week;
}
private static function startTimeToEndTime()
{
}
}