Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 5 additions & 19 deletions src/References/JsonPointer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ class JsonPointer
{
private string $value;

private string $filename;

/**
* @var array<array-key,string>
*/
Expand All @@ -16,14 +14,7 @@ class JsonPointer
public function __construct(string $value)
{
$this->value = $value;

$splitRef = explode('#', $value, 2);

$this->filename = $splitRef[0];

if (array_key_exists(1, $splitRef)) {
$this->propertyPaths = $this->decodePropertyPaths($splitRef[1]);
}
$this->propertyPaths = $this->decodePropertyPaths($value);
}

public function value(): string
Expand All @@ -35,11 +26,11 @@ public function append(string ...$values): self
{
$properties = $this->propertyPaths;

foreach ($values as $value) {
array_push($properties, $this->encodePath($value));
}
array_push($properties, ...$values);

$properties = array_map(fn (string $value) => $this->encodePath($value), $properties);

return new self($this->filename . '/' . implode('/', $properties));
return new self('/' . implode('/', $properties));
}

/**
Expand Down Expand Up @@ -69,11 +60,6 @@ private function decodePath(string $path): string
return strtr($path, ['~1' => '/', '~0' => '~', '%25' => '%']);
}

public function getFilename(): string
{
return $this->filename;
}

/**
* @return array<array-key,string>
*/
Expand Down
16 changes: 11 additions & 5 deletions src/References/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@ final class Reference
{
private string $value;

private string $filePath;

private JsonPointer $pointer;

public function __construct(string $value)
{
$this->value = $value;
$this->pointer = new JsonPointer($value);

$splitRef = explode('#', $value, 2);

$this->filePath = $splitRef[0];
$this->pointer = new JsonPointer($splitRef[1] ?? '');
}

public function withBase(string $base): self
{
return new self(Path::canonicalize($base.$this->value));
return new self(Path::canonicalize($base . $this->value));
}

public function value(): string
Expand All @@ -28,17 +34,17 @@ public function value(): string

public function basePath(): string
{
return dirname($this->path()).'/';
return dirname($this->path()) . '/';
}

public function path(): string
{
return $this->pointer->getFilename();
return $this->filePath;
}

public function isInternal(): bool
{
return $this->pointer->getFilename() === '';
return $this->path() === '';
}

public function properties(): array
Expand Down