Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply fixes from StyleCI #517

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 12 additions & 12 deletions src/Model.php
Expand Up @@ -492,9 +492,9 @@ public function replace(bool $replace = true)
public function refresh(bool $relation = false)
{
if ($this->exists) {
$this->data = $this->db()->find($this->getKey())->getData();
$this->origin = $this->data;
$this->get = [];
$this->data = $this->db()->find($this->getKey())->getData();
$this->origin = $this->data;
$this->get = [];

if ($relation) {
$this->relation = [];
Expand Down Expand Up @@ -571,8 +571,8 @@ public function save(array|object $data = [], string $sequence = null): bool
$this->trigger('AfterWrite');

// 重新记录原始数据
$this->origin = $this->data;
$this->get = [];
$this->origin = $this->data;
$this->get = [];

return true;
}
Expand Down Expand Up @@ -663,9 +663,9 @@ protected function updateData(): bool
$db = $this->db();

$db->transaction(function () use ($data, $allowFields, $db) {
$this->key = null;
$where = $this->getWhere();
$result = $db->where($where)
$this->key = null;
$where = $this->getWhere();
$result = $db->where($where)
->strict(false)
->cache(true)
->setOption('key', $this->key)
Expand Down Expand Up @@ -705,7 +705,7 @@ protected function insertData(string $sequence = null): bool
// 时间戳自动写入
if ($this->autoWriteTimestamp) {
if ($this->createTime && !array_key_exists($this->createTime, $data)) {
$data[$this->createTime] = $this->autoWriteTimestamp();
$data[$this->createTime] = $this->autoWriteTimestamp();
$this->data[$this->createTime] = $data[$this->createTime];
}

Expand Down Expand Up @@ -835,8 +835,8 @@ public function delete(): bool
}

// 读取更新条件
$where = $this->getWhere();
$db = $this->db();
$where = $this->getWhere();
$db = $this->db();

$db->transaction(function () use ($where, $db) {
// 删除当前模型数据
Expand All @@ -850,7 +850,7 @@ public function delete(): bool

$this->trigger('AfterDelete');

$this->exists = false;
$this->exists = false;

return true;
}
Expand Down
32 changes: 16 additions & 16 deletions src/Paginator.php
Expand Up @@ -86,8 +86,8 @@ abstract class Paginator implements ArrayAccess, Countable, IteratorAggregate, J
*/
protected $options = [
'var_page' => 'page',
'path' => '/',
'query' => [],
'path' => '/',
'query' => [],
'fragment' => '',
];

Expand Down Expand Up @@ -116,22 +116,22 @@ public function __construct($items, int $listRows, int $currentPage = 1, int $to

$this->options['path'] = '/' != $this->options['path'] ? rtrim($this->options['path'], '/') : $this->options['path'];

$this->simple = $simple;
$this->simple = $simple;
$this->listRows = $listRows;

if (!$items instanceof Collection) {
$items = Collection::make($items);
}

if ($simple) {
$this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = count($items) > ($this->listRows);
$items = $items->slice(0, $this->listRows);
$this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = count($items) > ($this->listRows);
$items = $items->slice(0, $this->listRows);
} else {
$this->total = $total;
$this->lastPage = (int) ceil($total / $listRows);
$this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = $this->currentPage < $this->lastPage;
$this->total = $total;
$this->lastPage = (int) ceil($total / $listRows);
$this->currentPage = $this->setCurrentPage($currentPage);
$this->hasMore = $this->currentPage < $this->lastPage;
}
$this->items = $items;
}
Expand Down Expand Up @@ -184,10 +184,10 @@ protected function url(int $page): string

if (!str_contains($this->options['path'], '[PAGE]')) {
$parameters = [$this->options['var_page'] => $page];
$path = $this->options['path'];
$path = $this->options['path'];
} else {
$parameters = [];
$path = str_replace('[PAGE]', (string) $page, $this->options['path']);
$path = str_replace('[PAGE]', (string) $page, $this->options['path']);
}

if (count($this->options['query']) > 0) {
Expand Down Expand Up @@ -526,11 +526,11 @@ public function toArray(): array
}

return [
'total' => $total,
'per_page' => $this->listRows(),
'total' => $total,
'per_page' => $this->listRows(),
'current_page' => $this->currentPage(),
'last_page' => $this->lastPage,
'data' => $this->items->toArray(),
'last_page' => $this->lastPage,
'data' => $this->items->toArray(),
];
}

Expand Down
18 changes: 9 additions & 9 deletions src/db/BaseBuilder.php
Expand Up @@ -42,16 +42,16 @@ abstract class BaseBuilder
* @var array
*/
protected $parser = [
'parseCompare' => ['=', '<>', '>', '>=', '<', '<='],
'parseLike' => ['LIKE', 'NOT LIKE'],
'parseBetween' => ['NOT BETWEEN', 'BETWEEN'],
'parseIn' => ['NOT IN', 'IN'],
'parseExp' => ['EXP'],
'parseNull' => ['NOT NULL', 'NULL'],
'parseCompare' => ['=', '<>', '>', '>=', '<', '<='],
'parseLike' => ['LIKE', 'NOT LIKE'],
'parseBetween' => ['NOT BETWEEN', 'BETWEEN'],
'parseIn' => ['NOT IN', 'IN'],
'parseExp' => ['EXP'],
'parseNull' => ['NOT NULL', 'NULL'],
'parseBetweenTime' => ['BETWEEN TIME', 'NOT BETWEEN TIME'],
'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'],
'parseExists' => ['NOT EXISTS', 'EXISTS'],
'parseColumn' => ['COLUMN'],
'parseTime' => ['< TIME', '> TIME', '<= TIME', '>= TIME'],
'parseExists' => ['NOT EXISTS', 'EXISTS'],
'parseColumn' => ['COLUMN'],
];

/**
Expand Down
82 changes: 41 additions & 41 deletions src/db/BaseQuery.php
Expand Up @@ -84,8 +84,8 @@ abstract class BaseQuery
*/
public function __construct(ConnectionInterface $connection)
{
$this->connection = $connection;
$this->prefix = $this->connection->getConfig('prefix');
$this->connection = $connection;
$this->prefix = $this->connection->getConfig('prefix');
}

/**
Expand Down Expand Up @@ -418,7 +418,7 @@ public function field(string|array|Raw|bool $field)
if (true === $field) {
// 获取全部字段
$fields = $this->getTableFields();
$field = $fields ?: ['*'];
$field = $fields ?: ['*'];
}

if (isset($this->options['field'])) {
Expand Down Expand Up @@ -449,7 +449,7 @@ public function withoutField(array|string $field)

// 字段排除
$fields = $this->getTableFields();
$field = $fields ? array_diff($fields, $field) : $field;
$field = $fields ? array_diff($fields, $field) : $field;

if (isset($this->options['field'])) {
$field = array_merge((array) $this->options['field'], $field);
Expand Down Expand Up @@ -483,7 +483,7 @@ public function tableField(string|array|bool $field, string $tableName, string $
if (true === $field) {
// 获取全部字段
$fields = $this->getTableFields($tableName);
$field = $fields ?: ['*'];
$field = $fields ?: ['*'];
}

// 添加统一的前缀
Expand Down Expand Up @@ -530,8 +530,8 @@ public function data(array $data)
public function removeOption(string $option = '')
{
if ('' === $option) {
$this->options = [];
$this->bind = [];
$this->options = [];
$this->bind = [];
} elseif (isset($this->options[$option])) {
unset($this->options[$option]);
}
Expand Down Expand Up @@ -710,36 +710,36 @@ public function order(string|array|Raw $field, string $order = '')
public function paginate(int|array $listRows = null, int|bool $simple = false): Paginator
{
if (is_int($simple)) {
$total = $simple;
$total = $simple;
$simple = false;
}

$defaultConfig = [
'query' => [], //url额外参数
'fragment' => '', //url锚点
'var_page' => 'page', //分页变量
'query' => [], //url额外参数
'fragment' => '', //url锚点
'var_page' => 'page', //分页变量
'list_rows' => 15, //每页数量
];

if (is_array($listRows)) {
$config = array_merge($defaultConfig, $listRows);
$listRows = intval($config['list_rows']);
$config = array_merge($defaultConfig, $listRows);
$listRows = intval($config['list_rows']);
} else {
$config = $defaultConfig;
$listRows = intval($listRows ?: $config['list_rows']);
$config = $defaultConfig;
$listRows = intval($listRows ?: $config['list_rows']);
}

$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = max($page, 1);
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = max($page, 1);
$config['path'] = $config['path'] ?? Paginator::getCurrentPath();

if (!isset($total) && !$simple) {
$options = $this->getOptions();

unset($this->options['order'], $this->options['cache'], $this->options['limit'], $this->options['page'], $this->options['field']);

$bind = $this->bind;
$total = $this->count();
$bind = $this->bind;
$total = $this->count();
if ($total > 0) {
$results = $this->options($options)->bind($bind)->page($page, $listRows)->select();
} else {
Expand All @@ -750,10 +750,10 @@ public function paginate(int|array $listRows = null, int|bool $simple = false):
}
}
} elseif ($simple) {
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
$results = $this->limit(($page - 1) * $listRows, $listRows + 1)->select();
$total = null;
} else {
$results = $this->page($page, $listRows)->select();
$results = $this->page($page, $listRows)->select();
}

$this->removeOption('limit');
Expand All @@ -776,21 +776,21 @@ public function paginate(int|array $listRows = null, int|bool $simple = false):
public function paginateX(int|array $listRows = null, string $key = null, string $sort = null): Paginator
{
$defaultConfig = [
'query' => [], //url额外参数
'fragment' => '', //url锚点
'var_page' => 'page', //分页变量
'query' => [], //url额外参数
'fragment' => '', //url锚点
'var_page' => 'page', //分页变量
'list_rows' => 15, //每页数量
];

$config = is_array($listRows) ? array_merge($defaultConfig, $listRows) : $defaultConfig;
$listRows = is_int($listRows) ? $listRows : (int) $config['list_rows'];
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = max($page, 1);
$config = is_array($listRows) ? array_merge($defaultConfig, $listRows) : $defaultConfig;
$listRows = is_int($listRows) ? $listRows : (int) $config['list_rows'];
$page = isset($config['page']) ? (int) $config['page'] : Paginator::getCurrentPage($config['var_page']);
$page = max($page, 1);

$config['path'] = $config['path'] ?? Paginator::getCurrentPath();

$key = $key ?: $this->getPk();
$options = $this->getOptions();
$key = $key ?: $this->getPk();
$options = $this->getOptions();

if (is_null($sort)) {
$order = $options['order'] ?? '';
Expand Down Expand Up @@ -871,7 +871,7 @@ public function more(int $limit, int|string $lastId = null, string $key = null,
$result->first();

return [
'data' => $result,
'data' => $result,
'lastId' => $last ? $last[$key] : null,
];
}
Expand Down Expand Up @@ -903,10 +903,10 @@ public function cache($key = true, $expire = null, $tag = null)

if ($key instanceof \DateTimeInterface || $key instanceof \DateInterval || (is_int($key) && is_null($expire))) {
$expire = $key;
$key = true;
$key = true;
}

$this->options['cache'] = [$key, $expire, $tag ?: $this->getTable()];
$this->options['cache'] = [$key, $expire, $tag ?: $this->getTable()];
return $this;
}

Expand All @@ -921,7 +921,7 @@ public function cache($key = true, $expire = null, $tag = null)
*/
public function cacheAlways($key = true, $expire = null, $tag = null)
{
$this->options['cache_always'] = true;
$this->options['cache_always'] = true;
return $this->cache($key, $expire, $tag);
}

Expand All @@ -936,7 +936,7 @@ public function cacheAlways($key = true, $expire = null, $tag = null)
*/
public function cacheForce($key = true, $expire = null, $tag = null)
{
$this->options['force_cache'] = true;
$this->options['force_cache'] = true;

return $this->cache($key, $expire, $tag);
}
Expand Down Expand Up @@ -1031,8 +1031,8 @@ public function sequence(string $sequence = null)
*/
public function json(array $json = [], bool $assoc = false)
{
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;

return $this;
}
Expand Down Expand Up @@ -1423,9 +1423,9 @@ public function parseOptions(): array
// 根据页数计算limit
[$page, $listRows] = $options['page'];

$page = $page > 0 ? $page : 1;
$listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);
$page = $page > 0 ? $page : 1;
$listRows = $listRows ?: (is_numeric($options['limit']) ? $options['limit'] : 20);
$offset = $listRows * ($page - 1);

$options['limit'] = $offset . ',' . $listRows;
}
Expand Down