AuthorPostUrl.php
3 months ago
CommentCount.php
3 months ago
FirstPost.php
3 months ago
FullName.php
3 months ago
HasRichEditing.php
3 months ago
LastPost.php
3 months ago
Meta.php
3 months ago
PostCount.php
3 months ago
PostCountOriginal.php
3 months ago
Property.php
3 months ago
Roles.php
3 months ago
ShowToolbar.php
3 months ago
TranslatedRoles.php
3 months ago
UserFilteredPostLink.php
3 months ago
UserLink.php
3 months ago
UserName.php
3 months ago
TranslatedRoles.php
44 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Formatter\User; |
| 6 | |
| 7 | use AC; |
| 8 | use AC\Exception\ValueNotFoundException; |
| 9 | use AC\Helper; |
| 10 | use AC\Type\Value; |
| 11 | |
| 12 | class TranslatedRoles implements AC\Formatter |
| 13 | { |
| 14 | |
| 15 | public function format(Value $value) |
| 16 | { |
| 17 | $user = get_userdata($value->get_id()); |
| 18 | |
| 19 | if ( ! $user) { |
| 20 | throw ValueNotFoundException::from_id($value->get_id()); |
| 21 | } |
| 22 | |
| 23 | $roles = []; |
| 24 | |
| 25 | foreach (AC\Helper\UserRoles::create()->find_all() as $role) { |
| 26 | if ( ! in_array($role->get_name(), $user->roles, true)) { |
| 27 | continue; |
| 28 | } |
| 29 | |
| 30 | $roles[] = Helper\Html::create()->tooltip($role->get_translate_label(), $role->get_name()); |
| 31 | } |
| 32 | |
| 33 | natcasesort($roles); |
| 34 | |
| 35 | if (empty($roles)) { |
| 36 | throw ValueNotFoundException::from_id($value->get_id()); |
| 37 | } |
| 38 | |
| 39 | return $value->with_value( |
| 40 | implode(', ', $roles) |
| 41 | ); |
| 42 | } |
| 43 | |
| 44 | } |