aboutsummaryrefslogtreecommitdiffstats
path: root/yamlishwriter-php-v0.0.1
diff options
context:
space:
mode:
authorMatěj Cepl <mcepl@redhat.com>2012-02-15 23:59:42 +0100
committerMatěj Cepl <mcepl@redhat.com>2012-02-16 00:24:01 +0100
commitd7e73ec4c238d0eb1c5493bb90c022d683123296 (patch)
treed81f22a220c40fd7528e098dd0d22060410415e0 /yamlishwriter-php-v0.0.1
downloadyamlish-d7e73ec4c238d0eb1c5493bb90c022d683123296.tar.gz
Initial commit with skeleton of the project.
Also added original Perl, together with PHP and Javascript ports.
Diffstat (limited to 'yamlishwriter-php-v0.0.1')
-rw-r--r--yamlishwriter-php-v0.0.1/Changes3
-rw-r--r--yamlishwriter-php-v0.0.1/MANIFEST10
-rw-r--r--yamlishwriter-php-v0.0.1/Makefile21
-rw-r--r--yamlishwriter-php-v0.0.1/README33
-rwxr-xr-xyamlishwriter-php-v0.0.1/dist.sh16
-rw-r--r--yamlishwriter-php-v0.0.1/lib/yamlishwriter.php125
-rw-r--r--yamlishwriter-php-v0.0.1/t/00-load.php9
-rw-r--r--yamlishwriter-php-v0.0.1/t/10-writer.php180
-rw-r--r--yamlishwriter-php-v0.0.1/t/harness.t29
-rw-r--r--yamlishwriter-php-v0.0.1/t/lib/TestLite.php79
10 files changed, 505 insertions, 0 deletions
diff --git a/yamlishwriter-php-v0.0.1/Changes b/yamlishwriter-php-v0.0.1/Changes
new file mode 100644
index 0000000..4bf0e3c
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/Changes
@@ -0,0 +1,3 @@
+0.0.1 2007-03-19
+ - Initial release
+ \ No newline at end of file
diff --git a/yamlishwriter-php-v0.0.1/MANIFEST b/yamlishwriter-php-v0.0.1/MANIFEST
new file mode 100644
index 0000000..1f7a3cc
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/MANIFEST
@@ -0,0 +1,10 @@
+./Changes
+./dist.sh
+./lib/yamlishwriter.php
+./Makefile
+./README
+./t/00-load.php
+./t/10-writer.php
+./t/harness.t
+./t/lib/TestLite.php
+./MANIFEST
diff --git a/yamlishwriter-php-v0.0.1/Makefile b/yamlishwriter-php-v0.0.1/Makefile
new file mode 100644
index 0000000..09e4803
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/Makefile
@@ -0,0 +1,21 @@
+# Makefile for YAMLish
+
+PHP = /usr/bin/php
+PROVE = prove -r t
+
+VERSION = 0.0.1
+DISTNAME = yamlishwriter-php-v$(VERSION)
+
+default:
+
+test:
+ PHP=$(PHP) $(PROVE)
+
+dist: manifest
+ ./dist.sh $(DISTNAME)
+
+manifest:
+ rm -f MANIFEST
+ find . -type f | grep -vE '\.svn|\.DS_Store|\.tmproj' > MANIFEST
+ echo './MANIFEST' >> MANIFEST
+ \ No newline at end of file
diff --git a/yamlishwriter-php-v0.0.1/README b/yamlishwriter-php-v0.0.1/README
new file mode 100644
index 0000000..bbd6204
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/README
@@ -0,0 +1,33 @@
+YAMLishWriter.php Version 0.0.1
+
+This is a simple implementation of a YAMLish writer for PHP. See
+
+ http://testanything.org/wiki/index.php/YAMLish
+
+for more information about YAMLish.
+
+You can find the YAMLishWriter class in lib/yamlishwriter.php in this
+distribution. YAMLishWriter is a self contained PHP class. To use it
+
+ // Or wherever it is on your include path
+ include('yamlishwriter.php');
+
+ // And then later
+ $writer = new YAMLishWriter;
+
+ $data = array( 'got' => 1, 'expected' => 2 );
+ $output = $writer->write($data);
+
+The returned value is an array containing the lines of YAMLish.
+
+This distribution contains a rudimentary test harness for which you'll
+need Perl prove command. To run the tests edit Makefile to reflect the
+location of your php command and then
+
+ make test
+
+Copyright 2007 Andy Armstrong, andy@hexten.net.
+
+YAMLishWriter is distributed under Perl Artistic Licence:
+
+ http://www.opensource.org/licenses/artistic-license.php
diff --git a/yamlishwriter-php-v0.0.1/dist.sh b/yamlishwriter-php-v0.0.1/dist.sh
new file mode 100755
index 0000000..5752e53
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/dist.sh
@@ -0,0 +1,16 @@
+#!/bin/sh
+
+distname=$1
+distarc="$distname.tar.gz"
+
+echo "Making $distarc"
+for f in `cat MANIFEST`
+do
+ d=`dirname $f`
+ mkdir -p "$distname/$d"
+ echo "Adding $f"
+ cp $f "$distname/$f"
+done
+
+tar zcf $distarc $distname
+rm -rf $distname
diff --git a/yamlishwriter-php-v0.0.1/lib/yamlishwriter.php b/yamlishwriter-php-v0.0.1/lib/yamlishwriter.php
new file mode 100644
index 0000000..62035a9
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/lib/yamlishwriter.php
@@ -0,0 +1,125 @@
+<?php
+
+ // Would be nice if this weren't a global function - but I can't find
+ // out how to make a class member work in preg_replace_callback
+ function __escape_unprintable($matched) {
+ $unprintable = array(
+ 'z', 'x01', 'x02', 'x03', 'x04', 'x05', 'x06', 'a',
+ 'x08', 't', 'n', 'v', 'f', 'r', 'x0e', 'x0f',
+ 'x10', 'x11', 'x12', 'x13', 'x14', 'x15', 'x16', 'x17',
+ 'x18', 'x19', 'x1a', 'e', 'x1c', 'x1d', 'x1e', 'x1f',
+ );
+
+ return '\\' . $unprintable[ ord( $matched[0] ) ];
+ }
+
+ class YAMLishWriter {
+ function YAMLishWriter() {
+
+ }
+
+ function write( $obj ) {
+ $out = array();
+
+ $this->_write_obj( $out, '---', $obj );
+ $out[] = '...';
+
+ return $out;
+ }
+
+ function _write_hash( &$out, $obj, $indent, $pad ) {
+ while ( list($k, $v) = each( $obj ) ) {
+ $this->_write_obj(
+ $out,
+ $pad . $this->_enc_scalar($k) . ':',
+ $v, $indent + 1
+ );
+ }
+ }
+
+ function _write_array( &$out, $obj, $indent ) {
+ $pad = '';
+ for ( $i = 0; $i < $indent; $i++ ) {
+ $pad .= ' ';
+ }
+
+ $nout = array();
+ $idx = 0;
+ while ( list($k, $v) = each( $obj ) ) {
+ if (gettype($k) != 'integer' || $k != $idx) {
+ # If we find it's not actually an array discard what we've done
+ # and write a hash instead. Usually we should find out pretty
+ # early.
+ $this->_write_hash( $out, $obj, $indent, $pad );
+ return;
+ }
+
+ $this->_write_obj( $nout, "$pad-", $v, $indent + 1);
+ $idx++;
+ }
+
+ array_splice( $out, count($out), 0, $nout );
+ }
+
+ function _enc_string ( $str ) {
+ if (preg_match('/[\x00-\x1f\"]/', $str)) {
+ $str = preg_replace('/\\\\/', '\\\\', $str);
+ $str = preg_replace('/"/', '\\"', $str);
+ $str = preg_replace_callback( '/([\x00-\x1f])/', '__escape_unprintable', $str );
+ return '"' . $str . '"';
+ }
+
+ if ( strlen($str) == 0 || preg_match( '/\s/', $str ) ) {
+ return "'" . preg_replace('/\'/', "''", $str) . "'";
+ }
+
+ return $str;
+ }
+
+ function _enc_scalar( $obj ) {
+ $type = gettype( $obj );
+ switch ($type) {
+ case 'boolean':
+ return $obj ? '1' : '0';
+
+ case 'double' :
+ case 'integer':
+ return "$obj";
+
+ case 'NULL':
+ return '~';
+
+ case 'string':
+ return $this->_enc_string( $obj );
+
+ default:
+ die('Unhandled scalar type ' . $type);
+ }
+ }
+
+ function _write_obj( &$out, $prefix, $obj, $indent = 0 ) {
+ $type = gettype( $obj );
+
+ switch ($type) {
+ case 'boolean':
+ case 'integer':
+ case 'double' :
+ case 'string':
+ case 'NULL':
+ $out[] = "$prefix " . $this->_enc_scalar( $obj );
+ break;
+
+ case 'array':
+ $out[] = $prefix;
+ $this->_write_array( $out, $obj, $indent );
+ break;
+
+ case 'object':
+ case 'resource':
+ default:
+ die('Unhandled type ' . $type);
+ }
+ }
+ }
+
+?>
diff --git a/yamlishwriter-php-v0.0.1/t/00-load.php b/yamlishwriter-php-v0.0.1/t/00-load.php
new file mode 100644
index 0000000..afe3000
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/t/00-load.php
@@ -0,0 +1,9 @@
+<?php
+
+ error_reporting(E_ALL);
+
+ include_once('./t/lib/TestLite.php');
+
+ plan(1);
+ ok(@include_once('./lib/yamlishwriter.php'), 'include library');
+?>
diff --git a/yamlishwriter-php-v0.0.1/t/10-writer.php b/yamlishwriter-php-v0.0.1/t/10-writer.php
new file mode 100644
index 0000000..146b356
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/t/10-writer.php
@@ -0,0 +1,180 @@
+<?php
+
+ error_reporting(E_ALL);
+
+ include_once('./t/lib/TestLite.php');
+
+ $schedule = array(
+ array(
+ 'name' => 'Simple scalar',
+ 'in' => 1,
+ 'out' => array(
+ '--- 1',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Undef',
+ 'in' => null,
+ 'out' => array(
+ '--- ~',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Unprintable',
+ 'in' => "\x01\n\t",
+ 'out' => array(
+ '--- "\x01\n\t"',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Simple array',
+ 'in' => array( 1, 2, 3 ),
+ 'out' => array(
+ '---',
+ '- 1',
+ '- 2',
+ '- 3',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Array, two elements, null',
+ 'in' => array( null, null ),
+ 'out' => array(
+ '---',
+ '- ~',
+ '- ~',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Nested array',
+ 'in' => array( 1, 2, array( 3, 4 ), 5 ),
+ 'out' => array(
+ '---',
+ '- 1',
+ '- 2',
+ '-',
+ ' - 3',
+ ' - 4',
+ '- 5',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Simple hash',
+ 'in' => array( 'one' => '1', 'two' => '2', 'three' => '3' ),
+ 'out' => array(
+ '---',
+ 'one: 1',
+ 'two: 2',
+ 'three: 3',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Nested hash',
+ 'in' => array(
+ 'one' => '1', 'two' => '2', 'more' => array( 'three' => '3', 'four' => '4' )
+ ),
+ 'out' => array(
+ '---',
+ 'one: 1',
+ 'two: 2',
+ 'more:',
+ ' three: 3',
+ ' four: 4',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Unprintable key',
+ 'in' => array( 'one' => '1', "\x02" => '2', 'three' => '3' ),
+ 'out' => array(
+ '---',
+ 'one: 1',
+ '"\x02": 2',
+ 'three: 3',
+ '...',
+ ),
+ ),
+ array(
+ 'name' => 'Complex',
+ 'in' => array(
+ 'bill-to' => array(
+ 'given' => 'Chris',
+ 'address' => array(
+ 'city' => 'Royal Oak',
+ 'postal' => '48046',
+ 'lines' => "458 Walkman Dr.\nSuite #292\n",
+ 'state' => 'MI'
+ ),
+ 'family' => 'Dumars'
+ ),
+ 'invoice' => '34843',
+ 'date' => '2001-01-23',
+ 'tax' => '251.42',
+ 'product' => array(
+ array(
+ 'sku' => 'BL394D',
+ 'quantity' => '4',
+ 'price' => '450.00',
+ 'description' => 'Basketball'
+ ),
+ array(
+ 'sku' => 'BL4438H',
+ 'quantity' => '1',
+ 'price' => '2392.00',
+ 'description' => 'Super Hoop'
+ )
+ ),
+ 'comments' =>
+ "Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\n",
+ 'total' => '4443.52'
+ ),
+ 'out' => array(
+ "---",
+ "bill-to:",
+ " given: Chris",
+ " address:",
+ " city: 'Royal Oak'",
+ " postal: 48046",
+ " lines: \"458 Walkman Dr.\\nSuite #292\\n\"",
+ " state: MI",
+ " family: Dumars",
+ "invoice: 34843",
+ "date: 2001-01-23",
+ "tax: 251.42",
+ "product:",
+ " -",
+ " sku: BL394D",
+ " quantity: 4",
+ " price: 450.00",
+ " description: Basketball",
+ " -",
+ " sku: BL4438H",
+ " quantity: 1",
+ " price: 2392.00",
+ " description: 'Super Hoop'",
+ "comments: \"Late afternoon is best. Backup contact is Nancy Billsmer @ 338-4338\\n\"",
+ "total: 4443.52",
+ "...",
+ ),
+ ),
+ );
+
+ plan( count($schedule) * 1 );
+
+ include_once('./lib/yamlishwriter.php');
+
+ foreach ($schedule as $test) {
+ $name = $test['name'];
+ $writer = new YAMLishWriter;
+ $got = $writer->write($test['in']);
+ is_deeply_array($got, $test['out'], "$name: output matches");
+ }
+
+?>
diff --git a/yamlishwriter-php-v0.0.1/t/harness.t b/yamlishwriter-php-v0.0.1/t/harness.t
new file mode 100644
index 0000000..abc51f8
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/t/harness.t
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+my $php = $ENV{PHP} || 'php';
+
+my $TESTS = 't/*.php';
+
+my $planned = 0;
+
+for my $test ( glob( $TESTS ) ) {
+ warn "# $test\n";
+ my $offset = $planned;
+ my @command = ( $php, $test );
+ open my $th, '-|', @command or die "Can't run $test ($?)\n";
+ while ( defined( my $line = <$th> ) ) {
+ chomp $line;
+ if ( $line =~ /^1..(\d+)/ ) {
+ $planned += $1;
+ }
+ else {
+ $line =~ s/^((?:not\s+)?ok\s+)(\d+)/$1 . ($2 + $offset)/e;
+ print "$line\n";
+ }
+ }
+ close $th or die "Can't run $test ($?)\n";
+}
+
+# Trailing plan
+print "1..$planned\n";
diff --git a/yamlishwriter-php-v0.0.1/t/lib/TestLite.php b/yamlishwriter-php-v0.0.1/t/lib/TestLite.php
new file mode 100644
index 0000000..231001b
--- /dev/null
+++ b/yamlishwriter-php-v0.0.1/t/lib/TestLite.php
@@ -0,0 +1,79 @@
+<?php
+
+ $NEXT_TEST = 1;
+
+ function plan($tests) {
+ print "1..$tests\n";
+ }
+
+ function ok($ok, $message = null) {
+ global $NEXT_TEST;
+ $out = "ok " . ($NEXT_TEST++);
+ if (!$ok) {
+ $out = 'not ' . $out;
+ }
+ if ($message) {
+ $out .= " $message";
+ }
+ print "$out\n";
+ }
+
+ function pass($message) {
+ ok(true, $message);
+ }
+
+ function fail($message) {
+ ok(false, $message);
+ }
+
+ function diag($message) {
+ $stdout = fopen('php://stderr', 'w');
+ fwrite( $stdout, "# $message\n" );
+ fclose( $stdout );
+ }
+
+ function is_deeply_array( $got, $expected, $message ) {
+ if ( gettype($got) != 'array' ) {
+ fail($message);
+ diag('$got is not an array');
+ return;
+ }
+
+ if ( gettype($expected) != 'array' ) {
+ fail($message);
+ diag('$expected is not an array');
+ return;
+ }
+
+ $ok = true;
+ $diag = array();
+ $got_c = count( $got );
+ $expected_c = count( $expected );
+
+ if ($got_c != $expected_c) {
+ $ok = false;
+ $diag[] = 'Array sizes differ:';
+ $diag[] = ' $got: ' . $got_c;
+ $diag[] = ' $expected: ' . $expected_c;
+ }
+
+ $count = max( $got_c, $expected_c );
+
+ for ($i = 0; $i < $count; $i++) {
+ if ( $got[$i] != $expected[$i] ) {
+ if ($ok) {
+ $ok = false;
+ $diag[] = 'Arrays differ:';
+ }
+ $diag[] = " \$got[$i]: $got[$i]";
+ $diag[] = " \$expected[$i]: $expected[$i]";
+ }
+ }
+
+ ok($ok, $message);
+ foreach ($diag as $d) {
+ diag($d);
+ }
+ }
+
+?>