-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5e30061
commit a3c0853
Showing
17 changed files
with
297 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context; | ||
|
||
use TypeLang\Mapper\Context\Path\ArrayIndexEntry; | ||
use TypeLang\Mapper\Context\Path\ObjectPropertyEntry; | ||
|
||
final class JsonPath extends Path | ||
{ | ||
public function __toString(): string | ||
{ | ||
$result = '$'; | ||
|
||
foreach ($this->only([ArrayIndexEntry::class, ObjectPropertyEntry::class]) as $entry) { | ||
$result .= match (true) { | ||
$entry instanceof ArrayIndexEntry => "[$entry]", | ||
$entry instanceof ObjectPropertyEntry => ".$entry", | ||
}; | ||
} | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context; | ||
|
||
use TypeLang\Mapper\Context\Path\ArrayIndexEntry; | ||
use TypeLang\Mapper\Context\Path\EntryInterface; | ||
use TypeLang\Mapper\Context\Path\ObjectPropertyEntry; | ||
|
||
/** | ||
* @template-implements \IteratorAggregate<array-key, EntryInterface> | ||
*/ | ||
class Path implements PathInterface, \IteratorAggregate | ||
{ | ||
public function __construct( | ||
/** | ||
* @var list<EntryInterface> | ||
*/ | ||
private array $entries = [], | ||
) {} | ||
|
||
public function enter(EntryInterface $entry): void | ||
{ | ||
$this->entries[] = $entry; | ||
} | ||
|
||
public function leave(): void | ||
{ | ||
if ($this->entries !== []) { | ||
\array_pop($this->entries); | ||
} | ||
} | ||
|
||
public function contains(mixed $value): bool | ||
{ | ||
foreach ($this->entries as $entry) { | ||
if ($entry->match($value)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @param list<class-string<EntryInterface>> $classes | ||
*/ | ||
private function match(EntryInterface $entry, array $classes): bool | ||
{ | ||
foreach ($classes as $class) { | ||
if ($entry instanceof $class) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* @template T of EntryInterface | ||
* @param list<class-string<T>> $classes | ||
* @return list<T> | ||
*/ | ||
public function only(array $classes): array | ||
{ | ||
$result = []; | ||
|
||
foreach ($this->entries as $entry) { | ||
if ($this->match($entry, $classes)) { | ||
$result[] = $entry; | ||
} | ||
} | ||
|
||
/** @var list<T> */ | ||
return $result; | ||
} | ||
|
||
public function getIterator(): \Traversable | ||
{ | ||
return new \ArrayIterator($this->entries); | ||
} | ||
|
||
/** | ||
* @return int<0, max> | ||
*/ | ||
public function count(): int | ||
{ | ||
return \count($this->entries); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context\Path; | ||
|
||
final class ArrayIndexEntry extends Entry | ||
{ | ||
/** | ||
* @param int|non-empty-string $index | ||
*/ | ||
public function __construct( | ||
public readonly int|string $index, | ||
) { | ||
parent::__construct((string) $this->index); | ||
} | ||
|
||
public function match(mixed $value): bool | ||
{ | ||
return $this->index === $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context\Path; | ||
|
||
abstract class Entry implements EntryInterface | ||
{ | ||
/** | ||
* @param non-empty-string $value | ||
*/ | ||
public function __construct( | ||
public readonly string $value, | ||
) {} | ||
|
||
public function match(mixed $value): bool | ||
{ | ||
return $this->value === $value; | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
return $this->value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context\Path; | ||
|
||
interface EntryInterface extends \Stringable | ||
{ | ||
public function match(mixed $value): bool; | ||
|
||
/** | ||
* Returns string representation of the path entry. | ||
* | ||
* @return non-empty-string | ||
*/ | ||
public function __toString(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context\Path; | ||
|
||
final class ObjectEntry extends Entry | ||
{ | ||
/** | ||
* @param class-string $class | ||
*/ | ||
public function __construct(string $class) | ||
{ | ||
parent::__construct($class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context\Path; | ||
|
||
final class ObjectPropertyEntry extends Entry {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TypeLang\Mapper\Context; | ||
|
||
use TypeLang\Mapper\Context\Path\EntryInterface; | ||
|
||
/** | ||
* @template-extends \Traversable<array-key, EntryInterface> | ||
*/ | ||
interface PathInterface extends \Traversable, \Countable | ||
{ | ||
public function enter(EntryInterface $entry): void; | ||
|
||
public function leave(): void; | ||
|
||
public function contains(mixed $value): bool; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.