This commit is contained in:
2023-12-12 15:35:35 +08:00
parent 510a13e3da
commit e2c0f62532
32 changed files with 922 additions and 1095 deletions
+124 -122
View File
@@ -26,153 +26,155 @@ use Traversable;
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, \Arrayable
{
/**
* @var ModelInterface[]
*/
protected array $_item = [];
/**
* @var ModelInterface[]
*/
protected array $_item = [];
/**
* @var ModelInterface|string|null
*/
protected ModelInterface|string|null $model;
/**
* @var ModelInterface|string|null
*/
protected ModelInterface|string|null $model;
/**
* @var ActiveQuery
*/
protected ActiveQuery $query;
/**
* @var ActiveQuery
*/
protected ActiveQuery $query;
public function clean()
{
unset($this->query, $this->model, $this->_item);
}
public function clean(): void
{
unset($this->query, $this->model, $this->_item);
}
/**
* Collection constructor.
*
* @param $query
* @param array $array
* @param ModelInterface|null $model
* @throws Exception
*/
public function __construct($query, array $array = [], ModelInterface $model = null)
{
$this->_item = $array;
$this->query = $query;
$this->model = $model;
/**
* Collection constructor.
*
* @param $query
* @param array $array
* @param ModelInterface|null $model
* @throws
*/
public function __construct($query, array $array = [], ModelInterface $model = null)
{
$this->_item = $array;
$this->query = $query;
$this->model = $model;
parent::__construct();
}
parent::__construct();
}
/**
* @return int
*/
#[Pure] public function getLength(): int
{
return count($this->_item);
}
/**
* @return int
*/
#[Pure] public function getLength(): int
{
return count($this->_item);
}
/**
* @param $item
*/
public function setItems($item)
{
$this->_item = $item;
}
/**
* @param $item
*/
public function setItems($item): void
{
$this->_item = $item;
}
/**
* @param $model
*/
public function setModel($model)
{
$this->model = $model;
}
/**
* @param $model
*/
public function setModel($model): void
{
$this->model = $model;
}
/**
* @param $item
*/
public function addItem($item)
{
$this->_item[] = $item;
}
/**
* @param $item
*/
public function addItem($item): void
{
$this->_item[] = $item;
}
/**
* @return Traversable|CollectionIterator|ArrayIterator
* @throws Exception
*/
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{
return new CollectionIterator($this->model, $this->_item);
}
/**
* @return Traversable|CollectionIterator|ArrayIterator
* @throws
*/
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{
return new CollectionIterator($this->model, $this->_item);
}
/**
* @return mixed
* @throws Exception
*/
public function getModel(): ModelInterface
{
return $this->model;
}
/**
* @return mixed
* @throws
*/
public function getModel(): ModelInterface
{
return $this->model;
}
/**
* @return ActiveQuery
*/
public function makeNewQuery(): ActiveQuery
{
return $this->model::query();
}
/**
* @return ActiveQuery
*/
public function makeNewQuery(): ActiveQuery
{
return $this->model::query();
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return !empty($this->_item) && isset($this->_item[$offset]);
}
/**
* @param mixed $offset
* @return bool
*/
public function offsetExists(mixed $offset): bool
{
return !empty($this->_item) && isset($this->_item[$offset]);
}
/**
* @param mixed $offset
* @return ModelInterface|null
* @throws Exception
*/
public function offsetGet(mixed $offset): ?ModelInterface
{
if (!$this->offsetExists($offset)) {
return NULL;
}
if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]);
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @return ModelInterface|null
* @throws
*/
public function offsetGet(mixed $offset): ?ModelInterface
{
if (!$this->offsetExists($offset)) {
return NULL;
}
if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]);
}
return $this->_item[$offset];
}
/**
* @param mixed $offset
* @param mixed $value
*/
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
* @param mixed $value
*/
#[ReturnTypeWillChange]
public function offsetSet(mixed $offset, mixed $value): void
{
$this->_item[$offset] = $value;
}
/**
* @param mixed $offset
*/
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
/**
* @param mixed $offset
*/
#[ReturnTypeWillChange]
public function offsetUnset(mixed $offset): void
{
if ($this->offsetExists($offset)) {
unset($this->_item[$offset]);
}
}
}
+32 -33
View File
@@ -16,45 +16,44 @@ use Exception;
class CollectionIterator extends \ArrayIterator
{
private ModelInterface|string $model;
private ModelInterface|string $model;
/**
* CollectionIterator constructor.
* @param $model
* @param array $array
* @param int $flags
* @throws Exception
*/
public function __construct($model, array $array = [], int $flags = 0)
{
$this->model = $model;
parent::__construct($array, $flags);
}
/**
* CollectionIterator constructor.
* @param $model
* @param array $array
* @param int $flags
* @throws
*/
public function __construct($model, array $array = [], int $flags = 0)
{
$this->model = $model;
parent::__construct($array, $flags);
}
/**
* @param $current
* @return ModelInterface
* @throws Exception
*/
protected function newModel($current): ModelInterface
{
return $this->model->populates($current);
}
/**
* @param $current
* @return ModelInterface
* @throws
*/
protected function newModel($current): ModelInterface
{
return $this->model->populates($current);
}
/**
* @throws Exception
*/
public function current(): ModelInterface
{
if (is_array($current = parent::current())) {
$current = $this->newModel($current);
}
return $current;
}
/**
* @throws
*/
public function current(): ModelInterface
{
if (is_array($current = parent::current())) {
$current = $this->newModel($current);
}
return $current;
}
}
+36 -36
View File
@@ -173,7 +173,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return string
* @throws Exception
* @throws
* get last exception or other error
*/
public function getLastError(): string
@@ -184,7 +184,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return bool
* @throws Exception
* @throws
*/
public function hasPrimary(): bool
{
@@ -193,7 +193,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return null|string
* @throws Exception
* @throws
*/
public function getPrimary(): ?string
{
@@ -206,7 +206,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return bool
* @throws Exception
* @throws
*/
public function hasPrimaryValue(): bool
{
@@ -219,7 +219,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return int|null
* @throws Exception
* @throws
*/
public function getPrimaryValue(): ?int
{
@@ -233,7 +233,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param int|string|array $param
* @param null $db
* @return Model|null
* @throws Exception
* @throws
*/
public static function findOne(int|string|array $param, $db = NULL): ?static
{
@@ -254,7 +254,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param int $param
* @param null $db
* @return Model|null
* @throws Exception
* @throws
*/
public static function primary(int $param, $db = NULL): ?static
{
@@ -267,7 +267,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return mixed
* @throws Exception
* @throws
*/
public function optimize(): mixed
{
@@ -287,7 +287,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param int|string|array $condition
* @return static|null
* @throws Exception
* @throws
*/
public static function first(int|string|array $condition): ?static
{
@@ -298,7 +298,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string|array $condition
* @return Collection
* @throws Exception
* @throws
*/
public static function all(string|array $condition): Collection
{
@@ -315,7 +315,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return ActiveQuery
* @throws Exception
* @throws
*/
public static function query(): ActiveQuery
{
@@ -327,7 +327,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return Connection
* @throws Exception
* @throws
*/
public function getConnection(): Connection
{
@@ -340,7 +340,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param array $attributes
*
* @return bool
* @throws Exception
* @throws
*/
protected static function deleteByCondition(array|string|null $condition = NULL, array $attributes = []): bool
{
@@ -356,7 +356,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return array
* @throws Exception
* @throws
*/
public function getAttributes(): array
{
@@ -402,7 +402,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $param
* @return $this
* @throws Exception
* @throws
*/
public function setAttributes(array $param): static
{
@@ -428,7 +428,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return $this|bool
* @throws Exception
* @throws
*/
private function insert(): bool|static
{
@@ -449,7 +449,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param array $condition
* @param array $change
* @return $this|bool
* @throws Exception
* @throws
*/
protected function updateInternal(array $old, array $condition, array $change): bool|static
{
@@ -469,7 +469,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return bool|$this
* @throws Exception
* @throws
*/
public function save(): static|bool
{
@@ -488,7 +488,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return array<array, array>
* @throws Exception
* @throws
*/
private function diff(): array
{
@@ -518,7 +518,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param array $rule
* @return bool
* @throws Exception
* @throws
*/
public function validator(array $rule): bool
{
@@ -536,7 +536,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param $rule
* @return Validator
* @throws Exception
* @throws
*/
private function resolve($rule): Validator
{
@@ -552,7 +552,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param string $name
* @return null
* @throws Exception
* @throws
*/
public function getAttribute(string $name)
{
@@ -562,7 +562,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return Relation|null
* @throws ReflectionException
* @throws
*/
public function getRelation(): ?Relation
{
@@ -573,7 +573,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param $attribute
* @return bool
* @throws Exception
* @throws
*/
public function has($attribute): bool
{
@@ -582,7 +582,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**ƒ
* @return string
* @throws Exception
* @throws
*/
public function getTable(): string
{
@@ -613,7 +613,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param self $model
* @return bool
* @throws Exception
* @throws
*/
public function beforeSave(self $model): bool
{
@@ -633,7 +633,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param $name
* @param $value
* @throws Exception
* @throws
*/
public function __set($name, $value): void
{
@@ -652,7 +652,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param $name
* @return mixed
* @throws Exception
* @throws
*/
public function __get($name): mixed
{
@@ -669,7 +669,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param $name
* @param null $value
* @return mixed
* @throws Exception
* @throws
*/
protected function withPropertyOverride($name, $value = null): mixed
{
@@ -720,7 +720,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param mixed $offset
* @return bool
* @throws Exception
* @throws
*/
public function offsetExists(mixed $offset): bool
{
@@ -730,7 +730,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param mixed $offset
* @return mixed
* @throws Exception
* @throws
*/
public function offsetGet(mixed $offset): mixed
{
@@ -740,18 +740,18 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @param mixed $offset
* @param mixed $value
* @throws Exception
* @throws
*/
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value): void
{
$this->__set($offset, $value);
}
/**
* @param mixed $offset
* @throws Exception
* @throws
*/
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset): void
{
if (!isset($this->_attributes[$offset]) && !isset($this->_oldAttributes[$offset])) {
return;
@@ -772,7 +772,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**
* @return Columns
* @throws Exception
* @throws
*/
public function getColumns(): Columns
{