Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
82.76% |
24 / 29 |
|
71.43% |
5 / 7 |
CRAP | |
0.00% |
0 / 1 |
| JavascriptCdnPath | |
82.76% |
24 / 29 |
|
71.43% |
5 / 7 |
19.66 | |
0.00% |
0 / 1 |
| __construct | |
33.33% |
2 / 6 |
|
0.00% |
0 / 1 |
8.74 | |||
| getRelativePath | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getRegularExpression | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getPackageName | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
| getVersion | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
2 | |||
| isLatestVersion | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPath | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
4.13 | |||
| 1 | <?php |
| 2 | namespace Apie\JavascriptCdn\ValueObjects; |
| 3 | |
| 4 | use Apie\CommonValueObjects\SemanticVersion; |
| 5 | use Apie\Core\ValueObjects\Interfaces\HasRegexValueObjectInterface; |
| 6 | use Apie\Core\ValueObjects\IsStringWithRegexValueObject; |
| 7 | use Apie\Core\ValueObjects\Utils; |
| 8 | use Apie\JavascriptCdn\Enums\Latest; |
| 9 | |
| 10 | class JavascriptCdnPath implements HasRegexValueObjectInterface |
| 11 | { |
| 12 | use IsStringWithRegexValueObject { |
| 13 | __construct as private __constructString; |
| 14 | } |
| 15 | |
| 16 | private string $packageName; |
| 17 | |
| 18 | private SemanticVersion|Latest $version; |
| 19 | |
| 20 | // we store null as '' because isset() returns false for null, but true for ''. |
| 21 | private string $path; |
| 22 | |
| 23 | public function __construct(string $internal, SemanticVersion|Latest|null $version = null, ?string $path = null) |
| 24 | { |
| 25 | if (func_num_args() === 1) { |
| 26 | $this->__constructString($internal); |
| 27 | } else { |
| 28 | $this->packageName = $internal; |
| 29 | $this->version = $version ?? Latest::Latest; |
| 30 | $this->path = $path ? : ''; |
| 31 | $this->__constructString($internal . '@' . Utils::toString($this->version) . ($path ? ('/' . $path) : '')); |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | public function getRelativePath(): string |
| 36 | { |
| 37 | $path = $this->getPath(); |
| 38 | return $this->getPackageName() . DIRECTORY_SEPARATOR . Utils::toString($this->getVersion()) . DIRECTORY_SEPARATOR . ($path ? $path : '/root.js'); |
| 39 | } |
| 40 | |
| 41 | public static function getRegularExpression(): string |
| 42 | { |
| 43 | $semanticVersion = '(latest|(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(-((0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?)'; |
| 44 | return '/^(|@)[^@]+@' . $semanticVersion .'(|\/(?!(.*\/)?\.\.(\/|$)).*)$/'; |
| 45 | } |
| 46 | |
| 47 | public function getPackageName(): string |
| 48 | { |
| 49 | if (isset($this->packageName)) { |
| 50 | return $this->packageName; |
| 51 | } |
| 52 | $startsWithAt = str_starts_with($this->internal, '@'); |
| 53 | $parts = explode('@', substr($this->internal, $startsWithAt ? 1 : 0), 2); |
| 54 | assert(count($parts) === 2); |
| 55 | $this->packageName = ($startsWithAt ? '@' : '') . $parts[0]; |
| 56 | return $this->packageName; |
| 57 | } |
| 58 | |
| 59 | public function getVersion(): SemanticVersion|Latest |
| 60 | { |
| 61 | if (isset($this->version)) { |
| 62 | return $this->version; |
| 63 | } |
| 64 | $packageName = $this->getPackageName(); |
| 65 | $parts = explode('/', substr($this->internal, strlen($packageName) + 1), 2); |
| 66 | $this->version = Latest::tryFrom($parts[0]) ?? new SemanticVersion($parts[0]); |
| 67 | return $this->version; |
| 68 | } |
| 69 | |
| 70 | public function isLatestVersion(): bool |
| 71 | { |
| 72 | return $this->getVersion() instanceof Latest; |
| 73 | } |
| 74 | |
| 75 | public function getPath(): ?string |
| 76 | { |
| 77 | if (isset($this->path)) { |
| 78 | return $this->path ? : null; |
| 79 | } |
| 80 | $prefix = $this->getPackageName() . '@' . Utils::toString($this->getVersion()); |
| 81 | $this->path = ltrim(substr($this->internal, strlen($prefix)), '/'); |
| 82 | return $this->path ? : null; |
| 83 | } |
| 84 | } |