Skip to content
Draft
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
5 changes: 5 additions & 0 deletions Neos.Media/Classes/Domain/Model/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@ public function getIdentifier()
return $this->persistenceManager->getIdentifierByObject($this);
}

public function getId(): AssetId
{
return AssetId::fromString($this->getIdentifier());
}

/**
* @return string
*/
Expand Down
18 changes: 18 additions & 0 deletions Neos.Media/Classes/Domain/Model/AssetId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

namespace Neos\Media\Domain\Model;

final readonly class AssetId
{
private function __construct(
public string $value
) {
}

public static function fromString(string $value): self
{
return new self($value);
}
}
15 changes: 15 additions & 0 deletions Neos.Neos/Classes/AssetUsage/ProvidesAssetIdsInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Neos\Neos\AssetUsage;

use Neos\Media\Domain\Model\AssetId;

interface ProvidesAssetIdsInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name sounds a bit strange why not AssetIdProviderInterface

{
/**
* @return list<AssetId>
*/
public function getAssetIds(): array;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i went with an "always plural" interface as we have to account for the empty case and that a value object handles more than one asset too. So this seems the best fit.

Also we should probably specify iterable so people could yield the asset ids for simplicity during nesting.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterable wound indeed be a better fit than array.

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
use Neos\ContentRepository\Core\SharedModel\Workspace\Workspaces;
use Neos\Flow\Annotations\Scope;
use Neos\Flow\Persistence\Doctrine\PersistenceManager;
use Neos\Media\Domain\Model\AssetId;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\AssetVariantInterface;
use Neos\Media\Domain\Model\ResourceBasedInterface;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Neos\AssetUsage\Domain\AssetUsageRepository;
use Neos\Neos\AssetUsage\Dto\AssetIdAndOriginalAssetId;
use Neos\Neos\AssetUsage\Dto\AssetIdsByProperty;
use Neos\Neos\Domain\Link\Link;
use Neos\Neos\AssetUsage\ProvidesAssetIdsInterface;
use Neos\Utility\TypeHandling;

/**
Expand Down Expand Up @@ -219,11 +220,9 @@ private function extractAssetIds(string $type, mixed $value): array
return array_map(static fn (array $match) => $match['assetId'], $matches);
}

if ($value instanceof Link) {
if ($value->href->getScheme() === 'asset') {
return [$value->href->getHost() . $value->href->getPath()];
}
return [];
if ($value instanceof ProvidesAssetIdsInterface) {
$assetIds = $value->getAssetIds();
return array_map(fn (AssetId $id) => $id->value, $assetIds);
}

if (is_subclass_of($type, ResourceBasedInterface::class)) {
Expand Down
34 changes: 33 additions & 1 deletion Neos.Neos/Classes/Domain/Link/Link.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
namespace Neos\Neos\Domain\Link;

use GuzzleHttp\Psr7\Uri;
use Neos\ContentRepository\Core\SharedModel\Node\NodeAggregateId;
use Neos\Flow\Annotations as Flow;
use Neos\Media\Domain\Model\AssetId;
use Neos\Neos\AssetUsage\ProvidesAssetIdsInterface;
use Psr\Http\Message\UriInterface;

/**
Expand All @@ -38,7 +41,7 @@
*
* @Flow\Proxy(false)
*/
final readonly class Link implements \JsonSerializable
final readonly class Link implements \JsonSerializable, ProvidesAssetIdsInterface
{
/**
* A selection of frequently used target attribute values
Expand Down Expand Up @@ -160,6 +163,35 @@ public function withDownload(bool $download): self
);
}

public function extractForScheme(string $scheme): string|null
{
if ($this->href->getScheme() !== $scheme) {
return null;
}
return $this->href->getHost() . $this->href->getPath();
}

public function extractNodeAggregateId(): ?NodeAggregateId
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a big fan of tryExtractWhatever if whatever is nullable, for readability

{
$rawNodeId = $this->extractForScheme('node');
return $rawNodeId ? NodeAggregateId::fromString($rawNodeId) : null;
}

public function extractAssetId(): ?AssetId
{
$rawAssetId = $this->extractForScheme('asset');
return $rawAssetId ? AssetId::fromString($rawAssetId) : null;
}

/**
* @return list<AssetId>
*/
public function getAssetIds(): array
{
$assetId = $this->extractAssetId();
return $assetId ? [$assetId] : [];
}

public function jsonSerialize(): mixed
{
return [
Expand Down
Loading