FrontMatterHydrator   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 10
c 0
b 0
f 0
ccs 13
cts 13
cp 1
wmc 6
lcom 0
cbo 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hydrate() 0 14 2
A dehydratePart() 0 13 4
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Infrastructure\Model\Hydrator;
38
39
use Apparat\Resource\Domain\Model\Hydrator\AbstractChoiceHydrator;
40
use Apparat\Resource\Domain\Model\Part\PartInterface;
41
use Apparat\Resource\Infrastructure\Model\Part\YamlPart;
42
43
/**
44
 * FrontMark part hydrator (combination of YAML / JSON front matter and CommonMark part)
45
 *
46
 * @package     Apparat\Resource
47
 * @subpackage  Apparat\Resource\Infrastructure
48
 */
49
class FrontMatterHydrator extends AbstractChoiceHydrator
50
{
51
    /**
52
     * Front matter part identifier
53
     *
54
     * @var string
55
     */
56
    const FRONTMATTER = 'frontmatter';
57
58
    /**
59
     * Translate data to a resource part
60
     *
61
     * @param string $data Part data
62
     * @return PartInterface Resource part
63
     */
64 20
    public function hydrate($data)
65
    {
66 20
        $aggregate = parent::hydrate(null);
67
68
        // If it's a JSON front matter
69 20
        if (!strncmp('{', trim($data), 1)) {
70 6
            $aggregate->assign(JsonHydrator::JSON, $data, 0);
71 6
            return $aggregate;
72
        }
73
74
        // Assign as YAML front matter
75 14
        $aggregate->assign(YamlHydrator::YAML, $data, 0);
76 14
        return $aggregate;
77
    }
78
79
    /**
80
     * Dehydrate a single part with a particular subhydrator
81
     *
82
     * @param string $subhydrator Subhydrator name
83
     * @param PartInterface $part Part instance
84
     * @return string Dehydrated part
85
     */
86 3
    protected function dehydratePart($subhydrator, PartInterface $part)
87
    {
88 3
        $content = trim(parent::dehydratePart($subhydrator, $part));
89
90
        // If it's a YAML part: Terminate if necessary
91 3
        if (strlen($content) && ($part instanceof YamlPart) &&
92 3
            !preg_match('%\R'.preg_quote(YamlPart::DOCUMENT_END).'$%', $content)
93
        ) {
94 2
            $content .= "\n".YamlPart::DOCUMENT_END;
95
        }
96
97 3
        return $content."\n";
98
    }
99
}
100