Skip to content

Commit

Permalink
feat: $offset parameter type widening for ArrayAccess methods (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi authored Jul 5, 2024
1 parent 4dbf5e6 commit 36a21c3
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
* feat: default count limits
* refactor: remove `Countable` from minimal classes
* refactor: rename `RestrictedCountStrategy` to `DisabledCountStrategy`
* feat: `$offset` parameter type widening for `ArrayAccess` methods

## 0.7.0

Expand Down
21 changes: 17 additions & 4 deletions packages/collections-common/src/Trait/ArrayAccessTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rekalogika\Domain\Collections\Common\Trait;

use Doctrine\Common\Collections\Collection;
use Rekalogika\Domain\Collections\Common\Internal\KeyTransformerUtil;

/**
* @template TKey of array-key
Expand All @@ -33,28 +34,34 @@ abstract private function ensureSafety(): void;
abstract private function getRealCollection(): Collection;

/**
* @param TKey $offset
* @param mixed $offset
*/
final public function offsetExists(mixed $offset): bool
{
/** @var TKey */
$offset = KeyTransformerUtil::transformInputToKey($this->keyTransformer, $offset);

// return $this->getSafeCollection()->containsKey($offset);

return $this->containsKey($offset);
}

/**
* @param TKey $offset
* @param mixed $offset
* @return T|null
*/
final public function offsetGet(mixed $offset): mixed
{
/** @var TKey */
$offset = KeyTransformerUtil::transformInputToKey($this->keyTransformer, $offset);

// return $this->getSafeCollection()->get($offset);

return $this->get($offset);
}

/**
* @param TKey|null $offset
* @param mixed $offset
* @param T $value
*/
final public function offsetSet(mixed $offset, mixed $value): void
Expand All @@ -65,16 +72,22 @@ final public function offsetSet(mixed $offset, mixed $value): void
return;
}

/** @var TKey */
$offset = KeyTransformerUtil::transformInputToKey($this->keyTransformer, $offset);

$this->ensureSafety();

$this->getRealCollection()->offsetSet($offset, $value);
}

/**
* @param TKey $offset
* @param mixed $offset
*/
final public function offsetUnset(mixed $offset): void
{
/** @var TKey */
$offset = KeyTransformerUtil::transformInputToKey($this->keyTransformer, $offset);

$this->ensureSafety();

// $this->getRealCollection()->offsetUnset($offset);
Expand Down
26 changes: 26 additions & 0 deletions packages/collections-contracts/src/Recollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,30 @@ public function remove(mixed $key): mixed;
*/
#[\Override]
public function set(mixed $key, mixed $value): void;

/**
* @param mixed $offset
*/
#[\Override]
public function offsetExists(mixed $offset): bool;

/**
* @param mixed $offset
* @return T|null
*/
#[\Override]
public function offsetGet(mixed $offset): mixed;

/**
* @param mixed $offset
* @param T $value
*/
#[\Override]
public function offsetSet(mixed $offset, mixed $value): void;

/**
* @param mixed $offset
*/
#[\Override]
public function offsetUnset(mixed $offset): void;
}
4 changes: 4 additions & 0 deletions packages/collections-orm/src/AbstractRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Rekalogika\Collections\ORM\Trait\RepositoryTrait;
use Rekalogika\Contracts\Collections\Repository;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;
use Rekalogika\Domain\Collections\Common\Trait\SafeCollectionTrait;

/**
Expand Down Expand Up @@ -81,6 +82,8 @@ abstract class AbstractRepository implements Repository
*/
private readonly ?int $hardLimit;

private readonly ?KeyTransformer $keyTransformer;

final public function __construct(
private EntityManagerInterface $entityManager,
) {
Expand All @@ -92,6 +95,7 @@ final public function __construct(
$this->softLimit = $configuration->getSoftLimit();
$this->hardLimit = $configuration->getHardLimit();
$this->orderBy = $configuration->getOrderBy();
$this->keyTransformer = $configuration->getKeyTransformer();

// set query builder
$criteria = Criteria::create()->orderBy($configuration->getOrderBy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\Order;
use Rekalogika\Domain\Collections\Common\Count\CountStrategy;
use Rekalogika\Domain\Collections\Common\KeyTransformer\KeyTransformer;

/**
* @template T of object
Expand All @@ -37,6 +38,7 @@ public function __construct(
?CountStrategy $count = null,
private readonly ?int $softLimit = null,
private readonly ?int $hardLimit = null,
private readonly ?KeyTransformer $keyTransformer = null,
) {
parent::__construct(
class: $class,
Expand All @@ -62,4 +64,12 @@ public function getHardLimit(): ?int
{
return $this->hardLimit;
}

/**
* @return null|KeyTransformer
*/
public function getKeyTransformer(): ?KeyTransformer
{
return $this->keyTransformer;
}
}
18 changes: 14 additions & 4 deletions packages/collections-orm/src/Trait/RepositoryTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Rekalogika\Collections\ORM\Trait;

use Rekalogika\Contracts\Collections\Exception\InvalidArgumentException;
use Rekalogika\Domain\Collections\Common\Internal\KeyTransformerUtil;

/**
* @template TKey of array-key
Expand Down Expand Up @@ -41,24 +42,30 @@ final public function clear(): void
}

/**
* @param TKey $offset
* @param mixed $offset
*/
final public function offsetExists(mixed $offset): bool
{
/** @var TKey */
$offset = KeyTransformerUtil::transformInputToKey($this->keyTransformer, $offset);

return $this->containsKey($offset);
}

/**
* @param TKey $offset
* @param mixed $offset
* @return T|null
*/
final public function offsetGet(mixed $offset): mixed
{
/** @var TKey */
$offset = KeyTransformerUtil::transformInputToKey($this->keyTransformer, $offset);

return $this->get($offset);
}

/**
* @param TKey|null $offset
* @param mixed $offset
* @param T $value
*/
final public function offsetSet(mixed $offset, mixed $value): void
Expand All @@ -71,10 +78,13 @@ final public function offsetSet(mixed $offset, mixed $value): void
}

/**
* @param TKey $offset
* @param mixed $offset
*/
final public function offsetUnset(mixed $offset): void
{
/** @var TKey */
$offset = KeyTransformerUtil::transformInputToKey($this->keyTransformer, $offset);

$this->remove($offset);
}

Expand Down

0 comments on commit 36a21c3

Please sign in to comment.