Files
kiri-core/Database/Collection.php
T

243 lines
4.0 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 13:38
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-12-17 14:09:14 +08:00
2020-08-31 12:38:32 +08:00
namespace Database;
use Database\Base\AbstractCollection;
use Exception;
2021-02-22 17:44:24 +08:00
use JetBrains\PhpStorm\Pure;
2020-08-31 12:38:32 +08:00
/**
* Class Collection
* @package Database
* @property-read $length
*/
class Collection extends AbstractCollection
{
/**
* @return array
*/
2020-12-15 14:04:02 +08:00
public function getItems(): array
2020-08-31 12:38:32 +08:00
{
2020-09-08 10:28:57 +08:00
// TODO: Change the autogenerated stub
return $this->_item;
2020-08-31 12:38:32 +08:00
}
/**
* @param $field
*
* @return array|null
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function values($field): ?array
2020-08-31 12:38:32 +08:00
{
if (empty($field) || !is_string($field)) {
return NULL;
}
$_tmp = [];
$data = $this->toArray();
foreach ($data as $val) {
/** @var ActiveRecord $val */
$_tmp[] = $val[$field];
}
return $_tmp;
}
/**
* @param string $field
* @return array|null
*/
2020-12-17 14:09:14 +08:00
public function keyBy(string $field): ?array
2020-08-31 12:38:32 +08:00
{
$array = $this->toArray();
$column = array_flip(array_column($array, $field));
foreach ($column as $key => $value) {
$column[$key] = $array[$value];
}
return $column;
}
/**
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function orderRand(): static
2020-08-31 12:38:32 +08:00
{
shuffle($this->_item);
return $this;
}
/**
* @param int $start
* @param int $length
*
* @return array
*/
2021-02-22 17:44:24 +08:00
#[Pure] public function slice($start = 0, $length = 20): array
2020-08-31 12:38:32 +08:00
{
if (empty($this->_item) || !is_array($this->_item)) {
return [];
}
if (count($this->_item) < $length) {
return $this->_item;
} else {
return array_slice($this->_item, $start, $length);
}
}
/**
* @param $field
* @param string $setKey
*
* @return array|null
*/
2020-12-17 14:09:14 +08:00
public function column($field, $setKey = ''): ?array
2020-08-31 12:38:32 +08:00
{
$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 $field
*
* @return float|int|null
*/
2020-12-17 14:09:14 +08:00
public function sum($field): float|int|null
2020-08-31 12:38:32 +08:00
{
$array = $this->column($field);
if (empty($array)) {
return NULL;
}
return array_sum($array);
}
/**
2020-12-17 14:09:14 +08:00
* @return ActiveRecord|array
2020-08-31 12:38:32 +08:00
*/
2021-02-22 17:44:24 +08:00
#[Pure] public function current(): ActiveRecord|array
2020-08-31 12:38:32 +08:00
{
return current($this->_item);
}
/**
* @return int
*/
2021-02-22 17:44:24 +08:00
#[Pure] public function size(): int
2020-08-31 12:38:32 +08:00
{
return (int)count($this->_item);
}
/**
* @return array
* @throws
*/
2020-12-17 14:09:14 +08:00
public function toArray(): array
2020-08-31 12:38:32 +08:00
{
$array = [];
2020-09-08 00:54:23 +08:00
foreach ($this as $value) {
2020-08-31 12:38:32 +08:00
if (!is_object($value)) {
continue;
}
$array[] = $value->toArray();
}
$this->_item = [];
return $array;
}
/**
* @throws Exception
* 批量删除
*/
2020-12-17 14:09:14 +08:00
public function delete(): bool
2020-08-31 12:38:32 +08:00
{
2021-02-22 17:44:24 +08:00
$model = $this->getModel();
2021-02-25 16:43:40 +08:00
if (!$model->hasPrimary()) return false;
2020-08-31 12:38:32 +08:00
$ids = [];
2020-09-08 00:54:23 +08:00
foreach ($this as $item) {
2021-02-25 16:43:40 +08:00
$id = $item->getPrimaryValue();
if (!empty($id)) {
$ids[] = $id;
}
2020-08-31 12:38:32 +08:00
}
2021-02-25 16:43:40 +08:00
return $model::find()->in($model->getPrimary(), $ids)->delete();
2020-08-31 12:38:32 +08:00
}
/**
* @param array $condition
2020-09-08 01:09:24 +08:00
* @return Collection
2020-08-31 12:38:32 +08:00
* @throws
*/
2020-12-17 14:09:14 +08:00
public function filter(array $condition): Collection|static
2020-08-31 12:38:32 +08:00
{
2020-09-08 01:27:36 +08:00
$_filters = [];
2020-08-31 12:38:32 +08:00
if (empty($condition)) {
2020-09-08 01:09:24 +08:00
return $this;
2020-08-31 12:38:32 +08:00
}
2020-09-08 00:54:23 +08:00
foreach ($this as $value) {
2020-09-08 01:27:36 +08:00
if (!$this->filterCheck($value, $condition)) {
2020-08-31 12:38:32 +08:00
continue;
}
$_filters[] = $value;
}
2021-02-24 14:22:56 +08:00
return new Collection($this->query, $_filters, $this->model);
2020-08-31 12:38:32 +08:00
}
2020-09-08 01:27:36 +08:00
/**
* @param $value
* @param $condition
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
private function filterCheck($value, $condition): bool
2020-09-08 01:27:36 +08:00
{
$_value = $value;
if ($_value instanceof ActiveRecord) {
$_value = $_value->toArray();
}
$_tmp = array_intersect_key($_value, $condition);
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
return false;
}
return true;
}
2020-08-31 12:38:32 +08:00
/**
* @param $key
* @param $value
2020-12-17 14:09:14 +08:00
* @return mixed
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
public function exists($key, $value): mixed
2020-08-31 12:38:32 +08:00
{
2020-09-08 00:54:23 +08:00
foreach ($this as $item) {
2020-08-31 12:38:32 +08:00
if ($item->$key === $value) {
return $item;
}
}
return null;
}
/**
* @return bool
*/
2021-02-25 16:43:40 +08:00
#[Pure] public function isEmpty(): bool
2020-08-31 12:38:32 +08:00
{
return $this->size() < 1;
}
}