This commit is contained in:
2020-12-17 14:58:54 +08:00
parent 3e9d5a5aff
commit bdd26f2b42
2 changed files with 49 additions and 50 deletions
+2 -2
View File
@@ -182,10 +182,10 @@ class ActiveRecord extends BaseActiveRecord
/** /**
* @param array $attributes * @param array $attributes
* @return bool * @return static
* @throws * @throws
*/ */
public function update(array $attributes): bool public function update(array $attributes): static
{ {
return $this->save($attributes); return $this->save($attributes);
} }
+47 -48
View File
@@ -100,7 +100,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @return array * @return array
*/ */
public function getActions() public function getActions(): array
{ {
return $this->actions; return $this->actions;
} }
@@ -108,7 +108,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @return bool * @return bool
*/ */
public function getIsCreate() public function getIsCreate(): bool
{ {
return $this->isNewExample === TRUE; return $this->isNewExample === TRUE;
} }
@@ -117,7 +117,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @param bool $bool * @param bool $bool
* @return $this * @return $this
*/ */
public function setIsCreate($bool = FALSE) public function setIsCreate($bool = FALSE): static
{ {
$this->isNewExample = $bool; $this->isNewExample = $bool;
return $this; return $this;
@@ -129,7 +129,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* get last exception or other error * get last exception or other error
* @throws ComponentException * @throws ComponentException
*/ */
public function getLastError() public function getLastError(): mixed
{ {
return Snowflake::app()->getLogger()->getLastError('mysql'); return Snowflake::app()->getLogger()->getLastError('mysql');
} }
@@ -138,7 +138,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function hasPrimary() public function hasPrimary(): bool
{ {
if ($this->primary !== NULL) { if ($this->primary !== NULL) {
return true; return true;
@@ -153,7 +153,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @throws Exception * @throws Exception
*/ */
public function hasAutoIncrement() public function hasAutoIncrement(): bool
{ {
$autoIncrement = $this->getAutoIncrement(); $autoIncrement = $this->getAutoIncrement();
return $autoIncrement !== null; return $autoIncrement !== null;
@@ -162,7 +162,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @throws Exception * @throws Exception
*/ */
public function getAutoIncrement() public function getAutoIncrement(): int|string|null
{ {
return static::getColumns()->getAutoIncrement(); return static::getColumns()->getAutoIncrement();
} }
@@ -218,7 +218,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @throws Exception * @throws Exception
* @throws Exception * @throws Exception
*/ */
public static function max($field = null) public static function max($field = null): ?ActiveRecord
{ {
$columns = static::getColumns(); $columns = static::getColumns();
if (empty($field)) { if (empty($field)) {
@@ -240,7 +240,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
public static function find():ActiveQuery public static function find(): ActiveQuery
{ {
return Snowflake::createObject(ActiveQuery::class, [get_called_class()]); return Snowflake::createObject(ActiveQuery::class, [get_called_class()]);
} }
@@ -272,7 +272,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @return array * @return array
*/ */
public function getAttributes() public function getAttributes(): array
{ {
return $this->_attributes; return $this->_attributes;
} }
@@ -280,7 +280,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @return array * @return array
*/ */
public function getOldAttributes() public function getOldAttributes(): array
{ {
return $this->_oldAttributes; return $this->_oldAttributes;
} }
@@ -290,7 +290,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @param $value * @param $value
* @return mixed * @return mixed
*/ */
public function setAttribute($name, $value) public function setAttribute($name, $value): mixed
{ {
return $this->_attributes[$name] = $value; return $this->_attributes[$name] = $value;
} }
@@ -300,7 +300,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @param $value * @param $value
* @return mixed * @return mixed
*/ */
public function setOldAttribute($name, $value) public function setOldAttribute($name, $value): mixed
{ {
return $this->_oldAttributes[$name] = $value; return $this->_oldAttributes[$name] = $value;
} }
@@ -310,7 +310,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function setAttributes(array $param) public function setAttributes(array $param): static
{ {
if (empty($param)) { if (empty($param)) {
return $this; return $this;
@@ -329,7 +329,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @param $param * @param $param
* @return $this * @return $this
*/ */
public function setOldAttributes($param) public function setOldAttributes($param): static
{ {
if (empty($param) || !is_array($param)) { if (empty($param) || !is_array($param)) {
return $this; return $this;
@@ -355,7 +355,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
private function insert($param, $attributes) private function insert($param, $attributes): bool|static
{ {
if (empty($param)) { if (empty($param)) {
return FALSE; return FALSE;
@@ -397,7 +397,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return $this|bool * @return $this|bool
* @throws Exception * @throws Exception
*/ */
private function update($attributes, $condition, $param) private function update($attributes, $condition, $param): bool|static
{ {
if (empty($param)) { if (empty($param)) {
return true; return true;
@@ -420,10 +420,10 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @param null $data * @param null $data
* @return bool|mixed|ActiveRecord * @return bool|BaseActiveRecord
* @throws Exception * @throws Exception
*/ */
public function save($data = NULL) public function save($data = NULL): bool|static
{ {
if (is_array($data)) { if (is_array($data)) {
$this->setAttributes($data); $this->setAttributes($data);
@@ -453,7 +453,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function validator(array $rule) public function validator(array $rule): bool
{ {
if (empty($rule)) return true; if (empty($rule)) return true;
$validate = $this->resolve($rule); $validate = $this->resolve($rule);
@@ -469,7 +469,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return Validator * @return Validator
* @throws Exception * @throws Exception
*/ */
private function resolve($rule) private function resolve($rule): Validator
{ {
$validate = Validator::getInstance(); $validate = Validator::getInstance();
$validate->setParams($this->_attributes); $validate->setParams($this->_attributes);
@@ -503,7 +503,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
private function filtration_and_separation() private function filtration_and_separation(): array
{ {
$_tmp = []; $_tmp = [];
$condition = []; $condition = [];
@@ -546,7 +546,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @return array * @return array
*/ */
public function getRelates() public function getRelates(): array
{ {
return $this->_relate; return $this->_relate;
} }
@@ -563,9 +563,9 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @param $name * @param $name
* @return mixed|null * @return mixed
*/ */
public function getRelate($name) public function getRelate($name): mixed
{ {
if (!isset($this->_relate[$name])) { if (!isset($this->_relate[$name])) {
return NULL; return NULL;
@@ -579,7 +579,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function has($attribute) public function has($attribute): bool
{ {
$format = static::getColumns()->format(); $format = static::getColumns()->format();
@@ -590,13 +590,13 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public static function getTable() public static function getTable(): string
{ {
$tablePrefix = static::getDb()->tablePrefix; $tablePrefix = static::getDb()->tablePrefix;
$table = static::tableName(); $table = static::tableName();
if (strpos($table, $tablePrefix) === 0) { if (str_starts_with($table, $tablePrefix)) {
return $table; return $table;
} }
@@ -619,16 +619,15 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function afterSave($attributes, $changeAttributes) public function afterSave($attributes, $changeAttributes): void
{ {
return true;
} }
/** /**
* @return Connection * @return Connection
* @throws Exception * @throws Exception
*/ */
public static function getDb() public static function getDb(): Connection
{ {
return static::setDatabaseConnect('db'); return static::setDatabaseConnect('db');
} }
@@ -636,7 +635,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @return static * @return static
*/ */
public function refresh() public function refresh(): static
{ {
$this->_oldAttributes = $this->_attributes; $this->_oldAttributes = $this->_attributes;
return $this; return $this;
@@ -691,11 +690,11 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @param $name * @param $name
* @return mixed|null * @return bool
*/ */
public function __isset($name) public function __isset($name): bool
{ {
return $this->_attributes[$name] ?? null; return isset($this->_attributes[$name]);
} }
/** /**
@@ -703,7 +702,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return array|null|ActiveRecord * @return array|null|ActiveRecord
* @throws Exception * @throws Exception
*/ */
private function resolveClass($call) private function resolveClass($call): array|ActiveRecord|null
{ {
if ($call instanceof HasOne) { if ($call instanceof HasOne) {
return $call->get(); return $call->get();
@@ -720,17 +719,17 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function offsetExists($offset) public function offsetExists(mixed $offset): bool
{ {
return $this->has($offset); return $this->has($offset);
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @return mixed|null * @return mixed
* @throws Exception * @throws Exception
*/ */
public function offsetGet($offset) public function offsetGet(mixed $offset): mixed
{ {
return $this->__get($offset); return $this->__get($offset);
} }
@@ -740,16 +739,16 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @param mixed $value * @param mixed $value
* @throws Exception * @throws Exception
*/ */
public function offsetSet($offset, $value) public function offsetSet(mixed $offset, mixed $value)
{ {
return $this->__set($offset, $value); $this->__set($offset, $value);
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @throws Exception * @throws Exception
*/ */
public function offsetUnset($offset) public function offsetUnset(mixed $offset)
{ {
if (!$this->has($offset)) { if (!$this->has($offset)) {
return; return;
@@ -764,7 +763,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @return array * @return array
*/ */
public function unset() public function unset(): array
{ {
$fields = func_get_args(); $fields = func_get_args();
$fields = array_shift($fields); $fields = array_shift($fields);
@@ -779,20 +778,20 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
/** /**
* @param $bsName * @param $dbName
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public static function setDatabaseConnect($bsName): Connection public static function setDatabaseConnect($dbName): Connection
{ {
return Snowflake::app()->db->get($bsName); return Snowflake::app()->db->get($dbName);
} }
/** /**
* @return Columns * @return Columns
* @throws Exception * @throws Exception
*/ */
public static function getColumns() public static function getColumns(): Columns
{ {
return static::getDb()->getSchema() return static::getDb()->getSchema()
->getColumns() ->getColumns()
@@ -804,7 +803,7 @@ abstract class BaseActiveRecord extends Component implements IOrm, \ArrayAccess
* @return static * @return static
* @throws * @throws
*/ */
public static function populate(array $data) public static function populate(array $data): static
{ {
$model = new static(); $model = new static();
$model->setAttributes($data); $model->setAttributes($data);