diff --git a/README.md b/README.md index 0563b96..aa49760 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ If the package doesn't fit your needs, you might take a look at the alternative * [Listing jobs](#listing-jobs) * [Retrieving information about a specific job](#retrieving-information-about-a-specific-job) * [Submitting a job](#submitting-a-job) + * [Code Tables](#code-tables) + * [Getting a single code table](#getting-a-codetable) * [Automatic retries on errors](#automatic-retries-on-errors) * [Laravel integration](#laravel-integration) * [Customizing the HTTP client stack](#customizing-the-http-client-stack) @@ -329,7 +331,7 @@ foreach ($bib->representations as $rep) { ## Users, loans, fees and requests -**Note**: Editing is not yet implemented. +**Note**: Editing is not fully implemented. ### Search @@ -341,6 +343,54 @@ foreach ($alma->users->search('last_name~Heggø AND first_name~Dan') as $user) { } ``` +### Contact Info Updates + +#### Update an address + +Example: + +```php +$uerr = $alma->users->get('EXTID_123456'); +$user->contactInfo->unsetPreferredAddress(); +$addresses = $user->contactInfo->getAddresses(); +$campusBox = 'Box 876'; +$changed = false; +foreach ($addresses as $address) { + if ($address->address_type[0]->value === 'school') { + $address->line1 = $campusBox; + $address->preferred = true; + $changed = true; + break; + } +} +if (!$changed) { + $new = json_decode('{ "preferred": true, "segment_type": "Internal", "line1": "My University", "line2": ".$campusBox.", "city": "Scottsdale", "state_province": "AZ", "postal_code": "85054", "country": { "value": "USA" }, "address_note": "string", "start_date": "2020-07-20", "end_date": "2021-07-20", "address_type": [ { "value": "school" } ] }'); + $user->contactInfo->addAddress($new); +} +$user->save(); +``` + +#### Add an SMS number + +Example: + +```php +$user = $alma->users->get('EXTID_123456'); +$user->contactInfo->setSmsSNumber('18005882300'); +$user->save(); +``` + +#### Change an email + +Example: + +```php +$user = $alma->users->get('EXTID_123456'); +$user->contactInfo->removeEmail('bounced@email.org'); +$user->contactInfo->addEmail('confirmed@email.net'); +$user->save(); +``` + ### Loans Example: @@ -530,6 +580,20 @@ $job = $alma->jobs['M43']; $instance = $alma->jobs['M43']->submit(); ``` +## Code Tables + +### Getting a Code Table + +To fetch a code table + +```php +$ct = $alma->codetables->get('systemJobStatus'); +echo "Rows for ".$ct->sub_system->value."'s ".$ct->name."\n"; +foreach ($ct->row as $row) { + echo "code: ".$row->code.", description: ".$row->description."\n"; +} +``` + ## Automatic retries on errors If the client receives a 429 (rate limiting) response from Alma, it will sleep for a short time (0.5 seconds by default) diff --git a/spec/Conf/CodeTableSpec.php b/spec/Conf/CodeTableSpec.php new file mode 100644 index 0000000..8b3b2b1 --- /dev/null +++ b/spec/Conf/CodeTableSpec.php @@ -0,0 +1,47 @@ +beConstructedWith($client, 'systemJobStatus'); + } + + protected function expectRequest($client) + { + $client->getXML('/conf/code-table/systemJobStatus') + ->shouldBeCalled() + ->willReturn(SpecHelper::getDummyData('codetable_response.json')); + } + + public function it_is_lazy(AlmaClient $client) + { + SpecHelper::expectNoRequests($client); + $this->shouldHaveType(CodeTable::class); + } + + public function it_fetches_record_data_when_needed(AlmaClient $client) + { + $this->expectRequest($client); + + $this->name->('systemJobStatus'); + $this->subSystem->value->shouldBe('INFRA'); + } + + public function it_can_exist(AlmaClient $client) + { + $this->expectRequest($client); + + $this->exists()->shouldBe(true); + } +} diff --git a/spec/Conf/CodeTablesSpec.php b/spec/Conf/CodeTablesSpec.php new file mode 100644 index 0000000..cc2c490 --- /dev/null +++ b/spec/Conf/CodeTablesSpec.php @@ -0,0 +1,40 @@ +beConstructedWith($client); + } + + public function it_provides_a_lazy_interface_to_codetable_objects(AlmaClient $client) + { + SpecHelper::expectNoRequests($client); + + $ctid = 'myCodeTable'; // str_random(); + $bib = $this->get($ctid); + + $bib->shouldHaveType(CodeTable::class); + $bib->code->shouldBe($ctid); + } + + public function it_provides_a_lazy_array_interface_to_codetable_objects(AlmaClient $client) + { + SpecHelper::expectNoRequests($client); + + $ctid = 'myCodeTable'; // str_random(); + $ct = $this[$ctid]; + + $ct->shouldHaveType(CodeTable::class); + $ct->code->shouldBe($ctid); + } + +} diff --git a/spec/Users/UserSpec.php b/spec/Users/UserSpec.php index a117d72..596b573 100644 --- a/spec/Users/UserSpec.php +++ b/spec/Users/UserSpec.php @@ -74,25 +74,129 @@ public function it_has_requests() public function it_has_sms() { - $this->getSmsNumber()->shouldBe('87654321'); + $this->contactInfo->getSmsNumber()->phone_number->shouldBe('87654321'); } public function it_can_change_sms() { - $this->setSmsNumber('12345678'); - $this->getSmsNumber()->shouldBe('12345678'); + $this->contactInfo->setSmsNumber('12345678'); + $this->contactInfo->getSmsNumber()->phone_number->shouldBe('12345678'); } public function it_can_add_sms() { - $this->setSmsNumber('9999999'); - $this->getSmsNumber()->shouldBe('9999999'); + $this->contactInfo->setSmsNumber('9999999'); + $this->contactInfo->getSmsNumber()->phone_number->shouldBe('9999999'); } public function it_can_remove_sms() { - $this->unsetSmsNumber(); - $this->getSmsNumber()->shouldBe(null); + $this->contactInfo->unsetSmsNumber(); + $this->contactInfo->getSmsNumber()->shouldBe(null); } + public function it_can_add_email() + { + $this->contactInfo->addEmail('example@example.com', 'work', true); + $this->contactInfo->getEmail()->email_address->shouldBe('example@example.com'); + } + + public function it_can_unset_email() + { + $this->contactInfo->unsetEmail(); + $this->contactInfo->getEmail()->shouldBe(null); + } + + public function it_can_remove_email() + { + $this->contactInfo->removeEmail('dan@banan.com'); + $this->contactInfo->allEmails()->shouldBe([]); + } + + public function it_can_add_address() + { + $this->contactInfo->addAddress([ + 'line1' => '123 Something Blvd.', + 'city' => 'Somewhere', + 'state_province' => 'IL', + 'postal_code' => '12345', + 'country' => 'USA', + 'address_type' => 'home' + ])->shouldBeAnInstanceOf('Scriptotek\Alma\Users\Address'); + $this->contactInfo->address[1]->shouldBeLike((object) [ + 'line1' => '123 Something Blvd.', + 'city' => 'Somewhere', + 'state_province' => 'IL', + 'postal_code' => '12345', + 'country' => (object) ['value' => 'USA'], + 'address_type' => [(object) ['value' => 'home']] + ]); + } + + public function it_can_remove_address() + { + $address = $this->contactInfo->addresses[0]; + $this->contactInfo->removeAddress($address); + $this->contactInfo->addresses->shouldBe([]); + } + + public function it_can_set_preferred_address() + { + $address = $this->contactInfo->addresses[0]; + $address->preferred = true; + $address->preferred->shouldBe(true); + $address->preferred = false; + $address->preferred->shouldBe(false); + } + + public function it_can_unset_preferred_address() + { + $this->contactInfo->unsetPreferredAddress(); + $this->contactInfo->getPreferredAddress()->shouldBe(null); + } + + public function it_can_set_address_type() + { + $address = $this->contactInfo->addresses[0]; + $address->setAddressType('work', 'Work'); + $address->data->address_type[0]->shouldBeLike((object)[ + 'value' => 'work', + 'desc' => 'Work' + ]); + $address->address_type = 'home'; + $address->data->address_type[0]->shouldBeLike((object)[ + 'value' => 'home' + ]); + } + + public function it_can_add_phone_number() + { + $this->contactInfo->addPhone('8675309', 'home', true); + $this->contactInfo->getPreferredPhone()->phone_number->shouldBe('8675309'); + } + + public function it_can_remove_phone_number() + { + $this->contactInfo->removePhone('12345678'); + $this->contactInfo->getPreferredPhone()->shouldBe(null); + } + + public function it_can_set_preferred_phone() + { + $this->contactInfo->setPreferredPhone('87654321'); + $this->contactInfo->getPreferredPhone()->phone_number->shouldBe('87654321'); + } + + public function it_can_unset_preferred_phone() + { + $this->contactInfo->unsetPreferredPhone(); + $this->contactInfo->getPreferredPhone()->shouldBe(null); + } + + public function it_can_get_all_phone_numbers() + { + $phones = $this->contactInfo->allPhones(); + $phones[0]->phone_number->shouldBe('12345678'); + $phones[1]->phone_number->shouldBe('87654321'); + } } diff --git a/spec/data/codetable_response.json b/spec/data/codetable_response.json new file mode 100644 index 0000000..f31288d --- /dev/null +++ b/spec/data/codetable_response.json @@ -0,0 +1,109 @@ +{ + "name": "systemJobStatus", + "description": "System Job Status", + "sub_system": { + "value": "INFRA", + "desc": "Infra" + }, + "patron_facing": true, + "language": { + "value": "en", + "desc": "English" + }, + "scope": { + "institution_id": { + "value": "01MY_INST", + "desc": "My Institution" + }, + "library_id": { + "value": "", + "desc": "" + } + }, + "row": [ + { + "code": "QUEUED", + "description": "Queued", + "default": false, + "enabled": true + }, + { + "code": "PENDING", + "description": "Pending", + "default": false, + "enabled": true + }, + { + "code": "INITIALIZING", + "description": "Initializing", + "default": false, + "enabled": true + }, + { + "code": "RUNNING", + "description": "Running", + "default": false, + "enabled": true + }, + { + "code": "MANUAL_HANDLING_REQUIRED", + "description": "Manual Handling Required", + "default": false, + "enabled": true + }, + { + "code": "FINALIZING", + "description": "Finalizing", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_SUCCESS", + "description": "Completed Successfully", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_NO_BULKS", + "description": "Completed with no Bulks", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_FAILED", + "description": "Completed with Errors", + "default": false, + "enabled": true + }, + { + "code": "FAILED", + "description": "Failed", + "default": false, + "enabled": true + }, + { + "code": "COMPLETED_WARNING", + "description": "Completed with Warnings", + "default": false, + "enabled": true + }, + { + "code": "USER_ABORTED", + "description": "Aborted by User", + "default": false, + "enabled": true + }, + { + "code": "SYSTEM_ABORTED", + "description": "Aborted by System", + "default": false, + "enabled": true + }, + { + "code": "SKIPPED", + "description": "Skipped", + "default": false, + "enabled": true + } + ] +} diff --git a/src/Client.php b/src/Client.php index d3b1714..1bd16d2 100644 --- a/src/Client.php +++ b/src/Client.php @@ -24,6 +24,7 @@ use Scriptotek\Alma\Conf\Jobs; use Scriptotek\Alma\Conf\Libraries; use Scriptotek\Alma\Conf\Library; +use Scriptotek\Alma\Conf\CodeTables; use Scriptotek\Alma\Exception\ClientException as AlmaClientException; use Scriptotek\Alma\Exception\InvalidApiKey; use Scriptotek\Alma\Exception\MaxNumberOfAttemptsExhausted; @@ -103,6 +104,11 @@ class Client */ public $jobs; + /** + * @var CodeTables + */ + public $codetables; + /** * @var TaskLists */ @@ -152,6 +158,7 @@ public function __construct( $this->conf = new Conf($this); $this->libraries = $this->conf->libraries; // shortcut $this->jobs = $this->conf->jobs; // shortcut + $this->codetables = $this->conf->codetables; // shortcut $this->taskLists = new TaskLists($this); diff --git a/src/Conf/CodeTable.php b/src/Conf/CodeTable.php new file mode 100644 index 0000000..d2ced0a --- /dev/null +++ b/src/Conf/CodeTable.php @@ -0,0 +1,69 @@ +code = $code; + } + + /** + * Return a list of rows referring to the code of the rows in the table. + * + * @param string $code - The code of the row in the Table we want to pull. + * + * @return array $found - The rows in the code table that match the code passed in. + */ + public function getRowByCode($code) + { + $found = array(); + $codeTable = json_decode($this->client->get($this->urlBase())); + foreach ($codeTable->row as $row) { + if ($row->code == $code) { + array_push($found,$row); + } + } + return($found); + } + + /** + * Check if we have the full representation of our data object. + * + * @param \stdClass $data + * + * @return bool + */ + protected function isInitialized($data) + { + return isset($data->name); + } + + /** + * Generate the base URL for this resource. + * + * @return string + */ + protected function urlBase() + { + return "/conf/code-tables/{$this->code}"; + } + +} diff --git a/src/Conf/CodeTables.php b/src/Conf/CodeTables.php new file mode 100644 index 0000000..0149528 --- /dev/null +++ b/src/Conf/CodeTables.php @@ -0,0 +1,59 @@ +client = $client; + } + + /** + * Get a CodeTable by identifier + * + * @param $code The identifier of a CodeTable + * + * @return CodeTable + */ + public function get($code) + { + return CodeTable::make($this->client, $code); + } + + /** + * Return a object containing a list of code tables. + * + * @return CodeTable ojbect list. + */ + public function getCodeTables() + { + return json_decode($this->client->get($this->urlBase())); + } + + /** + * Generate the base URL for this resource. + * + * @return string + */ + protected function urlBase() + { + return '/conf/code-tables'; + } + +} diff --git a/src/Conf/Conf.php b/src/Conf/Conf.php index 65d44e3..8c2e4d3 100644 --- a/src/Conf/Conf.php +++ b/src/Conf/Conf.php @@ -4,6 +4,7 @@ use Scriptotek\Alma\Client; use Scriptotek\Alma\Conf\Jobs; +use Scriptotek\Alma\Conf\CodeTables; class Conf { @@ -12,5 +13,6 @@ public function __construct(Client $client) $this->client = $client; $this->libraries = new Libraries($client); $this->jobs = new Jobs($client); + $this->codetables = new CodeTables($client); } } diff --git a/src/Model/SettableModel.php b/src/Model/SettableModel.php new file mode 100644 index 0000000..d2e149f --- /dev/null +++ b/src/Model/SettableModel.php @@ -0,0 +1,35 @@ +$method($value); + } + $method = 'set' . ucfirst($key); + if (method_exists($this, $method)) { + return $this->$method($value); + } + + // Otherwise set the key on the data object + $this->data->{$key} = $value; + } +} diff --git a/src/Users/Address.php b/src/Users/Address.php new file mode 100644 index 0000000..c19647c --- /dev/null +++ b/src/Users/Address.php @@ -0,0 +1,24 @@ + $address_type]; + if ($description) { + $addressTypeObj['desc'] = $description; + } + $this->data->address_type = [(object) $addressTypeObj]; + } +} diff --git a/src/Users/ContactInfo.php b/src/Users/ContactInfo.php new file mode 100644 index 0000000..e731c20 --- /dev/null +++ b/src/Users/ContactInfo.php @@ -0,0 +1,393 @@ +data->phone) { + foreach ($this->data->phone as $phone) { + if ($phone->preferred_sms) { + return Phone::make($this->client, $phone); + } + } + } + return null; + } + + /** + * Remove the preferred SMS flag from any number. + */ + public function unsetSmsNumber() + { + if ($this->data->phone) { + foreach ($this->data->phone as $phone) { + if ($phone->preferred_sms) { + $phone->preferred_sms = false; + } + } + } + } + + /** + * Set the user's preferred SMS number, creating a new internal mobile number if needed + * @param $number string The SMS-capable mobile phone number + */ + public function setSmsNumber($number) + { + $currentNumber = $this->getSmsNumber(); + if ($currentNumber && $number === $currentNumber->phone_number) { + return; + } + $this->unsetSmsNumber(); + if ($this->data->phone) { + foreach ($this->data->phone as $phone) { + if ($phone->phone_number === $number) { + $phone->preferred_sms = true; + return; + } + } + } + $this->addSmsNumber($number); + } + + /** + * Add the user's preferred SMS number as a new internal mobile number. + * @param $number string The SMS-capable mobile phone number + * + * @return Phone + */ + public function addSmsNumber($number) + { + $currentNumber = $this->getSmsNumber(); + if ($currentNumber) { + $this->unsetSmsNumber(); + } + if (!$this->data->phone) { + $this->data->phone = []; + } + $phone_obj = (object) [ + 'phone_number' => $number, + 'preferred' => false, + 'preferred_sms' => true, + 'segment_type' => 'Internal', + 'phone_type' => [(object) [ + 'value' => 'mobile', + 'desc' => 'Mobile' + ]] + ]; + $this->data->phone[] = $phone_obj; + return Phone::make($this->client, $phone_obj); + } + + /** + * Adds a new internal phone number to the user + * + * @param string $phone_number The phone number + * @param string $phone_type Type of the phone number (home, mobile, etc.) + * @param bool $preferred Whether this should be the user's preferred phone number + * + * @return Phone + */ + public function addPhone($phone_number, $phone_type, $preferred = false) + { + if (!$this->data->phone) { + $this->data->phone = []; + } + if ($preferred) { + $this->unsetPreferredPhone(); + } + $phone_obj = (object) [ + 'phone_number' => $phone_number, + 'preferred' => $preferred, + 'preferred_sms' => false, + 'segment_type' => 'Internal', + 'phone_type' => [(object) [ + 'value' => $phone_type + ]] + ]; + $this->data->phone[] = $phone_obj; + return Phone::make($this->client, $phone_obj); + } + + /** + * Gets the user's preferred phone number, or null if none are preferred + * + * @return Phone|null + */ + public function getPreferredPhone() + { + foreach ($this->data->phone as $phone) { + if ($phone->preferred) { + return Phone::make($this->client, $phone); + } + } + return null; + } + + /** + * Remove the preferred flag from all phone numbers + */ + public function unsetPreferredPhone() + { + foreach ($this->data->phone as $phone) { + if ($phone->preferred) { + $phone->preferred = false; + } + } + } + + /** + * Sets the given phone number as the user's preferred number, adding it as an internal home phone if necessary + * + * @param string $phone_number The phone number + * + * @throws Exception when the given phone number is not found in the user + */ + public function setPreferredPhone($phone_number) + { + $current_phone = $this->getPreferredPhone(); + if ($current_phone && $phone_number === $current_phone->phone_number) { + return; + } + $this->unsetPreferredPhone(); + foreach ($this->data->phone as $phone) { + if ($phone->phone_number === $phone_number) { + $phone->preferred = true; + return; + } + } + throw new Exception('Phone number ' . $phone_number . ' not found in user'); + } + + /** + * Removes a phone number from the user + * + * @param string The phone number + */ + public function removePhone($phone_number) + { + foreach ($this->data->phone as $key => $phone) { + if ($phone->phone_number === $phone_number) { + array_splice($this->data->phone, $key, 1); + return; + } + } + } + + /** + * Returns an array of all phone numbers associated with the user + * + * @return array An array of Phone objects + */ + public function allPhones() + { + $phones = []; + foreach ($this->data->phone as $phone) { + $phones[] = Phone::make($this->client, $phone); + } + return $phones; + } + + /** + * Gets the user's preferred email address + * @return Email The email address + */ + public function getEmail() + { + if ($this->data->email) { + foreach ($this->data->email as $email) { + if ($email->preferred) { + return Email::make($this->client, $email); + } + } + } + return null; + } + + /** + * Sets the user's preferred email address, adding a new email address if needed. + * @param string $email_address The email address + * + * @throws Exception when the given email address is not found in the user + */ + public function setEmail($email_address) + { + $current_email = $this->getEmail(); + if ($current_email && $email_address === $current_email->email_address) { + return; + } + $this->unsetEmail(); + if ($this->data->email) { + foreach ($this->data->email as $email) { + if ($email->email_address === $email_address) { + $email->preferred = true; + return; + } + } + } + throw new Exception('Email address ' . $email_address . ' not found in user'); + } + + /** + * Removes the preferred flag from all email addresses + */ + public function unsetEmail() + { + if ($this->data->email) { + foreach ($this->data->email as $email) { + if ($email->preferred) { + $email->preferred = false; + } + } + } + } + + /** + * Adds a new email address + * @param string $email_address The email address + * @param string $email_type The email type, defaults to 'personal' + * @param bool $preferred True if this should be the preferred email + * + * @return Email + */ + public function addEmail($email_address, $email_type = 'personal', $preferred = false) + { + if (!$this->data->email) { + $this->data->email = []; + } + if ($preferred) { + $this->unsetEmail(); + } + $email_obj = (object) [ + 'preferred' => $preferred, + 'segment_type' => 'Internal', + 'email_address' => $email_address, + 'description' => '', + 'email_type' =>[(object) [ + 'value' => $email_type, + ]] + ]; + $this->data->email[] = $email_obj; + return Email::make($this->client, $email_obj); + } + + /** + * Removes the given email address from the user + * + * @param string $email_address The email address to remove + */ + public function removeEmail($email_address) + { + foreach ($this->data->email as $key => $email) { + if ($email->email_address === $email_address) { + array_splice($this->data->email, $key, 1); + return; + } + } + } + + /** + * Returns an array of all email addresses associated with the user + * + * @return array An array of Email objects + */ + public function allEmails() + { + $emails = []; + foreach ($this->data->email as $email) { + $emails[] = Email::make($this->client, $email); + } + return $emails; + } + + /** + * Get an array of objects representing the user's addresses + * + * @return array An array of Address objects + */ + public function getAddresses() { + $addresses = []; + foreach ($this->address as $address) { + $addresses[] = Address::make($this->client, $address); + } + return $addresses; + } + + /** + * Adds a new address. + * + * @param array $address The address' properties + * @return Address A new address object based on the given values + */ + public function addAddress($address) + { + if (isset($address['country']) && is_string($address['country'])) { + $address['country'] = (object) ['value' => $address['country']]; + } + if (isset($address['address_type'])) { + if (is_string($address['address_type'])) { + $address['address_type'] = [(object)['value' => $address['address_type']]]; + } elseif (is_object($address['address_type'])) { + $address['address_type'] = [$address['address_type']]; + } + } + if (!$this->data->address) { + $this->data->address = []; + } + $address = (object) $address; + $this->data->address[] = $address; + return Address::make($this->client, $address); + } + + /** + * Removes an address from the user + * + * @param Address|stdClass $address Either the Address object to be removed or its underlying stdClass object + */ + public function removeAddress($address) + { + if ($address instanceof Address) { + $address = $address->data; + } + if (($key = array_search($address, $this->data->address)) !== false) { + array_splice($this->data->address, $key, 1); + } + } + + /** + * Returns the user's preferred address + * + * @return Address|null The address object or null if none are preferred + */ + public function getPreferredAddress() + { + foreach ($this->data->address as $address) { + if ($address->preferred) { + return Address::make($this->client, $address); + } + } + return null; + } + + /** + * Removes the preferred flag from all addresses + */ + public function unsetPreferredAddress() + { + foreach ($this->data->address as $address) { + if ($address->preferred) { + $address->preferred = false; + } + } + } +} diff --git a/src/Users/Email.php b/src/Users/Email.php new file mode 100644 index 0000000..d4cf2b0 --- /dev/null +++ b/src/Users/Email.php @@ -0,0 +1,24 @@ + $email_type]; + if ($description) { + $emailTypeObj['desc'] = $description; + } + $this->data->email_type = [(object) $emailTypeObj]; + } +} diff --git a/src/Users/Phone.php b/src/Users/Phone.php new file mode 100644 index 0000000..ed50833 --- /dev/null +++ b/src/Users/Phone.php @@ -0,0 +1,24 @@ + $phone_type]; + if ($description) { + $phoneTypeObj['desc'] = $description; + } + $this->data->phone_type = [(object) $phoneTypeObj]; + } +} diff --git a/src/Users/User.php b/src/Users/User.php index 37ab291..87dcaf6 100644 --- a/src/Users/User.php +++ b/src/Users/User.php @@ -22,6 +22,11 @@ class User extends LazyResource */ protected $_identifiers; + /** + * @var ContactInfo + */ + protected $_contact_info; + /** * @var Loans */ @@ -84,75 +89,11 @@ public function getIdentifiers() } /** - * Get the user's preferred SMS number. - * - * @return string|null - */ - public function getSmsNumber() - { - $this->init(); - if ($this->data->contact_info->phone) { - foreach ($this->data->contact_info->phone as $phone) { - if ($phone->preferred_sms) { - return $phone->phone_number; - } - } - } - return null; - } - - /** - * Remove the preferred SMS flag from any number. + * Get the user's contact info. */ - public function unsetSmsNumber() + public function getContactInfo() { - $this->init(); - if ($this->data->contact_info->phone) { - foreach ($this->data->contact_info->phone as $phone) { - if ($phone->preferred_sms) { - $phone->preferred_sms = false; - } - } - } - } - - /** - * Set the user's preferred SMS number, creating a new internal mobile number if needed - * @param $number string The SMS-capable mobile phone number - */ - public function setSmsNumber($number) - { - $currentNumber = $this->getSmsNumber(); - if ($number === $currentNumber) { - return; - } - $this->unsetSmsNumber(); - $updated = false; - if ($this->data->contact_info->phone) { - foreach ($this->data->contact_info->phone as $phone) { - if ($phone->phone_number === $number) { - $phone->preferred_sms = true; - } - } - } - if (!$updated) { - $this->addSmsNumber($number); - } - } - - /** - * Add the user's preferred SMS number as a new internal mobile number. - * @param $number string The SMS-capable mobile phone number - */ - public function addSmsNumber($number) - { - $currentNumber = $this->getSmsNumber(); - if ($currentNumber) { - $this->unsetSmsNumber(); - } - $phones = json_decode(json_encode($this->data->contact_info->phone), true); - $phones[] = json_decode('{"phone_number":'.json_encode($number).',"preferred":false,"preferred_sms":true,"segment_type":"Internal","phone_type":[{"value":"mobile","desc":"Mobile"}]}', true); - $this->data->contact_info->phone = json_decode(json_encode($phones)); + return $this->init()->_contact_info; } /** @@ -186,6 +127,7 @@ protected function isInitialized($data) protected function onData($data) { $this->_identifiers = UserIdentifiers::make($this->client, $data); + $this->_contact_info = ContactInfo::make($this->client, $data->contact_info); } /**