Example #1
0
 public function testCleanDecodeStripsComments()
 {
     $json = '// Comment at start' . PHP_EOL . '{"key1": "val1", /* Block comment */' . PHP_EOL . ' "key2": "val2"  // Line comment' . PHP_EOL . '} /* Comment at end */';
     $decoded = JsonUtils::cleanDecode($json, true);
     $expected = ['key1' => 'val1', 'key2' => 'val2'];
     $this->assertSame($decoded, $expected);
 }
Example #2
0
 /**
  * Process the passed data using the current spec
  *
  * @param  object $input The object to process
  * @return object The processed object
  */
 public function process($input)
 {
     if (!is_object($input)) {
         $input = JsonUtils::cleanDecode($input);
         if (!$input) {
             throw new Exception('$input is not valid JSON');
         }
     }
     $processed = $input;
     foreach ($this->spec->getTransforms() as $nodes) {
         $this->applyNodes($nodes, $processed);
     }
     return $processed;
 }
Example #3
0
 /**
  * Constructor
  *
  * @param string|object $spec The JSON spec to parse
  * @param Options|null  $options Tweaky options.  If not set, defaults will be used
  */
 public function __construct($spec, $options = null)
 {
     if (is_null($options)) {
         $options = new Options();
     } elseif (!is_a($options, 'Beequeue\\Tweaky\\Options')) {
         throw new Exception('$options passed to Spec must be of type Beequeue\\Tweaky\\Options');
     }
     $this->options = $options;
     if (is_string($spec)) {
         $spec = JsonUtils::cleanDecode($spec);
         if (!$spec) {
             throw new Exception('$spec passed to Spec is not valid JSON');
         }
     }
     $this->parse($spec);
 }