Пример #1
0
 public function testMalformedJsonError()
 {
     $expected = json_encode($this->_data) . '#';
     $this->assertException('JSON Error: Syntax error, malformed JSON', function () use($expected) {
         $result = Json::decode($expected);
     });
 }
Пример #2
0
 /**
  * returns rendered content
  *
  * @param string $content input content
  * @param string $data field to retrieve from configuration
  * @param array $options an array with additional options
  * @return string content as given
  * @filter
  */
 public function get($content, $data = null, array $options = array())
 {
     $defaults = array('assoc' => true, 'depth' => 512, 'default' => array(), 'flat' => false);
     $options += $defaults;
     $params = compact('content', 'data', 'options');
     return $this->_filter(__METHOD__, $params, function ($self, $params) {
         extract($params);
         try {
             $config = JsonFormat::decode($content, $options['assoc'], $options['depth']);
         } catch (JsonException $e) {
             return $options['default'];
         } catch (Exception $e) {
             return $options['default'];
         }
         if (empty($data)) {
             return $options['flat'] ? Set::flatten($config) : $config;
         }
         if (is_scalar($data) && isset($config[$data])) {
             return $config[$data];
         }
         $data = '/' . str_replace('.', '/', (string) $data) . '/.';
         $result = current(Set::extract((array) $config, $data));
         if (!empty($result)) {
             return $result;
         }
         return $options['default'];
     });
 }