HttpToHttps.php 869 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Casts;
  3. use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
  4. class HttpToHttps implements CastsAttributes
  5. {
  6. public function get($model, string $key, $value, array $attributes)
  7. {
  8. return static::replace($value);
  9. }
  10. public function set($model, string $key, $value, array $attributes)
  11. {
  12. return static::replace($value);
  13. }
  14. private static function replace($value): string
  15. {
  16. if (!$value) {
  17. return $value;
  18. }
  19. $value = str_replace('http://', 'https://', $value);
  20. $value = str_replace('-internal', '', $value);
  21. $arr = explode('/', $value);
  22. array_shift($arr);
  23. array_shift($arr);
  24. array_shift($arr);
  25. $baseUrl = env('ALI_OSS_URL', 'https://zhangsiyecdn.9026.com/');
  26. return $baseUrl . implode('/', $arr);
  27. }
  28. }