• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jojo1981 / decoder-aggregate / 12712243989

10 Jan 2025 03:19PM UTC coverage: 100.0%. Remained the same
12712243989

push

github

jojo1981
Use GitHub actions workflow for CI instead of Travis CI.

112 of 112 relevant lines covered (100.0%)

4.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/Encoder/YamlEncoder.php
1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of the jojo1981/decoder-aggregate package
4
 *
5
 * Copyright (c) 2021 Joost Nijhuis <jnijhuis81@gmail.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed in the root of the source code
9
 */
10
namespace Jojo1981\DecoderAggregate\Encoder;
11

12
use Jojo1981\DecoderAggregate\EncoderInterface;
13
use Jojo1981\DecoderAggregate\Exception\YamlEncoderException;
14
use Symfony\Component\Yaml\Yaml;
15
use Throwable;
16

17
/**
18
 * @package Jojo1981\DecoderAggregate\Encoder
19
 */
20
final class YamlEncoder implements EncoderInterface
21
{
22
    /** @var int */
23
    private int $inline;
24

25
    /** @var int */
26
    private int $indent;
27

28
    /** @var int */
29
    private int $flags;
30

31
    /**
32
     * @param int $inline
33
     * @param int $indent
34
     * @param int $flags
35
     */
36
    public function __construct(int $inline = 2, int $indent = 4, int $flags = 0)
37
    {
38
        $this->inline = $inline;
16✔
39
        $this->indent = $indent;
16✔
40
        $this->flags = $flags;
16✔
41
    }
42

43
    /**
44
     * @param mixed $data
45
     * @param array $options
46
     * @return string
47
     * @throws YamlEncoderException
48
     */
49
    public function encode($data, array $options = []): string
50
    {
51
        $options['inline'] = $options['inline'] ?? $this->inline;
14✔
52
        $options['indent'] = $options['indent'] ?? $this->indent;
14✔
53
        $options['flags'] = $options['flags'] ?? $this->flags;
14✔
54

55
        if ($options['flags'] & Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE) {
14✔
56
            $options['flags'] -= Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE;
1✔
57
        }
58

59
        try {
60
            return Yaml::dump($data, $options['inline'], $options['indent'], $options['flags'] + Yaml::DUMP_EXCEPTION_ON_INVALID_TYPE);
14✔
61
        } catch (Throwable $exception) {
1✔
62
            throw new YamlEncoderException('Could not encode data into a yaml string', 0, $exception);
1✔
63
        }
64
    }
65
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc