Skip to content

Commit

Permalink
fix: reorder class members to public > private
Browse files Browse the repository at this point in the history
  • Loading branch information
OliverSkroblin committed Mar 12, 2024
1 parent 5c1b07a commit 4305d88
Show file tree
Hide file tree
Showing 22 changed files with 393 additions and 405 deletions.
1 change: 1 addition & 0 deletions .php-cs-fixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
'trailing_comma_in_multiline' => true,
'array_syntax' => ['syntax' => 'short'],
'method_argument_space' => false,
'ordered_class_elements' => ['order' => ['use_trait', 'constant', 'property', 'construct', 'destruct', 'magic', 'phpunit', 'public', 'protected', 'private']],
])->setFinder($finder);
10 changes: 5 additions & 5 deletions src/Array/ArrayKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ class ArrayKeyStorage implements Storage
*/
private array $storage = [];

public function setup(): void
{
$this->storage = [];
}

public function destroy(): void
{
$this->storage = [];
Expand Down Expand Up @@ -57,9 +62,4 @@ public function mget(array $keys, StorageContext $context): Documents

return $documents;
}

public function setup(): void
{
$this->storage = [];
}
}
6 changes: 3 additions & 3 deletions src/Array/ArrayStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ public function __construct(
private readonly Collection $collection
) {}

public function clear(): void
public function setup(): void
{
$this->storage = [];
}

public function destroy(): void
public function clear(): void
{
$this->storage = [];
}

public function setup(): void
public function destroy(): void
{
$this->storage = [];
}
Expand Down
45 changes: 17 additions & 28 deletions src/Common/Aggregation/AggregationCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,36 +15,25 @@ public function cast(Collection $collection, Aggregation $aggregation, mixed $da
{
$type = SchemaUtil::type(collection: $collection, accessor: $aggregation->field);

switch ($type) {
case FieldType::INT:
// cast to float, because of avg aggregation
$caster = fn($value) => (float) $value;
break;
case FieldType::FLOAT:
$caster = fn($value) => round((float) $value, 6);
break;
case FieldType::BOOL:
$caster = function ($value) {
return match (true) {
$value === '1', $value === 'true' => true,
$value === '0', $value === 'false' => false,
default => (bool) $value
};
$caster = match ($type) {
FieldType::INT => fn($value) => (float) $value,
FieldType::FLOAT => fn($value) => round((float) $value, 6),
FieldType::BOOL => function ($value) {
return match (true) {
$value === '1', $value === 'true' => true,
$value === '0', $value === 'false' => false,
default => (bool) $value
};
break;
case FieldType::DATETIME:

$caster = function ($value) {
return match (true) {
is_string($value) => (new \DateTimeImmutable($value))->format('Y-m-d H:i:s'),
is_int($value) => (new \DateTimeImmutable('@' . $value))->format('Y-m-d H:i:s'),
default => $value
};
},
FieldType::DATETIME => function ($value) {
return match (true) {
is_string($value) => (new \DateTimeImmutable($value))->format('Y-m-d H:i:s'),
is_int($value) => (new \DateTimeImmutable('@' . $value))->format('Y-m-d H:i:s'),
default => $value
};
break;
default:
$caster = fn($value) => $value;
}
},
default => fn($value) => $value,
};

if ($aggregation instanceof Distinct) {
assert(is_array($data), 'Distinct aggregation must return an array');
Expand Down
32 changes: 16 additions & 16 deletions src/Common/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,6 @@ public function __construct(
public string $key
) {}

/**
* @return array<string, mixed>
*/
public function encode(): array
{
$data = json_decode(json_encode($this, self::JSON_OPTIONS), true);

if (!is_array($data)) {
throw new \RuntimeException(sprintf('Error: Failed to encode document class %s with key %s', static::class, $this->key));
}

$data['key'] = $this->key;

return $data;
}

/**
* @param array<mixed> $arguments
*/
Expand All @@ -55,4 +39,20 @@ public function __call(string $name, array $arguments): mixed
sprintf('Error: Call to undefined method %s::%s()', static::class, $name)
);
}

/**
* @return array<string, mixed>
*/
public function encode(): array
{
$data = json_decode(json_encode($this, self::JSON_OPTIONS), true);

if (!is_array($data)) {
throw new \RuntimeException(sprintf('Error: Failed to encode document class %s with key %s', static::class, $this->key));
}

$data['key'] = $this->key;

return $data;
}
}
14 changes: 7 additions & 7 deletions src/Common/Schema/SchemaUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ public static function type(Collection $collection, string $accessor, bool $inne
return $schema->type;
}

public static function searchable(Collection $collection, string $accessor): bool
{
$field = self::field(collection: $collection, accessor: $accessor);

return $field->searchable;
}

private static function field(Collection $collection, string $accessor): Field
{
$property = self::property(accessor: $accessor);
Expand Down Expand Up @@ -101,11 +108,4 @@ private static function field(Collection $collection, string $accessor): Field

return $field;
}

public static function searchable(Collection $collection, string $accessor): bool
{
$field = self::field(collection: $collection, accessor: $accessor);

return $field->searchable;
}
}
11 changes: 5 additions & 6 deletions src/Common/Storage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

interface Storage
{
/**
* Creates the storage
* @return void
*/
public function setup(): void;
/**
* @param array<string> $keys
*/
Expand All @@ -26,12 +31,6 @@ public function remove(array $keys): void;
*/
public function store(Documents $documents): void;

/**
* Creates the storage
* @return void
*/
public function setup(): void;

public function clear(): void;

public function destroy(): void;
Expand Down
10 changes: 5 additions & 5 deletions src/DynamoDB/DynamoDBKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ public function __construct(
private readonly Collection $collection
) {}

public function destroy(): void
{
$this->client->deleteTable(['TableName' => $this->collection->name]);
}

public function setup(): void
{
$table = new CreateTableInput([
Expand All @@ -54,6 +49,11 @@ public function setup(): void
}
}

public function destroy(): void
{
$this->client->deleteTable(['TableName' => $this->collection->name]);
}

public function clear(): void
{
$this->destroy();
Expand Down
24 changes: 12 additions & 12 deletions src/Meilisearch/MeilisearchStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ public function __construct(
private readonly Collection $collection
) {}

public function setup(): void
{
if (!$this->exists()) {
$this->client->createIndex(
uid: $this->collection->name,
options: ['primaryKey' => 'key']
);
}

$this->updateIndex();
}

public function destroy(): void
{
$this->client->deleteIndex($this->collection->name);
Expand Down Expand Up @@ -378,18 +390,6 @@ public function store(Documents $documents): void
$this->index()->addDocuments($data);
}

public function setup(): void
{
if (!$this->exists()) {
$this->client->createIndex(
uid: $this->collection->name,
options: ['primaryKey' => 'key']
);
}

$this->updateIndex();
}

public function index(): Indexes
{
return $this->client->index($this->collection->name);
Expand Down
4 changes: 2 additions & 2 deletions src/MongoDB/MongoDBKeyStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ public function __construct(
private readonly Client $client
) {}

public function setup(): void {}

public function clear(): void
{
$this->collection()->deleteMany([]);
Expand Down Expand Up @@ -106,8 +108,6 @@ public function store(Documents $documents): void
$this->collection()->insertMany(array_values($items));
}

public function setup(): void {}

private function collection(): Collection
{
return $this->client
Expand Down
Loading

0 comments on commit 4305d88

Please sign in to comment.