Skip to content

Commit

Permalink
tag
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed Nov 21, 2023
1 parent 5634632 commit 1784b13
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
6 changes: 4 additions & 2 deletions src/Entity/TagMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Windwalker\ORM\EntityTrait;
use Windwalker\ORM\Metadata\EntityMetadata;

use function Windwalker\unwrap_enum;

/**
* The TagMap class.
*/
Expand Down Expand Up @@ -64,9 +66,9 @@ public function getType(): string
return $this->type;
}

public function setType(string $type): static
public function setType(string|\BackedEnum $type): static
{
$this->type = $type;
$this->type = (string) unwrap_enum($type);

return $this;
}
Expand Down
49 changes: 32 additions & 17 deletions src/Services/TagService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Windwalker\Database\Driver\StatementInterface;
use Windwalker\ORM\EntityMapper;
use Windwalker\ORM\ORM;
use Windwalker\ORM\SelectorQuery;
use Windwalker\Utilities\Str;

/**
Expand Down Expand Up @@ -51,35 +52,31 @@ public function createTagsIfNew(iterable $tagIds): array
}

/**
* flushTags
*
* @param string $type
* @param mixed $targetId
* @param iterable $tagIds
* @param string|\BackedEnum $type
* @param mixed $targetId
* @param iterable $tagIds
*
* @return iterable<TagMap>
*
* @throws ReflectionException
*/
public function flushTagMapsFromInput(string $type, mixed $targetId, iterable $tagIds): iterable
public function flushTagMapsFromInput(string|\BackedEnum $type, mixed $targetId, iterable $tagIds): iterable
{
$tagIds = $this->createTagsIfNew($tagIds);

return $this->flushTagMaps($type, $targetId, $tagIds);
}

/**
* flushTags
*
* @param string $type
* @param mixed $targetId
* @param iterable $tagIds
* @param string|\BackedEnum $type
* @param mixed $targetId
* @param iterable $tagIds
*
* @return iterable<TagMap>
*
* @throws ReflectionException
*/
public function flushTagMaps(string $type, mixed $targetId, iterable $tagIds): iterable
public function flushTagMaps(string|\BackedEnum $type, mixed $targetId, iterable $tagIds): iterable
{
/** @var EntityMapper<TagMap> $tagMapMapper */
$tagMapMapper = $this->orm->mapper(TagMap::class);
Expand All @@ -101,36 +98,54 @@ public function flushTagMaps(string $type, mixed $targetId, iterable $tagIds): i
* @param string $type
* @param mixed $targetId
*
* @return array<StatementInterface>
* @return void
*
* @throws ReflectionException
*/
public function clearMapsOfTarget(string $type, mixed $targetId): array
public function clearMapsOfTarget(string $type, mixed $targetId): void
{
/** @var EntityMapper<TagMap> $tagMapMapper */
$tagMapMapper = $this->orm->mapper(TagMap::class);

return $tagMapMapper->deleteWhere(
$tagMapMapper->deleteWhere(
[
'type' => $type,
'target_id' => $targetId,
]
);
}

public function clearMapsOfTag(string $type, mixed $tagId): array
public function clearMapsOfTag(string $type, mixed $tagId): void
{
/** @var EntityMapper<TagMap> $tagMapMapper */
$tagMapMapper = $this->orm->mapper(TagMap::class);

return $tagMapMapper->deleteWhere(
$tagMapMapper->deleteWhere(
[
'type' => $type,
'tag_id' => $tagId,
]
);
}

public function getTagListQuery(string|\BackedEnum $type, mixed $targetIds = null): SelectorQuery
{
$query = $this->orm->from(Tag::class)
->leftJoin(TagMap::class)
->where('tag_map.type', $type);

if (is_iterable($targetIds)) {
$targetIds = iterator_to_array($targetIds);
}

if ($targetIds !== null && $targetIds !== []) {
$query->where('tag_map.target_id', $targetIds);
}

return $query->groupByJoins()
->setDefaultItemClass(Tag::class);
}

/**
* @return string
*/
Expand Down

0 comments on commit 1784b13

Please sign in to comment.