| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- <?php
- namespace App\Module\GameItems\Config;
- /**
- * 物品数值属性白名单配置
- *
- * 用于定义在生成物品配置表时,哪些数值属性应该被包含在输出中。
- * 未在白名单中的属性将被过滤掉,不会出现在生成的JSON配置中。
- */
- class NumericAttributesWhitelist
- {
- /**
- * 物品数值属性白名单
- *
- * 只有在此数组中列出的属性才会被包含在生成的物品配置表中
- *
- * @var array
- */
- public static array $whitelist = [
- 'crop_growth_time', // 减少作物生长时间
- 'pet_power', // 增加宠物体力
- 'pet_exp', // 增加宠物经验
- 'reward_group_id', // 使用后随机奖励物品组
- 'fram_remove_reward_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
- );
- }
- }
|