premier commit

This commit is contained in:
Angle
2026-01-16 16:29:06 +01:00
commit a58748f0cd
1082 changed files with 156212 additions and 0 deletions
@@ -0,0 +1,217 @@
<?php declare(strict_types=1);
namespace Grav\Framework\Relationships;
use Grav\Framework\Contracts\Object\IdentifierInterface;
use Grav\Framework\Contracts\Relationships\RelationshipInterface;
use Grav\Framework\Contracts\Relationships\RelationshipsInterface;
use Grav\Framework\Flex\FlexIdentifier;
use RuntimeException;
use function count;
/**
* Class Relationships
*
* @template T of \Grav\Framework\Contracts\Object\IdentifierInterface
* @template P of \Grav\Framework\Contracts\Object\IdentifierInterface
* @implements RelationshipsInterface<T,P>
*/
class Relationships implements RelationshipsInterface
{
/** @var P */
protected $parent;
/** @var array */
protected $options;
/** @var RelationshipInterface<T,P>[] */
protected $relationships;
/**
* Relationships constructor.
* @param P $parent
* @param array $options
*/
public function __construct(IdentifierInterface $parent, array $options)
{
$this->parent = $parent;
$this->options = $options;
$this->relationships = [];
}
/**
* @return bool
* @phpstan-pure
*/
public function isModified(): bool
{
return !empty($this->getModified());
}
/**
* @return RelationshipInterface<T,P>[]
* @phpstan-pure
*/
public function getModified(): array
{
$list = [];
foreach ($this->relationships as $name => $relationship) {
if ($relationship->isModified()) {
$list[$name] = $relationship;
}
}
return $list;
}
/**
* @return int
* @phpstan-pure
*/
public function count(): int
{
return count($this->options);
}
/**
* @param string $offset
* @return bool
* @phpstan-pure
*/
public function offsetExists($offset): bool
{
return isset($this->options[$offset]);
}
/**
* @param string $offset
* @return RelationshipInterface<T,P>|null
*/
public function offsetGet($offset): ?RelationshipInterface
{
if (!isset($this->relationships[$offset])) {
$options = $this->options[$offset] ?? null;
if (null === $options) {
return null;
}
$this->relationships[$offset] = $this->createRelationship($offset, $options);
}
return $this->relationships[$offset];
}
/**
* @param string $offset
* @param mixed $value
* @return never-return
*/
public function offsetSet($offset, $value)
{
throw new RuntimeException('Setting relationship is not supported', 500);
}
/**
* @param string $offset
* @return never-return
*/
public function offsetUnset($offset)
{
throw new RuntimeException('Removing relationship is not allowed', 500);
}
/**
* @return RelationshipInterface<T,P>|null
*/
public function current(): ?RelationshipInterface
{
$name = key($this->options);
if ($name === null) {
return null;
}
return $this->offsetGet($name);
}
/**
* @return string
* @phpstan-pure
*/
public function key(): string
{
return key($this->options);
}
/**
* @return void
* @phpstan-pure
*/
public function next(): void
{
next($this->options);
}
/**
* @return void
* @phpstan-pure
*/
public function rewind(): void
{
reset($this->options);
}
/**
* @return bool
* @phpstan-pure
*/
public function valid(): bool
{
return key($this->options) !== null;
}
/**
* @return array
*/
public function jsonSerialize(): array
{
$list = [];
foreach ($this as $name => $relationship) {
$list[$name] = $relationship->jsonSerialize();
}
return $list;
}
/**
* @param string $name
* @param array $options
* @return ToOneRelationship|ToManyRelationship
*/
private function createRelationship(string $name, array $options): RelationshipInterface
{
$data = null;
$parent = $this->parent;
if ($parent instanceof FlexIdentifier) {
$object = $parent->getObject();
if (!method_exists($object, 'initRelationship')) {
throw new RuntimeException(sprintf('Bad relationship %s', $name), 500);
}
$data = $object->initRelationship($name);
}
$cardinality = $options['cardinality'] ?? '';
switch ($cardinality) {
case 'to-one':
$relationship = new ToOneRelationship($parent, $name, $options, $data);
break;
case 'to-many':
$relationship = new ToManyRelationship($parent, $name, $options, $data ?? []);
break;
default:
throw new RuntimeException(sprintf('Bad relationship cardinality %s', $cardinality), 500);
}
return $relationship;
}
}
@@ -0,0 +1,259 @@
<?php declare(strict_types=1);
namespace Grav\Framework\Relationships;
use ArrayIterator;
use Grav\Framework\Compat\Serializable;
use Grav\Framework\Contracts\Object\IdentifierInterface;
use Grav\Framework\Contracts\Relationships\ToManyRelationshipInterface;
use Grav\Framework\Relationships\Traits\RelationshipTrait;
use function count;
use function is_callable;
/**
* Class ToManyRelationship
*
* @template T of IdentifierInterface
* @template P of IdentifierInterface
* @template-implements ToManyRelationshipInterface<T,P>
*/
class ToManyRelationship implements ToManyRelationshipInterface
{
/** @template-use RelationshipTrait<T> */
use RelationshipTrait;
use Serializable;
/** @var IdentifierInterface[] */
protected $identifiers = [];
/**
* ToManyRelationship constructor.
* @param string $name
* @param IdentifierInterface $parent
* @param iterable<IdentifierInterface> $identifiers
*/
public function __construct(IdentifierInterface $parent, string $name, array $options, iterable $identifiers = [])
{
$this->parent = $parent;
$this->name = $name;
$this->parseOptions($options);
$this->addIdentifiers($identifiers);
$this->modified = false;
}
/**
* @return string
* @phpstan-pure
*/
public function getCardinality(): string
{
return 'to-many';
}
/**
* @return int
* @phpstan-pure
*/
public function count(): int
{
return count($this->identifiers);
}
/**
* @return array
*/
public function fetch(): array
{
$list = [];
foreach ($this->identifiers as $identifier) {
if (is_callable([$identifier, 'getObject'])) {
$identifier = $identifier->getObject();
}
$list[] = $identifier;
}
return $list;
}
/**
* @param string $id
* @param string|null $type
* @return bool
* @phpstan-pure
*/
public function has(string $id, string $type = null): bool
{
return $this->getIdentifier($id, $type) !== null;
}
/**
* @param positive-int $pos
* @return IdentifierInterface|null
*/
public function getNthIdentifier(int $pos): ?IdentifierInterface
{
$items = array_keys($this->identifiers);
$key = $items[$pos - 1] ?? null;
if (null === $key) {
return null;
}
return $this->identifiers[$key] ?? null;
}
/**
* @param string $id
* @param string|null $type
* @return IdentifierInterface|null
* @phpstan-pure
*/
public function getIdentifier(string $id, string $type = null): ?IdentifierInterface
{
if (null === $type) {
$type = $this->getType();
}
if ($type === 'media' && !str_contains($id, '/')) {
$name = $this->name;
$id = $this->parent->getType() . '/' . $this->parent->getId() . '/'. $name . '/' . $id;
}
$key = "{$type}/{$id}";
return $this->identifiers[$key] ?? null;
}
/**
* @param string $id
* @param string|null $type
* @return T|null
*/
public function getObject(string $id, string $type = null): ?object
{
$identifier = $this->getIdentifier($id, $type);
if ($identifier && is_callable([$identifier, 'getObject'])) {
$identifier = $identifier->getObject();
}
return $identifier;
}
/**
* @param IdentifierInterface $identifier
* @return bool
*/
public function addIdentifier(IdentifierInterface $identifier): bool
{
return $this->addIdentifiers([$identifier]);
}
/**
* @param IdentifierInterface|null $identifier
* @return bool
*/
public function removeIdentifier(IdentifierInterface $identifier = null): bool
{
return !$identifier || $this->removeIdentifiers([$identifier]);
}
/**
* @param iterable<IdentifierInterface> $identifiers
* @return bool
*/
public function addIdentifiers(iterable $identifiers): bool
{
foreach ($identifiers as $identifier) {
$type = $identifier->getType();
$id = $identifier->getId();
$key = "{$type}/{$id}";
$this->identifiers[$key] = $this->checkIdentifier($identifier);
$this->modified = true;
}
return true;
}
/**
* @param iterable<IdentifierInterface> $identifiers
* @return bool
*/
public function replaceIdentifiers(iterable $identifiers): bool
{
$this->identifiers = [];
$this->modified = true;
return $this->addIdentifiers($identifiers);
}
/**
* @param iterable<IdentifierInterface> $identifiers
* @return bool
*/
public function removeIdentifiers(iterable $identifiers): bool
{
foreach ($identifiers as $identifier) {
$type = $identifier->getType();
$id = $identifier->getId();
$key = "{$type}/{$id}";
unset($this->identifiers[$key]);
$this->modified = true;
}
return true;
}
/**
* @return iterable<IdentifierInterface>
* @phpstan-pure
*/
public function getIterator(): iterable
{
return new ArrayIterator($this->identifiers);
}
/**
* @return array
*/
public function jsonSerialize(): array
{
$list = [];
foreach ($this->getIterator() as $item) {
$list[] = $item->jsonSerialize();
}
return $list;
}
/**
* @return array
*/
public function __serialize(): array
{
return [
'parent' => $this->parent,
'name' => $this->name,
'type' => $this->type,
'options' => $this->options,
'modified' => $this->modified,
'identifiers' => $this->identifiers,
];
}
/**
* @param array $data
* @return void
*/
public function __unserialize(array $data): void
{
$this->parent = $data['parent'];
$this->name = $data['name'];
$this->type = $data['type'];
$this->options = $data['options'];
$this->modified = $data['modified'];
$this->identifiers = $data['identifiers'];
}
}
@@ -0,0 +1,207 @@
<?php declare(strict_types=1);
namespace Grav\Framework\Relationships;
use ArrayIterator;
use Grav\Framework\Compat\Serializable;
use Grav\Framework\Contracts\Object\IdentifierInterface;
use Grav\Framework\Contracts\Relationships\ToOneRelationshipInterface;
use Grav\Framework\Relationships\Traits\RelationshipTrait;
use function is_callable;
/**
* Class ToOneRelationship
*
* @template T of IdentifierInterface
* @template P of IdentifierInterface
* @template-implements ToOneRelationshipInterface<T,P>
*/
class ToOneRelationship implements ToOneRelationshipInterface
{
/** @template-use RelationshipTrait<T> */
use RelationshipTrait;
use Serializable;
/** @var IdentifierInterface|null */
protected $identifier = null;
public function __construct(IdentifierInterface $parent, string $name, array $options, IdentifierInterface $identifier = null)
{
$this->parent = $parent;
$this->name = $name;
$this->parseOptions($options);
$this->replaceIdentifier($identifier);
$this->modified = false;
}
/**
* @return string
* @phpstan-pure
*/
public function getCardinality(): string
{
return 'to-one';
}
/**
* @return int
* @phpstan-pure
*/
public function count(): int
{
return $this->identifier ? 1 : 0;
}
/**
* @return object|null
*/
public function fetch(): ?object
{
$identifier = $this->identifier;
if (is_callable([$identifier, 'getObject'])) {
$identifier = $identifier->getObject();
}
return $identifier;
}
/**
* @param string|null $id
* @param string|null $type
* @return bool
* @phpstan-pure
*/
public function has(string $id = null, string $type = null): bool
{
return $this->getIdentifier($id, $type) !== null;
}
/**
* @param string|null $id
* @param string|null $type
* @return IdentifierInterface|null
* @phpstan-pure
*/
public function getIdentifier(string $id = null, string $type = null): ?IdentifierInterface
{
if ($id && $this->getType() === 'media' && !str_contains($id, '/')) {
$name = $this->name;
$id = $this->parent->getType() . '/' . $this->parent->getId() . '/'. $name . '/' . $id;
}
$identifier = $this->identifier ?? null;
if (null === $identifier || ($type && $type !== $identifier->getType()) || ($id && $id !== $identifier->getId())) {
return null;
}
return $identifier;
}
/**
* @param string|null $id
* @param string|null $type
* @return T|null
*/
public function getObject(string $id = null, string $type = null): ?object
{
$identifier = $this->getIdentifier($id, $type);
if ($identifier && is_callable([$identifier, 'getObject'])) {
$identifier = $identifier->getObject();
}
return $identifier;
}
/**
* @param IdentifierInterface $identifier
* @return bool
*/
public function addIdentifier(IdentifierInterface $identifier): bool
{
$this->identifier = $this->checkIdentifier($identifier);
$this->modified = true;
return true;
}
/**
* @param IdentifierInterface|null $identifier
* @return bool
*/
public function replaceIdentifier(IdentifierInterface $identifier = null): bool
{
if ($identifier === null) {
$this->identifier = null;
$this->modified = true;
return true;
}
return $this->addIdentifier($identifier);
}
/**
* @param IdentifierInterface|null $identifier
* @return bool
*/
public function removeIdentifier(IdentifierInterface $identifier = null): bool
{
if (null === $identifier || $this->has($identifier->getId(), $identifier->getType())) {
$this->identifier = null;
$this->modified = true;
return true;
}
return false;
}
/**
* @return iterable<IdentifierInterface>
* @phpstan-pure
*/
public function getIterator(): iterable
{
return new ArrayIterator((array)$this->identifier);
}
/**
* @return array|null
*/
public function jsonSerialize(): ?array
{
return $this->identifier ? $this->identifier->jsonSerialize() : null;
}
/**
* @return array
*/
public function __serialize(): array
{
return [
'parent' => $this->parent,
'name' => $this->name,
'type' => $this->type,
'options' => $this->options,
'modified' => $this->modified,
'identifier' => $this->identifier,
];
}
/**
* @param array $data
* @return void
*/
public function __unserialize(array $data): void
{
$this->parent = $data['parent'];
$this->name = $data['name'];
$this->type = $data['type'];
$this->options = $data['options'];
$this->modified = $data['modified'];
$this->identifier = $data['identifier'];
}
}
@@ -0,0 +1,128 @@
<?php declare(strict_types=1);
namespace Grav\Framework\Relationships\Traits;
use Grav\Framework\Contracts\Object\IdentifierInterface;
use Grav\Framework\Flex\FlexIdentifier;
use Grav\Framework\Media\MediaIdentifier;
use Grav\Framework\Object\Identifiers\Identifier;
use RuntimeException;
use function get_class;
/**
* Trait RelationshipTrait
*
* @template T of object
*/
trait RelationshipTrait
{
/** @var IdentifierInterface */
protected $parent;
/** @var string */
protected $name;
/** @var string */
protected $type;
/** @var array */
protected $options;
/** @var bool */
protected $modified = false;
/**
* @return string
* @phpstan-pure
*/
public function getName(): string
{
return $this->name;
}
/**
* @return string
* @phpstan-pure
*/
public function getType(): string
{
return $this->type;
}
/**
* @return bool
* @phpstan-pure
*/
public function isModified(): bool
{
return $this->modified;
}
/**
* @return IdentifierInterface
* @phpstan-pure
*/
public function getParent(): IdentifierInterface
{
return $this->parent;
}
/**
* @param IdentifierInterface $identifier
* @return bool
* @phpstan-pure
*/
public function hasIdentifier(IdentifierInterface $identifier): bool
{
return $this->getIdentifier($identifier->getId(), $identifier->getType()) !== null;
}
/**
* @return int
* @phpstan-pure
*/
abstract public function count(): int;
/**
* @return void
* @phpstan-pure
*/
public function check(): void
{
$min = $this->options['min'] ?? 0;
$max = $this->options['max'] ?? 0;
if ($min || $max) {
$count = $this->count();
if ($min && $count < $min) {
throw new RuntimeException(sprintf('%s relationship has too few objects in it', $this->name));
}
if ($max && $count > $max) {
throw new RuntimeException(sprintf('%s relationship has too many objects in it', $this->name));
}
}
}
/**
* @param IdentifierInterface $identifier
* @return IdentifierInterface
*/
private function checkIdentifier(IdentifierInterface $identifier): IdentifierInterface
{
if ($this->type !== $identifier->getType()) {
throw new RuntimeException(sprintf('Bad identifier type %s', $identifier->getType()));
}
if (get_class($identifier) !== Identifier::class) {
return $identifier;
}
if ($this->type === 'media') {
return new MediaIdentifier($identifier->getId());
}
return new FlexIdentifier($identifier->getId(), $identifier->getType());
}
private function parseOptions(array $options): void
{
$this->type = $options['type'];
$this->options = $options;
}
}