Skip to content

Commit eb3988f

Browse files
committed
Cover ConnectionBuilderTest
1 parent 983b5c7 commit eb3988f

File tree

1 file changed

+40
-9
lines changed

1 file changed

+40
-9
lines changed

Relay/Connection/Output/ConnectionBuilder.php

+40-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class ConnectionBuilder
2222
* @param array $args
2323
* @return Connection
2424
*/
25-
public static function connectionFromArray(array $data, array $args = [])
25+
public static function connectionFromArray($data, array $args = [])
2626
{
2727
return static::connectionFromArraySlice(
2828
$data,
@@ -49,7 +49,7 @@ public static function connectionFromArray(array $data, array $args = [])
4949
*
5050
* @return Connection
5151
*/
52-
public static function connectionFromArraySlice(array $arraySlice, array $args, array $meta)
52+
public static function connectionFromArraySlice($arraySlice, array $args, array $meta)
5353
{
5454
$connectionArguments = static::getOptionsWithDefaults(
5555
$args,
@@ -68,30 +68,30 @@ public static function connectionFromArraySlice(array $arraySlice, array $args,
6868
]
6969
);
7070

71-
$countArraySlice = count($arraySlice);
71+
$arraySliceLength = count($arraySlice);
7272
$after = $connectionArguments['after'];
7373
$before = $connectionArguments['before'];
7474
$first = $connectionArguments['first'];
7575
$last = $connectionArguments['last'];
7676
$sliceStart = $arraySliceMetaInfo['sliceStart'];
7777
$arrayLength = $arraySliceMetaInfo['arrayLength'];
78-
$sliceEnd = $sliceStart + $countArraySlice;
78+
$sliceEnd = $sliceStart + $arraySliceLength;
7979
$beforeOffset = static::getOffsetWithDefault($before, $arrayLength);
8080
$afterOffset = static::getOffsetWithDefault($after, -1);
8181

8282
$startOffset = max($sliceStart - 1, $afterOffset, -1) + 1;
8383
$endOffset = min($sliceEnd, $beforeOffset, $arrayLength);
8484

85-
if ($first !== null) {
85+
if (is_numeric($first)) {
8686
$endOffset = min($endOffset, $startOffset + $first);
8787
}
88-
if ($last !== null) {
88+
if (is_numeric($last)) {
8989
$startOffset = max($startOffset, $endOffset - $last);
9090
}
9191

9292
// If supplied slice is too large, trim it down before mapping over it.
9393
$offset = max($startOffset - $sliceStart, 0);
94-
$length = ($countArraySlice - ($sliceEnd - $endOffset)) - $offset;
94+
$length = ($arraySliceLength - ($sliceEnd - $endOffset)) - $offset;
9595

9696
$slice = array_slice(
9797
$arraySlice,
@@ -101,6 +101,13 @@ public static function connectionFromArraySlice(array $arraySlice, array $args,
101101

102102
$edges = [];
103103

104+
var_dump(compact(
105+
'countArraySlice', 'slice', 'end', 'length',
106+
'offset', 'first', 'after', 'before',
107+
'beforeOffset', 'afterOffset',
108+
'startOffset', 'endOffset', 'sliceStart'
109+
));
110+
104111
foreach($slice as $index => $value) {
105112
$edges[] = new Edge(static::offsetToCursor($startOffset + $index), $value);
106113
}
@@ -121,6 +128,30 @@ public static function connectionFromArraySlice(array $arraySlice, array $args,
121128
);
122129
}
123130

131+
/**
132+
* Return the cursor associated with an object in an array.
133+
* @param array $data
134+
* @param mixed $object
135+
* @return null|string
136+
*/
137+
public static function cursorForObjectInConnection($data, $object)
138+
{
139+
$offset = null;
140+
141+
foreach($data as $i => $entry) {
142+
if ($entry == $object) {
143+
$offset = $i;
144+
break;
145+
}
146+
}
147+
148+
if (null === $offset) {
149+
return null;
150+
}
151+
152+
return static::offsetToCursor($offset);
153+
}
154+
124155
/**
125156
* Given an optional cursor and a default offset, returns the offset
126157
* to use; if the cursor contains a valid offset, that will be used,
@@ -137,7 +168,7 @@ public static function getOffsetWithDefault($cursor, $defaultOffset)
137168
}
138169
$offset = static::cursorToOffset($cursor);
139170

140-
return is_nan($offset) ? $defaultOffset : $offset;
171+
return !is_numeric($offset) ? $defaultOffset : (int)$offset;
141172
}
142173

143174
/**
@@ -157,7 +188,7 @@ public static function offsetToCursor($offset)
157188
*/
158189
public static function cursorToOffset($cursor)
159190
{
160-
return intval(str_replace(static::PREFIX, '', base64_decode($cursor)));
191+
return str_replace(static::PREFIX, '', base64_decode($cursor, true));
161192
}
162193

163194
private static function getOptionsWithDefaults(array $options, array $defaults)

0 commit comments

Comments
 (0)