| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Module\GameItems\Config;
- /**
- * 宝箱内容属性白名单配置
- *
- * 用于定义在生成宝箱配置表时,宝箱内容中哪些属性应该被包含在输出中。
- * 未在白名单中的属性将被过滤掉,不会出现在生成的JSON配置中。
- */
- class ChestContentWhitelist
- {
- /**
- * 宝箱内容属性白名单
- *
- * 只有在此数组中列出的属性才会被包含在生成的宝箱配置表中
- *
- * @var array
- */
- public static array $whitelist = [
- 'item_id', // 物品ID
- 'type', // 类型(item或group)
- 'from_group', // 是否来自物品组
- ];
- /**
- * 检查属性是否在白名单中
- *
- * @param string $attribute 属性名
- * @return bool 如果属性在白名单中返回true,否则返回false
- */
- public static function isAllowed(string $attribute): bool
- {
- return in_array($attribute, self::$whitelist);
- }
- /**
- * 过滤对象,只保留白名单中的属性
- *
- * @param object|array|null $attributes 要过滤的属性对象或数组
- * @return array|null 过滤后的属性数组,如果输入为null则返回null
- */
- public static function filter($attributes): ?array
- {
- if (is_null($attributes)) {
- return null;
- }
- // 如果是对象,转换为数组
- if (is_object($attributes)) {
- $attributes = (array)$attributes;
- }
- // 过滤数组,只保留白名单中的属性
- return array_filter(
- $attributes,
- function ($key) {
- return self::isAllowed($key);
- },
- ARRAY_FILTER_USE_KEY
- );
- }
- }
|