@@ -22,7 +22,7 @@ class ConnectionBuilder
22
22
* @param array $args
23
23
* @return Connection
24
24
*/
25
- public static function connectionFromArray (array $ data , array $ args = [])
25
+ public static function connectionFromArray ($ data , array $ args = [])
26
26
{
27
27
return static ::connectionFromArraySlice (
28
28
$ data ,
@@ -49,7 +49,7 @@ public static function connectionFromArray(array $data, array $args = [])
49
49
*
50
50
* @return Connection
51
51
*/
52
- public static function connectionFromArraySlice (array $ arraySlice , array $ args , array $ meta )
52
+ public static function connectionFromArraySlice ($ arraySlice , array $ args , array $ meta )
53
53
{
54
54
$ connectionArguments = static ::getOptionsWithDefaults (
55
55
$ args ,
@@ -68,30 +68,30 @@ public static function connectionFromArraySlice(array $arraySlice, array $args,
68
68
]
69
69
);
70
70
71
- $ countArraySlice = count ($ arraySlice );
71
+ $ arraySliceLength = count ($ arraySlice );
72
72
$ after = $ connectionArguments ['after ' ];
73
73
$ before = $ connectionArguments ['before ' ];
74
74
$ first = $ connectionArguments ['first ' ];
75
75
$ last = $ connectionArguments ['last ' ];
76
76
$ sliceStart = $ arraySliceMetaInfo ['sliceStart ' ];
77
77
$ arrayLength = $ arraySliceMetaInfo ['arrayLength ' ];
78
- $ sliceEnd = $ sliceStart + $ countArraySlice ;
78
+ $ sliceEnd = $ sliceStart + $ arraySliceLength ;
79
79
$ beforeOffset = static ::getOffsetWithDefault ($ before , $ arrayLength );
80
80
$ afterOffset = static ::getOffsetWithDefault ($ after , -1 );
81
81
82
82
$ startOffset = max ($ sliceStart - 1 , $ afterOffset , -1 ) + 1 ;
83
83
$ endOffset = min ($ sliceEnd , $ beforeOffset , $ arrayLength );
84
84
85
- if ($ first !== null ) {
85
+ if (is_numeric ( $ first) ) {
86
86
$ endOffset = min ($ endOffset , $ startOffset + $ first );
87
87
}
88
- if ($ last !== null ) {
88
+ if (is_numeric ( $ last) ) {
89
89
$ startOffset = max ($ startOffset , $ endOffset - $ last );
90
90
}
91
91
92
92
// If supplied slice is too large, trim it down before mapping over it.
93
93
$ offset = max ($ startOffset - $ sliceStart , 0 );
94
- $ length = ($ countArraySlice - ($ sliceEnd - $ endOffset )) - $ offset ;
94
+ $ length = ($ arraySliceLength - ($ sliceEnd - $ endOffset )) - $ offset ;
95
95
96
96
$ slice = array_slice (
97
97
$ arraySlice ,
@@ -101,6 +101,13 @@ public static function connectionFromArraySlice(array $arraySlice, array $args,
101
101
102
102
$ edges = [];
103
103
104
+ var_dump (compact (
105
+ 'countArraySlice ' , 'slice ' , 'end ' , 'length ' ,
106
+ 'offset ' , 'first ' , 'after ' , 'before ' ,
107
+ 'beforeOffset ' , 'afterOffset ' ,
108
+ 'startOffset ' , 'endOffset ' , 'sliceStart '
109
+ ));
110
+
104
111
foreach ($ slice as $ index => $ value ) {
105
112
$ edges [] = new Edge (static ::offsetToCursor ($ startOffset + $ index ), $ value );
106
113
}
@@ -121,6 +128,30 @@ public static function connectionFromArraySlice(array $arraySlice, array $args,
121
128
);
122
129
}
123
130
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
+
124
155
/**
125
156
* Given an optional cursor and a default offset, returns the offset
126
157
* to use; if the cursor contains a valid offset, that will be used,
@@ -137,7 +168,7 @@ public static function getOffsetWithDefault($cursor, $defaultOffset)
137
168
}
138
169
$ offset = static ::cursorToOffset ($ cursor );
139
170
140
- return is_nan ($ offset ) ? $ defaultOffset : $ offset ;
171
+ return ! is_numeric ($ offset ) ? $ defaultOffset : ( int ) $ offset ;
141
172
}
142
173
143
174
/**
@@ -157,7 +188,7 @@ public static function offsetToCursor($offset)
157
188
*/
158
189
public static function cursorToOffset ($ cursor )
159
190
{
160
- return intval ( str_replace (static ::PREFIX , '' , base64_decode ($ cursor) ));
191
+ return str_replace (static ::PREFIX , '' , base64_decode ($ cursor, true ));
161
192
}
162
193
163
194
private static function getOptionsWithDefaults (array $ options , array $ defaults )
0 commit comments