Skip to content

Commit e4a4c1e

Browse files
chryslovelaceunknown
andauthored
Contact info (#2)
* add contactinfo and address classes and methods * added a couple more address methods & tests * added objects for Email and Phone * add fixes for early exit case, @throws annotations Co-authored-by: unknown <[email protected]>
1 parent 536c67f commit e4a4c1e

File tree

7 files changed

+620
-74
lines changed

7 files changed

+620
-74
lines changed

spec/Users/UserSpec.php

Lines changed: 111 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,129 @@ public function it_has_requests()
7474

7575
public function it_has_sms()
7676
{
77-
$this->getSmsNumber()->shouldBe('87654321');
77+
$this->contactInfo->getSmsNumber()->phone_number->shouldBe('87654321');
7878
}
7979

8080
public function it_can_change_sms()
8181
{
82-
$this->setSmsNumber('12345678');
83-
$this->getSmsNumber()->shouldBe('12345678');
82+
$this->contactInfo->setSmsNumber('12345678');
83+
$this->contactInfo->getSmsNumber()->phone_number->shouldBe('12345678');
8484
}
8585

8686
public function it_can_add_sms()
8787
{
88-
$this->setSmsNumber('9999999');
89-
$this->getSmsNumber()->shouldBe('9999999');
88+
$this->contactInfo->setSmsNumber('9999999');
89+
$this->contactInfo->getSmsNumber()->phone_number->shouldBe('9999999');
9090
}
9191

9292
public function it_can_remove_sms()
9393
{
94-
$this->unsetSmsNumber();
95-
$this->getSmsNumber()->shouldBe(null);
94+
$this->contactInfo->unsetSmsNumber();
95+
$this->contactInfo->getSmsNumber()->shouldBe(null);
9696
}
9797

98+
public function it_can_add_email()
99+
{
100+
$this->contactInfo->addEmail('[email protected]', 'work', true);
101+
$this->contactInfo->getEmail()->email_address->shouldBe('[email protected]');
102+
}
103+
104+
public function it_can_unset_email()
105+
{
106+
$this->contactInfo->unsetEmail();
107+
$this->contactInfo->getEmail()->shouldBe(null);
108+
}
109+
110+
public function it_can_remove_email()
111+
{
112+
$this->contactInfo->removeEmail('[email protected]');
113+
$this->contactInfo->allEmails()->shouldBe([]);
114+
}
115+
116+
public function it_can_add_address()
117+
{
118+
$this->contactInfo->addAddress([
119+
'line1' => '123 Something Blvd.',
120+
'city' => 'Somewhere',
121+
'state_province' => 'IL',
122+
'postal_code' => '12345',
123+
'country' => 'USA',
124+
'address_type' => 'home'
125+
])->shouldBeAnInstanceOf('Scriptotek\Alma\Users\Address');
126+
$this->contactInfo->address[1]->shouldBeLike((object) [
127+
'line1' => '123 Something Blvd.',
128+
'city' => 'Somewhere',
129+
'state_province' => 'IL',
130+
'postal_code' => '12345',
131+
'country' => (object) ['value' => 'USA'],
132+
'address_type' => [(object) ['value' => 'home']]
133+
]);
134+
}
135+
136+
public function it_can_remove_address()
137+
{
138+
$address = $this->contactInfo->addresses[0];
139+
$this->contactInfo->removeAddress($address);
140+
$this->contactInfo->addresses->shouldBe([]);
141+
}
142+
143+
public function it_can_set_preferred_address()
144+
{
145+
$address = $this->contactInfo->addresses[0];
146+
$address->preferred = true;
147+
$address->preferred->shouldBe(true);
148+
$address->preferred = false;
149+
$address->preferred->shouldBe(false);
150+
}
151+
152+
public function it_can_unset_preferred_address()
153+
{
154+
$this->contactInfo->unsetPreferredAddress();
155+
$this->contactInfo->getPreferredAddress()->shouldBe(null);
156+
}
157+
158+
public function it_can_set_address_type()
159+
{
160+
$address = $this->contactInfo->addresses[0];
161+
$address->setAddressType('work', 'Work');
162+
$address->data->address_type[0]->shouldBeLike((object)[
163+
'value' => 'work',
164+
'desc' => 'Work'
165+
]);
166+
$address->address_type = 'home';
167+
$address->data->address_type[0]->shouldBeLike((object)[
168+
'value' => 'home'
169+
]);
170+
}
171+
172+
public function it_can_add_phone_number()
173+
{
174+
$this->contactInfo->addPhone('8675309', 'home', true);
175+
$this->contactInfo->getPreferredPhone()->phone_number->shouldBe('8675309');
176+
}
177+
178+
public function it_can_remove_phone_number()
179+
{
180+
$this->contactInfo->removePhone('12345678');
181+
$this->contactInfo->getPreferredPhone()->shouldBe(null);
182+
}
183+
184+
public function it_can_set_preferred_phone()
185+
{
186+
$this->contactInfo->setPreferredPhone('87654321');
187+
$this->contactInfo->getPreferredPhone()->phone_number->shouldBe('87654321');
188+
}
189+
190+
public function it_can_unset_preferred_phone()
191+
{
192+
$this->contactInfo->unsetPreferredPhone();
193+
$this->contactInfo->getPreferredPhone()->shouldBe(null);
194+
}
195+
196+
public function it_can_get_all_phone_numbers()
197+
{
198+
$phones = $this->contactInfo->allPhones();
199+
$phones[0]->phone_number->shouldBe('12345678');
200+
$phones[1]->phone_number->shouldBe('87654321');
201+
}
98202
}

src/Model/SettableModel.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Scriptotek\Alma\Model;
4+
5+
use Scriptotek\Alma\Model\Model;
6+
7+
abstract class SettableModel extends Model
8+
{
9+
/**
10+
* Magic setter function to set properties on the underlying data object
11+
*
12+
* @param string $key
13+
* @param mixed $value
14+
*/
15+
public function __set($key, $value)
16+
{
17+
// Convert electronic_collections to ElectronicCollections
18+
$key_s = implode('', array_map(function ($x) {
19+
return ucfirst($x);
20+
}, explode('_', $key)));
21+
22+
// If there's a setter method, call it.
23+
$method = 'set' . ucfirst($key_s);
24+
if (method_exists($this, $method)) {
25+
return $this->$method($value);
26+
}
27+
$method = 'set' . ucfirst($key);
28+
if (method_exists($this, $method)) {
29+
return $this->$method($value);
30+
}
31+
32+
// Otherwise set the key on the data object
33+
$this->data->{$key} = $value;
34+
}
35+
}

src/Users/Address.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Scriptotek\Alma\Users;
4+
5+
use Scriptotek\Alma\Model\SettableModel;
6+
7+
class Address extends SettableModel
8+
{
9+
/**
10+
* Set the type of this address
11+
* Possible values are listed in the 'UserAddressTypes' code table.
12+
*
13+
* @param string $address_type The address type
14+
* @param string $description The description of this address type
15+
*/
16+
public function setAddressType($address_type, $description = null)
17+
{
18+
$addressTypeObj = ['value' => $address_type];
19+
if ($description) {
20+
$addressTypeObj['desc'] = $description;
21+
}
22+
$this->data->address_type = [(object) $addressTypeObj];
23+
}
24+
}

0 commit comments

Comments
 (0)