Completed
Push — add/changelog-tooling ( b30521...fa9ac3 )
by
unknown
1097:09 queued 1086:59
created

Parser::newChangelogEntry()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php // phpcs:ignore WordPress.Files.FileName.NotHyphenatedLowercase
2
/**
3
 * Base class for a changelog parser.
4
 *
5
 * @package automattic/jetpack-changelogger
6
 */
7
8
// phpcs:disable WordPress.WP.AlternativeFunctions, WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
9
10
namespace Automattic\Jetpack\Changelog;
11
12
/**
13
 * Base class for a changelog parser.
14
 */
15
abstract class Parser {
16
17
	/**
18
	 * Parse changelog data into a Changelog object.
19
	 *
20
	 * @param string $changelog Changelog contents.
21
	 * @return Changelog
22
	 */
23
	abstract public function parse( $changelog ); // @codeCoverageIgnore
24
25
	/**
26
	 * Parse changelog data from a file or stream into a Changelog object.
27
	 *
28
	 * @param string|resource $file Filename or stream resource.
29
	 * @return Changelog
30
	 */
31
	public function parseFromFile( $file ) {
32
		return $this->parse( is_string( $file ) ? file_get_contents( $file ) : stream_get_contents( $file ) );
33
	}
34
35
	/**
36
	 * Write a Changelog object to a string.
37
	 *
38
	 * Note that, while an implementation should try to handle any Changelog
39
	 * object appropriately, data loss may occur if the Changelog was not
40
	 * generated by an instance of the same Parser with the same configuration.
41
	 *
42
	 * @param Changelog $changelog Changelog object.
43
	 * @return string
44
	 */
45
	abstract public function format( Changelog $changelog ); // @codeCoverageIgnore
46
47
	/**
48
	 * Write a Changelog object to a file or stream.
49
	 *
50
	 * @param string|resource $file Filename or stream resource.
51
	 * @param Changelog       $changelog Changelog object.
52
	 * @return bool Whether the write succeeded.
53
	 */
54
	public function formatToFile( $file, Changelog $changelog ) {
55
		$contents = $this->format( $changelog );
56
57
		if ( is_string( $file ) ) {
58
			return file_put_contents( $file, $contents ) === strlen( $contents );
59
		} else {
60
			while ( '' !== $contents ) {
61
				$l = fwrite( $file, $contents );
62
				if ( ! $l ) { // Contrary to the docs, it sometimes returns 0 on error too.
63
					return false;
64
				}
65
				$contents = (string) substr( $contents, $l );
66
			}
67
			return true;
68
		}
69
	}
70
71
	/**
72
	 * Create a new ChangelogEntry.
73
	 *
74
	 * @param string $version See `ChangelogEntry::__construct()`.
75
	 * @param array  $data See `ChangelogEntry::__construct()`.
76
	 * @returns ChangelogEntry
77
	 */
78
	public function newChangelogEntry( $version, $data = array() ) {
79
		return new ChangelogEntry( $version, $data );
80
	}
81
82
	/**
83
	 * Create a new ChangeEntry.
84
	 *
85
	 * @param array $data See `ChangeEntry::__construct()`.
86
	 * @returns ChangeEntry
87
	 */
88
	public function newChangeEntry( $data = array() ) {
89
		return new ChangeEntry( $data );
90
	}
91
92
}
93