1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace App\Traits;
- use App\Enums\TechnicianLocationType;
- trait LocationValidationTrait
- {
- /**
- * 验证位置类型
- * 检查位置类型是否为有效值
- *
- * @param int|null $type 位置类型
- * @param int $code HTTP状态码
- * @param string $message 错误消息
- * @return void
- */
- protected function validateLocationType(?int $type, int $code = 422, string $message = '无效的位置类型'): void
- {
- abort_if(
- !in_array($type, [TechnicianLocationType::CURRENT->value, TechnicianLocationType::COMMON->value]),
- $code,
- $message
- );
- }
- /**
- * 验证经纬度
- * 检查经纬度的有效性
- *
- * @param float $latitude 纬度
- * @param float $longitude 经度
- * @param int $code HTTP状态码
- * @param string $latMessage 纬度错误消息
- * @param string $lngMessage 经度错误消息
- * @throws \Exception 当经纬度无效时抛出异常
- */
- protected function validateCoordinates(
- $latitude,
- $longitude,
- int $code = 422,
- string $latMessage = '无效的纬度坐标',
- string $lngMessage = '无效的经度坐标'
- ): void {
- // 验证纬度 (-90° to 90°)
- abort_if(
- !is_numeric($latitude) || !($latitude >= -90 && $latitude <= 90),
- $code,
- $latMessage
- );
- // 验证经度 (-180° to 180°)
- abort_if(
- !is_numeric($longitude) || !($longitude >= -180 && $longitude <= 180),
- $code,
- $lngMessage
- );
- }
- /**
- * 获取有效的位置类型列表
- *
- * @return array 位置类型列表
- */
- protected function getValidLocationTypes(): array
- {
- return [
- TechnicianLocationType::CURRENT->value,
- TechnicianLocationType::COMMON->value
- ];
- }
- /**
- * 检查是否为有效的位置类型
- *
- * @param int|null $type 位置类型
- * @return bool
- */
- protected function isValidLocationType(?int $type): bool
- {
- return in_array($type, $this->getValidLocationTypes());
- }
- }
|