-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
feat: Add OCP interface to format richtext into string #48137
Changes from all commits
70a886c
295eee6
117c7ee
b926df4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OC\RichObjectStrings; | ||
|
||
use OCP\RichObjectStrings\IRichTextFormatter; | ||
|
||
class RichTextFormatter implements IRichTextFormatter { | ||
/** | ||
* @throws \InvalidArgumentException if a parameter has no name or no type | ||
*/ | ||
public function richToParsed(string $message, array $parameters): string { | ||
$placeholders = []; | ||
$replacements = []; | ||
foreach ($parameters as $placeholder => $parameter) { | ||
$placeholders[] = '{' . $placeholder . '}'; | ||
foreach (['name','type'] as $requiredField) { | ||
if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) { | ||
throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing"); | ||
} | ||
} | ||
$replacements[] = match($parameter['type']) { | ||
'user' => '@' . $parameter['name'], | ||
'file' => $parameter['path'] ?? $parameter['name'], | ||
default => $parameter['name'], | ||
}; | ||
} | ||
return str_replace($placeholders, $replacements, $message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
|
||||||
/** | ||||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||||||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||||||
*/ | ||||||
|
||||||
namespace OCP\RichObjectStrings; | ||||||
|
||||||
/** | ||||||
* Parse rich text and format it with the richobjects | ||||||
* | ||||||
* @since 31.0.0 | ||||||
*/ | ||||||
interface IRichTextFormatter { | ||||||
/** | ||||||
* @since 31.0.0 | ||||||
* @param string $message | ||||||
* @param array<string,array<string,string>> $parameters | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m always afraid to break tools by doing that because the type and the name of the parameter is space separated to adding space in the type looks wrong. |
||||||
* @throws \InvalidArgumentException if a parameter has no name or no type | ||||||
*/ | ||||||
public function richToParsed(string $message, array $parameters): string; | ||||||
} |
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.
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.
why is this not covered by cs-fixed? 👀
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.
I guess https://github.com/nextcloud/server/blob/master/.php-cs-fixer.dist.php#L23-L35 is broken?
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.
Why would it be broken?
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.
Because this change is not flagged an cs error
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.
But do we have a rule for this?
The code @nickvergessen mentioned is only there to ignore git-ignored files.
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.
This is an array, not a function call so a space is not expected there I think
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.
But I agree that we should have a rule for it.