-
-
Notifications
You must be signed in to change notification settings - Fork 241
FEATURE: New link value object for new link editor (inspector) #5682
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+298
−9
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the Neos.Neos.Ui package. | ||
| * | ||
| * (c) Contributors of the Neos Project - www.neos.io | ||
| * | ||
| * This package is Open Source Software. For the full copyright and license | ||
| * information, please view the LICENSE file which was distributed with this | ||
| * source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Neos\Neos\Domain\Link; | ||
|
|
||
| use GuzzleHttp\Psr7\Uri; | ||
| use Neos\Flow\Annotations as Flow; | ||
| use Psr\Http\Message\UriInterface; | ||
|
|
||
| /** | ||
| * Link value object modeled partially after the html spec for <a> tags. | ||
| * | ||
| * Note that currently the link editor can only handle and write to | ||
| * the property {@see Link::$target} "_blank" | null | ||
| * and to {@see Link::$rel} ["noopener"] | null | ||
| * | ||
| * The Link values can be accessed in Fusion the following: | ||
| * | ||
| * ```fusion | ||
| * href = ${q(node).property("link").href} | ||
| * title = ${q(node).property("link").title} | ||
| * # ... | ||
| * ``` | ||
| * | ||
| * In case you need to cast the uri in {@see Link::$href} explicitly to a string | ||
| * you can use: `String.toString(link.href)` | ||
| * | ||
| * @Flow\Proxy(false) | ||
| */ | ||
| final readonly class Link implements \JsonSerializable | ||
| { | ||
| /** | ||
| * A selection of frequently used target attribute values | ||
| */ | ||
| public const TARGET_SELF = '_self'; | ||
| public const TARGET_BLANK = '_blank'; | ||
|
|
||
| /** | ||
| * A selection of frequently used rel attribute values | ||
| */ | ||
| public const REL_NOOPENER = 'noopener'; | ||
| public const REL_NOFOLLOW = 'nofollow'; | ||
|
|
||
| /** | ||
| * @param array<int, string> $rel | ||
| */ | ||
| private function __construct( | ||
| public UriInterface $href, | ||
| public ?string $title, | ||
| public ?string $target, | ||
| public array $rel, | ||
| public bool $download | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * Note: The signature of this method might be extended in the future, so it should always be used with named arguments | ||
| * @see https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments | ||
| * | ||
| * @param array<int, string> $rel | ||
| */ | ||
| public static function create( | ||
| UriInterface $href, | ||
| ?string $title = null, | ||
| ?string $target = null, | ||
| array $rel = [], | ||
| bool $download = false, | ||
| ): self { | ||
| $relMap = []; | ||
| foreach ($rel as $value) { | ||
| $relMap[strtolower($value)] = true; | ||
| } | ||
| return new self( | ||
| $href, | ||
| ($title === '' || $title === null) ? null : $title, | ||
| ($target === '' || $target === null) ? null : strtolower($target), | ||
| array_keys($relMap), | ||
| $download | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string, mixed> $array | ||
| */ | ||
| public static function fromArray(array $array): self | ||
| { | ||
| return self::create( | ||
| new Uri($array['href']), | ||
| $array['title'] ?? null, | ||
| $array['target'] ?? null, | ||
| $array['rel'] ?? [], | ||
| $array['download'] ?? false, | ||
| ); | ||
| } | ||
|
|
||
| public static function fromString(string $string): self | ||
| { | ||
| return self::create( | ||
| href: new Uri($string) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Note: The signature of this method might be extended in the future, so it should always be used with named arguments | ||
| * @see https://www.php.net/manual/en/functions.arguments.php#functions.named-arguments | ||
| * | ||
| * @param array<int, string>|null $rel | ||
| */ | ||
| public function with( | ||
| ?UriInterface $href = null, | ||
| ?string $title = null, | ||
| ?string $target = null, | ||
| ?array $rel = null, | ||
| ?bool $download = null, | ||
| ): self { | ||
| return self::create( | ||
| $href ?? $this->href, | ||
| $title ?? $this->title, | ||
| $target ?? $this->target, | ||
| $rel ?? $this->rel, | ||
| $download ?? $this->download, | ||
| ); | ||
| } | ||
|
|
||
| public function jsonSerialize(): mixed | ||
| { | ||
| return [ | ||
| 'href' => $this->href->__toString(), | ||
| 'title' => $this->title, | ||
| 'target' => $this->target, | ||
| 'rel' => $this->rel, | ||
| 'download' => $this->download, | ||
| ]; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Neos\Neos\Tests\Unit\Domain\Link; | ||
|
|
||
| use GuzzleHttp\Psr7\Uri; | ||
| use Neos\Neos\Domain\Link\Link; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| class LinkTest extends TestCase | ||
| { | ||
| /** | ||
| * @test | ||
| */ | ||
| public function emptyLink() | ||
| { | ||
| $link = Link::create( | ||
| href: new Uri('#') | ||
| ); | ||
| self::assertEquals('', (string)$link->href); | ||
| self::assertEquals(null, $link->title); | ||
| self::assertEquals(null, $link->target); | ||
| self::assertEquals([], $link->rel); | ||
| self::assertEquals(false, $link->download); | ||
|
|
||
| // another way to create it | ||
| self::assertEquals( | ||
| $link, | ||
| Link::fromString('#') | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function linkWithAttributes() | ||
| { | ||
| $link = Link::create( | ||
| href: new Uri('/bar'), | ||
| title: 'My link', | ||
| target: Link::TARGET_BLANK, | ||
| rel: [Link::REL_NOFOLLOW], | ||
| download: true | ||
| ); | ||
|
|
||
| self::assertEquals('/bar', (string)$link->href); | ||
| self::assertEquals('My link', $link->title); | ||
| self::assertEquals('_blank', $link->target); | ||
| self::assertEquals(['nofollow'], $link->rel); | ||
| self::assertEquals(true, $link->download); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function linkWithAttributesViaWither() | ||
| { | ||
| $emptyLink = Link::create( | ||
| href: new Uri('#') | ||
| ); | ||
|
|
||
| $link = $emptyLink->with( | ||
| href: new Uri('/bar'), | ||
| title: 'My link', | ||
| target: Link::TARGET_BLANK, | ||
| rel: [Link::REL_NOFOLLOW], | ||
| download: true | ||
| ); | ||
|
|
||
| self::assertEquals('/bar', (string)$link->href); | ||
| self::assertEquals('My link', $link->title); | ||
| self::assertEquals('_blank', $link->target); | ||
| self::assertEquals(['nofollow'], $link->rel); | ||
| self::assertEquals(true, $link->download); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function linkWithAttributesOneUpdated() | ||
| { | ||
| $link = Link::create( | ||
| href: new Uri('/bar'), | ||
| title: 'My link', | ||
| target: Link::TARGET_BLANK, | ||
| rel: [Link::REL_NOFOLLOW], | ||
| download: true | ||
| )->with( | ||
| title: 'My updated link' | ||
| ); | ||
|
|
||
| self::assertEquals('/bar', (string)$link->href); | ||
| self::assertEquals('My updated link', $link->title); | ||
| self::assertEquals('_blank', $link->target); | ||
| self::assertEquals(['nofollow'], $link->rel); | ||
| self::assertEquals(true, $link->download); | ||
| } | ||
|
|
||
| /** | ||
| * @test | ||
| */ | ||
| public function linkWithAttributesCanBeResetViaWith() | ||
| { | ||
| $link = Link::create( | ||
| href: new Uri('/bar'), | ||
| title: 'My link', | ||
| target: Link::TARGET_BLANK, | ||
| rel: [Link::REL_NOFOLLOW], | ||
| download: true | ||
| )->with( | ||
| title: '', | ||
| target: '', | ||
| rel: [], | ||
| download: false | ||
| ); | ||
|
|
||
| self::assertEquals('/bar', (string)$link->href); | ||
| self::assertEquals(null, $link->title); | ||
| self::assertEquals(null, $link->target); | ||
| self::assertEquals([], $link->rel); | ||
| self::assertEquals(false, $link->download); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as this resides in Neos it can be an generic uri but also something specific like a Neos asset uri or node uri.
we could add the following methods to simplify handling all cases:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Implemented in #5683