Skip to content

Commit 711dbdd

Browse files
committed
adding a few more test cases and enabling "NULL" to be considered as "UNDEFINED" when merging
1 parent 04bb125 commit 711dbdd

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

files/constants.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,6 @@
2020
const OBJECT_MERGE_OPT_CONFLICT_EXCEPTION = 0x1;
2121
const OBJECT_MERGE_OPT_UNIQUE_ARRAYS = 0x2;
2222
const OBJECT_MERGE_OPT_MERGE_ARRAY_VALUES = 0x4;
23+
const OBJECT_MERGE_OPT_NULL_AS_UNDEFINED = 0x8;
2324

2425
define('OBJECT_MERGE_UNDEFINED', uniqid('__OBJECT_MERGE_UNDEFINED_'));

files/functions.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,14 @@
2121
if (!function_exists('object_merge_value_undefined')) {
2222
/**
2323
* @param mixed $value
24+
* @param int $opts
2425
* @return bool
2526
*/
26-
function object_merge_value_undefined($value)
27+
function object_merge_value_undefined($value, $opts = 0)
2728
{
29+
if (null === $value && 0 !== ($opts & OBJECT_MERGE_OPT_NULL_AS_UNDEFINED)) {
30+
return true;
31+
}
2832
return $value === OBJECT_MERGE_UNDEFINED;
2933
}
3034
}

src/ObjectMerge.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ private static function _compareTypes($left, $right)
183183
return [
184184
$leftType,
185185
$rightType,
186-
($leftType === $rightType) || (self::NULL_T === $leftType && self::RESOURCE_T === $rightType) || (self::RESOURCE_T === $leftType && self::NULL_T === $rightType)
186+
($leftType === $rightType)
187+
|| (self::NULL_T === $leftType && self::RESOURCE_T === $rightType)
188+
|| (self::RESOURCE_T === $leftType && self::NULL_T === $rightType)
187189
];
188190
}
189191

@@ -268,10 +270,13 @@ private static function _mergeValues($key, $leftValue, $rightValue)
268270
{
269271
self::$_context[self::$_depth] = $key;
270272

271-
$leftUndefined = OBJECT_MERGE_UNDEFINED === $leftValue;
272-
$rightUndefined = OBJECT_MERGE_UNDEFINED === $rightValue;
273+
$leftUndefined = object_merge_value_undefined($leftValue, self::$_opts);
274+
$rightUndefined = object_merge_value_undefined($rightValue, self::$_opts);
273275

274276
if ($leftUndefined && $rightUndefined) {
277+
if (self::_optSet(OBJECT_MERGE_OPT_NULL_AS_UNDEFINED)) {
278+
return null;
279+
}
275280
throw new LogicException(self::_exceptionMessage('Both left and right values are "undefined"'));
276281
}
277282

tests/ObjectMergeTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,24 @@ class ObjectMergeTest extends TestCase
153153
'expected' => '{"arr":[7]}',
154154
'recurse' => true,
155155
'opts' => OBJECT_MERGE_OPT_MERGE_ARRAY_VALUES | OBJECT_MERGE_OPT_UNIQUE_ARRAYS
156+
],
157+
[
158+
'objects' => [
159+
'{"identity_queryCriteria":{"allOf":[{"$ref":"#/definitions/common_queryCriteria"},{"properties":{"filters":{"description":"Filters used to select specific resource scope","type":"array","items":{"type":"object","additionalProperties":false,"properties":{"type":{"description":"Group type","type":"string","enum":["SYSTEM","CONTROLBLADE","DATABLADE","DOMAIN","ZONE","THIRD_PARTY_ZONE","APGROUP","WLANGROUP","INDOORMAP","AP","WLAN","SWITCH_GROUP"]},"value":{"description":"Group ID","type":"string"},"operator":{"description":"operator","type":"string","enum":["eq"]}},"required":["type","value"]}},"options":{"description":"Specified feature required information","type":"object","additionalProperties":false,"properties":{"includeSharedResources":{"description":"Whether to include the resources of parent domain or not","type":"boolean"},"INCLUDE_RBAC_METADATA":{"description":"Whether to include RBAC metadata or not","type":"boolean"},"TENANT_ID":{"description":"Specify Tenant ID for query","type":"string"},"globalFilterId":{"description":"Specify GlobalFilter ID for query","type":"string"},"localUser_auditTime":{"description":"Audit time of local users","type":"object","properties":{"start":{"description":"start time for auditTime","type":"number"},"end":{"description":"end time for auditTime","type":"number"},"interval":{"description":"time interval in second","type":"number"}}},"localUser_firstName":{"description":"First name of local users","type":"string"},"localUser_lastName":{"description":"Last name of local users","type":"string"},"localUser_mailAddress":{"description":"Mail address of local users","type":"string"},"localUser_primaryPhoneNumber":{"description":"Primary phone number of local users","type":"string"},"localUser_displayName":{"description":"Display name of local users","type":"string"},"localUser_userName":{"description":"User name of local users","type":"string"},"localUser_userSource":{"description":"User source of local users","type":"string"},"localUser_subscriberType":{"description":"Subscriber type of local users","type":"string"},"localUser_status":{"description":"Status of local users","type":"string"},"guestPass_displayName":{"description":"Display name of guest pass","type":"string"},"guestPass_expiration":{"description":"Expiration time of guest pass","type":"object","properties":{"start":{"description":"start time of expiration","type":"number"},"end":{"description":"end time of expiration","type":"number"},"interval":{"description":"time interval in second","type":"number"}}},"guestPass_wlan":{"description":"WLAN which used by quest pass","type":"string"}}},"fullTextSearch":{}}}]}}',
160+
'{"identity_queryCriteria":{"allOf":[{},{"properties":{"fullTextSearch":{"$ref":"#/definitions/common_fullTextSearch"}}}]}}',
161+
],
162+
'expected' => '{"identity_queryCriteria":{"allOf":[{"$ref":"#/definitions/common_queryCriteria"},{"properties":{"filters":{"description":"Filters used to select specific resource scope","type":"array","items":{"type":"object","additionalProperties":false,"properties":{"type":{"description":"Group type","type":"string","enum":["SYSTEM","CONTROLBLADE","DATABLADE","DOMAIN","ZONE","THIRD_PARTY_ZONE","APGROUP","WLANGROUP","INDOORMAP","AP","WLAN","SWITCH_GROUP"]},"value":{"description":"Group ID","type":"string"},"operator":{"description":"operator","type":"string","enum":["eq"]}},"required":["type","value"]}},"options":{"description":"Specified feature required information","type":"object","additionalProperties":false,"properties":{"includeSharedResources":{"description":"Whether to include the resources of parent domain or not","type":"boolean"},"INCLUDE_RBAC_METADATA":{"description":"Whether to include RBAC metadata or not","type":"boolean"},"TENANT_ID":{"description":"Specify Tenant ID for query","type":"string"},"globalFilterId":{"description":"Specify GlobalFilter ID for query","type":"string"},"localUser_auditTime":{"description":"Audit time of local users","type":"object","properties":{"start":{"description":"start time for auditTime","type":"number"},"end":{"description":"end time for auditTime","type":"number"},"interval":{"description":"time interval in second","type":"number"}}},"localUser_firstName":{"description":"First name of local users","type":"string"},"localUser_lastName":{"description":"Last name of local users","type":"string"},"localUser_mailAddress":{"description":"Mail address of local users","type":"string"},"localUser_primaryPhoneNumber":{"description":"Primary phone number of local users","type":"string"},"localUser_displayName":{"description":"Display name of local users","type":"string"},"localUser_userName":{"description":"User name of local users","type":"string"},"localUser_userSource":{"description":"User source of local users","type":"string"},"localUser_subscriberType":{"description":"Subscriber type of local users","type":"string"},"localUser_status":{"description":"Status of local users","type":"string"},"guestPass_displayName":{"description":"Display name of guest pass","type":"string"},"guestPass_expiration":{"description":"Expiration time of guest pass","type":"object","properties":{"start":{"description":"start time of expiration","type":"number"},"end":{"description":"end time of expiration","type":"number"},"interval":{"description":"time interval in second","type":"number"}}},"guestPass_wlan":{"description":"WLAN which used by quest pass","type":"string"}}},"fullTextSearch":{"$ref":"#/definitions/common_fullTextSearch"}}}]}}',
163+
'recurse' => true,
164+
'opts' => OBJECT_MERGE_OPT_MERGE_ARRAY_VALUES | OBJECT_MERGE_OPT_UNIQUE_ARRAYS
165+
],
166+
[
167+
'objects' => [
168+
'{"title":"Unique Clients Trend Over Time","queryName":"trendTable","systemOwnerOnly":false,"component":"ReportTable","defaultParameters":{"granularity":"fifteen_minute","metric":"traffic","limit":10},"layout":{"width":"full","widgetTheme":"blue","headers":[{"component":"GranularityFilter","name":"limit","options":{"traffic":"User Traffic","rxBytes":"Rx User","txBytes":"Tx User","0":"All","10":"Top 10 Clients","20":"Top 20 Clients","50":"Top 50 Clients","100":"Top 100 Clients"}},{"component":"PeriodFilter","query":"topClientTrafficPercentage","content":{"text":"These ${name} consume <b>${percentage}</b> ( <b>${traffic}</b> ) of all user traffic ( <b>${totalTraffic}</b> ).","formats":{"totalTraffic":"bytesFormat","percentage":"percentFormat","traffic":"bytesFormat"}}}],"format":"bytesFormat","colors":["#5BA1E0","#76CEF5","#D9E6F5"],"drillDownRoute":"/report/client/${x}","subSections":[{"component":"LineChart","layout":{"width":"full","label":"authMethod","value":"clientCount","title":{"default":"Clients"},"formatMetadata":{"totalClients":"countFormat","subTitle":"${templateData.totalClients}"},"labelFormat":"label-value2","series":[{"color":"#66b1e8","key":"All Radios","values":"unique_users"},{"color":"#46a3bb","key":"2.4 GHz","values":"unique_users_2-4","area":true},{"color":"#beeee0","key":"5 GHz","values":"unique_users_5","area":true},{"color":"#F17CB0"},{"color":"#8c7024"},{"color":"#B276B2"},{"color":"#eddc44"},{"color":"#F15854"},{"color":"#26657c"},{"color":"#4D4D4D"}],"xAxisType":"time","yAxisType":"countFormat"},"title":"Client Count"},{"component":"LineChart","layout":{"hideLegendLabels":true,"xAxisType":"time","yAxisType":"bytesFormat","width":"full","series":[{"color":"#66b1e8","disabled":false,"key":"User Traffic","values":"traffic"},{"color":"#4e9ee6","disabled":false,"key":"Rx User","values":"rxBytes","area":true},{"color":"#cee9fa","disabled":true,"key":"Tx User","values":"txBytes","area":true},{"color":"#F17CB0","disabled":true},{"color":"#8c7024","disabled":true},{"color":"#B276B2","disabled":true},{"color":"#eddc44","disabled":true},{"color":"#F15854","disabled":true},{"color":"#26657c","disabled":true},{"color":"#4D4D4D","disabled":true}]},"title":"Traffic"}],"columns":[{"columnName":"timestamp","displayName":"Time Period","customComponent":"Interval"},{"columnName":"unique_users_2-4","displayName":"2.4 GHz","customComponent":"PercentBar","drillDownRoute":"/report/client/${clientMac}","color":"#5BE0C7"},{"columnName":"unique_users_5","displayName":"5 GHz","customComponent":"PercentBar","drillDownRoute":"/report/client/${clientMac}","hidden":true,"color":"#5BE0C7"},{"columnName":"unique_users","displayName":"Total","customComponent":"PercentBar","color":"#5BE0C7"},{"columnName":"rxBytes","displayName":"Rx User","customComponent":"PercentBar","format":"bytesFormat"},{"columnName":"txBytes","displayName":"Tx User","customComponent":"PercentBar","format":"bytesFormat"},{"columnName":"traffic","displayName":"User Traffic","customComponent":"PercentBar","format":"bytesFormat"},{"columnName":"manufacturer","displayName":"Manufacturer","hidden":true},{"columnName":"osType","displayName":"OS","hidden":true}]},"url":null,"id":16}',
169+
'{"title":"Top Clients by Traffic Percentile","queryName":"topPercentile","systemOwnerOnly":false,"component":null,"defaultParameters":{"granularity":"all"},"layout":null,"url":null,"id":17}'
170+
],
171+
'expected' => '{"title":"Top Clients by Traffic Percentile","queryName":"topPercentile","systemOwnerOnly":false,"component":"ReportTable","defaultParameters":{"granularity":"all","metric":"traffic","limit":10},"layout":{"width":"full","widgetTheme":"blue","headers":[{"component":"GranularityFilter","name":"limit","options":{"traffic":"User Traffic","rxBytes":"Rx User","txBytes":"Tx User","0":"All","10":"Top 10 Clients","20":"Top 20 Clients","50":"Top 50 Clients","100":"Top 100 Clients"}},{"component":"PeriodFilter","query":"topClientTrafficPercentage","content":{"text":"These ${name} consume <b>${percentage}</b> ( <b>${traffic}</b> ) of all user traffic ( <b>${totalTraffic}</b> ).","formats":{"totalTraffic":"bytesFormat","percentage":"percentFormat","traffic":"bytesFormat"}}}],"format":"bytesFormat","colors":["#5BA1E0","#76CEF5","#D9E6F5"],"drillDownRoute":"/report/client/${x}","subSections":[{"component":"LineChart","layout":{"width":"full","label":"authMethod","value":"clientCount","title":{"default":"Clients"},"formatMetadata":{"totalClients":"countFormat","subTitle":"${templateData.totalClients}"},"labelFormat":"label-value2","series":[{"color":"#66b1e8","key":"All Radios","values":"unique_users"},{"color":"#46a3bb","key":"2.4 GHz","values":"unique_users_2-4","area":true},{"color":"#beeee0","key":"5 GHz","values":"unique_users_5","area":true},{"color":"#F17CB0"},{"color":"#8c7024"},{"color":"#B276B2"},{"color":"#eddc44"},{"color":"#F15854"},{"color":"#26657c"},{"color":"#4D4D4D"}],"xAxisType":"time","yAxisType":"countFormat"},"title":"Client Count"},{"component":"LineChart","layout":{"hideLegendLabels":true,"xAxisType":"time","yAxisType":"bytesFormat","width":"full","series":[{"color":"#66b1e8","disabled":false,"key":"User Traffic","values":"traffic"},{"color":"#4e9ee6","disabled":false,"key":"Rx User","values":"rxBytes","area":true},{"color":"#cee9fa","disabled":true,"key":"Tx User","values":"txBytes","area":true},{"color":"#F17CB0","disabled":true},{"color":"#8c7024","disabled":true},{"color":"#B276B2","disabled":true},{"color":"#eddc44","disabled":true},{"color":"#F15854","disabled":true},{"color":"#26657c","disabled":true},{"color":"#4D4D4D","disabled":true}]},"title":"Traffic"}],"columns":[{"columnName":"timestamp","displayName":"Time Period","customComponent":"Interval"},{"columnName":"unique_users_2-4","displayName":"2.4 GHz","customComponent":"PercentBar","drillDownRoute":"/report/client/${clientMac}","color":"#5BE0C7"},{"columnName":"unique_users_5","displayName":"5 GHz","customComponent":"PercentBar","drillDownRoute":"/report/client/${clientMac}","hidden":true,"color":"#5BE0C7"},{"columnName":"unique_users","displayName":"Total","customComponent":"PercentBar","color":"#5BE0C7"},{"columnName":"rxBytes","displayName":"Rx User","customComponent":"PercentBar","format":"bytesFormat"},{"columnName":"txBytes","displayName":"Tx User","customComponent":"PercentBar","format":"bytesFormat"},{"columnName":"traffic","displayName":"User Traffic","customComponent":"PercentBar","format":"bytesFormat"},{"columnName":"manufacturer","displayName":"Manufacturer","hidden":true},{"columnName":"osType","displayName":"OS","hidden":true}]},"url":null,"id":17}',
172+
'recurse' => true,
173+
'opts' => OBJECT_MERGE_OPT_MERGE_ARRAY_VALUES | OBJECT_MERGE_OPT_UNIQUE_ARRAYS | OBJECT_MERGE_OPT_NULL_AS_UNDEFINED
156174
]
157175
);
158176

0 commit comments

Comments
 (0)