Writing_On_GitHub_Blob::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
/**
3
 * API Blob model.
4
 * @package Writing_On_GitHub
5
 */
6
7
/**
8
 * Class Writing_On_GitHub_Blob
9
 */
10
class Writing_On_GitHub_Blob {
11
12
    /**
13
     * Complete blob content.
14
     *
15
     * @var string
16
     */
17
    protected $content;
18
19
    /**
20
     * Blob sha.
21
     *
22
     * @var string
23
     */
24
    protected $sha;
25
26
    /**
27
     * Blob path.
28
     *
29
     * @var string
30
     */
31
    protected $path;
32
33
    /**
34
     * Post id.
35
     *
36
     * @var int
37
     */
38
    protected $id;
39
40
    /**
41
     * Whether the blob has frontmatter.
42
     *
43
     * @var boolean
44
     */
45
    protected $frontmatter = false;
46
47
    /**
48
     * The front matter of github post
49
     * @var string
50
     */
51
    protected $front_matter = '';
52
53
    /**
54
     * Content without front matter
55
     * @var string
56
     */
57
    protected $post_content;
58
59
    /**
60
     * Instantiates a new Blob object.
61
     *
62
     * @param stdClass $data Raw blob data.
63
     */
64
    public function __construct( stdClass $data ) {
65
        $this->interpret_data( $data );
66
    }
67
68
    public function id() {
69
        return $this->id;
70
    }
71
72
    public function set_id($id) {
73
        $this->id = $id;
74
    }
75
76
    /**
77
     * Returns the raw blob content.
78
     *
79
     * @return string
80
     */
81
    public function content() {
82
        return $this->content;
83
    }
84
85
    /**
86
     * Set's the blob's content.
87
     *
88
     * @param string $content Raw blob content.
89
     * @param bool   $base64 Whether the content is base64 encoded.
90
     *
91
     * @return $this
92
     */
93
    public function set_content( $content, $base64 = false ) {
94
        if ( $base64 ) {
95
            $content = base64_decode( $content );
96
        }
97
98
        // remove whitespace from the beginning of content,
99
        // To prevent blank lines before yml
100
        $this->content = ltrim( $content );
101
102
        $this->frontmatter = '---' === substr( $this->content, 0, 3 );
103
104
        return $this;
105
    }
106
    /**
107
     * Returns the blob sha.
108
     *
109
     * @return string
110
     */
111
    public function sha() {
112
        return $this->sha;
113
    }
114
115
    /**
116
     * Return's the blob path.
117
     *
118
     * @return string
119
     */
120
    public function path() {
121
        return $this->path;
122
    }
123
124
    /**
125
     * Whether the blob has frontmatter.
126
     *
127
     * @return bool
128
     */
129
    public function has_frontmatter() {
130
        return $this->frontmatter;
131
    }
132
133
    /**
134
     * The front matter of github post
135
     * @return string
136
     */
137
    public function front_matter() {
138
        return $this->front_matter;
139
    }
140
141
    /**
142
     * Content without front matter
143
     * @return string
144
     */
145
    public function post_content() {
146
        if ( ! $this->post_content ) {
147
            $this->content_import();
148
        }
149
        return $this->post_content;
150
    }
151
152
    /**
153
     * Returns the formatted/filtered blob content used for import.
154
     *
155
     * @return string
156
     */
157
    public function content_import() {
158
        $this->post_content = $content = $this->content();
159
160
        if ( $this->has_frontmatter() ) {
161
            // Break out content.
162
            preg_match( '/(^---(.*?)---$(\r\n|\n|\r)?)?(.*)/ms', $content, $matches );
163
            $this->front_matter = $matches[1];
164
            $this->post_content = $content = array_pop( $matches );
165
        }
166
167
        if ( function_exists( 'wpmarkdown_markdown_to_html' ) ) {
168
            $content = wpmarkdown_markdown_to_html( $content );
169
        }
170
171
        /**
172
         * Filters the content for import.
173
         */
174
        return apply_filters( 'wogh_content_import', trim( $content ) );
175
    }
176
177
    /**
178
     * Returns the blob meta.
179
     *
180
     * @return array
181
     */
182
    public function meta() {
183
        $meta = array();
184
185
        if ( $this->has_frontmatter() ) {
186
            // Break out meta, if present.
187
            preg_match( '/(^---(.*?)---$)?(.*)/ms', $this->content(), $matches );
188
            array_pop( $matches );
189
190
            $meta = spyc_load( $matches[2] );
191
            if ( 'yes' == get_option('wogh_ignore_author') ) {
192
                unset($meta['author']);
193
            }
194
            // if ( isset( $meta['link'] ) ) {
195
            //  $meta['link'] = str_replace( home_url(), '', $meta['link'] );
196
            // }
197
        }
198
199
        return $meta;
200
    }
201
202
    /**
203
     * Formats the blob into an API call body.
204
     *
205
     * @return stdClass
206
     */
207
    // public function to_body() {
208
    //  $data = new stdClass;
209
210
    //  $data->mode = '100644';
211
    //  $data->type = 'blob';
212
213
    //  $data->path = $this->path();
214
215
    //  if ( $this->sha() ) {
216
    //      $data->sha = $this->sha();
217
    //  } else {
218
    //      $data->content = $this->content();
219
    //  }
220
221
    //  return $data;
222
    // }
223
224
225
    /**
226
     * Formats the blob into an API call body.
227
     *
228
     * @return stdClass
229
     */
230
    public function to_body() {
231
        $data = new stdClass;
232
233
        // $data->mode = '100644';
234
        // $data->type = 'blob';
235
236
        $data->path = $this->path();
237
        $data->content = base64_encode( $this->content() );
238
        $data->sha = $this->sha;
239
240
        return $data;
241
    }
242
243
    /**
244
     * Interprets the blob's data into properties.
245
     */
246
    protected function interpret_data( $data ) {
247
        $this->sha  = isset( $data->sha  ) ? $data->sha  : '';
248
        $this->path = isset( $data->path ) ? $data->path : '';
249
250
        $this->set_content(
251
            isset( $data->content ) ? $data->content : '',
252
            isset( $data->encoding ) && 'base64' === $data->encoding ? true : false
253
        );
254
    }
255
}
256