decode() публичный Метод

public decode ( $attributes )
Пример #1
0
 /**
  * Attributes decoding test
  */
 public function testDecodeAttributes()
 {
     $decoder = new Decoder();
     $attributes = $decoder->decode(array('"Hello"', array('name' => 'href', 'value' => '/'), array('name' => 'data-id', 'value' => 34), array('name' => 'first-name', 'value' => "'Bob'"), array('name' => 'last-name', 'value' => '"Dylan"'), array('name' => 'checked', 'value' => true)));
     $this->assertSame($attributes[0], 'Hello');
     $this->assertSame($attributes[1]['value'], '/');
     $this->assertSame($attributes[2]['value'], 34);
     $this->assertSame($attributes[3]['value'], 'Bob');
     $this->assertSame($attributes[4]['value'], 'Dylan');
     $this->assertSame($attributes[5]['value'], true);
 }
Пример #2
0
 /**
  * @covers \Magento\Framework\Url\Encoder::encode
  * @covers \Magento\Framework\Url\Decoder::decode
  */
 public function testDecode()
 {
     $urlBuilderMock = $this->getMock('Magento\\Framework\\UrlInterface', [], [], '', false);
     /** @var $urlBuilderMock \Magento\Framework\UrlInterface|\PHPUnit_Framework_MockObject_MockObject */
     $decoder = new Decoder($urlBuilderMock);
     $encoder = new Encoder();
     $data = uniqid();
     $result = $encoder->encode($data);
     $urlBuilderMock->expects($this->once())->method('sessionUrlVar')->with($this->equalTo($data))->will($this->returnValue($result));
     $this->assertNotContains('&', $result);
     $this->assertNotContains('%', $result);
     $this->assertNotContains('+', $result);
     $this->assertNotContains('=', $result);
     $this->assertEquals($result, $decoder->decode($result));
 }
Пример #3
0
 public function search()
 {
     require_once 'src/Fetcher.php';
     require_once 'src/Decoder.php';
     $q = null;
     $results = [];
     if (isset($_GET['q'])) {
         $q = $_GET['q'];
         $fetcher = new Fetcher();
         if ($text = $fetcher->search($q)) {
             $decoder = new Decoder();
             $results = $decoder->decode($text);
         }
     }
     include_once 'html/index.html';
 }
Пример #4
0
 /**
  * Decodes the given $encodedValue string which is
  * encoded in the JSON format
  *
  * Uses ext/json's json_decode if available.
  *
  * @param string $encodedValue Encoded in JSON format
  * @param int $objectDecodeType Optional; flag indicating how to decode
  * objects. See {@link Zend_Json_Decoder::decode()} for details.
  * @return mixed
  */
 public static function decode($encodedValue, $objectDecodeType = self::TYPE_ARRAY)
 {
     $encodedValue = (string) $encodedValue;
     if (function_exists('json_decode') && self::$useBuiltinEncoderDecoder !== true) {
         $decode = json_decode($encodedValue, $objectDecodeType);
         // php < 5.3
         if (!function_exists('json_last_error')) {
             if ($decode === $encodedValue) {
                 throw new Exception('Decoding failed');
             }
             // php >= 5.3
         } elseif (($jsonLastErr = json_last_error()) != JSON_ERROR_NONE) {
             switch ($jsonLastErr) {
                 case JSON_ERROR_DEPTH:
                     throw new Exception('Decoding failed: Maximum stack depth exceeded');
                 case JSON_ERROR_CTRL_CHAR:
                     throw new Exception('Decoding failed: Unexpected control character found');
                 case JSON_ERROR_SYNTAX:
                     throw new Exception('Decoding failed: Syntax error');
                 default:
                     throw new Exception('Decoding failed');
             }
         }
         return $decode;
     }
     return Decoder::decode($encodedValue, $objectDecodeType);
 }
Пример #5
0
 /**
  * Decodes a NEON string.
  * @param  string
  * @return mixed
  */
 public static function decode(string $input)
 {
     $decoder = new Decoder();
     return $decoder->decode($input);
 }
Пример #6
0
 /**
  * @expectedException InvalidArgumentException
  * @covers PHP\BitTorrent\Decoder::decode
  */
 public function testGenericDecodeWithInvalidData()
 {
     $this->decoder->decode('foo');
 }
Пример #7
0
 /**
  * Decode a bencode encoded string
  *
  * @param  string  $string The string to decode.
  * @param  string  $decodeType Flag used to indicate whether the decoded
  *   value should be returned as an object or an array.
  * @return mixed   Returns the appropriate data type for the decoded data.
  */
 public static function decode($string, $decodeType = self::TYPE_ARRAY)
 {
     return Decoder::decode($string, $decodeType);
 }
Пример #8
0
 /**
  * @expectedException \RuntimeException
  * @expectedExceptionMessage Json decode error
  */
 public function testJsonErrorOnDecode()
 {
     $json = 'invalid json';
     Decoder::decode($json);
 }
Пример #9
0
 // Preparing response array
 $response = array();
 if (isset($request->action) && isset($request->data) && isset($request->rot)) {
     if ($request->action === 'encode') {
         // Receiving encoded text
         $response['data'] = Encoder::encode($request->data, (int) $request->rot);
         // Receiving input frequency
         $response['frequency'] = Encoder::getFrequency($request->data);
         // Setting success status
         $response['status'] = 'success';
         // Setting solution attempt
         $response['solution'] = Solver::tryGuess($request->data);
     } else {
         if ($request->action === 'decode') {
             // Receiving decoded text
             $response['data'] = Decoder::decode($request->data, (int) $request->rot);
             // Setting success status
             $response['frequency'] = Decoder::getFrequency($request->data);
             // Setting success status
             $response['status'] = 'success';
             // Setting solution attempt
             $response['solution'] = Solver::tryGuess($request->data);
         } else {
             // Default for external POST requests
             //Setting error message
             $response['error'] = 'Wrong params for Caesar code';
             // Setting fail status
             $response['status'] = 'fail';
         }
     }
 }
Пример #10
0
 public static function tryGuess($str)
 {
     $result = array();
     if (self::isEnglish($str) > 50) {
         $result['eng']['message'] = 'This is an english text with the probability ';
         $result['eng']['probability'] = self::isEnglish($str);
         return json_encode($result);
     } else {
         for ($i = 0; $i < 26; $i++) {
             $dec = Decoder::decode($str, $i);
             $prob = self::isEnglish($dec);
             if ($prob > 40) {
                 $result[$i]['rot'] = $i;
                 $result[$i]['probability'] = $prob;
                 $result[$i]['decoded'] = $dec;
             }
         }
         return count($result) ? json_encode($result) : json_encode(array('message' => 'Unfortunately, there is no matches for this text yet!'));
     }
 }
Пример #11
0
 /**
  * @param $value
  * @param int $type
  * @return mixed
  * @throws JsonDecodingException
  */
 public static function decode($value, $type = self::TYPE_OBJECT)
 {
     $decoder = new Decoder($type);
     return $decoder->decode($value);
 }