|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Repository\VersionProviders; |
| 4 | + |
| 5 | + |
| 6 | +use App\Exceptions\VersionProviderException; |
| 7 | +use JsonException; |
| 8 | + |
| 9 | +/*** |
| 10 | + * @example json?filename=composer.json&key=extra.version&pretty=true&unescaped_slashes=false |
| 11 | + */ |
| 12 | +readonly class JsonVersionProvider implements VersionProviderInterface |
| 13 | +{ |
| 14 | + use FilePathToolsTrait; |
| 15 | + |
| 16 | + public string $filename; |
| 17 | + |
| 18 | + public function __construct( |
| 19 | + string $filename, |
| 20 | + public string $key, |
| 21 | + string $workingDirectory, |
| 22 | + public bool $pretty_print = true, |
| 23 | + public bool $unescaped_slashes = false, |
| 24 | + ) |
| 25 | + { |
| 26 | + $this->filename = $this->getFilePath($filename, $workingDirectory); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @throws VersionProviderException |
| 31 | + */ |
| 32 | + public function getVersion(): string |
| 33 | + { |
| 34 | + if (!file_exists($this->filename)) { |
| 35 | + throw new VersionProviderException('json', 'File does not exist'); |
| 36 | + } |
| 37 | + |
| 38 | + $json = file_get_contents($this->filename); |
| 39 | + try { |
| 40 | + $json = json_decode($json, true, flags: JSON_THROW_ON_ERROR); |
| 41 | + } catch (JsonException $e) { |
| 42 | + throw new VersionProviderException('json', 'File is not a valid json file'); |
| 43 | + } |
| 44 | + $key = explode('.', $this->key); |
| 45 | + |
| 46 | + foreach ($key as $k) { |
| 47 | + if(!isset($json[$k])) { |
| 48 | + throw new VersionProviderException('json', 'JSON does contains the provided key (' . $k . '): '. $this->key); |
| 49 | + } |
| 50 | + |
| 51 | + $json = $json[$k]; |
| 52 | + } |
| 53 | + |
| 54 | + if(is_array($json)) { |
| 55 | + throw new VersionProviderException('json', 'The provided JSON key, contains an array: '. $this->key); |
| 56 | + } |
| 57 | + |
| 58 | + if(!is_string($json)) { |
| 59 | + throw new VersionProviderException('json', 'The provided JSON key, is not a string value: '. $this->key); |
| 60 | + } |
| 61 | + |
| 62 | + return $json; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @throws VersionProviderException |
| 67 | + */ |
| 68 | + public function setVersion(string $version): void |
| 69 | + { |
| 70 | + $json = file_get_contents($this->filename); |
| 71 | + try { |
| 72 | + $json = json_decode($json, true, flags: JSON_THROW_ON_ERROR); |
| 73 | + } catch (JsonException $e) { |
| 74 | + throw new VersionProviderException('json', 'File is not a valid json file'); |
| 75 | + } |
| 76 | + |
| 77 | + $json = $this->dot_set($json, $this->key, $version); |
| 78 | + |
| 79 | + $flags = 0; |
| 80 | + |
| 81 | + if($this->pretty_print) { |
| 82 | + $flags |= JSON_PRETTY_PRINT; |
| 83 | + } |
| 84 | + |
| 85 | + if($this->unescaped_slashes) { |
| 86 | + $flags |= JSON_UNESCAPED_SLASHES; |
| 87 | + } |
| 88 | + |
| 89 | + $json = json_encode($json, $flags); |
| 90 | + file_put_contents($this->filename, $json); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * @throws VersionProviderException |
| 95 | + */ |
| 96 | + private function dot_set(mixed $json, string $key, string $value): mixed |
| 97 | + { |
| 98 | + if(!is_array($json)) { |
| 99 | + throw new VersionProviderException('json', 'The provided JSON key, does not contains an array: '. $key); |
| 100 | + } |
| 101 | + |
| 102 | + $k = explode('.', $key); |
| 103 | + $current_key = $k[0]; |
| 104 | + if(!array_key_exists($current_key, $json)) { |
| 105 | + throw new VersionProviderException('json', 'The provided JSON key, does not contains an key: '. $current_key); |
| 106 | + } |
| 107 | + |
| 108 | + if(count($k) > 1) { |
| 109 | + unset($k[0]); |
| 110 | + $json[$current_key] = $this->dot_set($json[$current_key], implode('.', $k), $value); |
| 111 | + } else { |
| 112 | + $json[$current_key] = $value; |
| 113 | + } |
| 114 | + return $json; |
| 115 | + } |
| 116 | +} |
0 commit comments