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
10 changes: 10 additions & 0 deletions src/Contracts/FieldTypes/FieldTypeHandlerContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Atproto\Contracts\FieldTypes;

use Atproto\FieldTypes\Definitions\AbstractDefinition;

interface FieldTypeHandlerContract
{
public function handle($value, AbstractDefinition $definition);
}
11 changes: 11 additions & 0 deletions src/Contracts/FieldTypes/TypePairFactoryContract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Atproto\Contracts\FieldTypes;

use Atproto\FieldTypes\Definitions\AbstractDefinition;

interface TypePairFactoryContract
{
public static function handler(): FieldTypeHandlerContract;
public static function definition(...$args): AbstractDefinition;
}
2 changes: 1 addition & 1 deletion src/Contracts/LexiconContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

interface LexiconContract extends SerializableContract
{
public function nsid(): string;
public static function nsid(): string;
}
13 changes: 13 additions & 0 deletions src/FieldTypes/CastableFields/CastableArrayField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Atproto\FieldTypes\CastableFields;

/**
* @method self|null|int maxLength(?int $maxLength = null)
* @method self|null|int minLength(?int $minLength = null)
* @method self|null|string items(?string $items = null)
* @method self|null|string description(?string $description = null)
*/
class CastableArrayField extends CastableField
{
}
38 changes: 38 additions & 0 deletions src/FieldTypes/CastableFields/CastableField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Atproto\FieldTypes\CastableFields;

use Atproto\Contracts\FieldTypes\FieldTypeHandlerContract;
use Atproto\FieldTypes\Definitions\AbstractDefinition;

class CastableField
{
private FieldTypeHandlerContract $handler;
private AbstractDefinition $definition;

public function __construct(
FieldTypeHandlerContract $handler,
AbstractDefinition $definition
)
{
$this->definition = $definition;
$this->handler = $handler;
}

public function handler(): FieldTypeHandlerContract
{
return $this->handler;
}

public function definition(): AbstractDefinition
{
return $this->definition;
}

public function __call(string $name, array $args): self
{
$this->definition()->$name(...$args);

return $this;
}
}
14 changes: 14 additions & 0 deletions src/FieldTypes/CastableFields/CastableObjectField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Atproto\FieldTypes\CastableFields;

/**
* @method self|null|string description(?string $description = null)
* @method self|null|bool nullable(?bool $nullable = null)
* @method self|array properties(?array $props = null)
* @method self|array required(?array $props = null)
*
*/
class CastableObjectField extends CastableField
{
}
12 changes: 12 additions & 0 deletions src/FieldTypes/CastableFields/CastableUnionField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Atproto\FieldTypes\CastableFields;

/**
* @method self|null|string description(?string $description = null)
* @method array refs()
* @method self|bool closed(?bool $closed = null)
*/
class CastableUnionField extends CastableField
{
}
39 changes: 39 additions & 0 deletions src/FieldTypes/Definitions/AbstractDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Atproto\FieldTypes\Definitions;

use JsonSerializable;

abstract class AbstractDefinition implements JsonSerializable
{
protected array $meta = [];

public function metadata(): array
{
return array_filter($this->meta);
}

public function jsonSerialize(): array
{
return $this->metadata();
}

public function type(): string
{
return $this->meta['type'];
}

/**
* @return static|?string
*/
public function description(?string $description)
{
if (is_null($description)) {
return $this->meta['description'] ?? null;
}

$this->meta['description'] = $description;

return $this;
}
}
67 changes: 67 additions & 0 deletions src/FieldTypes/Definitions/ArrTypeDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Atproto\FieldTypes\Definitions;

use Atproto\Contracts\FieldTypes\FieldTypeHandlerContract;

class ArrTypeDefinition extends AbstractDefinition
{
private FieldTypeHandlerContract $handler;
private AbstractDefinition $definition;

public function __construct(
FieldTypeHandlerContract $handler,
AbstractDefinition $definition
)
{
$this->definition = $definition;
$this->handler = $handler;
$this->meta['type'] = 'array';
}

public function handler(): FieldTypeHandlerContract
{
return $this->handler;
}

public function definition(): AbstractDefinition
{
return $this->definition;
}

/**
* @return static|string
*/
public function items(?string $items = null)
{
if (is_null($items)) {
return $this->meta['items'] ?? null;
}

$this->meta['items'] = $items;

return $this;
}

public function maxLength(?int $maxLength = null)
{
if (is_null($maxLength)) {
return $this->meta['maxLength'] ?? null;
}

$this->meta['maxLength'] = $maxLength;

return $this;
}

public function minLength(?int $minLength = null)
{
if (is_null($minLength)) {
return $this->meta['minLength'] ?? null;
}

$this->meta['minLength'] = $minLength;

return $this;
}
}
62 changes: 62 additions & 0 deletions src/FieldTypes/Definitions/ObjectTypeDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Atproto\FieldTypes\Definitions;

use Atproto\FieldTypes\CastableFields\CastableField;

class ObjectTypeDefinition extends AbstractDefinition
{
private string $target;

public function __construct(
string $target
)
{
$this->target = $target;
$this->meta['type'] = 'object';
}

public function target(): string
{
return $this->target;
}

public function nullable(?bool $nullable = null)
{
if (is_null($nullable)) {
return $this->meta['nullable'] ?? null;
}

$this->meta['nullable'] = $nullable;

return $this;
}

/**
* @param null|array<string, CastableField> $props
*/
public function properties(?array $props = null)
{
if (is_null($props)) {
return $this->meta['properties'] ?? [];
}

$this->meta['properties'] = $props;

return $this;
}

/**
* @param null|array<string, CastableField> $props
*/
public function required(?array $props = null)
{
if (is_null($props)) {
return $this->meta['required'] ?? [];
}

$this->meta['required'] = $props;

return $this;
}
}
43 changes: 43 additions & 0 deletions src/FieldTypes/Definitions/UnionTypeDefinition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Atproto\FieldTypes\Definitions;

use Atproto\Contracts\LexiconContract;
use InvalidArgumentException;

class UnionTypeDefinition extends AbstractDefinition
{
/**
* @param list<class-string<LexiconContract>> $refs
*/
public function __construct(array $refs)
{
$nonLexiconRefs = array_filter($refs, fn ($ref) => ! in_array(LexiconContract::class, class_implements($ref), true));

if (! empty($nonLexiconRefs)) {
throw new InvalidArgumentException(sprintf(
'Each ref must implement LexiconContract. Invalid: %s',
implode(', ', $nonLexiconRefs)
));
}

$this->meta['refs'] = $refs;
$this->meta['type'] = 'union';
}

public function refs()
{
return $this->meta['refs'] ?? [];
}

public function closed(?bool $closed = null)
{
if (is_null($closed)) {
return $this->meta['closed'] ?? false;
}

$this->meta['closed'] = $closed;

return $this;
}
}
33 changes: 33 additions & 0 deletions src/FieldTypes/Factories/ArrTypePairFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Atproto\FieldTypes\Factories;

use Atproto\Contracts\FieldTypes\FieldTypeHandlerContract;
use Atproto\Contracts\FieldTypes\TypePairFactoryContract;
use Atproto\FieldTypes\Definitions\AbstractDefinition;
use Atproto\FieldTypes\Definitions\ArrTypeDefinition;
use Atproto\FieldTypes\Handlers\ArrTypeHandler;

class ArrTypePairFactory implements TypePairFactoryContract
{
public static function handler(): FieldTypeHandlerContract
{
return new ArrTypeHandler();
}

/**
* @param FieldTypeHandlerContract $handler
* @param AbstractDefinition $definition
* @return AbstractDefinition
*/
public static function definition(...$args): AbstractDefinition
{
$parameters = self::definitionParameters(...$args);
return new ArrTypeDefinition(...$parameters);
}

private static function definitionParameters(FieldTypeHandlerContract $handler, AbstractDefinition $definition): array
{
return [$handler, $definition];
}
}
32 changes: 32 additions & 0 deletions src/FieldTypes/Factories/ObjectTypePairFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Atproto\FieldTypes\Factories;

use Atproto\Contracts\FieldTypes\FieldTypeHandlerContract;
use Atproto\Contracts\FieldTypes\TypePairFactoryContract;
use Atproto\FieldTypes\Definitions\AbstractDefinition;
use Atproto\FieldTypes\Definitions\ObjectTypeDefinition;
use Atproto\FieldTypes\Handlers\ObjectTypeHandler;

class ObjectTypePairFactory implements TypePairFactoryContract
{
public static function handler(): FieldTypeHandlerContract
{
return new ObjectTypeHandler();
}

/**
* @param class-string $target
* @return AbstractDefinition
*/
public static function definition(...$args): AbstractDefinition
{
$parameters = self::definitionParameters(...$args);
return new ObjectTypeDefinition(...$parameters);
}

private static function definitionParameters(string $target): array
{
return [$target];
}
}
Loading