|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * Copyright (c) 2024 Kai Sassnowski |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @see https://github.com/roach-php/roach |
| 12 | + */ |
| 13 | + |
| 14 | +namespace RoachPHP\Downloader\Proxy; |
| 15 | + |
| 16 | +final class ProxyOptions |
| 17 | +{ |
| 18 | + /** |
| 19 | + * @param array<int, string> $excludedDomains |
| 20 | + */ |
| 21 | + public function __construct( |
| 22 | + private readonly ?string $httpProxyURL = null, |
| 23 | + private readonly ?string $httpsProxyURL = null, |
| 24 | + private readonly array $excludedDomains = [], |
| 25 | + ) { |
| 26 | + } |
| 27 | + |
| 28 | + public static function make(): self |
| 29 | + { |
| 30 | + return new self(); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Configure the same proxy URL to be used for HTTP and HTTPS. |
| 35 | + */ |
| 36 | + public static function allProtocols(string $url): self |
| 37 | + { |
| 38 | + return new self($url, $url, []); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Configure the proxy URL to be used for requests using HTTP. |
| 43 | + */ |
| 44 | + public function http(string $url): self |
| 45 | + { |
| 46 | + return new self($url, $this->httpsProxyURL, $this->excludedDomains); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Configure the proxy URL to be used for requests using HTTPs. |
| 51 | + */ |
| 52 | + public function https(string $url): self |
| 53 | + { |
| 54 | + return new self($this->httpProxyURL, $url, $this->excludedDomains); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Configure the domains or TLDs that should not use proxies. |
| 59 | + * |
| 60 | + * @param array<int, string>|string $domains |
| 61 | + */ |
| 62 | + public function exclude(array|string $domains): self |
| 63 | + { |
| 64 | + return new self( |
| 65 | + $this->httpProxyURL, |
| 66 | + $this->httpsProxyURL, |
| 67 | + (array) $domains, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + public function isEmpty(): bool |
| 72 | + { |
| 73 | + return null === $this->httpProxyURL |
| 74 | + && null === $this->httpsProxyURL |
| 75 | + && \count($this->excludedDomains) === 0; |
| 76 | + } |
| 77 | + |
| 78 | + public function equals(self $other): bool |
| 79 | + { |
| 80 | + return $this->httpProxyURL === $other->httpProxyURL |
| 81 | + && $this->httpsProxyURL === $other->httpsProxyURL |
| 82 | + && $this->excludedDomains === $other->excludedDomains; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * @return array{ |
| 87 | + * http?: string, |
| 88 | + * https?: string, |
| 89 | + * no?: array<int, string> |
| 90 | + * } |
| 91 | + */ |
| 92 | + public function toArray(): array |
| 93 | + { |
| 94 | + return \array_filter([ |
| 95 | + 'http' => $this->httpProxyURL, |
| 96 | + 'https' => $this->httpsProxyURL, |
| 97 | + 'no' => $this->excludedDomains, |
| 98 | + ]); |
| 99 | + } |
| 100 | +} |
0 commit comments