Skip to content

Commit d7213c5

Browse files
committed
Make the timestamp parameter optional for interaction additions - default is the current time
1 parent 800d3fc commit d7213c5

14 files changed

+79
-60
lines changed

src/RecommApi/Requests/AddBookmark.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AddBookmark extends Request {
2323
*/
2424
protected $item_id;
2525
/**
26-
* @var string|float $timestamp Unix timestamp of the bookmark. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
26+
* @var string|float $timestamp UTC timestamp of the bookmark as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
2727
*/
2828
protected $timestamp;
2929
/**
@@ -39,22 +39,24 @@ class AddBookmark extends Request {
3939
* Construct the request
4040
* @param string $user_id User who bookmarked the item
4141
* @param string $item_id Bookmarked item
42-
* @param string|float $timestamp Unix timestamp of the bookmark. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
4342
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
4443
* - Allowed parameters:
44+
* - *timestamp*
45+
* - Type: string|float
46+
* - Description: UTC timestamp of the bookmark as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
4547
* - *cascadeCreate*
4648
* - Type: bool
4749
* - Description: Sets whether the given user/item should be created if not present in the database.
4850
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
4951
*/
50-
public function __construct($user_id, $item_id, $timestamp, $optional = array()) {
52+
public function __construct($user_id, $item_id, $optional = array()) {
5153
$this->user_id = $user_id;
5254
$this->item_id = $item_id;
53-
$this->timestamp = $timestamp;
55+
$this->timestamp = isset($optional['timestamp']) ? $optional['timestamp'] : null;
5456
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
5557
$this->optional = $optional;
5658

57-
$existing_optional = array('cascadeCreate');
59+
$existing_optional = array('timestamp','cascadeCreate');
5860
foreach ($this->optional as $key => $value) {
5961
if (!in_array($key, $existing_optional))
6062
throw new UnknownOptionalParameterException($key);
@@ -95,7 +97,8 @@ public function getBodyParameters() {
9597
$p = array();
9698
$p['userId'] = $this->user_id;
9799
$p['itemId'] = $this->item_id;
98-
$p['timestamp'] = $this->timestamp;
100+
if (isset($this->optional['timestamp']))
101+
$p['timestamp'] = $this-> optional['timestamp'];
99102
if (isset($this->optional['cascadeCreate']))
100103
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
101104
return $p;

src/RecommApi/Requests/AddCartAddition.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AddCartAddition extends Request {
2323
*/
2424
protected $item_id;
2525
/**
26-
* @var string|float $timestamp Unix timestamp of the cart addition. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
26+
* @var string|float $timestamp UTC timestamp of the cart addition as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
2727
*/
2828
protected $timestamp;
2929
/**
@@ -39,22 +39,24 @@ class AddCartAddition extends Request {
3939
* Construct the request
4040
* @param string $user_id User who added the item to the cart
4141
* @param string $item_id Item added to the cart
42-
* @param string|float $timestamp Unix timestamp of the cart addition. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
4342
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
4443
* - Allowed parameters:
44+
* - *timestamp*
45+
* - Type: string|float
46+
* - Description: UTC timestamp of the cart addition as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
4547
* - *cascadeCreate*
4648
* - Type: bool
4749
* - Description: Sets whether the given user/item should be created if not present in the database.
4850
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
4951
*/
50-
public function __construct($user_id, $item_id, $timestamp, $optional = array()) {
52+
public function __construct($user_id, $item_id, $optional = array()) {
5153
$this->user_id = $user_id;
5254
$this->item_id = $item_id;
53-
$this->timestamp = $timestamp;
55+
$this->timestamp = isset($optional['timestamp']) ? $optional['timestamp'] : null;
5456
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
5557
$this->optional = $optional;
5658

57-
$existing_optional = array('cascadeCreate');
59+
$existing_optional = array('timestamp','cascadeCreate');
5860
foreach ($this->optional as $key => $value) {
5961
if (!in_array($key, $existing_optional))
6062
throw new UnknownOptionalParameterException($key);
@@ -95,7 +97,8 @@ public function getBodyParameters() {
9597
$p = array();
9698
$p['userId'] = $this->user_id;
9799
$p['itemId'] = $this->item_id;
98-
$p['timestamp'] = $this->timestamp;
100+
if (isset($this->optional['timestamp']))
101+
$p['timestamp'] = $this-> optional['timestamp'];
99102
if (isset($this->optional['cascadeCreate']))
100103
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
101104
return $p;

src/RecommApi/Requests/AddDetailView.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AddDetailView extends Request {
2323
*/
2424
protected $item_id;
2525
/**
26-
* @var string|float $timestamp Unix timestamp of the view. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
26+
* @var string|float $timestamp UTC timestamp of the view as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
2727
*/
2828
protected $timestamp;
2929
/**
@@ -43,9 +43,11 @@ class AddDetailView extends Request {
4343
* Construct the request
4444
* @param string $user_id User who viewed the item
4545
* @param string $item_id Viewed item
46-
* @param string|float $timestamp Unix timestamp of the view. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
4746
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
4847
* - Allowed parameters:
48+
* - *timestamp*
49+
* - Type: string|float
50+
* - Description: UTC timestamp of the view as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
4951
* - *duration*
5052
* - Type: int
5153
* - Description: Duration of the view
@@ -54,15 +56,15 @@ class AddDetailView extends Request {
5456
* - Description: Sets whether the given user/item should be created if not present in the database.
5557
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
5658
*/
57-
public function __construct($user_id, $item_id, $timestamp, $optional = array()) {
59+
public function __construct($user_id, $item_id, $optional = array()) {
5860
$this->user_id = $user_id;
5961
$this->item_id = $item_id;
60-
$this->timestamp = $timestamp;
62+
$this->timestamp = isset($optional['timestamp']) ? $optional['timestamp'] : null;
6163
$this->duration = isset($optional['duration']) ? $optional['duration'] : null;
6264
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
6365
$this->optional = $optional;
6466

65-
$existing_optional = array('duration','cascadeCreate');
67+
$existing_optional = array('timestamp','duration','cascadeCreate');
6668
foreach ($this->optional as $key => $value) {
6769
if (!in_array($key, $existing_optional))
6870
throw new UnknownOptionalParameterException($key);
@@ -103,7 +105,8 @@ public function getBodyParameters() {
103105
$p = array();
104106
$p['userId'] = $this->user_id;
105107
$p['itemId'] = $this->item_id;
106-
$p['timestamp'] = $this->timestamp;
108+
if (isset($this->optional['timestamp']))
109+
$p['timestamp'] = $this-> optional['timestamp'];
107110
if (isset($this->optional['duration']))
108111
$p['duration'] = $this-> optional['duration'];
109112
if (isset($this->optional['cascadeCreate']))

src/RecommApi/Requests/AddPurchase.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AddPurchase extends Request {
2323
*/
2424
protected $item_id;
2525
/**
26-
* @var string|float $timestamp Unix timestamp of the purchase. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
26+
* @var string|float $timestamp UTC timestamp of the purchase as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
2727
*/
2828
protected $timestamp;
2929
/**
@@ -39,22 +39,24 @@ class AddPurchase extends Request {
3939
* Construct the request
4040
* @param string $user_id User who purchased the item
4141
* @param string $item_id Purchased item
42-
* @param string|float $timestamp Unix timestamp of the purchase. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
4342
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
4443
* - Allowed parameters:
44+
* - *timestamp*
45+
* - Type: string|float
46+
* - Description: UTC timestamp of the purchase as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
4547
* - *cascadeCreate*
4648
* - Type: bool
4749
* - Description: Sets whether the given user/item should be created if not present in the database.
4850
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
4951
*/
50-
public function __construct($user_id, $item_id, $timestamp, $optional = array()) {
52+
public function __construct($user_id, $item_id, $optional = array()) {
5153
$this->user_id = $user_id;
5254
$this->item_id = $item_id;
53-
$this->timestamp = $timestamp;
55+
$this->timestamp = isset($optional['timestamp']) ? $optional['timestamp'] : null;
5456
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
5557
$this->optional = $optional;
5658

57-
$existing_optional = array('cascadeCreate');
59+
$existing_optional = array('timestamp','cascadeCreate');
5860
foreach ($this->optional as $key => $value) {
5961
if (!in_array($key, $existing_optional))
6062
throw new UnknownOptionalParameterException($key);
@@ -95,7 +97,8 @@ public function getBodyParameters() {
9597
$p = array();
9698
$p['userId'] = $this->user_id;
9799
$p['itemId'] = $this->item_id;
98-
$p['timestamp'] = $this->timestamp;
100+
if (isset($this->optional['timestamp']))
101+
$p['timestamp'] = $this-> optional['timestamp'];
99102
if (isset($this->optional['cascadeCreate']))
100103
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
101104
return $p;

src/RecommApi/Requests/AddRating.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class AddRating extends Request {
2323
*/
2424
protected $item_id;
2525
/**
26-
* @var string|float $timestamp Unix timestamp of the rating. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
26+
* @var string|float $timestamp UTC timestamp of the rating as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
2727
*/
2828
protected $timestamp;
2929
/**
@@ -43,24 +43,26 @@ class AddRating extends Request {
4343
* Construct the request
4444
* @param string $user_id User who submitted the rating
4545
* @param string $item_id Rated item
46-
* @param string|float $timestamp Unix timestamp of the rating. If you don't have the timestamp value available, you may use some artificial value, such as 0. It is preferable, however, to provide the timestamp whenever possible as the user's preferences may evolve over time.
4746
* @param float $rating Rating rescaled to interval [-1.0,1.0], where -1.0 means the worst rating possible, 0.0 means neutral, and 1.0 means absolutely positive rating. For example, in the case of 5-star evaluations, rating = (numStars-3)/2 formula may be used for the conversion.
4847
* @param array $optional Optional parameters given as an array containing pairs name of the parameter => value
4948
* - Allowed parameters:
49+
* - *timestamp*
50+
* - Type: string|float
51+
* - Description: UTC timestamp of the rating as ISO8601-1 pattern or UTC epoch time. The default value is the current time.
5052
* - *cascadeCreate*
5153
* - Type: bool
5254
* - Description: Sets whether the given user/item should be created if not present in the database.
5355
* @throws Exceptions\UnknownOptionalParameterException UnknownOptionalParameterException if an unknown optional parameter is given in $optional
5456
*/
55-
public function __construct($user_id, $item_id, $timestamp, $rating, $optional = array()) {
57+
public function __construct($user_id, $item_id, $rating, $optional = array()) {
5658
$this->user_id = $user_id;
5759
$this->item_id = $item_id;
58-
$this->timestamp = $timestamp;
5960
$this->rating = $rating;
61+
$this->timestamp = isset($optional['timestamp']) ? $optional['timestamp'] : null;
6062
$this->cascade_create = isset($optional['cascadeCreate']) ? $optional['cascadeCreate'] : null;
6163
$this->optional = $optional;
6264

63-
$existing_optional = array('cascadeCreate');
65+
$existing_optional = array('timestamp','cascadeCreate');
6466
foreach ($this->optional as $key => $value) {
6567
if (!in_array($key, $existing_optional))
6668
throw new UnknownOptionalParameterException($key);
@@ -101,8 +103,9 @@ public function getBodyParameters() {
101103
$p = array();
102104
$p['userId'] = $this->user_id;
103105
$p['itemId'] = $this->item_id;
104-
$p['timestamp'] = $this->timestamp;
105106
$p['rating'] = $this->rating;
107+
if (isset($this->optional['timestamp']))
108+
$p['timestamp'] = $this-> optional['timestamp'];
106109
if (isset($this->optional['cascadeCreate']))
107110
$p['cascadeCreate'] = $this-> optional['cascadeCreate'];
108111
return $p;

tests/AddBookmarkTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class AddBookmarkTest extends AddInteractionTestCase {
66

7-
protected function createRequest($user_id, $item_id, $timestamp, $optional=array()) {
8-
return new AddBookmark($user_id, $item_id, $timestamp, $optional);
7+
protected function createRequest($user_id, $item_id, $optional=array()) {
8+
return new AddBookmark($user_id, $item_id, $optional);
99
}
1010
}
1111
?>

tests/AddCartAdditionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class AddCartAdditionTest extends AddInteractionTestCase {
66

7-
protected function createRequest($user_id, $item_id, $timestamp, $optional=array()) {
8-
return new AddCartAddition($user_id, $item_id, $timestamp, $optional);
7+
protected function createRequest($user_id, $item_id, $optional=array()) {
8+
return new AddCartAddition($user_id, $item_id, $optional);
99
}
1010
}
1111
?>

tests/AddDetailViewTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class AddDetailViewTest extends AddInteractionTestCase {
66

7-
protected function createRequest($user_id, $item_id, $timestamp, $optional=array()) {
8-
return new AddDetailView($user_id, $item_id, $timestamp, $optional);
7+
protected function createRequest($user_id, $item_id, $optional=array()) {
8+
return new AddDetailView($user_id, $item_id, $optional);
99
}
1010
}
1111
?>

tests/AddInteractionTestCase.php

+11-7
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,26 @@
66

77
abstract class AddInteractionTestCase extends RecombeeTestCase {
88

9-
abstract protected function createRequest($user_id, $item_id, $timestamp, $optional = array());
9+
abstract protected function createRequest($user_id, $item_id, $optional = array());
1010

1111
public function testAddInteraction() {
1212

1313
//does not fail with cascadeCreate
14-
$req = $this->createRequest('u_id', 'i_id', 0, ['cascadeCreate' => True]);
14+
$req = $this->createRequest('u_id', 'i_id', ['cascadeCreate' => True]);
15+
$this->client->send($req);
16+
17+
//does not fail with valid timestamp
18+
$req = $this->createRequest('u_id', 'i_id', ['cascadeCreate' => True, 'timestamp' => '2013-10-29T09:38:41.341Z']);
1519
$this->client->send($req);
1620

1721
//does not fail with existing item and user
18-
$req = $this->createRequest('entity_id', 'entity_id', 0);
22+
$req = $this->createRequest('entity_id', 'entity_id');
1923
$this->client->send($req);
2024

2125
//fails with nonexisting item id
2226
try {
2327

24-
$req = $this->createRequest('entity_id', 'notexisting_id', 0);
28+
$req = $this->createRequest('entity_id', 'notexisting_id');
2529
$this->client->send($req);
2630
throw new \Exception('Exception was not thrown');
2731
}
@@ -32,7 +36,7 @@ public function testAddInteraction() {
3236

3337
//fails with nonexisting user id
3438
try {
35-
$req = $this->createRequest('notexisting_id', 'entity_id', 0);
39+
$req = $this->createRequest('notexisting_id', 'entity_id');
3640
$this->client->send($req);
3741
throw new \Exception('Exception was not thrown');
3842
}
@@ -43,7 +47,7 @@ public function testAddInteraction() {
4347

4448
//fails with invalid time
4549
try {
46-
$req = $this->createRequest('entity_id', 'entity_id', -15);
50+
$req = $this->createRequest('entity_id', 'entity_id', ['timestamp' => -15]);
4751
$this->client->send($req);
4852
throw new \Exception('Exception was not thrown');
4953
}
@@ -54,7 +58,7 @@ public function testAddInteraction() {
5458

5559
//really stores interaction to the system
5660
try {
57-
$req = $this->createRequest('entity_id', 'entity_id', 0);
61+
$req = $this->createRequest('u_id', 'i_id', ['timestamp' => '2013-10-29T09:38:41.341Z']);
5862
$this->client->send($req);
5963
throw new \Exception('Exception was not thrown');
6064
}

tests/AddPurchaseTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
class AddPurchaseTest extends AddInteractionTestCase {
66

7-
protected function createRequest($user_id, $item_id, $timestamp, $optional=array()) {
8-
return new AddPurchase($user_id, $item_id, $timestamp, $optional);
7+
protected function createRequest($user_id, $item_id, $optional=array()) {
8+
return new AddPurchase($user_id, $item_id, $optional);
99
}
1010
}
1111
?>

0 commit comments

Comments
 (0)