AuthorPostUrl.php
2 days ago
CommentCount.php
2 days ago
FirstPost.php
2 days ago
FullName.php
2 days ago
HasRichEditing.php
2 days ago
LastPost.php
2 days ago
Meta.php
2 days ago
PostCount.php
2 days ago
PostCountOriginal.php
2 days ago
Property.php
2 days ago
Roles.php
2 days ago
ShowToolbar.php
2 days ago
TranslatedRoles.php
2 days ago
UserFilteredPostLink.php
2 days ago
UserLink.php
2 days ago
UserName.php
2 days ago
TranslatedRoles.php
45 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 | public function format(Value $value) |
| 15 | { |
| 16 | $user = get_userdata($value->get_id()); |
| 17 | |
| 18 | if (! $user) { |
| 19 | throw ValueNotFoundException::from_id($value->get_id()); |
| 20 | } |
| 21 | |
| 22 | $roles = []; |
| 23 | $role_names = AC\Helper\UserRoles::create()->find_role_names($user); |
| 24 | |
| 25 | foreach (AC\Helper\UserRoles::create()->find_all() as $role) { |
| 26 | if (! in_array($role->get_name(), $role_names, 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 | } |
| 45 |