/** * @covers Xoops\Core\Yaml::dump * @covers Xoops\Core\Yaml::load */ public function testDumpAndLoad() { $inputArray = array('one' => 1, 'two' => array(1, 2), 'three' => ''); $string = Yaml::dump($inputArray); $this->assertTrue(!empty($string)); $this->assertTrue(is_string($string)); $outputArray = Yaml::load((string) $string); $this->assertTrue(is_array($outputArray)); $this->assertSame($inputArray, $outputArray); }
<?php # # S P Y C # a simple php yaml class # # Feel free to dump an array to YAML, and then to load that YAML back into an # array. This is a good way to test the limitations of the parser and maybe # learn some basic YAML. # include '../../Yaml.php'; $yaml = new Yaml(); $array[] = 'Sequence item'; $array['The Key'] = 'Mapped value'; $array[] = array('A sequence', 'of a sequence'); $array[] = array('first' => 'A sequence', 'second' => 'of mapped values'); $array['Mapped'] = array('A sequence', 'which is mapped'); $array['A Note'] = 'What if your text is too long?'; $array['Another Note'] = 'If that is the case, the dumper will probably fold your text by using a block.'; $array['The trick?'] = 'The trick is that we overrode the default indent, 2, to 4 and the default wordwrap, 40, to 60.'; $array['Old Dog'] = "And if you want\n to preserve line breaks, \ngo ahead!"; $array['key:withcolon'] = "Should support this to"; print_r($yaml->dump($array, 4, 60));
/** * Save submission to file * * @param array $data Array of values to store * @param array $config Array of configuration values * @param string $location Path to folder where submissions should be saved * @param string $prefix Filename prefix to use for submission file * @param string $suffix Filename suffix to use for submission file * @return void */ private function save($data, $config, $location, $is_spam = false) { if (array_get($this->config, 'master_killswitch')) { return; } $EXT = array_get($config, 'submission_save_extension', 'yaml'); // Clean up whitespace array_walk_recursive($data, function (&$value, $key) { $value = trim($value); }); if (!File::exists($location)) { Folder::make($location); } if ($format = array_get($config, 'filename_format')) { $now = time(); $time_variables = array('year' => date('Y', $now), 'month' => date('m', $now), 'day' => date('d', $now), 'hour' => date('G', $now), 'minute' => date('i', $now), 'minutes' => date('i', $now), 'second' => date('s', $now), 'seconds' => date('s', $now)); $available_variables = $time_variables + $data; $filename = Parse::template($format, $available_variables); } else { $filename = date('Y-m-d-Gi-s', time()); } if ($is_spam) { $location = $location . 'spam/'; Folder::make($location); } // Put it in the right folder $filename = $location . $filename; // Ensure a unique filename in the event two forms are submitted in the same second if (File::exists($filename . '.' . $EXT)) { for ($i = 1; $i < 60; $i++) { if (!file_exists($filename . '-' . $i . '.' . $EXT)) { $filename = $filename . '-' . $i; break; } } } $filename .= '.' . $EXT; if ($EXT === 'json') { File::put($filename, json_encode($data)); } else { File::put($filename, Yaml::dump($data) . '---'); } }
<?php # # S P Y C # a simple php yaml class # # license: [MIT License, http://www.opensource.org/licenses/mit-license.php] # include '../../Yaml.php'; $yaml = new Yaml(); $array = $yaml->load('../Fixtures/sample.yaml'); echo '<pre><a href="../Fixtures/sample.yaml">sample.yaml</a> loaded into PHP:<br/>'; print_r($array); echo '</pre>'; echo '<pre>YAML Data dumped back:<br/>'; echo $yaml->dump($array); echo '</pre>';