Skip to content

Commit d329340

Browse files
committed
Merge pull request #59 from brianjmiller/issue48
Adjust `asVersion` trait to handle arrays and empty values
2 parents 3aa6057 + 608ec2f commit d329340

22 files changed

+801
-136
lines changed

src/Agent.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,14 @@ public function asVersion($version) {
4646
}
4747

4848
//
49-
// only one of these
49+
// only one of these, note that if 'account' has been set
50+
// but is returned empty then no IFI will be included
5051
//
5152
if (isset($this->account)) {
52-
$result['account'] = $this->account->asVersion($version);
53+
$versioned_acct = $this->account->asVersion($version);
54+
if (! empty($versioned_acct)) {
55+
$result['account'] = $versioned_acct;
56+
}
5357
}
5458
elseif (isset($this->mbox_sha1sum)) {
5559
$result['mbox_sha1sum'] = $this->mbox_sha1sum;
@@ -90,7 +94,7 @@ public function compareWithSignature($fromSig) {
9094

9195
return array('success' => false, 'reason' => 'Comparison of this.mbox to signature.mbox_sha1sum failed: no match');
9296
}
93-
else if (isset($fromSig->mbox) && isset($this->mbox_sha1sum)) {
97+
elseif (isset($fromSig->mbox) && isset($this->mbox_sha1sum)) {
9498
if ($fromSig->getMbox_sha1sum() === $this->mbox_sha1sum) {
9599
return array('success' => true, 'reason' => null);
96100
}

src/AsVersionTrait.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,20 @@ public function asVersion($version) {
4646
if ($value instanceof VersionableInterface) {
4747
$value = $value->asVersion($version);
4848
}
49-
if (isset($value)) {
49+
else if (is_array($value) && !empty($value)) {
50+
$tmp_value = array();
51+
foreach ($value as $element) {
52+
if ($element instanceof VersionableInterface) {
53+
array_push($tmp_value, $element->asVersion($version));
54+
}
55+
else {
56+
array_push($tmp_value, $element);
57+
}
58+
}
59+
$value = $tmp_value;
60+
}
61+
62+
if (isset($value) && (!is_array($value) || !empty($value))) {
5063
$result[$property] = $value;
5164
}
5265
}

src/Context.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -129,15 +129,4 @@ public function setExtensions($value) {
129129
return $this;
130130
}
131131
public function getExtensions() { return $this->extensions; }
132-
133-
private function _asVersion(array &$result, $version) {
134-
foreach ($result as $property => $value) {
135-
if (! isset($value)) {
136-
unset($result[$property]);
137-
}
138-
elseif ($value instanceof VersionableInterface) {
139-
$result[$property] = $value->asVersion($version);
140-
}
141-
}
142-
}
143132
}

src/ContextActivities.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ public function __construct() {
3434
}
3535
}
3636

37-
private function _asVersion(array &$result, $version) {
38-
foreach ($result as $property => $value) {
39-
if (empty($value)) {
40-
unset($result[$property]);
41-
}
42-
elseif (is_array($value)) {
43-
$this->_asVersion($value, $version);
44-
$result[$property] = $value;
45-
}
46-
elseif ($value instanceof VersionableInterface) {
47-
$result[$property] = $value->asVersion($version);
48-
}
49-
}
50-
}
51-
5237
private function _listSetter($prop, $value) {
5338
if (is_array($value)) {
5439
if (isset($value['id'])) {

src/Result.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ public function __construct() {
4040
}
4141
}
4242

43+
private function _asVersion(&$result, $version) {
44+
//
45+
// empty string is an invalid duration
46+
//
47+
if (isset($result['duration']) && $result['duration'] == '') {
48+
unset($result['duration']);
49+
}
50+
}
51+
4352
public function setScore($value) {
4453
if (! $value instanceof Score && is_array($value)) {
4554
$value = new Score($value);

src/StatementBase.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,18 +59,6 @@ public function __construct() {
5959
}
6060

6161
private function _asVersion(&$result, $version) {
62-
foreach ($result as $property => $value) {
63-
if ($value !== false && empty($value)) {
64-
unset($result[$property]);
65-
}
66-
elseif (is_array($value)) {
67-
$this->_asVersion($value, $version);
68-
$result[$property] = $value;
69-
}
70-
elseif ($value instanceof VersionableInterface) {
71-
$result[$property] = $value->asVersion($version);
72-
}
73-
}
7462
if (isset($result['target'])) {
7563
$result['object'] = $result['target'];
7664
unset($result['target']);

tests/ActivityDefinitionTest.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,55 @@ public function testUsesAsVersionTrait() {
6565

6666
// TODO: need more robust test (happy-path)
6767
public function testAsVersion() {
68-
$args = ['name' => [self::NAME]];
69-
$obj = new ActivityDefinition($args);
70-
$versioned = $obj->asVersion('test');
68+
$args = [
69+
'name' => ['en' => self::NAME],
70+
'extensions' => []
71+
];
72+
$args['extensions'][COMMON_EXTENSION_ID_1] = 'test';
7173

72-
$this->assertEquals($versioned, $args, "name only: test");
74+
$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
75+
$versioned = $obj->asVersion('1.0.0');
76+
77+
$this->assertEquals($versioned, $args, "serialized version matches original");
78+
}
79+
80+
public function testAsVersionEmpty() {
81+
$args = [];
82+
83+
$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
84+
$versioned = $obj->asVersion('1.0.0');
85+
86+
$this->assertEquals($versioned, $args, "serialized version matches original");
87+
}
88+
89+
public function testAsVersionEmptyLanguageMap() {
90+
$args = ['name' => []];
91+
92+
$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
93+
$versioned = $obj->asVersion('1.0.0');
94+
95+
unset($args['name']);
96+
97+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
98+
}
99+
100+
public function testAsVersionEmptyExtensions() {
101+
$args = ['extensions' => []];
102+
103+
$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
104+
$versioned = $obj->asVersion('1.0.0');
105+
106+
unset($args['extensions']);
107+
108+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
109+
}
110+
111+
public function testAsVersionEmptyStringInLanguageMap() {
112+
$args = ['name' => ['en' => '']];
113+
114+
$obj = ActivityDefinition::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
115+
$versioned = $obj->asVersion('1.0.0');
116+
117+
$this->assertEquals($versioned, $args, "serialized version matches original");
73118
}
74119
}

tests/ActivityTest.php

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,58 @@ public function testFromJSONIDOnly() {
7575

7676
// TODO: need to loop versions
7777
public function testAsVersion() {
78-
$obj = new Activity(
79-
array('id' => COMMON_ACTIVITY_ID)
80-
);
78+
$args = [
79+
'id' => COMMON_ACTIVITY_ID,
80+
'definition' => [
81+
'name' => [
82+
'en' => 'test'
83+
]
84+
]
85+
];
86+
87+
$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
8188
$versioned = $obj->asVersion('1.0.0');
8289

83-
$this->assertEquals(
84-
$versioned,
85-
[ 'objectType' => 'Activity', 'id' => COMMON_ACTIVITY_ID ],
86-
"id only: 1.0.0"
87-
);
90+
$args['objectType'] = 'Activity';
91+
92+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
93+
}
94+
95+
public function testAsVersionEmpty() {
96+
$args = [];
97+
98+
$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
99+
$versioned = $obj->asVersion('1.0.0');
100+
101+
$args['objectType'] = 'Activity';
102+
103+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
104+
}
105+
106+
public function testAsVersionIdOnly() {
107+
$args = [ 'id' => COMMON_ACTIVITY_ID ];
108+
109+
$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
110+
$versioned = $obj->asVersion('1.0.0');
111+
112+
$args['objectType'] = 'Activity';
113+
114+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
115+
}
116+
117+
public function testAsVersionEmptyDefinition() {
118+
$args = [
119+
'id' => COMMON_ACTIVITY_ID,
120+
'definition' => []
121+
];
122+
123+
$obj = Activity::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
124+
$versioned = $obj->asVersion('1.0.0');
125+
126+
$args['objectType'] = 'Activity';
127+
unset($args['definition']);
128+
129+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
88130
}
89131

90132
public function testCompareWithSignature() {

tests/AgentAccountTest.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,36 @@ public function testHomePage() {
5959
}
6060

6161
public function testAsVersion() {
62-
$args = ['name' => COMMON_ACCT_NAME, 'homePage' => COMMON_ACCT_HOMEPAGE];
63-
$obj = new AgentAccount($args);
62+
$args = [
63+
'name' => COMMON_ACCT_NAME,
64+
'homePage' => COMMON_ACCT_HOMEPAGE
65+
];
66+
67+
$obj = AgentAccount::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
68+
$versioned = $obj->asVersion('1.0.0');
69+
70+
$this->assertEquals($versioned, $args, "serialized version matches original");
71+
}
72+
73+
public function testAsVersionEmpty() {
74+
$args = [];
75+
76+
$obj = AgentAccount::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
77+
$versioned = $obj->asVersion('1.0.0');
78+
79+
$this->assertEquals($versioned, $args, "serialized version matches original");
80+
}
81+
82+
public function testAsVersionEmptyStrings() {
83+
$args = [
84+
'name' => '',
85+
'homePage' => ''
86+
];
87+
88+
$obj = AgentAccount::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
6489
$versioned = $obj->asVersion('1.0.0');
6590

66-
$this->assertEquals($versioned, $args, "all properties: test");
91+
$this->assertEquals($versioned, $args, "serialized version matches original");
6792
}
6893

6994
public function testCompareWithSignature() {

tests/AgentTest.php

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,87 @@ public function testFromJSONInstantiations() {
6565
}
6666

6767
// TODO: need to loop versions
68-
public function testAsVersion() {
69-
$obj = new Agent(
70-
[ 'mbox' => COMMON_MBOX ]
71-
);
68+
public function testAsVersionMbox() {
69+
$args = [
70+
'mbox' => COMMON_MBOX
71+
];
72+
73+
$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
7274
$versioned = $obj->asVersion('1.0.0');
7375

74-
$this->assertEquals(
75-
[ 'objectType' => 'Agent', 'mbox' => COMMON_MBOX ],
76-
$versioned,
77-
"mbox only: 1.0.0"
78-
);
76+
$args['objectType'] = 'Agent';
77+
78+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
79+
}
80+
81+
public function testAsVersionMboxSha1() {
82+
$args = [
83+
'mbox_sha1sum' => COMMON_MBOX_SHA1
84+
];
85+
86+
$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
87+
$versioned = $obj->asVersion('1.0.0');
88+
89+
$args['objectType'] = 'Agent';
90+
91+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
92+
}
93+
94+
public function testAsVersionAccount() {
95+
$args = [
96+
'account' => [
97+
'name' => COMMON_ACCT_NAME,
98+
'homePage' => COMMON_ACCT_HOMEPAGE
99+
]
100+
];
101+
102+
$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
103+
$versioned = $obj->asVersion('1.0.0');
104+
105+
$args['objectType'] = 'Agent';
106+
107+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
108+
}
109+
110+
public function testAsVersionAccountEmptyStrings() {
111+
$args = [
112+
'account' => [
113+
'name' => '',
114+
'homePage' => ''
115+
]
116+
];
117+
118+
$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
119+
$versioned = $obj->asVersion('1.0.0');
120+
121+
$args['objectType'] = 'Agent';
122+
123+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
124+
}
125+
126+
public function testAsVersionEmptyAccount() {
127+
$args = [
128+
'account' => []
129+
];
130+
131+
$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
132+
$versioned = $obj->asVersion('1.0.0');
133+
134+
$args['objectType'] = 'Agent';
135+
unset($args['account']);
136+
137+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
138+
}
139+
140+
public function testAsVersionEmpty() {
141+
$args = [];
142+
143+
$obj = Agent::fromJSON(json_encode($args, JSON_UNESCAPED_SLASHES));
144+
$versioned = $obj->asVersion('1.0.0');
145+
146+
$args['objectType'] = 'Agent';
147+
148+
$this->assertEquals($versioned, $args, "serialized version matches corrected");
79149
}
80150

81151
public function testIsIdentified() {

0 commit comments

Comments
 (0)