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
4 changes: 3 additions & 1 deletion phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,7 @@
<file>src/</file>
<file>tests/</file>

<rule ref="Respect" />
<rule ref="Respect">
<exclude name="SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming.SuperfluousPrefix" />
</rule>
</ruleset>
57 changes: 32 additions & 25 deletions src/AbstractMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,30 @@

abstract class AbstractMapper
{
protected ?Styles\Stylable $style = null;
protected Styles\Stylable|null $style = null;

protected SplObjectStorage $new;

protected SplObjectStorage $tracked;

protected SplObjectStorage $changed;

protected SplObjectStorage $removed;

/** @var array<string, Collection> */
protected array $collections = [];

abstract protected function createStatement(Collection $fromCollection, mixed $withExtra = null): mixed;

protected function parseHydrated(SplObjectStorage $hydrated): mixed
public function __construct()
{
$this->tracked->addAll($hydrated);
$hydrated->rewind();

return $hydrated->current();
$this->tracked = new SplObjectStorage();
$this->changed = new SplObjectStorage();
$this->removed = new SplObjectStorage();
$this->new = new SplObjectStorage();
}

public function getStyle(): Styles\Stylable
{
if (null === $this->style) {
if ($this->style === null) {
$this->setStyle(new Styles\Standard());
}

Expand All @@ -43,14 +46,6 @@ public function setStyle(Styles\Stylable $style): static
return $this;
}

public function __construct()
{
$this->tracked = new SplObjectStorage();
$this->changed = new SplObjectStorage();
$this->removed = new SplObjectStorage();
$this->new = new SplObjectStorage();
}

abstract public function flush(): void;

public function reset(): void
Expand Down Expand Up @@ -78,6 +73,7 @@ public function fetch(Collection $fromCollection, mixed $withExtra = null): mixe
return $this->parseHydrated($hydrated);
}

/** @return array<int, mixed> */
public function fetchAll(Collection $fromCollection, mixed $withExtra = null): array
{
$statement = $this->createStatement($fromCollection, $withExtra);
Expand Down Expand Up @@ -123,13 +119,29 @@ public function isTracked(object $entity): bool
return $this->tracked->offsetExists($entity);
}

public function registerCollection(string $alias, Collection $collection): void
{
$collection->setMapper($this);
$this->collections[$alias] = $collection;
}

abstract protected function createStatement(Collection $fromCollection, mixed $withExtra = null): mixed;

protected function parseHydrated(SplObjectStorage $hydrated): mixed
{
$this->tracked->addAll($hydrated);
$hydrated->rewind();

return $hydrated->current();
}

protected function fetchHydrated(Collection $collection, mixed $statement): SplObjectStorage|false
{
if (!$collection->hasMore()) {
return $this->fetchSingle($collection, $statement);
} else {
return $this->fetchMulti($collection, $statement);
}

return $this->fetchMulti($collection, $statement);
}

public function __get(string $name): Collection
Expand All @@ -154,17 +166,12 @@ public function __set(string $alias, mixed $collection): void
$this->registerCollection($alias, $collection);
}

/** @param array<int, mixed> $children */
public function __call(string $name, array $children): Collection
{
$collection = Collection::__callstatic($name, $children);
$collection->setMapper($this);

return $collection;
}

public function registerCollection(string $alias, Collection $collection): void
{
$collection->setMapper($this);
$this->collections[$alias] = $collection;
}
}
16 changes: 10 additions & 6 deletions src/CollectionIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,32 @@
use RecursiveArrayIterator;
use RecursiveIteratorIterator;

use function is_array;

final class CollectionIterator extends RecursiveArrayIterator
{
/** @var array<string, int> */
protected array $namesCounts = [];

public static function recursive(mixed $target): RecursiveIteratorIterator
{
return new RecursiveIteratorIterator(new static($target), 1);
}

/** @param array<string, int> $namesCounts */
public function __construct(mixed $target = [], array &$namesCounts = [])
{
$this->namesCounts = &$namesCounts;

parent::__construct(is_array($target) ? $target : [$target]);
}

public static function recursive(mixed $target): RecursiveIteratorIterator
{
return new RecursiveIteratorIterator(new static($target), 1);
}

public function key(): string|int|null
{
$name = $this->current()->getName();

if (isset($this->namesCounts[$name])) {
return $name.++$this->namesCounts[$name];
return $name . ++$this->namesCounts[$name];
}

$this->namesCounts[$name] = 1;
Expand Down
126 changes: 66 additions & 60 deletions src/Collections/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,33 @@

namespace Respect\Data\Collections;

use Respect\Data\AbstractMapper;
use ArrayAccess;
use Respect\Data\AbstractMapper;
use RuntimeException;

class Collection implements ArrayAccess
{
protected bool $required = true;
protected ?AbstractMapper $mapper = null;
protected ?string $name = null;
protected mixed $condition = [];
protected ?Collection $parent = null;
protected ?Collection $next = null;
protected ?Collection $last = null;

protected AbstractMapper|null $mapper = null;

protected Collection|null $parent = null;

protected Collection|null $next = null;

protected Collection|null $last = null;

/** @var Collection[] */
protected array $children = [];

/** @var array<string, mixed> */
protected array $extras = [];

public function __construct(protected string|null $name = null, protected mixed $condition = [])
{
$this->last = $this;
}

public function extra(string $name, mixed $specs): static
{
$this->extras[$name] = $specs;
Expand Down Expand Up @@ -50,49 +60,6 @@ public static function using(mixed $condition): static
return $collection;
}

public static function __callStatic(string $name, array $children): static
{
$collection = new static();

return $collection->__call($name, $children);
}

public function __construct(?string $name = null, mixed $condition = [])
{
$this->name = $name;
$this->condition = $condition;
$this->last = $this;
}

public function __get(string $name): static
{
if (isset($this->mapper) && isset($this->mapper->$name)) {
return $this->stack(clone $this->mapper->$name);
}

return $this->stack(new self($name));
}

public function __call(string $name, array $children): static
{
if (!isset($this->name)) {
$this->name = $name;
foreach ($children as $child) {
if ($child instanceof Collection) {
$this->addChild($child);
} else {
$this->setCondition($child);
}
}

return $this;
}

$collection = self::__callStatic($name, $children);

return $this->stack($collection);
}

public function addChild(Collection $child): void
{
$clone = clone $child;
Expand All @@ -105,7 +72,7 @@ public function addChild(Collection $child): void
public function persist(object $object): mixed
{
if (!$this->mapper) {
throw new \RuntimeException();
throw new RuntimeException();
}

return $this->mapper->persist($object, $this);
Expand All @@ -114,7 +81,7 @@ public function persist(object $object): mixed
public function remove(object $object): mixed
{
if (!$this->mapper) {
throw new \RuntimeException();
throw new RuntimeException();
}

return $this->mapper->remove($object, $this);
Expand All @@ -123,7 +90,7 @@ public function remove(object $object): mixed
public function fetch(mixed $extra = null): mixed
{
if (!$this->mapper) {
throw new \RuntimeException();
throw new RuntimeException();
}

return $this->mapper->fetch($this, $extra);
Expand All @@ -132,7 +99,7 @@ public function fetch(mixed $extra = null): mixed
public function fetchAll(mixed $extra = null): mixed
{
if (!$this->mapper) {
throw new \RuntimeException();
throw new RuntimeException();
}

return $this->mapper->fetchAll($this, $extra);
Expand All @@ -149,22 +116,22 @@ public function getCondition(): mixed
return $this->condition;
}

public function getName(): ?string
public function getName(): string|null
{
return $this->name;
}

public function getNext(): ?Collection
public function getNext(): Collection|null
{
return $this->next;
}

public function getParentName(): ?string
public function getParentName(): string|null
{
return $this->parent ? $this->parent->getName() : null;
}

public function getNextName(): ?string
public function getNextName(): string|null
{
return $this->next ? $this->next->getName() : null;
}
Expand All @@ -181,7 +148,7 @@ public function hasMore(): bool

public function hasNext(): bool
{
return !is_null($this->next);
return $this->next !== null;
}

public function isRequired(): bool
Expand Down Expand Up @@ -216,11 +183,12 @@ public function setCondition(mixed $condition): void
$this->condition = $condition;
}

public function setMapper(?AbstractMapper $mapper = null): void
public function setMapper(AbstractMapper|null $mapper = null): void
{
foreach ($this->children as $child) {
$child->setMapper($mapper);
}

$this->mapper = $mapper;
}

Expand Down Expand Up @@ -248,4 +216,42 @@ public function stack(Collection $collection): static

return $this;
}

/** @param array<int, mixed> $children */
public static function __callStatic(string $name, array $children): static
{
$collection = new static();

return $collection->__call($name, $children);
}

public function __get(string $name): static
{
if (isset($this->mapper) && isset($this->mapper->$name)) {
return $this->stack(clone $this->mapper->$name);
}

return $this->stack(new self($name));
}

/** @param array<int, mixed> $children */
public function __call(string $name, array $children): static
{
if (!isset($this->name)) {
$this->name = $name;
foreach ($children as $child) {
if ($child instanceof Collection) {
$this->addChild($child);
} else {
$this->setCondition($child);
}
}

return $this;
}

$collection = self::__callStatic($name, $children);

return $this->stack($collection);
}
}
1 change: 1 addition & 0 deletions src/Collections/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
interface Filterable
{
public function getFilters(Collection $collection): mixed;

public function filterable(Collection $collection): mixed;
}
Loading
Loading