Skip to content

Commit

Permalink
Merge pull request #24 from systopia/allow-tagged-data-multiple
Browse files Browse the repository at this point in the history
TaggedDataContainer: Allow `add()` to be called multiple times with the same pair of tag and data pointer
  • Loading branch information
dontub authored Mar 26, 2024
2 parents c958077 + 59e53e6 commit bb8befa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
24 changes: 18 additions & 6 deletions src/Tags/TaggedDataContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

namespace Systopia\JsonSchema\Tags;

use Systopia\JsonSchema\Exceptions\InvalidArgumentException;

final class TaggedDataContainer implements TaggedDataContainerInterface
{
/**
Expand All @@ -33,31 +31,42 @@ final class TaggedDataContainer implements TaggedDataContainerInterface
*/
private array $extra = [];

/**
* {@inheritDoc}
*/
public function add(string $tag, string $dataPointer, $data, $extra): void
{
if ($this->has($tag, $dataPointer)) {
throw new InvalidArgumentException(sprintf('Data for tag "%s" at "%s" already exists', $tag, $dataPointer));
}

$this->data[$tag][$dataPointer] = $data;
$this->extra[$tag][$dataPointer] = $extra;
}

/**
* {@inheritDoc}
*/
public function get(string $tag, string $dataPointer)
{
return $this->data[$tag][$dataPointer] ?? null;
}

/**
* {@inheritDoc}
*/
public function has(string $tag, string $dataPointer): bool
{
return \array_key_exists($dataPointer, $this->data[$tag] ?? []);
}

/**
* {@inheritDoc}
*/
public function getAll(): array
{
return $this->data;
}

/**
* {@inheritDoc}
*/
public function getByTag(string $tag): array
{
return $this->data[$tag] ?? [];
Expand All @@ -68,6 +77,9 @@ public function hasTag(string $tag): bool
return isset($this->data[$tag]);
}

/**
* {@inheritDoc}
*/
public function getExtra(string $tag, string $dataPointer)
{
return $this->extra[$tag][$dataPointer] ?? null;
Expand Down
4 changes: 4 additions & 0 deletions src/Tags/TaggedDataContainerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
interface TaggedDataContainerInterface
{
/**
* If the method is called multiple times with the same pair of tag and data
* pointer, the last data will be available. This might happen e.g. in case
* of sub schemas.
*
* @param null|mixed $data
* @param null|mixed $extra
*/
Expand Down
5 changes: 0 additions & 5 deletions tests/Tags/TaggedDataContainerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
namespace Systopia\JsonSchema\Test\Tags;

use PHPUnit\Framework\TestCase;
use Systopia\JsonSchema\Exceptions\InvalidArgumentException;
use Systopia\JsonSchema\Tags\TaggedDataContainer;

/**
Expand Down Expand Up @@ -66,9 +65,5 @@ public function test(): void
self::assertSame(['/foo' => null], $container->getByTag('test2'));
self::assertTrue($container->has('test2', '/foo'));
self::assertNull($container->get('test2', '/foo'));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Data for tag "test" at "/foo" already exists');
$container->add('test', '/foo', 'exists', null);
}
}

0 comments on commit bb8befa

Please sign in to comment.