scriptotek /
bibrex
| 1 | <?php |
||
| 2 | |||
| 3 | namespace App\Alma; |
||
| 4 | |||
| 5 | use Scriptotek\Alma\Client as AlmaClient; |
||
| 6 | use Scriptotek\Alma\Exception\ResourceNotFound; |
||
| 7 | use Scriptotek\Alma\Users\User as AlmaUser; |
||
| 8 | |||
| 9 | class User |
||
| 10 | { |
||
| 11 | public $primaryId; |
||
| 12 | public $barcodes; |
||
| 13 | public $universityIds; |
||
| 14 | public $group; |
||
| 15 | public $email; |
||
| 16 | public $phone; |
||
| 17 | public $lang; |
||
| 18 | public $firstName; |
||
| 19 | public $lastName; |
||
| 20 | public $name; |
||
| 21 | public $blocks; |
||
| 22 | public $type = 'alma'; |
||
| 23 | |||
| 24 | protected $user; |
||
| 25 | |||
| 26 | public function __construct(AlmaUser $user) |
||
| 27 | { |
||
| 28 | $this->user = $user; |
||
| 29 | |||
| 30 | foreach ($user->contact_info->email as $e) { |
||
| 31 | if ($e->preferred) { |
||
| 32 | $this->email = $e->email_address; |
||
| 33 | } |
||
| 34 | } |
||
| 35 | foreach ($user->contact_info->phone as $e) { |
||
| 36 | if ($e->preferred) { |
||
| 37 | $this->phone = $e->phone_number; |
||
| 38 | } |
||
| 39 | } |
||
| 40 | if (in_array($user->preferred_language->value, ['no', 'nb', 'nob'])) { |
||
| 41 | $this->lang = 'nob'; |
||
| 42 | } elseif (in_array($user->preferred_language->value, ['nn', 'nno'])) { |
||
| 43 | $this->lang = 'nno'; |
||
| 44 | } else { |
||
| 45 | $this->lang = 'eng'; |
||
| 46 | } |
||
| 47 | |||
| 48 | $this->firstName = $user->first_name; |
||
| 49 | $this->lastName = $user->last_name; |
||
| 50 | $this->primaryId = $user->primary_id; |
||
| 51 | $this->group = $user->user_group->desc; |
||
| 52 | $this->name = $this->lastName . ', ' . $this->firstName; |
||
| 53 | |||
| 54 | $this->blocks = array_filter($user->user_block, function ($block) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 55 | return $block->block_status == 'ACTIVE'; |
||
| 56 | }); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Lookup user by primary or non-primary identifier. |
||
| 61 | * |
||
| 62 | * @param AlmaClient $alma |
||
| 63 | * @param $identifier |
||
| 64 | * @return User|null |
||
| 65 | */ |
||
| 66 | public static function lookup(AlmaClient $alma, $identifier) |
||
| 67 | { |
||
| 68 | if (empty($identifier)) { |
||
| 69 | return null; |
||
| 70 | } |
||
| 71 | if (is_null($alma->key)) { |
||
|
0 ignored issues
–
show
|
|||
| 72 | return null; |
||
| 73 | } |
||
| 74 | try { |
||
| 75 | return new self($alma->users->get($identifier)->init()); |
||
| 76 | } catch (ResourceNotFound $e) { |
||
| 77 | return null; |
||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public static function isUserBarcode($value) |
||
| 82 | { |
||
| 83 | // 2-5 letters followed by 5-8 digits, making up 10 characters in total. |
||
| 84 | return preg_match('/^[a-zA-Z]{2}[0-9a-zA-Z]{3}[0-9a-zA-Z]{5}$/', $value); |
||
| 85 | } |
||
| 86 | |||
| 87 | public function getBarcodes() |
||
| 88 | { |
||
| 89 | // if (self::isUserBarcode($this->primaryId)) { |
||
| 90 | // return $this->primaryId; |
||
| 91 | // } |
||
| 92 | return $this->user->barcodes; |
||
| 93 | } |
||
| 94 | |||
| 95 | public function getUniversityIds() |
||
| 96 | { |
||
| 97 | return $this->user->universityIds; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getFees() |
||
| 101 | { |
||
| 102 | return $this->user->fees->total_sum; |
||
| 103 | } |
||
| 104 | |||
| 105 | public function getIdentifiers() |
||
| 106 | { |
||
| 107 | return $this->user->identifiers; |
||
| 108 | } |
||
| 109 | } |
||
| 110 |