Skip to content

Commit 4c8634d

Browse files
author
Martin Brecht-Precht
committed
Added YAML handler
1 parent a6efe3b commit 4c8634d

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"markenwerk/stack-util": "~1.0",
2929
"markenwerk/string-builder": "^1.0.4",
3030
"markenwerk/json-pretty-printer": "~1.0",
31+
"mustangostang/spyc": ">=0.6.1",
3132
"league/commonmark": "^0.13"
3233
}
3334
}

example/yaml.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Markdom\Test;
4+
5+
use Markdom\Dispatcher\JsonDispatcher;
6+
use Markdom\Handler\YamlHandler;
7+
8+
require_once(__DIR__ . '/../vendor/autoload.php');
9+
10+
$handler = new YamlHandler();
11+
$handler
12+
->setPrettyPrint(false)
13+
->setWordWrap(false);
14+
$dispatcher = new JsonDispatcher(file_get_contents(__DIR__ . '/example-data.json'));
15+
$dispatcher->dispatchTo($handler);
16+
fwrite(STDOUT, $handler->getResult());

src/Handler/YamlHandler.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
namespace Markdom\Handler;
4+
5+
/**
6+
* Class YamlHandler
7+
*
8+
* @package Markdom\Handler
9+
*/
10+
class YamlHandler extends PhpObjectHandler
11+
{
12+
13+
/**
14+
* @var bool
15+
*/
16+
private $prettyPrint = false;
17+
18+
/**
19+
* @var bool
20+
*/
21+
private $wordWrap = false;
22+
23+
/**
24+
* @return bool
25+
*/
26+
public function getPrettyPrint()
27+
{
28+
return $this->prettyPrint;
29+
}
30+
31+
/**
32+
* @param bool $prettyPrint
33+
* @return $this
34+
*/
35+
public function setPrettyPrint($prettyPrint)
36+
{
37+
$this->prettyPrint = $prettyPrint;
38+
return $this;
39+
}
40+
41+
/**
42+
* @return boolean
43+
*/
44+
public function getWordWrap()
45+
{
46+
return $this->wordWrap;
47+
}
48+
49+
/**
50+
* @param boolean $wordWrap
51+
* @return $this
52+
*/
53+
public function setWordWrap($wordWrap)
54+
{
55+
$this->wordWrap = $wordWrap;
56+
return $this;
57+
}
58+
59+
/**
60+
* @return string
61+
*/
62+
public function getResult()
63+
{
64+
$indent = ($this->prettyPrint !== false) ? 4 : false;
65+
/** @noinspection PhpUndefinedClassInspection */
66+
$yaml = new \Spyc();
67+
/** @noinspection PhpParamsInspection */
68+
return $yaml->YAMLDump(parent::getResult(), $indent, $this->getWordWrap());
69+
}
70+
71+
}

0 commit comments

Comments
 (0)