Skip to content

Commit

Permalink
WIP clean Meta feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Jan 19, 2024
1 parent c3a94c3 commit 02a9a63
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 59 deletions.
30 changes: 30 additions & 0 deletions src/Attributes/MetaConfigAttribute.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
/**
* Copyright (c) 2024 Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <[email protected]>
*/

namespace Dbout\WpOrm\Attributes;

/**
* @since 3.0.0
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
class MetaConfigAttribute
{
/**
* @param string $metaClass Meta className
* @param string $foreignKey
* @param string $columnKey Column contains the meta key
* @param string $columnValue Column contains the meta value
*/
public function __construct(
public readonly string $metaClass,
public readonly string $foreignKey,
public readonly string $columnKey = 'meta_key',
public readonly string $columnValue = 'meta_value',
) {
}
}
62 changes: 22 additions & 40 deletions src/Models/Meta/WithMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,16 @@

namespace Dbout\WpOrm\Models\Meta;

use Dbout\WpOrm\Attributes\MetaConfigAttribute;
use Dbout\WpOrm\Exceptions\WpOrmException;
use Illuminate\Database\Eloquent\Relations\HasMany;

trait WithMeta
{
protected array $metaConfig = [
'class' => '',
'columnKey' => '',
'columnValue' => '',
'foreignKey' => '',
];
/**
* @var MetaConfigAttribute
*/
protected MetaConfigAttribute $metaConfig;

/**
* @var array
Expand All @@ -41,19 +40,23 @@ protected static function bootWithMeta(): void
*/
public function initializeWithMeta(): void
{
foreach ($this->metaConfig as $optionKey => $optionValue) {
if ($optionValue === null || $optionValue === '') {
throw new WpOrmException(sprintf('Please define %s key in metaConfig property.', $optionKey));
}
$reflection = new \ReflectionClass(static::class);
$configs = $reflection->getAttributes(MetaConfigAttribute::class);
if ($configs === []) {
throw new WpOrmException(sprintf('Please define attribute %s.', MetaConfigAttribute::class));
}

/** @var MetaConfigAttribute $config */
$config = $configs[0];
$this->metaConfig = $config;
}

/**
* @return HasMany
*/
public function metas(): HasMany
{
return $this->hasMany($this->metaConfig['class'], $this->getMetaForeignKey());
return $this->hasMany($this->metaConfig->metaClass, $this->metaConfig->foreignKey);
}

/**
Expand All @@ -63,7 +66,8 @@ public function metas(): HasMany
public function getMeta(string $metaKey): ?AbstractMeta
{
/** @var ?AbstractMeta $value */
$value = $this->metas()->firstWhere($this->getMetaColumnKey(), $metaKey);
// @phpstan-ignore-next-line
$value = $this->metas()->firstWhere($this->metaConfig->columnKey, $metaKey);
return $value;
}

Expand All @@ -88,8 +92,9 @@ public function getMetaValue(string $metaKey): mixed
*/
public function hasMeta(string $metaKey): bool
{
// @phpstan-ignore-next-line
return $this->metas()
->where($this->getMetaColumnKey(), $metaKey)
->where($this->metaConfig->columnKey, $metaKey)
->exists();
}

Expand All @@ -108,11 +113,11 @@ public function setMeta(string $metaKey, mixed $value): ?AbstractMeta
/** @var AbstractMeta $instance */
$instance = $this->metas()
->firstOrNew([
$this->getMetaForeignKey() => $metaKey,
$this->metaConfig->foreignKey => $metaKey,
]);

$instance->fill([
$this->getMetaColumnValue() => $value,
$this->metaConfig->columnValue => $value,
])->save();

return $instance;
Expand All @@ -129,8 +134,9 @@ public function deleteMeta(string $metaKey): bool
return true;
}

// @phpstan-ignore-next-line
return $this->metas()
->where($this->getMetaColumnKey(), $metaKey)
->where($this->metaConfig->columnKey, $metaKey)
->forceDelete();
}

Expand All @@ -145,28 +151,4 @@ protected function saveTmpMetas(): void

$this->_tmpMetas = [];
}

/**
* @return string
*/
protected function getMetaColumnKey(): string
{
return $this->metaConfig['columnKey'] ?? '';
}

/**
* @return string
*/
protected function getMetaColumnValue(): string
{
return $this->metaConfig['columnValue'] ?? '';
}

/**
* @return string
*/
protected function getMetaForeignKey(): string
{
return $this->metaConfig['foreignKey'] ?? '';
}
}
12 changes: 1 addition & 11 deletions src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Dbout\WpOrm\Api\PostInterface;
use Dbout\WpOrm\Api\UserInterface;
use Dbout\WpOrm\Builders\PostBuilder;
use Dbout\WpOrm\Models\Meta\AbstractMeta;
use Dbout\WpOrm\Models\Meta\PostMeta;
use Dbout\WpOrm\Models\Meta\WithMeta;
use Dbout\WpOrm\Orm\AbstractModel;
Expand All @@ -27,6 +26,7 @@
* @property-read static|null $parent
* @property-read Comment[] $comments
*/
#[\Dbout\WpOrm\Attributes\MetaConfigAttribute(PostMeta::class, PostMeta::POST_ID)]
class Post extends AbstractModel implements PostInterface
{
use WithMeta;
Expand Down Expand Up @@ -56,16 +56,6 @@ class Post extends AbstractModel implements PostInterface
self::MODIFIED_GMT => 'datetime',
];

/**
* @var array
*/
protected array $metaConfig = [
'class' => PostMeta::class,
'columnKey' => Meta\AbstractMeta::META_KEY,
'columnValue' => AbstractMeta::META_VALUE,
'foreignKey' => PostMeta::POST_ID,
];

/**
* @var string
*/
Expand Down
9 changes: 1 addition & 8 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use Dbout\WpOrm\Api\PostInterface;
use Dbout\WpOrm\Api\UserInterface;
use Dbout\WpOrm\Builders\UserBuilder;
use Dbout\WpOrm\Models\Meta\AbstractMeta;
use Dbout\WpOrm\Models\Meta\UserMeta;
use Dbout\WpOrm\Models\Meta\WithMeta;
use Dbout\WpOrm\Orm\AbstractModel;
Expand All @@ -25,6 +24,7 @@
* @property-read Comment[] $comments
* @property-read Post[] $posts
*/
#[\Dbout\WpOrm\Attributes\MetaConfigAttribute(UserMeta::class, UserMeta::USER_ID)]
class User extends AbstractModel implements UserInterface
{
use WithMeta;
Expand All @@ -45,13 +45,6 @@ class User extends AbstractModel implements UserInterface
self::REGISTERED => 'datetime',
];

protected array $metaConfig = [
'class' => UserMeta::class,
'columnKey' => AbstractMeta::META_KEY,
'columnValue' => AbstractMeta::META_VALUE,
'foreignKey' => UserMeta::USER_ID,
];

/**
* @inheritDoc
*/
Expand Down

0 comments on commit 02a9a63

Please sign in to comment.