Ai
2 Star 0 Fork 1

zms/hyperf-casts

forked from zlf/app-hyperf-casts 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
StringListCasts.php 1.07 KB
一键复制 编辑 原始数据 按行查看 历史
zms 提交于 2025-03-13 23:48 +08:00 . 3/13
<?php
declare(strict_types=1);
namespace Zms\HyperfCasts;
use Hyperf\Contract\CastsAttributes;
use Zms\Unit\Str;
/**
* 数组连接转字符串 如 ['a','b'] a,b
* Class ArrjoinCasts
* @package Core\Casts
*/
class StringListCasts implements CastsAttributes
{
/**
* 获取结果
*/
public function get($model, $key, $value, $attributes)
{
if (is_array($value)) {
return $value;
} elseif (is_string($value) && strlen($value) > 0) {
$data = [];
foreach (Str::explode(',', $value) as $item) {
$data[] = (string)$item;
}
return $data;
}
return [];
}
/**
* 设置数据
*/
public function set($model, $key, $value, $attributes)
{
if (is_array($value)) {
foreach ($value as $index => $item) {
$value[$index] = strval($item);
}
return implode(',', $value);
} elseif (is_string($value) && strlen($value) > 0) {
return $value;
}
return '';
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zms-code/hyperf-casts.git
git@gitee.com:zms-code/hyperf-casts.git
zms-code
hyperf-casts
hyperf-casts
master

搜索帮助