Skip to content

Commit

Permalink
Refactor Pointer::createFromParams()
Browse files Browse the repository at this point in the history
  • Loading branch information
DaveRandom committed Jun 8, 2018
1 parent 940bcfc commit a6b4b4c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/Pointer.php
Expand Up @@ -48,6 +48,20 @@ private static function splitRelativePointerComponents(string $pointer): array
: [$pointer, null];
}

/**
* @throws InvalidPointerException
*/
private static function validateRelativePointerParams(?int $relativeLevels, ?bool $isKeyLookup): void
{
if ($relativeLevels < 0) {
throw new InvalidPointerException('Relative levels must be positive');
}

if ($isKeyLookup && $relativeLevels === null) {
throw new InvalidPointerException('Key lookups are only valid for relative pointers');
}
}

private function __construct() { }

/**
Expand All @@ -74,23 +88,17 @@ public static function createFromString(string $pointer): self
*/
public static function createFromParameters(array $path, ?int $relativeLevels = null, ?bool $isKeyLookup = false): self
{
if ($relativeLevels < 0) {
throw new InvalidPointerException('Relative levels must be positive');
}

if ($isKeyLookup && $relativeLevels === null) {
throw new InvalidPointerException('Key lookups are only valid for relative pointers');
}
self::validateRelativePointerParams($relativeLevels, $isKeyLookup);

$result = new self();

$result->relativeLevels = $relativeLevels;
$result->keyLookup = $isKeyLookup ?? false;

foreach ($path as $component) {
$result->path[] = (string)$component;
}

$result->relativeLevels = $relativeLevels;
$result->keyLookup = $isKeyLookup ?? false;

return $result;
}

Expand Down

0 comments on commit a6b4b4c

Please sign in to comment.