Skip to content

Commit d7cdaf9

Browse files
authored
Prepare for multiple versions of PublicCode object. (#20)
Fix #4.
1 parent f48380f commit d7cdaf9

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

README.md

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,24 @@ $parser = new Parser();
6969

7070
$publicCode = $parser->parseFile('/path/to/publiccode.yml');
7171

72-
$name = $publicCode->getName();
73-
$url = $publicCode->getUrl();
74-
$landingUrl = $publicCode->getLandingUrl();
72+
if ($publicCode instanceof PublicCodeV0) {
73+
$name = $publicCode->getName();
74+
$url = $publicCode->getUrl();
75+
$landingUrl = $publicCode->getLandingUrl();
7576

76-
$descriptionIt = $publicCode->getDescription('it');
77-
$descriptionEn = $publicCode->getDescription('en');
78-
$allDescriptions = $publicCode->getAllDescriptions();
77+
$descriptionIt = $publicCode->getDescription('it');
78+
$descriptionEn = $publicCode->getDescription('en');
79+
$allDescriptions = $publicCode->getAllDescriptions();
7980

80-
$platforms = $publicCode->getPlatforms(); // ['web', 'android', 'ios', etc.]
81+
$platforms = $publicCode->getPlatforms(); // ['web', 'android', 'ios', etc.]
82+
}
83+
84+
/* In the future, when v1 will be released */
85+
/*
86+
else if ($publicCode instanceof PublicCodeV1) {
87+
...
88+
}
89+
*/
8190
```
8291

8392
### Advanced config

phpstan.neon.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ parameters:
55
- tests
66
excludePaths:
77
- vendor
8-
- src/PublicCode.php # TODO: re-enable it after a proper typed mapping with the Go struct
8+
- src/PublicCodeV0.php # TODO: re-enable it after a proper typed mapping with the Go struct

src/Parser.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ public function __destruct()
4444
* Parse publiccode.yml content
4545
*
4646
* @param string $content YAML content
47-
* @return PublicCode
47+
* @return PublicCodeV0
4848
* @throws ParserException
4949
* @throws ValidationException
5050
*/
51-
public function parse(string $content): PublicCode
51+
public function parse(string $content): PublicCodeV0
5252
{
5353
/** @phpstan-ignore-next-line */
5454
$result = $this->ffi->ParseString($this->handle, $content);
@@ -64,11 +64,11 @@ public function parse(string $content): PublicCode
6464
* Parse a publiccode.yml file
6565
*
6666
* @param string $filePath Path to publiccode.yml
67-
* @return PublicCode
67+
* @return PublicCodeV0
6868
* @throws ParserException
6969
* @throws ValidationException
7070
*/
71-
public function parseFile(string $filePath): PublicCode
71+
public function parseFile(string $filePath): PublicCodeV0
7272
{
7373
/** @phpstan-ignore-next-line */
7474
$result = $this->ffi->ParseFile($this->handle, $filePath);
@@ -172,11 +172,11 @@ private function findLibrary(): string
172172
* Process FFI result and convert to PublicCode object
173173
*
174174
* @param mixed $result FFI ParseResult pointer
175-
* @return PublicCode
175+
* @return PublicCodeV0
176176
* @throws ValidationException
177177
* @throws ParserException
178178
*/
179-
private function processResult($result): PublicCode
179+
private function processResult($result): PublicCodeV0
180180
{
181181
if ($result->ErrorCount > 0) {
182182
/** @var list<non-empty-string> */
@@ -219,6 +219,6 @@ private function processResult($result): PublicCode
219219
throw new ParserException('Failed to decode JSON data: ' . json_last_error_msg());
220220
}
221221

222-
return new PublicCode($data);
222+
return new PublicCodeV0($data);
223223
}
224224
}

src/PublicCode.php renamed to src/PublicCodeV0.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
use JsonSerializable;
88

9-
class PublicCode implements JsonSerializable
9+
class PublicCodeV0 implements JsonSerializable
1010
{
1111
private array $data;
1212

tests/ParserTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Bfabio\PublicCodeParser\Tests;
66

77
use Bfabio\PublicCodeParser\Parser;
8+
use Bfabio\PublicCodeParser\PublicCodeV0;
89
use PHPUnit\Framework\TestCase;
910

1011
class ParserTest extends TestCase
@@ -26,6 +27,13 @@ protected function setUp(): void
2627
$this->yaml = $content;
2728
}
2829

30+
public function testPublicCodeType(): void
31+
{
32+
$publicCode = $this->parser->parse($this->yaml);
33+
34+
$this->assertInstanceOf(PublicCodeV0::class, $publicCode);
35+
}
36+
2937
public function testPublicCodeAccessors(): void
3038
{
3139
$publicCode = $this->parser->parse($this->yaml);

0 commit comments

Comments
 (0)