From e40e393b0c28d554b64fd45f2a38b39768904cdb Mon Sep 17 00:00:00 2001 From: flashwave Date: Mon, 3 Jun 2024 22:04:36 +0000 Subject: [PATCH] Renamed SharpConfig to FileConfig. --- src/FileConfig.php | 136 ++++++++++++++++++ ...gValueInfo.php => FileConfigValueInfo.php} | 6 +- src/SharpConfig.php | 133 +---------------- ...SharpConfigTest.php => FileConfigTest.php} | 27 ++-- 4 files changed, 160 insertions(+), 142 deletions(-) create mode 100644 src/FileConfig.php rename src/{SharpConfigValueInfo.php => FileConfigValueInfo.php} (93%) rename tests/{SharpConfigTest.php => FileConfigTest.php} (87%) diff --git a/src/FileConfig.php b/src/FileConfig.php new file mode 100644 index 0000000..92e1765 --- /dev/null +++ b/src/FileConfig.php @@ -0,0 +1,136 @@ + $values + */ + public function __construct(private array $values) {} + + public function getSeparator(): string { + return ':'; + } + + public function scopeTo(string ...$prefix): IConfig { + return new ScopedConfig($this, $prefix); + } + + public function hasValues(string|array $names): bool { + if(is_string($names)) + return array_key_exists($names, $this->values); + + foreach($names as $name) + if(!array_key_exists($name, $this->values)) + return false; + + return true; + } + + public function getAllValueInfos(int $range = 0, int $offset = 0): array { + if($range === 0) + return array_values($this->values); + + if($range < 0) + throw new InvalidArgumentException('$range must be a positive integer.'); + if($offset < 0) + throw new InvalidArgumentException('$offset must be greater than zero if a range is specified.'); + + return array_slice($this->values, $offset, $range); + } + + public function getValueInfos(string|array $names): array { + if(is_string($names)) + return array_key_exists($names, $this->values) ? [$this->values[$names]] : []; + + $infos = []; + + foreach($names as $name) + if(array_key_exists($name, $this->values)) + $infos[] = $this->values[$name]; + + return $infos; + } + + /** + * Creates an instance of FileConfig from an array of lines. + * + * @param string[] $lines Config lines. + * @return FileConfig + */ + public static function fromLines(array $lines): self { + $values = []; + + foreach($lines as $line) { + $line = trim($line); + if($line === '' || $line[0] === '#' || $line[0] === ';') + continue; + + $info = new FileConfigValueInfo(...explode(' ', $line, 2)); + $values[$info->getName()] = $info; + } + + return new FileConfig($values); + } + + /** + * Creates an instance of FileConfig from a string. + * + * @param string $lines Config lines. + * @param non-empty-string $newLine Line separator character. + * @return FileConfig + */ + public static function fromString(string $lines, string $newLine = "\n"): self { + return self::fromLines(explode($newLine, $lines)); + } + + /** + * Creates an instance of FileConfig from a file. + * + * @param string $path Config file path. + * @throws InvalidArgumentException If $path does not exist. + * @return FileConfig + */ + public static function fromFile(string $path): self { + if(!is_file($path)) + throw new InvalidArgumentException('$path does not exist.'); + + return self::fromStream(FileStream::openRead($path)); + } + + /** + * Creates an instance of FileConfig from a readable stream. + * + * @param Stream $stream Config file stream. + * @throws InvalidArgumentException If $stream is not readable. + * @return FileConfig + */ + public static function fromStream(Stream $stream): self { + if(!$stream->canRead()) + throw new InvalidArgumentException('$stream must be readable.'); + + $values = []; + while(($line = $stream->readLine()) !== null) { + $line = trim($line); + if($line === '' || $line[0] === '#' || $line[0] === ';') + continue; + + $info = new FileConfigValueInfo(...explode(' ', $line, 2)); + $values[$info->getName()] = $info; + } + + return new FileConfig($values); + } +} diff --git a/src/SharpConfigValueInfo.php b/src/FileConfigValueInfo.php similarity index 93% rename from src/SharpConfigValueInfo.php rename to src/FileConfigValueInfo.php index b8010e8..e004e1a 100644 --- a/src/SharpConfigValueInfo.php +++ b/src/FileConfigValueInfo.php @@ -1,11 +1,11 @@ $values - */ - public function __construct(private array $values) {} - - public function getSeparator(): string { - return ':'; - } - - public function scopeTo(string ...$prefix): IConfig { - return new ScopedConfig($this, $prefix); - } - - public function hasValues(string|array $names): bool { - if(is_string($names)) - return array_key_exists($names, $this->values); - - foreach($names as $name) - if(!array_key_exists($name, $this->values)) - return false; - - return true; - } - - public function getAllValueInfos(int $range = 0, int $offset = 0): array { - if($range === 0) - return array_values($this->values); - - if($range < 0) - throw new InvalidArgumentException('$range must be a positive integer.'); - if($offset < 0) - throw new InvalidArgumentException('$offset must be greater than zero if a range is specified.'); - - return array_slice($this->values, $offset, $range); - } - - public function getValueInfos(string|array $names): array { - if(is_string($names)) - return array_key_exists($names, $this->values) ? [$this->values[$names]] : []; - - $infos = []; - - foreach($names as $name) - if(array_key_exists($name, $this->values)) - $infos[] = $this->values[$name]; - - return $infos; - } - - /** - * Creates an instance of SharpConfig from an array of lines. - * - * @param string[] $lines Config lines. - * @return SharpConfig - */ - public static function fromLines(array $lines): self { - $values = []; - - foreach($lines as $line) { - $line = trim($line); - if($line === '' || $line[0] === '#' || $line[0] === ';') - continue; - - $info = new SharpConfigValueInfo(...explode(' ', $line, 2)); - $values[$info->getName()] = $info; - } - - return new SharpConfig($values); - } - - /** - * Creates an instance of SharpConfig from a string. - * - * @param string $lines Config lines. - * @param non-empty-string $newLine Line separator character. - * @return SharpConfig - */ - public static function fromString(string $lines, string $newLine = "\n"): self { - return self::fromLines(explode($newLine, $lines)); - } - - /** - * Creates an instance of SharpConfig from a file. - * - * @param string $path Config file path. - * @throws InvalidArgumentException If $path does not exist. - * @return SharpConfig - */ - public static function fromFile(string $path): self { - if(!is_file($path)) - throw new InvalidArgumentException('$path does not exist.'); - - return self::fromStream(FileStream::openRead($path)); - } - - /** - * Creates an instance of SharpConfig from a readable stream. - * - * @param Stream $stream Config file stream. - * @throws InvalidArgumentException If $stream is not readable. - * @return SharpConfig - */ - public static function fromStream(Stream $stream): self { - if(!$stream->canRead()) - throw new InvalidArgumentException('$stream must be readable.'); - - $values = []; - while(($line = $stream->readLine()) !== null) { - $line = trim($line); - if($line === '' || $line[0] === '#' || $line[0] === ';') - continue; - - $info = new SharpConfigValueInfo(...explode(' ', $line, 2)); - $values[$info->getName()] = $info; - } - - return new SharpConfig($values); - } -} +class SharpConfig extends FileConfig {} diff --git a/tests/SharpConfigTest.php b/tests/FileConfigTest.php similarity index 87% rename from tests/SharpConfigTest.php rename to tests/FileConfigTest.php index 5b8d36a..402f4d1 100644 --- a/tests/SharpConfigTest.php +++ b/tests/FileConfigTest.php @@ -1,53 +1,60 @@ expectException(\RuntimeException::class); + \Syokuhou\FileConfig::fromString('test value')->removeValues('test'); \Syokuhou\SharpConfig::fromString('test value')->removeValues('test'); } public function testImmutableSetString(): void { $this->expectException(\RuntimeException::class); + \Syokuhou\FileConfig::fromString('test value')->setString('test', 'the'); \Syokuhou\SharpConfig::fromString('test value')->setString('test', 'the'); } public function testImmutableSetInteger(): void { $this->expectException(\RuntimeException::class); + \Syokuhou\FileConfig::fromString('test 1234')->setInteger('test', 5678); \Syokuhou\SharpConfig::fromString('test 1234')->setInteger('test', 5678); } public function testImmutableSetFloat(): void { $this->expectException(\RuntimeException::class); + \Syokuhou\FileConfig::fromString('test 56.78')->setFloat('test', 12.34); \Syokuhou\SharpConfig::fromString('test 56.78')->setFloat('test', 12.34); } public function testImmutableSetBoolean(): void { $this->expectException(\RuntimeException::class); + \Syokuhou\FileConfig::fromString('test true')->setBoolean('test', false); \Syokuhou\SharpConfig::fromString('test true')->setBoolean('test', false); } public function testImmutableSetArray(): void { $this->expectException(\RuntimeException::class); + \Syokuhou\FileConfig::fromString('test words words words')->setArray('test', ['meow', 'meow', 'meow']); \Syokuhou\SharpConfig::fromString('test words words words')->setArray('test', ['meow', 'meow', 'meow']); } public function testImmutableSetValues(): void { $this->expectException(\RuntimeException::class); - \Syokuhou\SharpConfig::fromString('')->setValues([ + \Syokuhou\FileConfig::fromString('')->setValues([ 'stringval' => 'the', 'intval' => 1234, 'floatval' => 56.78, @@ -57,7 +64,7 @@ final class SharpConfigTest extends TestCase { } public function testScoping(): void { - $config = \Syokuhou\SharpConfig::fromLines([ + $config = \Syokuhou\FileConfig::fromLines([ 'test Inaccessible', 'scoped:test Accessible', ]); @@ -70,7 +77,7 @@ final class SharpConfigTest extends TestCase { } public function testHasValues(): void { - $config = \Syokuhou\SharpConfig::fromLines([ + $config = \Syokuhou\FileConfig::fromLines([ 'test 123', 'scoped:test true', 'scoped:meow meow', @@ -85,7 +92,7 @@ final class SharpConfigTest extends TestCase { } public function testGetAllValueInfos(): void { - $config = \Syokuhou\SharpConfig::fromFile(__DIR__ . '/sharpchat.cfg'); + $config = \Syokuhou\FileConfig::fromFile(__DIR__ . '/sharpchat.cfg'); $all = $config->getAllValueInfos(); $expected = [ @@ -131,7 +138,7 @@ final class SharpConfigTest extends TestCase { } public function testGetValues(): void { - $config = \Syokuhou\SharpConfig::fromFile(__DIR__ . '/sharpchat.cfg'); + $config = \Syokuhou\FileConfig::fromFile(__DIR__ . '/sharpchat.cfg'); $this->assertNull($config->getValueInfo('doesnotexist'));