Files
kiri-databases/Collection.php
T

235 lines
5.2 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:38
*/
declare(strict_types=1);
namespace Database;
use Database\Base\AbstractCollection;
use Exception;
use JetBrains\PhpStorm\Pure;
/**
* Class Collection
* @package Database
* @property-read $length
*/
class Collection extends AbstractCollection
{
2023-12-12 15:35:35 +08:00
/**
2023-12-13 14:47:23 +08:00
* @param string $field
2023-12-12 15:35:35 +08:00
*
* @return array|null
*/
2023-12-13 14:47:23 +08:00
public function values(string $field): ?array
2023-12-12 15:35:35 +08:00
{
2023-12-13 14:47:23 +08:00
return array_values($this->column($field));
2023-12-12 15:35:35 +08:00
}
/**
* @param array $attributes
* @return bool
* @throws
*/
public function update(array $attributes): bool
{
2023-12-13 14:47:23 +08:00
if ($this->isEmpty()) {
2025-12-31 00:19:29 +08:00
return $this->getLogger()->logCategory('No data by update', 'mysql');
2023-07-31 23:08:59 +08:00
}
2023-12-13 14:47:23 +08:00
return $this->batch()->update($attributes);
2023-12-12 15:35:35 +08:00
}
/**
* @param string $field
* @return array|null
*/
public function keyBy(string $field): ?array
{
$array = $this->toArray();
$column = array_flip(array_column($array, $field));
foreach ($column as $key => $value) {
$column[$key] = $array[$value];
}
return $column;
}
/**
* @param int $start
* @param int $length
*
* @return array
*/
#[Pure] public function slice(int $start = 0, int $length = 20): array
{
2023-12-13 14:47:23 +08:00
if (empty($this->getItems())) {
2023-12-12 15:35:35 +08:00
return [];
}
2023-12-13 14:47:23 +08:00
if (\count($this->getItems()) < $length) {
return $this->getItems();
2023-12-12 15:35:35 +08:00
} else {
2023-12-13 14:47:23 +08:00
return array_slice($this->getItems(), $start, $length);
2023-12-12 15:35:35 +08:00
}
}
/**
* @param string $field
* @param string $setKey
*
* @return array|null
*/
public function column(string $field, string $setKey = ''): ?array
{
$data = $this->toArray();
if (empty($data)) {
return [];
}
if (!empty($setKey) && is_string($setKey)) {
return array_column($data, $field, $setKey);
} else {
return array_column($data, $field);
}
}
/**
* @param string $field
*
* @return float|int|null
*/
public function sum(string $field): float|int|null
{
$array = $this->column($field);
if (empty($array)) {
return NULL;
}
return array_sum($array);
}
/**
* @return ModelInterface|array
*/
#[Pure] public function current(): ModelInterface|array
{
2023-12-13 14:47:23 +08:00
return current($this->getItems());
2023-12-12 15:35:35 +08:00
}
/**
* @return int
*/
#[Pure] public function size(): int
{
2023-12-13 14:47:23 +08:00
return count($this->getItems());
2023-12-12 15:35:35 +08:00
}
/**
* @return array
* @throws
*/
public function toArray(): array
{
$array = [];
/** @var Model $value */
foreach ($this as $value) {
$array[] = $value->toArray();
}
return $array;
}
/**
* @throws
* 批量删除
*/
public function delete(): bool
{
$model = $this->getModel();
2023-12-13 14:47:23 +08:00
if ($this->isEmpty()) {
2025-12-31 00:19:29 +08:00
return $this->getLogger()->logCategory('No data by delete', 'mysql');
2023-12-13 14:47:23 +08:00
}
if (!$model->hasPrimary()) {
throw new Exception('Must set primary key. if you want to delete data');
2023-12-12 15:35:35 +08:00
}
2023-12-13 14:47:23 +08:00
return $this->batch()->delete();
}
/**
* @return ActiveQuery
* @throws Exception
*/
private function batch(): ActiveQuery
{
return $this->makeNewQuery()->whereIn($this->getModel()->getPrimary(),
$this->column($this->getModel()->getPrimary()));
2023-12-12 15:35:35 +08:00
}
/**
* @param array $condition
* @return Collection
* @throws
*/
public function filter(array $condition): Collection|static
{
$_filters = [];
if (empty($condition)) {
return $this;
}
foreach ($this as $value) {
if (!$this->filterCheck($value, $condition)) {
continue;
}
$_filters[] = $value;
}
2023-12-13 14:47:23 +08:00
return new Collection($this->query, $this->model, $_filters);
2023-12-12 15:35:35 +08:00
}
/**
2023-12-18 18:11:58 +08:00
* @param array|ModelInterface $value
* @param array $condition
2023-12-12 15:35:35 +08:00
* @return bool
*/
2023-12-18 18:11:58 +08:00
private function filterCheck(array|ModelInterface $value, array $condition): bool
2023-12-12 15:35:35 +08:00
{
$_value = $value;
if ($_value instanceof ModelInterface) {
$_value = $_value->toArray();
}
$_tmp = array_intersect_key($_value, $condition);
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
return false;
}
return true;
}
/**
2023-12-18 18:11:58 +08:00
* @param string $key
* @param mixed $value
2023-12-12 15:35:35 +08:00
* @return mixed
*/
2023-12-18 18:11:58 +08:00
public function exists(string $key, mixed $value): mixed
2023-12-12 15:35:35 +08:00
{
foreach ($this as $item) {
if ($item->$key === $value) {
return $item;
}
}
return null;
}
/**
* @return bool
*/
#[Pure] public function isEmpty(): bool
{
return $this->size() < 1;
}
2022-01-09 03:49:51 +08:00
}