@@ -92,16 +92,9 @@ public function getType($val)
9292 Abstracter::RECURSION => Type::TYPE_RECURSION ,
9393 Abstracter::UNDEFINED => Type::TYPE_UNDEFINED ,
9494 );
95- if (isset ($ debugVals [$ val ])) {
96- return [$ debugVals [$ val ], null ];
97- }
98- if (\is_numeric ($ val ) === false ) {
99- return [Type::TYPE_STRING , $ this ->getTypeMore ($ val )];
100- }
101- $ typeMore = $ this ->abstracter ->type ->isTimestamp ($ val )
102- ? Type::TYPE_TIMESTAMP
103- : Type::TYPE_STRING_NUMERIC ;
104- return [Type::TYPE_STRING , $ typeMore ];
95+ return isset ($ debugVals [$ val ])
96+ ? [$ debugVals [$ val ], null ]
97+ : [Type::TYPE_STRING , $ this ->getTypeMore ($ val )];
10598 }
10699
107100 /**
@@ -114,26 +107,40 @@ public function getType($val)
114107 */
115108 private function absFinish (Abstraction $ abs )
116109 {
117- $ typeMore = $ abs ['typeMore ' ];
118- if ($ abs ['brief ' ] && $ typeMore !== Type::TYPE_STRING_BINARY ) {
119- $ matches = [];
120- $ maxLen = $ abs ['maxlen ' ] > -1
121- ? $ abs ['maxlen ' ]
122- : 128 ;
123- $ regex = '/^([^\r\n]{1, ' . $ maxLen . '})/ ' ;
124- \preg_match ($ regex , $ abs ['value ' ], $ matches );
125- $ abs ['value ' ] = $ matches
126- ? $ matches [1 ]
127- : \substr ($ abs ['value ' ], 0 , $ maxLen );
128- $ abs ['strlenValue ' ] = \strlen ($ abs ['value ' ]);
129- }
110+ $ this ->trimValueIfBrief ($ abs );
111+
112+ // cleanup length info
130113 if ($ abs ['strlen ' ] === $ abs ['strlenValue ' ] && $ abs ['strlen ' ] === \strlen ($ abs ['value ' ])) {
131114 unset($ abs ['strlen ' ], $ abs ['strlenValue ' ]);
132115 }
116+
117+ // remove temporary values
133118 unset($ abs ['maxlen ' ], $ abs ['valueRaw ' ]);
134119 return $ abs ;
135120 }
136121
122+ /**
123+ * Trim the abstraction value if in brief mode and not binary
124+ *
125+ * @param Abstraction $abs Abstraction to modify
126+ *
127+ * @return void
128+ */
129+ private function trimValueIfBrief (Abstraction $ abs )
130+ {
131+ if (!$ abs ['brief ' ] || $ abs ['typeMore ' ] === Type::TYPE_STRING_BINARY ) {
132+ return ;
133+ }
134+ $ maxLen = $ abs ['maxlen ' ] > -1 ? $ abs ['maxlen ' ] : 128 ;
135+ $ matches = [];
136+ $ regex = '/^([^\r\n]{1, ' . $ maxLen . '})/ ' ;
137+ \preg_match ($ regex , $ abs ['value ' ], $ matches );
138+ $ abs ['value ' ] = $ matches
139+ ? $ matches [1 ]
140+ : \substr ($ abs ['value ' ], 0 , $ maxLen );
141+ $ abs ['strlenValue ' ] = \strlen ($ abs ['value ' ]);
142+ }
143+
137144 /**
138145 * Get a string abstraction..
139146 *
@@ -312,31 +319,43 @@ private function getAbsSerialized(Abstraction $abs)
312319 * @param string $cat category (ie base64, binary, other)
313320 * @param int $strlen string length
314321 *
315- * @return int -1 for no limit
322+ * @return int max length value ( -1 for no limit)
316323 */
317324 private function getMaxLen ($ cat , $ strlen )
318325 {
319326 $ stringMaxLen = $ this ->cfg ['brief ' ]
320327 ? $ this ->cfg ['stringMaxLenBrief ' ]
321328 : $ this ->cfg ['stringMaxLen ' ];
322- $ maxLen = \array_key_exists ($ cat , $ stringMaxLen )
329+ $ maxLen = \array_key_exists ($ cat , $ stringMaxLen )
323330 ? $ stringMaxLen [$ cat ]
324331 : $ stringMaxLen ['other ' ];
325- if (\is_array ($ maxLen ) === false ) {
326- return $ maxLen !== null
327- ? $ maxLen
328- : -1 ;
332+
333+ if (\is_array ($ maxLen )) {
334+ $ maxLen = $ this ->getBreakpointBasedMaxLen ($ maxLen , $ strlen );
329335 }
336+ return $ maxLen !== null
337+ ? $ maxLen
338+ : -1 ;
339+ }
340+
341+ /**
342+ * Calculate max length based on breakpoints
343+ *
344+ * @param array $breakpoints array of breakpoint => length values
345+ * @param int $strlen string length to check against breakpoints
346+ *
347+ * @return int max length value (-1 for no limit)
348+ */
349+ private function getBreakpointBasedMaxLen (array $ breakpoints , $ strlen )
350+ {
330351 $ len = -1 ;
331- foreach ($ maxLen as $ breakpoint => $ lenNew ) {
352+ foreach ($ breakpoints as $ breakpoint => $ lenNew ) {
332353 if ($ breakpoint > $ strlen ) {
333354 break ;
334355 }
335356 $ len = $ lenNew ;
336357 }
337- return $ len !== null
338- ? $ len
339- : -1 ;
358+ return $ len ;
340359 }
341360
342361 /**
@@ -348,23 +367,20 @@ private function getMaxLen($cat, $strlen)
348367 */
349368 private function getTypeMore ($ val )
350369 {
351- $ strLen = \strlen ($ val );
352- $ strLenEncoded = $ this ->cfg ['stringMinLen ' ]['encoded ' ];
353- $ typeMore = null ;
354- if ($ strLenEncoded > -1 && $ strLen >= $ strLenEncoded ) {
355- $ typeMore = $ this ->getTypeMoreEncoded ($ val );
356- }
370+ $ typeMore = \is_numeric ($ val )
371+ ? $ this ->getTypeMoreNumeric ($ val )
372+ : $ this ->getTypeMoreEncoded ($ val );
357373 if ($ typeMore ) {
358374 return $ typeMore ;
359375 }
360376 if ($ this ->debug ->utf8 ->isUtf8 ($ val ) === false ) {
361377 return Type::TYPE_STRING_BINARY ;
362378 }
379+ $ strLen = \strlen ($ val );
363380 $ maxlen = $ this ->getMaxLen ('other ' , $ strLen );
364- if ($ maxlen > -1 && $ strLen > $ maxlen ) {
365- return Type::TYPE_STRING_LONG ;
366- }
367- return null ;
381+ return $ maxlen > -1 && $ strLen > $ maxlen
382+ ? Type::TYPE_STRING_LONG
383+ : null ;
368384 }
369385
370386 /**
@@ -376,6 +392,12 @@ private function getTypeMore($val)
376392 */
377393 private function getTypeMoreEncoded ($ val )
378394 {
395+ $ strLen = \strlen ($ val );
396+ $ minLen = $ this ->cfg ['stringMinLen ' ]['encoded ' ];
397+ if ($ minLen < 0 || $ strLen < $ minLen ) {
398+ return null ; // not long enough to test
399+ }
400+
379401 if ($ this ->debug ->stringUtil ->isBase64Encoded ($ val )) {
380402 return Type::TYPE_STRING_BASE64 ;
381403 }
@@ -387,4 +409,18 @@ private function getTypeMoreEncoded($val)
387409 }
388410 return null ;
389411 }
412+
413+ /**
414+ * Determine if value is timestamp or plain numeric
415+ *
416+ * @param string $val numeric string value
417+ *
418+ * @return string
419+ */
420+ private function getTypeMoreNumeric ($ val )
421+ {
422+ return $ this ->abstracter ->type ->isTimestamp ($ val )
423+ ? Type::TYPE_TIMESTAMP
424+ : Type::TYPE_STRING_NUMERIC ;
425+ }
390426}
0 commit comments