|
|
@@ -1,64 +0,0 @@
|
|
|
-<?php
|
|
|
-
|
|
|
-namespace App\Module\GameItems\Config;
|
|
|
-
|
|
|
-/**
|
|
|
- * 宝箱开启消耗属性白名单配置
|
|
|
- *
|
|
|
- * 用于定义在生成宝箱配置表时,宝箱开启消耗中哪些属性应该被包含在输出中。
|
|
|
- * 未在白名单中的属性将被过滤掉,不会出现在生成的JSON配置中。
|
|
|
- */
|
|
|
-class ChestCostWhitelist
|
|
|
-{
|
|
|
- /**
|
|
|
- * 宝箱开启消耗属性白名单
|
|
|
- *
|
|
|
- * 只有在此数组中列出的属性才会被包含在生成的宝箱配置表中
|
|
|
- *
|
|
|
- * @var array
|
|
|
- */
|
|
|
- public static array $whitelist = [
|
|
|
- 'cost_type', // 消耗类型
|
|
|
- 'cost_id', // 消耗ID
|
|
|
- 'cost_quantity', // 消耗数量
|
|
|
- 'item_name', // 物品名称(当cost_type为物品时)
|
|
|
- ];
|
|
|
-
|
|
|
- /**
|
|
|
- * 检查属性是否在白名单中
|
|
|
- *
|
|
|
- * @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
|
|
|
- );
|
|
|
- }
|
|
|
-}
|