Skip to content

Commit

Permalink
Start clean Meta feature
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitriBouteille committed Jan 19, 2024
1 parent 285d9c2 commit c3a94c3
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 138 deletions.
5 changes: 5 additions & 0 deletions src/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class Comment extends AbstractModel implements CommentInterface
*/
protected $table = 'comments';

/**
* @inheritDoc
*/
protected $guarded = [];

/**
* @inheritDoc
*/
Expand Down
6 changes: 4 additions & 2 deletions src/Models/CustomPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ abstract class CustomPost extends Post implements CustomModelTypeInterface
*/
public function __construct(array $attributes = [])
{
$this->attributes = [
$this->setRawAttributes([
self::TYPE => $this->_type,
];
]);

parent::__construct($attributes);
}
Expand Down Expand Up @@ -68,9 +68,11 @@ final public function setPostType(string $type): never

/**
* @return string|null
* @deprecated Remove in next version - Use constant
*/
public static function type(): ?string
{
// @phpstan-ignore-next-line
return (new static())->getPostType();
}

Expand Down
23 changes: 4 additions & 19 deletions src/Models/Meta/AbstractMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Dbout\WpOrm\Orm\AbstractModel;

abstract class AbstractMeta extends AbstractModel implements MetaInterface
abstract class AbstractMeta extends AbstractModel
{
final public const META_KEY = 'meta_key';
final public const META_VALUE = 'meta_value';
Expand All @@ -22,10 +22,11 @@ abstract class AbstractMeta extends AbstractModel implements MetaInterface
public $timestamps = false;

/**
* @var array
* @inheritDoc
*/
protected $fillable = [
self::META_VALUE, self::META_KEY,
self::META_VALUE,
self::META_KEY,
];

/**
Expand Down Expand Up @@ -63,20 +64,4 @@ public function setValue(string $value): self
$this->setAttribute(self::META_VALUE, $value);
return $this;
}

/**
* @return string
*/
public function getKeyColumn(): string
{
return self::META_KEY;
}

/**
* @return string
*/
public function getValueColumn(): string
{
return self::META_VALUE;
}
}
7 changes: 3 additions & 4 deletions src/Models/Meta/MetaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ interface MetaInterface
/**
* @return string
*/
public function getFkColumn(): string;

public static function getMetaFkColumn(): string;

/**
* @return string
*/
public function getKeyColumn(): string;
public static function getMetaKeyColumn(): string;

/**
* @return string
*/
public function getValueColumn(): string;
public static function getMetaValueColumn(): string;
}
11 changes: 2 additions & 9 deletions src/Models/Meta/PostMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Dbout\WpOrm\Models\Meta;

use Dbout\WpOrm\Api\PostInterface;
use Dbout\WpOrm\Models\Post;
use Illuminate\Database\Eloquent\Relations\HasOne;

Expand Down Expand Up @@ -35,14 +36,6 @@ class PostMeta extends AbstractMeta
*/
public function post(): HasOne
{
return $this->hasOne(Post::class, Post::POST_ID, self::POST_ID);
}

/**
* @inheritDoc
*/
public function getFkColumn(): string
{
return self::POST_ID;
return $this->hasOne(Post::class, PostInterface::POST_ID, self::POST_ID);
}
}
11 changes: 2 additions & 9 deletions src/Models/Meta/UserMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Dbout\WpOrm\Models\Meta;

use Dbout\WpOrm\Api\UserInterface;
use Dbout\WpOrm\Models\User;
use Illuminate\Database\Eloquent\Relations\HasOne;

Expand Down Expand Up @@ -35,14 +36,6 @@ class UserMeta extends AbstractMeta
*/
public function user(): HasOne
{
return $this->hasOne(User::class, User::USER_ID, self::USER_ID);
}

/**
* @return string
*/
public function getFkColumn(): string
{
return self::USER_ID;
return $this->hasOne(User::class, UserInterface::USER_ID, self::USER_ID);
}
}
88 changes: 51 additions & 37 deletions src/Models/Meta/WithMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@

namespace Dbout\WpOrm\Models\Meta;

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

trait WithMeta
{
/**
* @var AbstractMeta|null
*/
protected ?AbstractMeta $metaModel = null;
protected array $metaConfig = [
'class' => '',
'columnKey' => '',
'columnValue' => '',
'foreignKey' => '',
];

/**
* @var array
Expand All @@ -26,38 +28,32 @@ trait WithMeta
/**
* @return void
*/
protected static function bootWithMeta()
protected static function bootWithMeta(): void
{
static::saved(function ($model) {
$model->saveTmpMetas();
});
}

/**
* @throws MetaNotSupportedException
* @throws \ReflectionException
* @throws WpOrmException
* @return void
*/
public function initializeWithMeta(): void
{
$metaClass = $this->getMetaClass();
$object = (new \ReflectionClass($metaClass));
if (!$object->implementsInterface(MetaInterface::class)) {
throw new MetaNotSupportedException(sprintf(
"Model %s must be implement %s",
$metaClass,
MetaInterface::class
));
foreach ($this->metaConfig as $optionKey => $optionValue) {
if ($optionValue === null || $optionValue === '') {
throw new WpOrmException(sprintf('Please define %s key in metaConfig property.', $optionKey));
}
}

$this->metaModel = $object->newInstanceWithoutConstructor();
}

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

/**
Expand All @@ -66,26 +62,24 @@ public function metas(): HasMany
*/
public function getMeta(string $metaKey): ?AbstractMeta
{
return $this->metas()
->firstWhere($this->metaModel->getKeyColumn(), $metaKey);
/** @var ?AbstractMeta $value */
$value = $this->metas()->firstWhere($this->getMetaColumnKey(), $metaKey);
return $value;
}

/**
* @param string $metaKey
* @return mixed|null
*/
public function getMetaValue(string $metaKey)
public function getMetaValue(string $metaKey): mixed
{
if (!$this->exists) {
return $this->_tmpMetas[$metaKey] ?? null;
}

$meta = $this->getMeta($metaKey);
if (!$meta) {
return null;
}
return $meta?->getValue();

return $meta->getValue();
}

/**
Expand All @@ -95,29 +89,30 @@ public function getMetaValue(string $metaKey)
public function hasMeta(string $metaKey): bool
{
return $this->metas()
->where($this->metaModel->getKeyColumn(), $metaKey)
->where($this->getMetaColumnKey(), $metaKey)
->exists();
}

/**
* @param string $metaKey
* @param $value
* @param mixed $value
* @return AbstractMeta|null
*/
public function setMeta(string $metaKey, $value): ?AbstractMeta
public function setMeta(string $metaKey, mixed $value): ?AbstractMeta
{
if (!$this->exists) {
$this->_tmpMetas[$metaKey] = $value;
return null;
}

/** @var AbstractMeta $instance */
$instance = $this->metas()
->firstOrNew([
$this->metaModel->getKeyColumn() => $metaKey,
$this->getMetaForeignKey() => $metaKey,
]);

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

return $instance;
Expand All @@ -135,15 +130,10 @@ public function deleteMeta(string $metaKey): bool
}

return $this->metas()
->where($this->metaModel->getKeyColumn(), $metaKey)
->where($this->getMetaColumnKey(), $metaKey)
->forceDelete();
}

/**
* @return string
*/
abstract public function getMetaClass(): string;

/**
* @return void
*/
Expand All @@ -155,4 +145,28 @@ 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'] ?? '';
}
}
7 changes: 1 addition & 6 deletions src/Models/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ class Option extends AbstractModel implements OptionInterface
/**
* @inheritDoc
*/
protected $fillable = [
self::OPTION_ID,
self::NAME,
self::VALUE,
self::AUTOLOAD,
];
protected $guarded = [];

/**
* @inheritDoc
Expand Down
Loading

0 comments on commit c3a94c3

Please sign in to comment.