Issues (100)

src/Impl/Util/IoUtil.php (1 issue)

1
<?php
2
3
namespace Xml\Impl\Util;
4
5
use Xml\Impl\Instance\DomDocumentExt;
6
use Xml\Instance\DomDocumentInterface;
7
8
class IoUtil
9
{
10
    /**
11
     * @param resource $file
12
     */
13
    public static function closeSilently($file): void
14
    {
15
        try {
16
            fclose($file);
17
        } catch (\Exception $e) {
18
            // ignored
19
        }
20
    }
21
22
    /**
23
     * @param resource $inputStream
24
     * @param bool $trim
25
     */
26
    public static function getStringFromInputStream($inputStream, bool $trim = true): ?string
27
    {
28
        if ($trim) {
29
            $meta = stream_get_meta_data($inputStream);
30
            $data = fread($inputStream, filesize($meta['uri']));
31
            return trim($data);
32
        } else {
33
            $meta = stream_get_meta_data($inputStream);
34
            return fread($inputStream, filesize($meta['uri']));
35
        }
36
    }
37
38
    /**
39
     * @param resource $outputStream
40
     */
41
    public static function convertOutputStreamToInputStream($outputStream): ?string
42
    {
43
        return $outputStream;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $outputStream returns the type resource which is incompatible with the type-hinted return null|string.
Loading history...
44
    }
45
46
    public static function convertXmlDocumentToString(DomDocumentInterface $document): string
47
    {
48
        $source = new DomDocumentExt();
49
        self::transformDocumentToXml($document, $source);
50
        return $source->saveXML();
51
    }
52
53
    /**
54
     * @param DomDocumentInterface $document
55
     * @param resource $stream
56
     */
57
    public static function writeDocumentToOutputStream(DomDocumentInterface $document, $stream): void
58
    {
59
        $source = new DomDocumentExt();
60
        self::transformDocumentToXml($document, $source);
61
        fwrite($stream, $source->saveXML());
62
    }
63
64
    /**
65
     * @param DomDocumentInterface $document
66
     * @param mixed $result
67
     */
68
    public static function transformDocumentToXml(DomDocumentInterface $document, &$result): void
69
    {
70
        $result = $document->getDomSource();
71
    }
72
}
73