for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
namespace League\Torrent\Helper;
use \InvalidArgumentException;
class Encoder
{
/**
* @param $mixed
*
* @return string
*/
public static function encode($mixed)
if (is_numeric($mixed)) {
return self::encodeInteger($mixed);
}
if (is_array($mixed)) {
return self::encodeArray($mixed);
if (is_string($mixed) || is_null($mixed)) {
return self::encodeString((string) $mixed);
throw new InvalidArgumentException('Variables of type ' . gettype($mixed) . ' can not be encoded.');
* @param string $string
public static function encodeString($string)
return strlen($string) . ':' . $string;
* @param $integer
public static function encodeInteger($integer)
return 'i' . $integer . 'e';
* @param $array
public static function encodeArray($array)
if (FileSystem::is_list($array)) {
$return = 'l';
foreach ($array as $value) {
$return .= self::encode($value);
} else {
ksort($array, SORT_STRING);
$return = 'd';
foreach ($array as $key => $value) {
$return .= self::encode(strval($key)) . self::encode($value);
return $return . 'e';