@@ -37,7 +37,7 @@ |
||
37 | 37 | * @param string $endpoint API endpoint. |
38 | 38 | * @param array $body Request body. |
39 | 39 | * |
40 | - * @return stdClass|WP_Error |
|
40 | + * @return stdClass |
|
41 | 41 | */ |
42 | 42 | protected function call( $method, $endpoint, $body = array() ) { |
43 | 43 | if ( is_wp_error( $error = $this->can_call() ) ) { |
@@ -26,7 +26,7 @@ discard block |
||
26 | 26 | * |
27 | 27 | * @param Writing_On_GitHub $app Application container. |
28 | 28 | */ |
29 | - public function __construct( Writing_On_GitHub $app ) { |
|
29 | + public function __construct(Writing_On_GitHub $app) { |
|
30 | 30 | $this->app = $app; |
31 | 31 | } |
32 | 32 | |
@@ -39,8 +39,8 @@ discard block |
||
39 | 39 | * |
40 | 40 | * @return stdClass|WP_Error |
41 | 41 | */ |
42 | - protected function call( $method, $endpoint, $body = array() ) { |
|
43 | - if ( is_wp_error( $error = $this->can_call() ) ) { |
|
42 | + protected function call($method, $endpoint, $body = array()) { |
|
43 | + if (is_wp_error($error = $this->can_call())) { |
|
44 | 44 | return $error; |
45 | 45 | } |
46 | 46 | |
@@ -51,24 +51,23 @@ discard block |
||
51 | 51 | ), |
52 | 52 | ); |
53 | 53 | |
54 | - if ( 'GET' !== $method ) { |
|
55 | - $args['body'] = function_exists( 'wp_json_encode' ) ? |
|
56 | - wp_json_encode( $body ) : |
|
57 | - json_encode( $body ); |
|
54 | + if ('GET' !== $method) { |
|
55 | + $args['body'] = function_exists('wp_json_encode') ? |
|
56 | + wp_json_encode($body) : json_encode($body); |
|
58 | 57 | } |
59 | 58 | |
60 | 59 | $tmpbody = isset($args['body']) ? $args['body'] : ''; |
61 | 60 | error_log("writing-on-github-call $method $endpoint $tmpbody"); |
62 | 61 | |
63 | - $response = wp_remote_request( $endpoint, $args ); |
|
64 | - $status = wp_remote_retrieve_header( $response, 'status' ); |
|
65 | - $body = json_decode( wp_remote_retrieve_body( $response ) ); |
|
62 | + $response = wp_remote_request($endpoint, $args); |
|
63 | + $status = wp_remote_retrieve_header($response, 'status'); |
|
64 | + $body = json_decode(wp_remote_retrieve_body($response)); |
|
66 | 65 | |
67 | - if ( '2' !== substr( $status, 0, 1 ) && '3' !== substr( $status, 0, 1 ) ) { |
|
66 | + if ('2' !== substr($status, 0, 1) && '3' !== substr($status, 0, 1)) { |
|
68 | 67 | return new WP_Error( |
69 | - strtolower( str_replace( ' ', '_', $status ) ), |
|
68 | + strtolower(str_replace(' ', '_', $status)), |
|
70 | 69 | sprintf( |
71 | - __( 'Method %s to endpoint %s failed with error: %s', 'writing-on-github' ), |
|
70 | + __('Method %s to endpoint %s failed with error: %s', 'writing-on-github'), |
|
72 | 71 | $method, |
73 | 72 | $endpoint, |
74 | 73 | $body && $body->message ? $body->message : 'Unknown error' |
@@ -85,28 +84,28 @@ discard block |
||
85 | 84 | * @return true|WP_Error |
86 | 85 | */ |
87 | 86 | protected function can_call() { |
88 | - if ( ! $this->oauth_token() ) { |
|
87 | + if ( ! $this->oauth_token()) { |
|
89 | 88 | return new WP_Error( |
90 | 89 | 'missing_token', |
91 | - __( 'Writing On GitHub needs an auth token. Please update your settings.', 'writing-on-github' ) |
|
90 | + __('Writing On GitHub needs an auth token. Please update your settings.', 'writing-on-github') |
|
92 | 91 | ); |
93 | 92 | } |
94 | 93 | |
95 | 94 | $repo = $this->repository(); |
96 | 95 | |
97 | - if ( ! $repo ) { |
|
96 | + if ( ! $repo) { |
|
98 | 97 | return new WP_Error( |
99 | 98 | 'missing_repository', |
100 | - __( 'Writing On GitHub needs a repository. Please update your settings.', 'writing-on-github' ) |
|
99 | + __('Writing On GitHub needs a repository. Please update your settings.', 'writing-on-github') |
|
101 | 100 | ); |
102 | 101 | } |
103 | 102 | |
104 | - $parts = explode( '/', $repo ); |
|
103 | + $parts = explode('/', $repo); |
|
105 | 104 | |
106 | - if ( 2 !== count( $parts ) ) { |
|
105 | + if (2 !== count($parts)) { |
|
107 | 106 | return new WP_Error( |
108 | 107 | 'malformed_repository', |
109 | - __( 'Writing On GitHub needs a properly formed repository. Please update your settings.', 'writing-on-github' ) |
|
108 | + __('Writing On GitHub needs a properly formed repository. Please update your settings.', 'writing-on-github') |
|
110 | 109 | ); |
111 | 110 | } |
112 | 111 | |
@@ -119,7 +118,7 @@ discard block |
||
119 | 118 | * @return string |
120 | 119 | */ |
121 | 120 | public function repository() { |
122 | - return (string) get_option( self::REPO_OPTION_KEY ); |
|
121 | + return (string) get_option(self::REPO_OPTION_KEY); |
|
123 | 122 | } |
124 | 123 | |
125 | 124 | /** |
@@ -128,18 +127,18 @@ discard block |
||
128 | 127 | * @return string |
129 | 128 | */ |
130 | 129 | public function oauth_token() { |
131 | - return (string) get_option( self::TOKEN_OPTION_KEY ); |
|
130 | + return (string) get_option(self::TOKEN_OPTION_KEY); |
|
132 | 131 | } |
133 | 132 | |
134 | 133 | /** |
135 | 134 | * Returns the GitHub host to sync with (for GitHub Enterprise support) |
136 | 135 | */ |
137 | 136 | public function api_base() { |
138 | - return get_option( self::HOST_OPTION_KEY ); |
|
137 | + return get_option(self::HOST_OPTION_KEY); |
|
139 | 138 | } |
140 | 139 | |
141 | 140 | public function branch() { |
142 | - $branch = get_option( self::BRANCH_OPTION_KEY ); |
|
141 | + $branch = get_option(self::BRANCH_OPTION_KEY); |
|
143 | 142 | return $branch ? $branch : 'master'; |
144 | 143 | } |
145 | 144 | |
@@ -200,11 +199,11 @@ discard block |
||
200 | 199 | * |
201 | 200 | * Returns String the relative API call path |
202 | 201 | */ |
203 | - public function content_endpoint( $path = false ) { |
|
202 | + public function content_endpoint($path = false) { |
|
204 | 203 | $url = $this->api_base() . '/repos/'; |
205 | 204 | $url = $url . $this->repository() . '/contents'; |
206 | 205 | |
207 | - if ( ! empty($path) ) { |
|
206 | + if ( ! empty($path)) { |
|
208 | 207 | $url .= '/' . $path; |
209 | 208 | } |
210 | 209 |
@@ -31,6 +31,7 @@ |
||
31 | 31 | /** |
32 | 32 | * Delete the file. |
33 | 33 | * |
34 | + * @param string $message |
|
34 | 35 | * @return array |
35 | 36 | */ |
36 | 37 | public function delete_file( $path, $sha, $message ) { |
@@ -16,9 +16,9 @@ discard block |
||
16 | 16 | */ |
17 | 17 | protected function export_user() { |
18 | 18 | $user_id = get_current_user_id(); |
19 | - $user = get_userdata( $user_id ); |
|
19 | + $user = get_userdata($user_id); |
|
20 | 20 | |
21 | - if ( $user ) { |
|
21 | + if ($user) { |
|
22 | 22 | return array( |
23 | 23 | 'name' => $user->display_name, |
24 | 24 | 'email' => $user->user_email, |
@@ -33,17 +33,17 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return array |
35 | 35 | */ |
36 | - public function delete_file( $path, $sha, $message ) { |
|
36 | + public function delete_file($path, $sha, $message) { |
|
37 | 37 | $body = new stdClass(); |
38 | 38 | $body->message = $message; |
39 | 39 | $body->sha = $sha; |
40 | 40 | $body->branch = $this->branch(); |
41 | 41 | |
42 | - if ( $author = $this->export_user() ) { |
|
42 | + if ($author = $this->export_user()) { |
|
43 | 43 | $body->author = $author; |
44 | 44 | } |
45 | 45 | |
46 | - return $this->call( 'DELETE', $this->content_endpoint( $path ), $body ); |
|
46 | + return $this->call('DELETE', $this->content_endpoint($path), $body); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | /** |
@@ -51,17 +51,17 @@ discard block |
||
51 | 51 | * |
52 | 52 | * @return array |
53 | 53 | */ |
54 | - public function create_file( $blob, $message ) { |
|
54 | + public function create_file($blob, $message) { |
|
55 | 55 | $body = $blob->to_body(); |
56 | 56 | $body->message = $message; |
57 | 57 | $body->branch = $this->branch(); |
58 | 58 | unset($body->sha); |
59 | 59 | |
60 | - if ( $author = $this->export_user() ) { |
|
60 | + if ($author = $this->export_user()) { |
|
61 | 61 | $body->author = $author; |
62 | 62 | } |
63 | 63 | |
64 | - return $this->call( 'PUT', $this->content_endpoint( $blob->path() ), $body ); |
|
64 | + return $this->call('PUT', $this->content_endpoint($blob->path()), $body); |
|
65 | 65 | } |
66 | 66 | |
67 | 67 | /** |
@@ -69,15 +69,15 @@ discard block |
||
69 | 69 | * |
70 | 70 | * @return array |
71 | 71 | */ |
72 | - public function update_file( $blob, $message ) { |
|
72 | + public function update_file($blob, $message) { |
|
73 | 73 | $body = $blob->to_body(); |
74 | 74 | $body->message = $message; |
75 | 75 | $body->branch = $this->branch(); |
76 | 76 | |
77 | - if ( $author = $this->export_user() ) { |
|
77 | + if ($author = $this->export_user()) { |
|
78 | 78 | $body->author = $author; |
79 | 79 | } |
80 | 80 | |
81 | - return $this->call( 'PUT', $this->content_endpoint( $blob->path() ), $body ); |
|
81 | + return $this->call('PUT', $this->content_endpoint($blob->path()), $body); |
|
82 | 82 | } |
83 | 83 | } |
@@ -167,7 +167,7 @@ discard block |
||
167 | 167 | * |
168 | 168 | * @param int $post_id Post ID. |
169 | 169 | * |
170 | - * @return boolean |
|
170 | + * @return null|boolean |
|
171 | 171 | */ |
172 | 172 | public function export_post( $post_id ) { |
173 | 173 | if ( wp_is_post_revision( $post_id ) ) { |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | * |
200 | 200 | * @param int $post_id Post ID. |
201 | 201 | * |
202 | - * @return boolean |
|
202 | + * @return null|boolean |
|
203 | 203 | */ |
204 | 204 | public function delete_post( $post_id ) { |
205 | 205 | if ( wp_is_post_revision( $post_id ) ) { |
@@ -21,7 +21,7 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @param Writing_On_GitHub $app Applicatio container. |
23 | 23 | */ |
24 | - public function __construct( Writing_On_GitHub $app ) { |
|
24 | + public function __construct(Writing_On_GitHub $app) { |
|
25 | 25 | $this->app = $app; |
26 | 26 | } |
27 | 27 | |
@@ -34,57 +34,57 @@ discard block |
||
34 | 34 | */ |
35 | 35 | public function pull_posts() { |
36 | 36 | $this->set_ajax(); |
37 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
38 | - return $this->app->response()->error( new WP_Error( |
|
37 | + if ( ! $this->app->semaphore()->is_open()) { |
|
38 | + return $this->app->response()->error(new WP_Error( |
|
39 | 39 | 'semaphore_locked', |
40 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::pull_posts()' ) |
|
41 | - ) ); |
|
40 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::pull_posts()') |
|
41 | + )); |
|
42 | 42 | } |
43 | 43 | |
44 | - if ( ! $this->app->request()->is_secret_valid() ) { |
|
45 | - return $this->app->response()->error( new WP_Error( |
|
44 | + if ( ! $this->app->request()->is_secret_valid()) { |
|
45 | + return $this->app->response()->error(new WP_Error( |
|
46 | 46 | 'invalid_headers', |
47 | - __( 'Failed to validate secret.', 'writing-on-github' ) |
|
48 | - ) ); |
|
47 | + __('Failed to validate secret.', 'writing-on-github') |
|
48 | + )); |
|
49 | 49 | } |
50 | 50 | |
51 | 51 | // ping |
52 | - if ( $this->app->request()->is_ping() ) { |
|
53 | - return $this->app->response()->success( __( 'Wordpress is ready.', 'writing-on-github' ) ); |
|
52 | + if ($this->app->request()->is_ping()) { |
|
53 | + return $this->app->response()->success(__('Wordpress is ready.', 'writing-on-github')); |
|
54 | 54 | } |
55 | 55 | |
56 | 56 | // push |
57 | - if ( ! $this->app->request()->is_push() ) { |
|
58 | - return $this->app->response()->error( new WP_Error( |
|
57 | + if ( ! $this->app->request()->is_push()) { |
|
58 | + return $this->app->response()->error(new WP_Error( |
|
59 | 59 | 'invalid_headers', |
60 | - __( 'Failed to validate webhook event.', 'writing-on-github' ) |
|
61 | - ) ); |
|
60 | + __('Failed to validate webhook event.', 'writing-on-github') |
|
61 | + )); |
|
62 | 62 | } |
63 | 63 | $payload = $this->app->request()->payload(); |
64 | 64 | |
65 | - if ( ! $payload->should_import() ) { |
|
66 | - return $this->app->response()->error( new WP_Error( |
|
65 | + if ( ! $payload->should_import()) { |
|
66 | + return $this->app->response()->error(new WP_Error( |
|
67 | 67 | 'invalid_payload', |
68 | 68 | sprintf( |
69 | - __( "%s won't be imported.", 'writing-on-github' ), |
|
70 | - strtolower( $payload->get_commit_id() ) ? : '[Missing Commit ID]' |
|
69 | + __("%s won't be imported.", 'writing-on-github'), |
|
70 | + strtolower($payload->get_commit_id()) ?: '[Missing Commit ID]' |
|
71 | 71 | ) |
72 | - ) ); |
|
72 | + )); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | $this->app->semaphore()->lock(); |
76 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
77 | - remove_action( 'delete_post', array( $this, 'delete_post' ) ); |
|
76 | + remove_action('save_post', array($this, 'export_post')); |
|
77 | + remove_action('delete_post', array($this, 'delete_post')); |
|
78 | 78 | |
79 | - $result = $this->app->import()->payload( $payload ); |
|
79 | + $result = $this->app->import()->payload($payload); |
|
80 | 80 | |
81 | 81 | $this->app->semaphore()->unlock(); |
82 | 82 | |
83 | - if ( is_wp_error( $result ) ) { |
|
84 | - return $this->app->response()->error( $result ); |
|
83 | + if (is_wp_error($result)) { |
|
84 | + return $this->app->response()->error($result); |
|
85 | 85 | } |
86 | 86 | |
87 | - return $this->app->response()->success( $result ); |
|
87 | + return $this->app->response()->success($result); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | /** |
@@ -92,35 +92,35 @@ discard block |
||
92 | 92 | * |
93 | 93 | * @return boolean |
94 | 94 | */ |
95 | - public function import_master( $user_id ) { |
|
96 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
97 | - return $this->app->response()->error( new WP_Error( |
|
95 | + public function import_master($user_id) { |
|
96 | + if ( ! $this->app->semaphore()->is_open()) { |
|
97 | + return $this->app->response()->error(new WP_Error( |
|
98 | 98 | 'semaphore_locked', |
99 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::import_master()' ) |
|
100 | - ) ); |
|
99 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::import_master()') |
|
100 | + )); |
|
101 | 101 | } |
102 | 102 | |
103 | 103 | $this->app->semaphore()->lock(); |
104 | - remove_action( 'save_post', array( $this, 'export_post' ) ); |
|
105 | - remove_action( 'save_post', array( $this, 'delete_post' ) ); |
|
104 | + remove_action('save_post', array($this, 'export_post')); |
|
105 | + remove_action('save_post', array($this, 'delete_post')); |
|
106 | 106 | |
107 | - if ( $user_id ) { |
|
108 | - wp_set_current_user( $user_id ); |
|
107 | + if ($user_id) { |
|
108 | + wp_set_current_user($user_id); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | $result = $this->app->import()->master(); |
112 | 112 | |
113 | 113 | $this->app->semaphore()->unlock(); |
114 | 114 | |
115 | - if ( is_wp_error( $result ) ) { |
|
116 | - update_option( '_wogh_import_error', $result->get_error_message() ); |
|
115 | + if (is_wp_error($result)) { |
|
116 | + update_option('_wogh_import_error', $result->get_error_message()); |
|
117 | 117 | |
118 | - return $this->app->response()->error( $result ); |
|
118 | + return $this->app->response()->error($result); |
|
119 | 119 | } |
120 | 120 | |
121 | - update_option( '_wogh_import_complete', 'yes' ); |
|
121 | + update_option('_wogh_import_complete', 'yes'); |
|
122 | 122 | |
123 | - return $this->app->response()->success( $result ); |
|
123 | + return $this->app->response()->success($result); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | /** |
@@ -128,33 +128,33 @@ discard block |
||
128 | 128 | * |
129 | 129 | * @return boolean |
130 | 130 | */ |
131 | - public function export_all( $user_id ) { |
|
132 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
133 | - return $this->app->response()->error( new WP_Error( |
|
131 | + public function export_all($user_id) { |
|
132 | + if ( ! $this->app->semaphore()->is_open()) { |
|
133 | + return $this->app->response()->error(new WP_Error( |
|
134 | 134 | 'semaphore_locked', |
135 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_all()' ) |
|
136 | - ) ); |
|
135 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_all()') |
|
136 | + )); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | $this->app->semaphore()->lock(); |
140 | 140 | |
141 | - if ( $user_id ) { |
|
142 | - wp_set_current_user( $user_id ); |
|
141 | + if ($user_id) { |
|
142 | + wp_set_current_user($user_id); |
|
143 | 143 | } |
144 | 144 | |
145 | 145 | $result = $this->app->export()->full(); |
146 | 146 | $this->app->semaphore()->unlock(); |
147 | 147 | |
148 | 148 | // Maybe move option updating out of this class/upgrade message display? |
149 | - if ( is_wp_error( $result ) ) { |
|
150 | - update_option( '_wogh_export_error', $result->get_error_message() ); |
|
149 | + if (is_wp_error($result)) { |
|
150 | + update_option('_wogh_export_error', $result->get_error_message()); |
|
151 | 151 | |
152 | - return $this->app->response()->error( $result ); |
|
152 | + return $this->app->response()->error($result); |
|
153 | 153 | } else { |
154 | - update_option( '_wogh_export_complete', 'yes' ); |
|
155 | - update_option( '_wogh_fully_exported', 'yes' ); |
|
154 | + update_option('_wogh_export_complete', 'yes'); |
|
155 | + update_option('_wogh_fully_exported', 'yes'); |
|
156 | 156 | |
157 | - return $this->app->response()->success( $result ); |
|
157 | + return $this->app->response()->success($result); |
|
158 | 158 | } |
159 | 159 | } |
160 | 160 | |
@@ -167,27 +167,27 @@ discard block |
||
167 | 167 | * |
168 | 168 | * @return boolean |
169 | 169 | */ |
170 | - public function export_post( $post_id ) { |
|
171 | - if ( wp_is_post_revision( $post_id ) ) { |
|
170 | + public function export_post($post_id) { |
|
171 | + if (wp_is_post_revision($post_id)) { |
|
172 | 172 | return; |
173 | 173 | } |
174 | 174 | |
175 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
176 | - return $this->app->response()->error( new WP_Error( |
|
175 | + if ( ! $this->app->semaphore()->is_open()) { |
|
176 | + return $this->app->response()->error(new WP_Error( |
|
177 | 177 | 'semaphore_locked', |
178 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::export_post()' ) |
|
179 | - ) ); |
|
178 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::export_post()') |
|
179 | + )); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | $this->app->semaphore()->lock(); |
183 | - $result = $this->app->export()->update( $post_id ); |
|
183 | + $result = $this->app->export()->update($post_id); |
|
184 | 184 | $this->app->semaphore()->unlock(); |
185 | 185 | |
186 | - if ( is_wp_error( $result ) ) { |
|
187 | - return $this->app->response()->error( $result ); |
|
186 | + if (is_wp_error($result)) { |
|
187 | + return $this->app->response()->error($result); |
|
188 | 188 | } |
189 | 189 | |
190 | - return $this->app->response()->success( $result ); |
|
190 | + return $this->app->response()->success($result); |
|
191 | 191 | } |
192 | 192 | |
193 | 193 | /** |
@@ -199,27 +199,27 @@ discard block |
||
199 | 199 | * |
200 | 200 | * @return boolean |
201 | 201 | */ |
202 | - public function delete_post( $post_id ) { |
|
203 | - if ( wp_is_post_revision( $post_id ) ) { |
|
202 | + public function delete_post($post_id) { |
|
203 | + if (wp_is_post_revision($post_id)) { |
|
204 | 204 | return; |
205 | 205 | } |
206 | 206 | |
207 | - if ( ! $this->app->semaphore()->is_open() ) { |
|
208 | - return $this->app->response()->error( new WP_Error( |
|
207 | + if ( ! $this->app->semaphore()->is_open()) { |
|
208 | + return $this->app->response()->error(new WP_Error( |
|
209 | 209 | 'semaphore_locked', |
210 | - sprintf( __( '%s : Semaphore is locked, import/export already in progress.', 'writing-on-github' ), 'Controller::delete_post()' ) |
|
211 | - ) ); |
|
210 | + sprintf(__('%s : Semaphore is locked, import/export already in progress.', 'writing-on-github'), 'Controller::delete_post()') |
|
211 | + )); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | $this->app->semaphore()->lock(); |
215 | - $result = $this->app->export()->delete( $post_id ); |
|
215 | + $result = $this->app->export()->delete($post_id); |
|
216 | 216 | $this->app->semaphore()->unlock(); |
217 | 217 | |
218 | - if ( is_wp_error( $result ) ) { |
|
219 | - return $this->app->response()->error( $result ); |
|
218 | + if (is_wp_error($result)) { |
|
219 | + return $this->app->response()->error($result); |
|
220 | 220 | } |
221 | 221 | |
222 | - return $this->app->response()->success( $result ); |
|
222 | + return $this->app->response()->success($result); |
|
223 | 223 | } |
224 | 224 | |
225 | 225 | /** |
@@ -228,8 +228,8 @@ discard block |
||
228 | 228 | * than just returning data. |
229 | 229 | */ |
230 | 230 | protected function set_ajax() { |
231 | - if ( ! defined( 'WOGH_AJAX' ) ) { |
|
232 | - define( 'WOGH_AJAX', true ); |
|
231 | + if ( ! defined('WOGH_AJAX')) { |
|
232 | + define('WOGH_AJAX', true); |
|
233 | 233 | } |
234 | 234 | } |
235 | 235 | } |
@@ -108,7 +108,6 @@ discard block |
||
108 | 108 | * and associates their author as well as their latest |
109 | 109 | * |
110 | 110 | * @param Writing_On_GitHub_Post[] $posts Array of Posts to save. |
111 | - * @param string $email Author email. |
|
112 | 111 | * |
113 | 112 | * @return string|WP_Error |
114 | 113 | */ |
@@ -178,6 +177,9 @@ discard block |
||
178 | 177 | return __( 'Successfully saved posts.', 'writing-on-github' ); |
179 | 178 | } |
180 | 179 | |
180 | + /** |
|
181 | + * @param Writing_On_GitHub_Post $post |
|
182 | + */ |
|
181 | 183 | protected function post_args( $post ) { |
182 | 184 | $args = $post->get_args(); |
183 | 185 | $meta = $post->get_meta(); |
@@ -182,45 +182,45 @@ discard block |
||
182 | 182 | |
183 | 183 | // update tags |
184 | 184 | if ( isset( $meta['tags'] ) && $meta['tags'] ) { |
185 | - $args['tags_input'] = $meta['tags']; |
|
185 | + $args['tags_input'] = $meta['tags']; |
|
186 | 186 | } |
187 | 187 | |
188 | 188 | // update categories |
189 | 189 | if ( isset( $meta['categories'] ) && $meta['categories'] ) { |
190 | - $categories = $meta['categories']; |
|
191 | - if (!is_array($categories)) { |
|
192 | - $categories = array($categories); |
|
193 | - } |
|
194 | - $terms = get_terms(array( |
|
195 | - 'taxonomy' => 'category', |
|
196 | - 'fields' => 'id=>name', |
|
197 | - 'hide_empty' => 0, |
|
198 | - 'name' => $categories |
|
199 | - ) |
|
200 | - ); |
|
201 | - $map = array(); |
|
202 | - foreach ($categories as $name) { |
|
203 | - $map[$name] = 1; |
|
204 | - } |
|
205 | - |
|
206 | - $ids = array(); |
|
207 | - if (!empty($terms)) { |
|
208 | - foreach ($terms as $id => $name) { |
|
209 | - $ids[] = $id; |
|
210 | - unset($map[$name]); |
|
211 | - } |
|
212 | - } |
|
213 | - |
|
214 | - // create new terms |
|
215 | - if (!empty($map)) { |
|
216 | - foreach ($map as $name => $value) { |
|
217 | - $term = wp_insert_term($name, 'category', array('parent' => 0)); |
|
218 | - // array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
219 | - $ids[] = $term['term_id']; |
|
220 | - } |
|
221 | - } |
|
222 | - |
|
223 | - $args['post_category'] = $ids; |
|
190 | + $categories = $meta['categories']; |
|
191 | + if (!is_array($categories)) { |
|
192 | + $categories = array($categories); |
|
193 | + } |
|
194 | + $terms = get_terms(array( |
|
195 | + 'taxonomy' => 'category', |
|
196 | + 'fields' => 'id=>name', |
|
197 | + 'hide_empty' => 0, |
|
198 | + 'name' => $categories |
|
199 | + ) |
|
200 | + ); |
|
201 | + $map = array(); |
|
202 | + foreach ($categories as $name) { |
|
203 | + $map[$name] = 1; |
|
204 | + } |
|
205 | + |
|
206 | + $ids = array(); |
|
207 | + if (!empty($terms)) { |
|
208 | + foreach ($terms as $id => $name) { |
|
209 | + $ids[] = $id; |
|
210 | + unset($map[$name]); |
|
211 | + } |
|
212 | + } |
|
213 | + |
|
214 | + // create new terms |
|
215 | + if (!empty($map)) { |
|
216 | + foreach ($map as $name => $value) { |
|
217 | + $term = wp_insert_term($name, 'category', array('parent' => 0)); |
|
218 | + // array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
|
219 | + $ids[] = $term['term_id']; |
|
220 | + } |
|
221 | + } |
|
222 | + |
|
223 | + $args['post_category'] = $ids; |
|
224 | 224 | } |
225 | 225 | |
226 | 226 | return $args; |
@@ -422,12 +422,12 @@ discard block |
||
422 | 422 | if ( ! empty( $display_name ) ) { |
423 | 423 | $search_string = esc_attr( $display_name ); |
424 | 424 | $query = new WP_User_Query( array( |
425 | - 'search' => "{$search_string}", |
|
426 | - 'search_columns' => array( |
|
427 | - 'display_name', |
|
428 | - 'user_nicename', |
|
429 | - 'user_login', |
|
430 | - ) |
|
425 | + 'search' => "{$search_string}", |
|
426 | + 'search_columns' => array( |
|
427 | + 'display_name', |
|
428 | + 'user_nicename', |
|
429 | + 'user_login', |
|
430 | + ) |
|
431 | 431 | ) ); |
432 | 432 | $users = $query->get_results(); |
433 | 433 | $user = empty($users) ? false : $users[0]; |
@@ -21,21 +21,21 @@ discard block |
||
21 | 21 | * |
22 | 22 | * @var array |
23 | 23 | */ |
24 | - protected $whitelisted_post_types = array( 'post', 'page' ); |
|
24 | + protected $whitelisted_post_types = array('post', 'page'); |
|
25 | 25 | |
26 | 26 | /** |
27 | 27 | * Currently whitelisted post statuses. |
28 | 28 | * |
29 | 29 | * @var array |
30 | 30 | */ |
31 | - protected $whitelisted_post_statuses = array( 'publish' ); |
|
31 | + protected $whitelisted_post_statuses = array('publish'); |
|
32 | 32 | |
33 | 33 | /** |
34 | 34 | * Instantiates a new Database object. |
35 | 35 | * |
36 | 36 | * @param Writing_On_GitHub $app Application container. |
37 | 37 | */ |
38 | - public function __construct( Writing_On_GitHub $app ) { |
|
38 | + public function __construct(Writing_On_GitHub $app) { |
|
39 | 39 | $this->app = $app; |
40 | 40 | } |
41 | 41 | |
@@ -45,31 +45,31 @@ discard block |
||
45 | 45 | * @return Writing_On_GitHub_Post[]|WP_Error |
46 | 46 | */ |
47 | 47 | public function fetch_all_supported() { |
48 | - $args = array( |
|
48 | + $args = array( |
|
49 | 49 | 'post_type' => $this->get_whitelisted_post_types(), |
50 | 50 | 'post_status' => $this->get_whitelisted_post_statuses(), |
51 | 51 | 'nopaging' => true, |
52 | 52 | 'fields' => 'ids', |
53 | 53 | ); |
54 | 54 | |
55 | - $query = new WP_Query( apply_filters( 'wogh_pre_fetch_all_supported', $args ) ); |
|
55 | + $query = new WP_Query(apply_filters('wogh_pre_fetch_all_supported', $args)); |
|
56 | 56 | |
57 | 57 | $post_ids = $query->get_posts(); |
58 | 58 | |
59 | - if ( ! $post_ids ) { |
|
59 | + if ( ! $post_ids) { |
|
60 | 60 | return new WP_Error( |
61 | 61 | 'no_results', |
62 | - __( 'Querying for supported posts returned no results.', 'writing-on-github' ) |
|
62 | + __('Querying for supported posts returned no results.', 'writing-on-github') |
|
63 | 63 | ); |
64 | 64 | } |
65 | 65 | |
66 | 66 | $results = array(); |
67 | - foreach ( $post_ids as $post_id ) { |
|
67 | + foreach ($post_ids as $post_id) { |
|
68 | 68 | // Do not export posts that have already been exported |
69 | - if ( ! get_post_meta( $post_id, '_wogh_sha', true ) || |
|
70 | - ! get_post_meta( $post_id, '_wogh_github_path', true) ) { |
|
69 | + if ( ! get_post_meta($post_id, '_wogh_sha', true) || |
|
70 | + ! get_post_meta($post_id, '_wogh_github_path', true)) { |
|
71 | 71 | |
72 | - $results[] = new Writing_On_GitHub_Post( $post_id, $this->app->api() ); |
|
72 | + $results[] = new Writing_On_GitHub_Post($post_id, $this->app->api()); |
|
73 | 73 | } |
74 | 74 | } |
75 | 75 | |
@@ -83,10 +83,10 @@ discard block |
||
83 | 83 | * |
84 | 84 | * @return WP_Error|Writing_On_GitHub_Post |
85 | 85 | */ |
86 | - public function fetch_by_id( $post_id ) { |
|
87 | - $post = new Writing_On_GitHub_Post( $post_id, $this->app->api() ); |
|
86 | + public function fetch_by_id($post_id) { |
|
87 | + $post = new Writing_On_GitHub_Post($post_id, $this->app->api()); |
|
88 | 88 | |
89 | - if ( ! $this->is_post_supported( $post ) ) { |
|
89 | + if ( ! $this->is_post_supported($post)) { |
|
90 | 90 | return new WP_Error( |
91 | 91 | 'unsupported_post', |
92 | 92 | sprintf( |
@@ -111,7 +111,7 @@ discard block |
||
111 | 111 | * |
112 | 112 | * @return string|WP_Error |
113 | 113 | */ |
114 | - public function save_posts( array $posts ) { |
|
114 | + public function save_posts(array $posts) { |
|
115 | 115 | |
116 | 116 | /** |
117 | 117 | * Whether an error has occurred. |
@@ -120,20 +120,19 @@ discard block |
||
120 | 120 | */ |
121 | 121 | $error = false; |
122 | 122 | |
123 | - foreach ( $posts as $post ) { |
|
124 | - $args = apply_filters( 'wogh_pre_import_args', $this->post_args( $post ), $post ); |
|
123 | + foreach ($posts as $post) { |
|
124 | + $args = apply_filters('wogh_pre_import_args', $this->post_args($post), $post); |
|
125 | 125 | |
126 | 126 | remove_filter('content_save_pre', 'wp_filter_post_kses'); |
127 | 127 | $post_id = $post->is_new() ? |
128 | - wp_insert_post( $args, true ) : |
|
129 | - wp_update_post( $args, true ); |
|
128 | + wp_insert_post($args, true) : wp_update_post($args, true); |
|
130 | 129 | add_filter('content_save_pre', 'wp_filter_post_kses'); |
131 | 130 | |
132 | - if ( is_wp_error( $post_id ) ) { |
|
133 | - if ( ! $error ) { |
|
131 | + if (is_wp_error($post_id)) { |
|
132 | + if ( ! $error) { |
|
134 | 133 | $error = $post_id; |
135 | 134 | } else { |
136 | - $error->add( $post_id->get_error_code(), $post_id->get_error_message() ); |
|
135 | + $error->add($post_id->get_error_code(), $post_id->get_error_message()); |
|
137 | 136 | } |
138 | 137 | |
139 | 138 | // Abort saving if updating the post fails. |
@@ -142,53 +141,53 @@ discard block |
||
142 | 141 | |
143 | 142 | // $this->set_revision_author( $post_id, $user_id ); |
144 | 143 | |
145 | - if ( $post->is_new() ) { |
|
144 | + if ($post->is_new()) { |
|
146 | 145 | $author = false; |
147 | 146 | $meta = $post->get_meta(); |
148 | - if ( ! empty( $meta ) && ! empty( $meta['author'] ) ) { |
|
147 | + if ( ! empty($meta) && ! empty($meta['author'])) { |
|
149 | 148 | $author = $meta['author']; |
150 | 149 | } |
151 | - $user = $this->fetch_commit_user( $author ); |
|
152 | - $user_id = ! is_wp_error( $user ) ? $user->ID : 0; |
|
153 | - $this->set_post_author( $post_id, $user_id ); |
|
150 | + $user = $this->fetch_commit_user($author); |
|
151 | + $user_id = ! is_wp_error($user) ? $user->ID : 0; |
|
152 | + $this->set_post_author($post_id, $user_id); |
|
154 | 153 | } |
155 | 154 | |
156 | - $post->set_post( get_post( $post_id ) ); |
|
155 | + $post->set_post(get_post($post_id)); |
|
157 | 156 | |
158 | - $meta = apply_filters( 'wogh_pre_import_meta', $post->get_meta(), $post ); |
|
157 | + $meta = apply_filters('wogh_pre_import_meta', $post->get_meta(), $post); |
|
159 | 158 | |
160 | - unset( $meta['tags'] ); |
|
161 | - unset( $meta['categories'] ); |
|
162 | - unset( $meta['author'] ); |
|
163 | - unset( $meta['post_date'] ); |
|
164 | - unset( $meta['post_excerpt'] ); |
|
165 | - unset( $meta['permalink'] ); |
|
159 | + unset($meta['tags']); |
|
160 | + unset($meta['categories']); |
|
161 | + unset($meta['author']); |
|
162 | + unset($meta['post_date']); |
|
163 | + unset($meta['post_excerpt']); |
|
164 | + unset($meta['permalink']); |
|
166 | 165 | |
167 | - foreach ( $meta as $key => $value ) { |
|
168 | - update_post_meta( $post_id, $key, $value ); |
|
166 | + foreach ($meta as $key => $value) { |
|
167 | + update_post_meta($post_id, $key, $value); |
|
169 | 168 | } |
170 | 169 | } |
171 | 170 | |
172 | - if ( $error ) { |
|
171 | + if ($error) { |
|
173 | 172 | return $error; |
174 | 173 | } |
175 | 174 | |
176 | - return __( 'Successfully saved posts.', 'writing-on-github' ); |
|
175 | + return __('Successfully saved posts.', 'writing-on-github'); |
|
177 | 176 | } |
178 | 177 | |
179 | - protected function post_args( $post ) { |
|
178 | + protected function post_args($post) { |
|
180 | 179 | $args = $post->get_args(); |
181 | 180 | $meta = $post->get_meta(); |
182 | 181 | |
183 | 182 | // update tags |
184 | - if ( isset( $meta['tags'] ) && $meta['tags'] ) { |
|
183 | + if (isset($meta['tags']) && $meta['tags']) { |
|
185 | 184 | $args['tags_input'] = $meta['tags']; |
186 | 185 | } |
187 | 186 | |
188 | 187 | // update categories |
189 | - if ( isset( $meta['categories'] ) && $meta['categories'] ) { |
|
188 | + if (isset($meta['categories']) && $meta['categories']) { |
|
190 | 189 | $categories = $meta['categories']; |
191 | - if (!is_array($categories)) { |
|
190 | + if ( ! is_array($categories)) { |
|
192 | 191 | $categories = array($categories); |
193 | 192 | } |
194 | 193 | $terms = get_terms(array( |
@@ -204,7 +203,7 @@ discard block |
||
204 | 203 | } |
205 | 204 | |
206 | 205 | $ids = array(); |
207 | - if (!empty($terms)) { |
|
206 | + if ( ! empty($terms)) { |
|
208 | 207 | foreach ($terms as $id => $name) { |
209 | 208 | $ids[] = $id; |
210 | 209 | unset($map[$name]); |
@@ -212,7 +211,7 @@ discard block |
||
212 | 211 | } |
213 | 212 | |
214 | 213 | // create new terms |
215 | - if (!empty($map)) { |
|
214 | + if ( ! empty($map)) { |
|
216 | 215 | foreach ($map as $name => $value) { |
217 | 216 | $term = wp_insert_term($name, 'category', array('parent' => 0)); |
218 | 217 | // array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id); |
@@ -233,77 +232,77 @@ discard block |
||
233 | 232 | * |
234 | 233 | * @return string|WP_Error |
235 | 234 | */ |
236 | - public function delete_post_by_path( $path ) { |
|
237 | - $query = new WP_Query( array( |
|
235 | + public function delete_post_by_path($path) { |
|
236 | + $query = new WP_Query(array( |
|
238 | 237 | 'meta_key' => '_wogh_github_path', |
239 | 238 | 'meta_value' => $path, |
240 | 239 | 'meta_compare' => '=', |
241 | 240 | 'posts_per_page' => 1, |
242 | 241 | 'fields' => 'ids', |
243 | - ) ); |
|
242 | + )); |
|
244 | 243 | |
245 | 244 | $post_id = $query->get_posts(); |
246 | - $post_id = array_pop( $post_id ); |
|
245 | + $post_id = array_pop($post_id); |
|
247 | 246 | |
248 | - if ( ! $post_id ) { |
|
249 | - $parts = explode( '/', $path ); |
|
250 | - $filename = array_pop( $parts ); |
|
251 | - $directory = $parts ? array_shift( $parts ) : ''; |
|
247 | + if ( ! $post_id) { |
|
248 | + $parts = explode('/', $path); |
|
249 | + $filename = array_pop($parts); |
|
250 | + $directory = $parts ? array_shift($parts) : ''; |
|
252 | 251 | |
253 | - if ( false !== strpos( $directory, 'post' ) ) { |
|
254 | - preg_match( '/([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md/', $filename, $matches ); |
|
252 | + if (false !== strpos($directory, 'post')) { |
|
253 | + preg_match('/([0-9]{4})-([0-9]{2})-([0-9]{2})-(.*)\.md/', $filename, $matches); |
|
255 | 254 | $title = $matches[4]; |
256 | 255 | |
257 | - $query = new WP_Query( array( |
|
256 | + $query = new WP_Query(array( |
|
258 | 257 | 'name' => $title, |
259 | 258 | 'posts_per_page' => 1, |
260 | 259 | 'post_type' => $this->get_whitelisted_post_types(), |
261 | 260 | 'fields' => 'ids', |
262 | - ) ); |
|
261 | + )); |
|
263 | 262 | |
264 | 263 | $post_id = $query->get_posts(); |
265 | - $post_id = array_pop( $post_id ); |
|
264 | + $post_id = array_pop($post_id); |
|
266 | 265 | } |
267 | 266 | |
268 | - if ( ! $post_id ) { |
|
269 | - preg_match( '/(.*)\.md/', $filename, $matches ); |
|
267 | + if ( ! $post_id) { |
|
268 | + preg_match('/(.*)\.md/', $filename, $matches); |
|
270 | 269 | $title = $matches[1]; |
271 | 270 | |
272 | - $query = new WP_Query( array( |
|
271 | + $query = new WP_Query(array( |
|
273 | 272 | 'name' => $title, |
274 | 273 | 'posts_per_page' => 1, |
275 | 274 | 'post_type' => $this->get_whitelisted_post_types(), |
276 | 275 | 'fields' => 'ids', |
277 | - ) ); |
|
276 | + )); |
|
278 | 277 | |
279 | 278 | $post_id = $query->get_posts(); |
280 | - $post_id = array_pop( $post_id ); |
|
279 | + $post_id = array_pop($post_id); |
|
281 | 280 | } |
282 | 281 | } |
283 | 282 | |
284 | - if ( ! $post_id ) { |
|
283 | + if ( ! $post_id) { |
|
285 | 284 | return new WP_Error( |
286 | 285 | 'path_not_found', |
287 | 286 | sprintf( |
288 | - __( 'Post not found for path %s.', 'writing-on-github' ), |
|
287 | + __('Post not found for path %s.', 'writing-on-github'), |
|
289 | 288 | $path |
290 | 289 | ) |
291 | 290 | ); |
292 | 291 | } |
293 | 292 | |
294 | - $result = wp_delete_post( $post_id ); |
|
293 | + $result = wp_delete_post($post_id); |
|
295 | 294 | |
296 | 295 | // If deleting fails... |
297 | - if ( false === $result ) { |
|
298 | - $post = get_post( $post_id ); |
|
296 | + if (false === $result) { |
|
297 | + $post = get_post($post_id); |
|
299 | 298 | |
300 | 299 | // ...and the post both exists and isn't in the trash... |
301 | - if ( $post && 'trash' !== $post->post_status ) { |
|
300 | + if ($post && 'trash' !== $post->post_status) { |
|
302 | 301 | // ... then something went wrong. |
303 | 302 | return new WP_Error( |
304 | 303 | 'db_error', |
305 | 304 | sprintf( |
306 | - __( 'Failed to delete post ID %d.', 'writing-on-github' ), |
|
305 | + __('Failed to delete post ID %d.', 'writing-on-github'), |
|
307 | 306 | $post_id |
308 | 307 | ) |
309 | 308 | ); |
@@ -311,25 +310,25 @@ discard block |
||
311 | 310 | } |
312 | 311 | |
313 | 312 | return sprintf( |
314 | - __( 'Successfully deleted post ID %d.', 'writing-on-github' ), |
|
313 | + __('Successfully deleted post ID %d.', 'writing-on-github'), |
|
315 | 314 | $post_id |
316 | 315 | ); |
317 | 316 | } |
318 | 317 | |
319 | - public function delete_post( $post_id ) { |
|
320 | - $result = wp_delete_post( $post_id ); |
|
318 | + public function delete_post($post_id) { |
|
319 | + $result = wp_delete_post($post_id); |
|
321 | 320 | |
322 | 321 | // If deleting fails... |
323 | - if ( false === $result ) { |
|
324 | - $post = get_post( $post_id ); |
|
322 | + if (false === $result) { |
|
323 | + $post = get_post($post_id); |
|
325 | 324 | |
326 | 325 | // ...and the post both exists and isn't in the trash... |
327 | - if ( $post && 'trash' !== $post->post_status ) { |
|
326 | + if ($post && 'trash' !== $post->post_status) { |
|
328 | 327 | // ... then something went wrong. |
329 | 328 | return new WP_Error( |
330 | 329 | 'db_error', |
331 | 330 | sprintf( |
332 | - __( 'Failed to delete post ID %d.', 'writing-on-github' ), |
|
331 | + __('Failed to delete post ID %d.', 'writing-on-github'), |
|
333 | 332 | $post_id |
334 | 333 | ) |
335 | 334 | ); |
@@ -337,7 +336,7 @@ discard block |
||
337 | 336 | } |
338 | 337 | |
339 | 338 | return sprintf( |
340 | - __( 'Successfully deleted post ID %d.', 'writing-on-github' ), |
|
339 | + __('Successfully deleted post ID %d.', 'writing-on-github'), |
|
341 | 340 | $post_id |
342 | 341 | ); |
343 | 342 | } |
@@ -348,7 +347,7 @@ discard block |
||
348 | 347 | * @return array |
349 | 348 | */ |
350 | 349 | protected function get_whitelisted_post_types() { |
351 | - return apply_filters( 'wogh_whitelisted_post_types', $this->whitelisted_post_types ); |
|
350 | + return apply_filters('wogh_whitelisted_post_types', $this->whitelisted_post_types); |
|
352 | 351 | } |
353 | 352 | |
354 | 353 | /** |
@@ -357,7 +356,7 @@ discard block |
||
357 | 356 | * @return array |
358 | 357 | */ |
359 | 358 | protected function get_whitelisted_post_statuses() { |
360 | - return apply_filters( 'wogh_whitelisted_post_statuses', $this->whitelisted_post_statuses ); |
|
359 | + return apply_filters('wogh_whitelisted_post_statuses', $this->whitelisted_post_statuses); |
|
361 | 360 | } |
362 | 361 | |
363 | 362 | /** |
@@ -367,12 +366,12 @@ discard block |
||
367 | 366 | * |
368 | 367 | * @return string Whitelist formatted for query |
369 | 368 | */ |
370 | - protected function format_for_query( $whitelist ) { |
|
371 | - foreach ( $whitelist as $key => $value ) { |
|
372 | - $whitelist[ $key ] = "'$value'"; |
|
369 | + protected function format_for_query($whitelist) { |
|
370 | + foreach ($whitelist as $key => $value) { |
|
371 | + $whitelist[$key] = "'$value'"; |
|
373 | 372 | } |
374 | 373 | |
375 | - return implode( ', ', $whitelist ); |
|
374 | + return implode(', ', $whitelist); |
|
376 | 375 | } |
377 | 376 | |
378 | 377 | /** |
@@ -383,25 +382,25 @@ discard block |
||
383 | 382 | * |
384 | 383 | * @return boolean True if supported, false if not. |
385 | 384 | */ |
386 | - protected function is_post_supported( Writing_On_GitHub_Post $post ) { |
|
387 | - if ( wp_is_post_revision( $post->id ) ) { |
|
385 | + protected function is_post_supported(Writing_On_GitHub_Post $post) { |
|
386 | + if (wp_is_post_revision($post->id)) { |
|
388 | 387 | return false; |
389 | 388 | } |
390 | 389 | |
391 | 390 | // We need to allow trashed posts to be queried, but they are not whitelisted for export. |
392 | - if ( ! in_array( $post->status(), $this->get_whitelisted_post_statuses() ) && 'trash' !== $post->status() ) { |
|
391 | + if ( ! in_array($post->status(), $this->get_whitelisted_post_statuses()) && 'trash' !== $post->status()) { |
|
393 | 392 | return false; |
394 | 393 | } |
395 | 394 | |
396 | - if ( ! in_array( $post->type(), $this->get_whitelisted_post_types() ) ) { |
|
395 | + if ( ! in_array($post->type(), $this->get_whitelisted_post_types())) { |
|
397 | 396 | return false; |
398 | 397 | } |
399 | 398 | |
400 | - if ( $post->has_password() ) { |
|
399 | + if ($post->has_password()) { |
|
401 | 400 | return false; |
402 | 401 | } |
403 | 402 | |
404 | - return apply_filters( 'wogh_is_post_supported', true, $post ); |
|
403 | + return apply_filters('wogh_is_post_supported', true, $post); |
|
405 | 404 | } |
406 | 405 | |
407 | 406 | /** |
@@ -414,35 +413,35 @@ discard block |
||
414 | 413 | * |
415 | 414 | * @return WP_Error|WP_User |
416 | 415 | */ |
417 | - protected function fetch_commit_user( $display_name ) { |
|
416 | + protected function fetch_commit_user($display_name) { |
|
418 | 417 | // If we can't find a user and a default hasn't been set, |
419 | 418 | // we're just going to set the revision author to 0. |
420 | 419 | $user = false; |
421 | 420 | |
422 | - if ( ! empty( $display_name ) ) { |
|
423 | - $search_string = esc_attr( $display_name ); |
|
424 | - $query = new WP_User_Query( array( |
|
421 | + if ( ! empty($display_name)) { |
|
422 | + $search_string = esc_attr($display_name); |
|
423 | + $query = new WP_User_Query(array( |
|
425 | 424 | 'search' => "{$search_string}", |
426 | 425 | 'search_columns' => array( |
427 | 426 | 'display_name', |
428 | 427 | 'user_nicename', |
429 | 428 | 'user_login', |
430 | 429 | ) |
431 | - ) ); |
|
430 | + )); |
|
432 | 431 | $users = $query->get_results(); |
433 | 432 | $user = empty($users) ? false : $users[0]; |
434 | 433 | } |
435 | 434 | |
436 | - if ( ! $user ) { |
|
435 | + if ( ! $user) { |
|
437 | 436 | // Use the default user. |
438 | - $user = get_user_by( 'id', (int) get_option( 'wogh_default_user' ) ); |
|
437 | + $user = get_user_by('id', (int) get_option('wogh_default_user')); |
|
439 | 438 | } |
440 | 439 | |
441 | - if ( ! $user ) { |
|
440 | + if ( ! $user) { |
|
442 | 441 | return new WP_Error( |
443 | 442 | 'user_not_found', |
444 | 443 | sprintf( |
445 | - __( 'Commit user not found for email %s', 'writing-on-github' ), |
|
444 | + __('Commit user not found for email %s', 'writing-on-github'), |
|
446 | 445 | |
447 | 446 | ) |
448 | 447 | ); |
@@ -460,28 +459,28 @@ discard block |
||
460 | 459 | * |
461 | 460 | * @return string|WP_Error |
462 | 461 | */ |
463 | - protected function set_revision_author( $post_id, $user_id ) { |
|
464 | - $revision = wp_get_post_revisions( $post_id ); |
|
462 | + protected function set_revision_author($post_id, $user_id) { |
|
463 | + $revision = wp_get_post_revisions($post_id); |
|
465 | 464 | |
466 | - if ( ! $revision ) { |
|
467 | - $new_revision = wp_save_post_revision( $post_id ); |
|
465 | + if ( ! $revision) { |
|
466 | + $new_revision = wp_save_post_revision($post_id); |
|
468 | 467 | |
469 | - if ( ! $new_revision || is_wp_error( $new_revision ) ) { |
|
470 | - return new WP_Error( 'db_error', 'There was a problem saving a new revision.' ); |
|
468 | + if ( ! $new_revision || is_wp_error($new_revision)) { |
|
469 | + return new WP_Error('db_error', 'There was a problem saving a new revision.'); |
|
471 | 470 | } |
472 | 471 | |
473 | 472 | // `wp_save_post_revision` returns the ID, whereas `get_post_revision` returns the whole object |
474 | 473 | // in order to be consistent, let's make sure we have the whole object before continuing. |
475 | - $revision = get_post( $new_revision ); |
|
474 | + $revision = get_post($new_revision); |
|
476 | 475 | |
477 | - if ( ! $revision ) { |
|
478 | - return new WP_Error( 'db_error', 'There was a problem retrieving the newly recreated revision.' ); |
|
476 | + if ( ! $revision) { |
|
477 | + return new WP_Error('db_error', 'There was a problem retrieving the newly recreated revision.'); |
|
479 | 478 | } |
480 | 479 | } else { |
481 | - $revision = array_shift( $revision ); |
|
480 | + $revision = array_shift($revision); |
|
482 | 481 | } |
483 | 482 | |
484 | - return $this->set_post_author( $revision->ID, $user_id ); |
|
483 | + return $this->set_post_author($revision->ID, $user_id); |
|
485 | 484 | } |
486 | 485 | |
487 | 486 | /** |
@@ -494,7 +493,7 @@ discard block |
||
494 | 493 | * |
495 | 494 | * @return string|WP_Error |
496 | 495 | */ |
497 | - protected function set_post_author( $post_id, $user_id ) { |
|
496 | + protected function set_post_author($post_id, $user_id) { |
|
498 | 497 | global $wpdb; |
499 | 498 | |
500 | 499 | $result = $wpdb->update( |
@@ -505,25 +504,25 @@ discard block |
||
505 | 504 | array( |
506 | 505 | 'ID' => (int) $post_id, |
507 | 506 | ), |
508 | - array( '%d' ), |
|
509 | - array( '%d' ) |
|
507 | + array('%d'), |
|
508 | + array('%d') |
|
510 | 509 | ); |
511 | 510 | |
512 | - if ( false === $result ) { |
|
513 | - return new WP_Error( 'db_error', $wpdb->last_error ); |
|
511 | + if (false === $result) { |
|
512 | + return new WP_Error('db_error', $wpdb->last_error); |
|
514 | 513 | } |
515 | 514 | |
516 | - if ( 0 === $result ) { |
|
515 | + if (0 === $result) { |
|
517 | 516 | return sprintf( |
518 | - __( 'No change for post ID %d.', 'writing-on-github' ), |
|
517 | + __('No change for post ID %d.', 'writing-on-github'), |
|
519 | 518 | $post_id |
520 | 519 | ); |
521 | 520 | } |
522 | 521 | |
523 | - clean_post_cache( $post_id ); |
|
522 | + clean_post_cache($post_id); |
|
524 | 523 | |
525 | 524 | return sprintf( |
526 | - __( 'Successfully updated post ID %d.', 'writing-on-github' ), |
|
525 | + __('Successfully updated post ID %d.', 'writing-on-github'), |
|
527 | 526 | $post_id |
528 | 527 | ); |
529 | 528 | } |
@@ -536,7 +535,7 @@ discard block |
||
536 | 535 | * |
537 | 536 | * @return bool|int |
538 | 537 | */ |
539 | - public function set_post_sha( $post, $sha ) { |
|
540 | - return update_post_meta( $post->id, '_wogh_sha', $sha ); |
|
538 | + public function set_post_sha($post, $sha) { |
|
539 | + return update_post_meta($post->id, '_wogh_sha', $sha); |
|
541 | 540 | } |
542 | 541 | } |
@@ -142,6 +142,10 @@ |
||
142 | 142 | return true; |
143 | 143 | } |
144 | 144 | |
145 | + /** |
|
146 | + * @param Writing_On_GitHub_Post $post |
|
147 | + * @param Writing_On_GitHub_Persist_Client $persist |
|
148 | + */ |
|
145 | 149 | protected function new_post( $post, $persist ) { |
146 | 150 | $github_path = $post->github_path(); |
147 | 151 | $old_github_path = $post->old_github_path(); |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param Writing_On_GitHub $app Application container. |
24 | 24 | */ |
25 | - public function __construct( Writing_On_GitHub $app ) { |
|
25 | + public function __construct(Writing_On_GitHub $app) { |
|
26 | 26 | $this->app = $app; |
27 | 27 | } |
28 | 28 | |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | public function full() { |
35 | 35 | $posts = $this->app->database()->fetch_all_supported(); |
36 | 36 | |
37 | - if ( is_wp_error( $posts ) ) { |
|
37 | + if (is_wp_error($posts)) { |
|
38 | 38 | return $posts; |
39 | 39 | } |
40 | 40 | |
@@ -53,11 +53,11 @@ discard block |
||
53 | 53 | |
54 | 54 | // $result = $this->app->api()->persist()->commit( $master ); |
55 | 55 | |
56 | - if ( is_wp_error( $result ) ) { |
|
56 | + if (is_wp_error($result)) { |
|
57 | 57 | return $result; |
58 | 58 | } |
59 | 59 | |
60 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
60 | + return __('Export to GitHub completed successfully.', 'writing-on-github'); |
|
61 | 61 | } |
62 | 62 | |
63 | 63 | |
@@ -66,10 +66,10 @@ discard block |
||
66 | 66 | * @param int $post_id |
67 | 67 | * @return boolean |
68 | 68 | */ |
69 | - protected function github_path( $post_id ) { |
|
70 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
69 | + protected function github_path($post_id) { |
|
70 | + $github_path = get_post_meta($post_id, '_wogh_github_path', true); |
|
71 | 71 | |
72 | - if ( $github_path && $this->app->api()->fetch()->exists( $github_path ) ) { |
|
72 | + if ($github_path && $this->app->api()->fetch()->exists($github_path)) { |
|
73 | 73 | return $github_path; |
74 | 74 | } |
75 | 75 | |
@@ -83,29 +83,29 @@ discard block |
||
83 | 83 | * |
84 | 84 | * @return string|WP_Error |
85 | 85 | */ |
86 | - public function update( $post_id ) { |
|
87 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
86 | + public function update($post_id) { |
|
87 | + $post = $this->app->database()->fetch_by_id($post_id); |
|
88 | 88 | |
89 | - if ( is_wp_error( $post ) ) { |
|
89 | + if (is_wp_error($post)) { |
|
90 | 90 | return $post; |
91 | 91 | } |
92 | 92 | |
93 | - if ( 'trash' === $post->status() ) { |
|
94 | - return $this->delete( $post_id ); |
|
93 | + if ('trash' === $post->status()) { |
|
94 | + return $this->delete($post_id); |
|
95 | 95 | } |
96 | 96 | |
97 | - if ( $old_github_path = $this->github_path( $post->id() ) ) { |
|
97 | + if ($old_github_path = $this->github_path($post->id())) { |
|
98 | 98 | error_log("old_github_path: $old_github_path"); |
99 | 99 | $post->set_old_github_path($old_github_path); |
100 | 100 | } |
101 | 101 | |
102 | - $result = $this->new_posts( array( $post ) ); |
|
102 | + $result = $this->new_posts(array($post)); |
|
103 | 103 | |
104 | - if ( is_wp_error( $result ) ) { |
|
104 | + if (is_wp_error($result)) { |
|
105 | 105 | return $result; |
106 | 106 | } |
107 | 107 | |
108 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
108 | + return __('Export to GitHub completed successfully.', 'writing-on-github'); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
@@ -115,16 +115,16 @@ discard block |
||
115 | 115 | * |
116 | 116 | * @return string|WP_Error |
117 | 117 | */ |
118 | - public function new_posts( array $posts ) { |
|
118 | + public function new_posts(array $posts) { |
|
119 | 119 | $error = false; |
120 | 120 | |
121 | 121 | $persist = $this->app->api()->persist(); |
122 | 122 | |
123 | - foreach ( $posts as $post ) { |
|
124 | - $result = $this->new_post( $post, $persist ); |
|
125 | - if ( is_wp_error( $result ) ) { |
|
126 | - if ( $error ) { |
|
127 | - $error->add( $result->get_error_code(), $result->get_error_message() ); |
|
123 | + foreach ($posts as $post) { |
|
124 | + $result = $this->new_post($post, $persist); |
|
125 | + if (is_wp_error($result)) { |
|
126 | + if ($error) { |
|
127 | + $error->add($result->get_error_code(), $result->get_error_message()); |
|
128 | 128 | } else { |
129 | 129 | $error = $result; |
130 | 130 | } |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | |
136 | 136 | // $result = $this->app->api()->persist()->commit( $master ); |
137 | 137 | |
138 | - if ( is_wp_error( $error ) ) { |
|
138 | + if (is_wp_error($error)) { |
|
139 | 139 | return $error; |
140 | 140 | } |
141 | 141 | |
@@ -143,13 +143,13 @@ discard block |
||
143 | 143 | return true; |
144 | 144 | } |
145 | 145 | |
146 | - protected function new_post( $post, $persist ) { |
|
146 | + protected function new_post($post, $persist) { |
|
147 | 147 | $github_path = $post->github_path(); |
148 | 148 | $old_github_path = $post->old_github_path(); |
149 | 149 | $blob = $post->to_blob(); |
150 | 150 | $result = false; |
151 | 151 | |
152 | - if ( $old_github_path && $old_github_path != $github_path ) { |
|
152 | + if ($old_github_path && $old_github_path != $github_path) { |
|
153 | 153 | // rename |
154 | 154 | $message = apply_filters( |
155 | 155 | 'wogh_commit_msg_move_post', |
@@ -157,20 +157,20 @@ discard block |
||
157 | 157 | 'Moving %s to %s via WordPress at %s (%s)', |
158 | 158 | $old_github_path, $github_path, |
159 | 159 | site_url(), |
160 | - get_bloginfo( 'name' ) |
|
160 | + get_bloginfo('name') |
|
161 | 161 | ) |
162 | 162 | ) . $this->get_commit_msg_tag(); |
163 | 163 | |
164 | - $result = $persist->delete_file( $post->old_github_path(), $blob->sha(), $message ); |
|
165 | - if ( is_wp_error( $result ) ) { |
|
164 | + $result = $persist->delete_file($post->old_github_path(), $blob->sha(), $message); |
|
165 | + if (is_wp_error($result)) { |
|
166 | 166 | return $result; |
167 | 167 | } |
168 | 168 | |
169 | - $result = $persist->create_file( $blob, $message ); |
|
170 | - if ( is_wp_error( $result ) ) { |
|
169 | + $result = $persist->create_file($blob, $message); |
|
170 | + if (is_wp_error($result)) { |
|
171 | 171 | return $result; |
172 | 172 | } |
173 | - } elseif ( ! $old_github_path ) { |
|
173 | + } elseif ( ! $old_github_path) { |
|
174 | 174 | // create new |
175 | 175 | $message = apply_filters( |
176 | 176 | 'wogh_commit_msg_new_post', |
@@ -178,14 +178,14 @@ discard block |
||
178 | 178 | 'Creating new posts %s from WordPress at %s (%s)', |
179 | 179 | $github_path, |
180 | 180 | site_url(), |
181 | - get_bloginfo( 'name' ) |
|
181 | + get_bloginfo('name') |
|
182 | 182 | ) |
183 | 183 | ) . $this->get_commit_msg_tag(); |
184 | - $result = $persist->create_file( $blob, $message ); |
|
185 | - if ( is_wp_error( $result ) ) { |
|
184 | + $result = $persist->create_file($blob, $message); |
|
185 | + if (is_wp_error($result)) { |
|
186 | 186 | return $result; |
187 | 187 | } |
188 | - } elseif ( $old_github_path && $old_github_path == $github_path ) { |
|
188 | + } elseif ($old_github_path && $old_github_path == $github_path) { |
|
189 | 189 | // update |
190 | 190 | $message = apply_filters( |
191 | 191 | 'wogh_commit_msg_update_post', |
@@ -193,11 +193,11 @@ discard block |
||
193 | 193 | 'Creating new posts %s from WordPress at %s (%s)', |
194 | 194 | $github_path, |
195 | 195 | site_url(), |
196 | - get_bloginfo( 'name' ) |
|
196 | + get_bloginfo('name') |
|
197 | 197 | ) |
198 | 198 | ) . $this->get_commit_msg_tag(); |
199 | - $result = $persist->update_file( $blob, $message ); |
|
200 | - if ( is_wp_error( $result ) ) { |
|
199 | + $result = $persist->update_file($blob, $message); |
|
200 | + if (is_wp_error($result)) { |
|
201 | 201 | return $result; |
202 | 202 | } |
203 | 203 | } |
@@ -216,14 +216,14 @@ discard block |
||
216 | 216 | * |
217 | 217 | * @return string|WP_Error |
218 | 218 | */ |
219 | - public function delete( $post_id ) { |
|
220 | - $post = $this->app->database()->fetch_by_id( $post_id ); |
|
219 | + public function delete($post_id) { |
|
220 | + $post = $this->app->database()->fetch_by_id($post_id); |
|
221 | 221 | |
222 | - if ( is_wp_error( $post ) ) { |
|
222 | + if (is_wp_error($post)) { |
|
223 | 223 | return $post; |
224 | 224 | } |
225 | 225 | |
226 | - $github_path = get_post_meta( $post_id, '_wogh_github_path', true ); |
|
226 | + $github_path = get_post_meta($post_id, '_wogh_github_path', true); |
|
227 | 227 | |
228 | 228 | $message = apply_filters( |
229 | 229 | 'wogh_commit_msg_delete', |
@@ -231,18 +231,18 @@ discard block |
||
231 | 231 | 'Deleting %s via WordPress at %s (%s)', |
232 | 232 | $github_path, |
233 | 233 | site_url(), |
234 | - get_bloginfo( 'name' ) |
|
234 | + get_bloginfo('name') |
|
235 | 235 | ), |
236 | 236 | $post |
237 | 237 | ) . $this->get_commit_msg_tag(); |
238 | 238 | |
239 | - $result = $this->app->api()->persist()->delete_file( $github_path, $post->sha(), $message ); |
|
239 | + $result = $this->app->api()->persist()->delete_file($github_path, $post->sha(), $message); |
|
240 | 240 | |
241 | - if ( is_wp_error( $result ) ) { |
|
241 | + if (is_wp_error($result)) { |
|
242 | 242 | return $result; |
243 | 243 | } |
244 | 244 | |
245 | - return __( 'Export to GitHub completed successfully.', 'writing-on-github' ); |
|
245 | + return __('Export to GitHub completed successfully.', 'writing-on-github'); |
|
246 | 246 | } |
247 | 247 | |
248 | 248 | |
@@ -253,8 +253,8 @@ discard block |
||
253 | 253 | * |
254 | 254 | * @return bool |
255 | 255 | */ |
256 | - public function set_user( $user_id ) { |
|
257 | - return update_option( self::EXPORT_USER_OPTION, (int) $user_id ); |
|
256 | + public function set_user($user_id) { |
|
257 | + return update_option(self::EXPORT_USER_OPTION, (int) $user_id); |
|
258 | 258 | } |
259 | 259 | |
260 | 260 | /** |
@@ -263,10 +263,10 @@ discard block |
||
263 | 263 | * @return string |
264 | 264 | */ |
265 | 265 | protected function get_commit_msg_tag() { |
266 | - $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
|
266 | + $tag = apply_filters('wogh_commit_msg_tag', 'wogh'); |
|
267 | 267 | |
268 | - if ( ! $tag ) { |
|
269 | - throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
|
268 | + if ( ! $tag) { |
|
269 | + throw new Exception(__('Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github')); |
|
270 | 270 | } |
271 | 271 | |
272 | 272 | return ' - ' . $tag; |
@@ -140,6 +140,9 @@ discard block |
||
140 | 140 | return __( 'Payload processed', 'writing-on-github' ); |
141 | 141 | } |
142 | 142 | |
143 | + /** |
|
144 | + * @param boolean $delete_ids |
|
145 | + */ |
|
143 | 146 | protected function compare( $files, &$delete_ids ) { |
144 | 147 | if ( is_wp_error( $files ) ) { |
145 | 148 | return $files; |
@@ -217,7 +220,6 @@ discard block |
||
217 | 220 | /** |
218 | 221 | * Checks whether the provided blob should be imported. |
219 | 222 | * |
220 | - * @param Writing_On_GitHub_Blob $blob Blob to validate. |
|
221 | 223 | * |
222 | 224 | * @return bool |
223 | 225 | */ |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * |
23 | 23 | * @param Writing_On_GitHub $app Application container. |
24 | 24 | */ |
25 | - public function __construct( Writing_On_GitHub $app ) { |
|
25 | + public function __construct(Writing_On_GitHub $app) { |
|
26 | 26 | $this->app = $app; |
27 | 27 | } |
28 | 28 | |
@@ -70,21 +70,21 @@ discard block |
||
70 | 70 | // return __( 'Payload processed', 'writing-on-github' ); |
71 | 71 | // } |
72 | 72 | |
73 | - public function payload( Writing_On_GitHub_Payload $payload ) { |
|
73 | + public function payload(Writing_On_GitHub_Payload $payload) { |
|
74 | 74 | |
75 | - $result = $this->app->api()->fetch()->compare( $payload->get_before_commit_id() ); |
|
75 | + $result = $this->app->api()->fetch()->compare($payload->get_before_commit_id()); |
|
76 | 76 | |
77 | - if ( is_wp_error( $result ) ) { |
|
77 | + if (is_wp_error($result)) { |
|
78 | 78 | return $result; |
79 | 79 | } |
80 | 80 | |
81 | - $result = $this->import_files( $result ); |
|
81 | + $result = $this->import_files($result); |
|
82 | 82 | |
83 | - if ( is_wp_error( $result ) ) { |
|
83 | + if (is_wp_error($result)) { |
|
84 | 84 | return $files; |
85 | 85 | } |
86 | 86 | |
87 | - return __( 'Payload processed', 'writing-on-github' ); |
|
87 | + return __('Payload processed', 'writing-on-github'); |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | /** |
@@ -92,23 +92,23 @@ discard block |
||
92 | 92 | * @param array $files [Writing_On_GitHub_File_Info] |
93 | 93 | * @return string|WP_ERROR |
94 | 94 | */ |
95 | - protected function import_files( $files ) { |
|
95 | + protected function import_files($files) { |
|
96 | 96 | |
97 | - $error = false; |
|
97 | + $error = false; |
|
98 | 98 | $delete_ids = false; |
99 | 99 | |
100 | - $result = $this->compare( $files, $delete_ids ); |
|
100 | + $result = $this->compare($files, $delete_ids); |
|
101 | 101 | |
102 | - if ( is_wp_error( $result ) ) { |
|
102 | + if (is_wp_error($result)) { |
|
103 | 103 | return $result; |
104 | 104 | } |
105 | 105 | |
106 | - if ( $delete_ids ) { |
|
106 | + if ($delete_ids) { |
|
107 | 107 | foreach ($delete_ids as $id) { |
108 | - $result = $this->app->database()->delete_post( $id ); |
|
109 | - if ( is_wp_error( $result ) ) { |
|
110 | - if ( $error ) { |
|
111 | - $error->add( $result->get_error_code(), $result->get_error_message() ); |
|
108 | + $result = $this->app->database()->delete_post($id); |
|
109 | + if (is_wp_error($result)) { |
|
110 | + if ($error) { |
|
111 | + $error->add($result->get_error_code(), $result->get_error_message()); |
|
112 | 112 | } else { |
113 | 113 | $error = $result; |
114 | 114 | } |
@@ -127,21 +127,21 @@ discard block |
||
127 | 127 | public function master() { |
128 | 128 | $result = $this->app->api()->fetch()->tree_recursive(); |
129 | 129 | |
130 | - if ( is_wp_error( $result ) ) { |
|
130 | + if (is_wp_error($result)) { |
|
131 | 131 | return $result; |
132 | 132 | } |
133 | 133 | |
134 | - $result = $this->import_files( $result ); |
|
134 | + $result = $this->import_files($result); |
|
135 | 135 | |
136 | - if ( is_wp_error( $result ) ) { |
|
136 | + if (is_wp_error($result)) { |
|
137 | 137 | return $result; |
138 | 138 | } |
139 | 139 | |
140 | - return __( 'Payload processed', 'writing-on-github' ); |
|
140 | + return __('Payload processed', 'writing-on-github'); |
|
141 | 141 | } |
142 | 142 | |
143 | - protected function compare( $files, &$delete_ids ) { |
|
144 | - if ( is_wp_error( $files ) ) { |
|
143 | + protected function compare($files, &$delete_ids) { |
|
144 | + if (is_wp_error($files)) { |
|
145 | 145 | return $files; |
146 | 146 | } |
147 | 147 | |
@@ -150,38 +150,38 @@ discard block |
||
150 | 150 | |
151 | 151 | $idsmap = array(); |
152 | 152 | |
153 | - foreach ( $files as $file ) { |
|
154 | - if ( ! $this->importable_file( $file ) ) { |
|
153 | + foreach ($files as $file) { |
|
154 | + if ( ! $this->importable_file($file)) { |
|
155 | 155 | continue; |
156 | 156 | } |
157 | 157 | |
158 | 158 | $blob = $this->app->api()->fetch()->blob($file); |
159 | 159 | // network error ? |
160 | - if ( is_wp_error($blob) ) { |
|
160 | + if (is_wp_error($blob)) { |
|
161 | 161 | continue; |
162 | 162 | } |
163 | 163 | |
164 | - if ( ! $this->importable_blob($blob) ) { |
|
164 | + if ( ! $this->importable_blob($blob)) { |
|
165 | 165 | continue; |
166 | 166 | } |
167 | 167 | |
168 | - $post = $this->blob_to_post( $blob ); |
|
168 | + $post = $this->blob_to_post($blob); |
|
169 | 169 | |
170 | - if ( $file->status == 'removed' ) { |
|
171 | - if ( $blob->id() ) { |
|
170 | + if ($file->status == 'removed') { |
|
171 | + if ($blob->id()) { |
|
172 | 172 | $idsmap[$blob->id()] = true; |
173 | 173 | } |
174 | - } elseif ( $post != false ) { |
|
174 | + } elseif ($post != false) { |
|
175 | 175 | $posts[] = $post; |
176 | - if ( $post->is_new() ) { |
|
176 | + if ($post->is_new()) { |
|
177 | 177 | $new[] = $post; |
178 | 178 | } |
179 | 179 | } |
180 | 180 | } |
181 | 181 | |
182 | 182 | foreach ($posts as $post) { |
183 | - if ( $post->id() && isset( $idsmap[$post->id()] ) ) { |
|
184 | - unset( $idsmap[$post->id()] ); |
|
183 | + if ($post->id() && isset($idsmap[$post->id()])) { |
|
184 | + unset($idsmap[$post->id()]); |
|
185 | 185 | } |
186 | 186 | } |
187 | 187 | $delete_ids = array(); |
@@ -191,16 +191,16 @@ discard block |
||
191 | 191 | |
192 | 192 | // $this->app->database()->save_posts( $posts, $commit->author_email() ); |
193 | 193 | |
194 | - $result = $this->app->database()->save_posts( $posts ); |
|
194 | + $result = $this->app->database()->save_posts($posts); |
|
195 | 195 | |
196 | - if ( is_wp_error( $result ) ) { |
|
196 | + if (is_wp_error($result)) { |
|
197 | 197 | return $result; |
198 | 198 | } |
199 | 199 | |
200 | - if ( $new ) { |
|
201 | - $result = $this->app->export()->new_posts( $new ); |
|
200 | + if ($new) { |
|
201 | + $result = $this->app->export()->new_posts($new); |
|
202 | 202 | |
203 | - if ( is_wp_error( $result ) ) { |
|
203 | + if (is_wp_error($result)) { |
|
204 | 204 | return $result; |
205 | 205 | } |
206 | 206 | } |
@@ -215,11 +215,11 @@ discard block |
||
215 | 215 | * |
216 | 216 | * @return bool |
217 | 217 | */ |
218 | - protected function importable_file( Writing_On_GitHub_File_Info $file ) { |
|
218 | + protected function importable_file(Writing_On_GitHub_File_Info $file) { |
|
219 | 219 | |
220 | 220 | // only _pages and _posts |
221 | - if ( strncasecmp($file->path, '_pages/', strlen('_pages/') ) != 0 && |
|
222 | - strncasecmp($file->path, '_posts/', strlen('_posts/') ) != 0 ) { |
|
221 | + if (strncasecmp($file->path, '_pages/', strlen('_pages/')) != 0 && |
|
222 | + strncasecmp($file->path, '_posts/', strlen('_posts/')) != 0) { |
|
223 | 223 | return false; |
224 | 224 | } |
225 | 225 | |
@@ -238,7 +238,7 @@ discard block |
||
238 | 238 | * |
239 | 239 | * @return bool |
240 | 240 | */ |
241 | - protected function importable_blob( Writing_On_GitHub_Blob $blob ) { |
|
241 | + protected function importable_blob(Writing_On_GitHub_Blob $blob) { |
|
242 | 242 | // global $wpdb; |
243 | 243 | |
244 | 244 | // // Skip the repo's readme. |
@@ -251,7 +251,7 @@ discard block |
||
251 | 251 | // return false; |
252 | 252 | // } |
253 | 253 | |
254 | - if ( ! $blob->has_frontmatter() ) { |
|
254 | + if ( ! $blob->has_frontmatter()) { |
|
255 | 255 | return false; |
256 | 256 | } |
257 | 257 | |
@@ -265,57 +265,57 @@ discard block |
||
265 | 265 | * |
266 | 266 | * @return Writing_On_GitHub_Post |
267 | 267 | */ |
268 | - protected function blob_to_post( Writing_On_GitHub_Blob $blob ) { |
|
269 | - $args = array( 'post_content' => $blob->content_import() ); |
|
268 | + protected function blob_to_post(Writing_On_GitHub_Blob $blob) { |
|
269 | + $args = array('post_content' => $blob->content_import()); |
|
270 | 270 | $meta = $blob->meta(); |
271 | 271 | |
272 | 272 | $id = false; |
273 | 273 | |
274 | - if ( $meta ) { |
|
275 | - if ( array_key_exists( 'layout', $meta ) ) { |
|
274 | + if ($meta) { |
|
275 | + if (array_key_exists('layout', $meta)) { |
|
276 | 276 | $args['post_type'] = $meta['layout']; |
277 | - unset( $meta['layout'] ); |
|
277 | + unset($meta['layout']); |
|
278 | 278 | } |
279 | 279 | |
280 | - if ( array_key_exists( 'published', $meta ) ) { |
|
280 | + if (array_key_exists('published', $meta)) { |
|
281 | 281 | $args['post_status'] = true === $meta['published'] ? 'publish' : 'draft'; |
282 | - unset( $meta['published'] ); |
|
282 | + unset($meta['published']); |
|
283 | 283 | } |
284 | 284 | |
285 | - if ( array_key_exists( 'post_title', $meta ) ) { |
|
285 | + if (array_key_exists('post_title', $meta)) { |
|
286 | 286 | $args['post_title'] = $meta['post_title']; |
287 | - unset( $meta['post_title'] ); |
|
287 | + unset($meta['post_title']); |
|
288 | 288 | } |
289 | 289 | |
290 | - if ( array_key_exists( 'post_name', $meta ) ) { |
|
290 | + if (array_key_exists('post_name', $meta)) { |
|
291 | 291 | $args['post_name'] = $meta['post_name']; |
292 | - unset( $meta['post_name'] ); |
|
292 | + unset($meta['post_name']); |
|
293 | 293 | } |
294 | 294 | |
295 | - if ( array_key_exists( 'ID', $meta ) ) { |
|
295 | + if (array_key_exists('ID', $meta)) { |
|
296 | 296 | $id = $args['ID'] = $meta['ID']; |
297 | 297 | $blob->set_id($id); |
298 | - unset( $meta['ID'] ); |
|
298 | + unset($meta['ID']); |
|
299 | 299 | } |
300 | 300 | } |
301 | 301 | |
302 | 302 | $meta['_wogh_sha'] = $blob->sha(); |
303 | 303 | |
304 | - if ( $id ) { |
|
305 | - $old_sha = get_post_meta( $id, '_wogh_sha', true ); |
|
306 | - $old_github_path = get_post_meta( $id, '_wogh_github_path', true ); |
|
304 | + if ($id) { |
|
305 | + $old_sha = get_post_meta($id, '_wogh_sha', true); |
|
306 | + $old_github_path = get_post_meta($id, '_wogh_github_path', true); |
|
307 | 307 | |
308 | 308 | // dont save post when has same sha |
309 | - if ( $old_sha && $old_sha == $meta['_wogh_sha'] && |
|
310 | - $old_github_path && $old_github_path == $blob->path() ) { |
|
309 | + if ($old_sha && $old_sha == $meta['_wogh_sha'] && |
|
310 | + $old_github_path && $old_github_path == $blob->path()) { |
|
311 | 311 | return false; |
312 | 312 | } |
313 | 313 | } |
314 | 314 | |
315 | - $post = new Writing_On_GitHub_Post( $args, $this->app->api() ); |
|
316 | - $post->set_old_github_path( $blob->path() ); |
|
317 | - $post->set_meta( $meta ); |
|
318 | - $blob->set_id( $post->id() ); |
|
315 | + $post = new Writing_On_GitHub_Post($args, $this->app->api()); |
|
316 | + $post->set_old_github_path($blob->path()); |
|
317 | + $post->set_meta($meta); |
|
318 | + $blob->set_id($post->id()); |
|
319 | 319 | |
320 | 320 | return $post; |
321 | 321 | } |
@@ -41,7 +41,7 @@ |
||
41 | 41 | /** |
42 | 42 | * Validates the header's secret. |
43 | 43 | * |
44 | - * @return true|WP_Error |
|
44 | + * @return boolean |
|
45 | 45 | */ |
46 | 46 | public function is_secret_valid() { |
47 | 47 | $headers = $this->headers(); |
@@ -34,7 +34,7 @@ discard block |
||
34 | 34 | * |
35 | 35 | * @param Writing_On_GitHub $app Application container. |
36 | 36 | */ |
37 | - public function __construct( Writing_On_GitHub $app ) { |
|
37 | + public function __construct(Writing_On_GitHub $app) { |
|
38 | 38 | $this->app = $app; |
39 | 39 | } |
40 | 40 | |
@@ -49,8 +49,8 @@ discard block |
||
49 | 49 | $this->raw_data = $this->read_raw_data(); |
50 | 50 | |
51 | 51 | // Validate request secret. |
52 | - $hash = hash_hmac( 'sha1', $this->raw_data, $this->secret() ); |
|
53 | - if ( 'sha1=' . $hash !== $headers['X-Hub-Signature'] ) { |
|
52 | + $hash = hash_hmac('sha1', $this->raw_data, $this->secret()); |
|
53 | + if ('sha1=' . $hash !== $headers['X-Hub-Signature']) { |
|
54 | 54 | return false; |
55 | 55 | } |
56 | 56 | |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | * @return Writing_On_GitHub_Payload |
91 | 91 | */ |
92 | 92 | public function payload() { |
93 | - return new Writing_On_GitHub_Payload( $this->app, $this->raw_data ); |
|
93 | + return new Writing_On_GitHub_Payload($this->app, $this->raw_data); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
@@ -101,11 +101,11 @@ discard block |
||
101 | 101 | * @return array |
102 | 102 | */ |
103 | 103 | protected function headers() { |
104 | - if ( $this->headers ) { |
|
104 | + if ($this->headers) { |
|
105 | 105 | return $this->headers; |
106 | 106 | } |
107 | 107 | |
108 | - if ( function_exists( 'getallheaders' ) ) { |
|
108 | + if (function_exists('getallheaders')) { |
|
109 | 109 | |
110 | 110 | $this->headers = getallheaders(); |
111 | 111 | return $this->headers; |
@@ -115,9 +115,9 @@ discard block |
||
115 | 115 | * @see http://www.php.net/manual/en/function.getallheaders.php |
116 | 116 | */ |
117 | 117 | $this->headers = array(); |
118 | - foreach ( $_SERVER as $name => $value ) { |
|
119 | - if ( 'HTTP_' === substr( $name, 0, 5 ) ) { |
|
120 | - $this->headers[ str_replace( ' ', '-', ucwords( strtolower( str_replace( '_', ' ', substr( $name, 5 ) ) ) ) ) ] = $value; |
|
118 | + foreach ($_SERVER as $name => $value) { |
|
119 | + if ('HTTP_' === substr($name, 0, 5)) { |
|
120 | + $this->headers[str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))))] = $value; |
|
121 | 121 | } |
122 | 122 | } |
123 | 123 | |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | * @return string |
131 | 131 | */ |
132 | 132 | protected function read_raw_data() { |
133 | - return file_get_contents( 'php://input' ); |
|
133 | + return file_get_contents('php://input'); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
@@ -139,6 +139,6 @@ discard block |
||
139 | 139 | * @return string |
140 | 140 | */ |
141 | 141 | protected function secret() { |
142 | - return get_option( 'wogh_secret' ); |
|
142 | + return get_option('wogh_secret'); |
|
143 | 143 | } |
144 | 144 | } |
@@ -361,7 +361,7 @@ discard block |
||
361 | 361 | * Creates a literal block for dumping |
362 | 362 | * @access private |
363 | 363 | * @return string |
364 | - * @param $value |
|
364 | + * @param string $value |
|
365 | 365 | * @param $indent int The value of the indent |
366 | 366 | */ |
367 | 367 | private function _doLiteralBlock($value,$indent) { |
@@ -430,6 +430,9 @@ discard block |
||
430 | 430 | return in_array($value, $words, true); |
431 | 431 | } |
432 | 432 | |
433 | + /** |
|
434 | + * @param string $value |
|
435 | + */ |
|
433 | 436 | private function isTranslationWord($value) { |
434 | 437 | return ( |
435 | 438 | self::isTrueWord($value) || |
@@ -471,11 +474,17 @@ discard block |
||
471 | 474 | |
472 | 475 | // LOADING FUNCTIONS |
473 | 476 | |
477 | + /** |
|
478 | + * @param string $input |
|
479 | + */ |
|
474 | 480 | private function __load($input) { |
475 | 481 | $Source = $this->loadFromSource($input); |
476 | 482 | return $this->loadWithSource($Source); |
477 | 483 | } |
478 | 484 | |
485 | + /** |
|
486 | + * @param string $input |
|
487 | + */ |
|
479 | 488 | private function __loadString($input) { |
480 | 489 | $Source = $this->loadFromString($input); |
481 | 490 | return $this->loadWithSource($Source); |
@@ -692,6 +701,7 @@ discard block |
||
692 | 701 | /** |
693 | 702 | * Used in inlines to check for more inlines or quoted strings |
694 | 703 | * @access private |
704 | + * @param string $inline |
|
695 | 705 | * @return array |
696 | 706 | */ |
697 | 707 | private function _inlineEscape($inline) { |
@@ -822,12 +832,18 @@ discard block |
||
822 | 832 | return $explode; |
823 | 833 | } |
824 | 834 | |
835 | + /** |
|
836 | + * @param integer $lineIndent |
|
837 | + */ |
|
825 | 838 | private function literalBlockContinues ($line, $lineIndent) { |
826 | 839 | if (!trim($line)) return true; |
827 | 840 | if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true; |
828 | 841 | return false; |
829 | 842 | } |
830 | 843 | |
844 | + /** |
|
845 | + * @param boolean $alias |
|
846 | + */ |
|
831 | 847 | private function referenceContentsByAlias ($alias) { |
832 | 848 | do { |
833 | 849 | if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; } |
@@ -926,6 +942,9 @@ discard block |
||
926 | 942 | |
927 | 943 | } |
928 | 944 | |
945 | + /** |
|
946 | + * @param string $line |
|
947 | + */ |
|
929 | 948 | private static function startsLiteralBlock ($line) { |
930 | 949 | $lastChar = substr (trim($line), -1); |
931 | 950 | if ($lastChar != '>' && $lastChar != '|') return false; |
@@ -935,6 +954,9 @@ discard block |
||
935 | 954 | return $lastChar; |
936 | 955 | } |
937 | 956 | |
957 | + /** |
|
958 | + * @param string $line |
|
959 | + */ |
|
938 | 960 | private static function greedilyNeedNextLine($line) { |
939 | 961 | $line = trim ($line); |
940 | 962 | if (!strlen($line)) return false; |
@@ -944,6 +966,10 @@ discard block |
||
944 | 966 | return false; |
945 | 967 | } |
946 | 968 | |
969 | + /** |
|
970 | + * @param string $literalBlock |
|
971 | + * @param string $literalBlockStyle |
|
972 | + */ |
|
947 | 973 | private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) { |
948 | 974 | $line = self::stripIndent($line, $indent); |
949 | 975 | if ($literalBlockStyle !== '|') { |
@@ -963,6 +989,9 @@ discard block |
||
963 | 989 | return $literalBlock . $line; |
964 | 990 | } |
965 | 991 | |
992 | + /** |
|
993 | + * @param string $literalBlock |
|
994 | + */ |
|
966 | 995 | function revertLiteralPlaceHolder ($lineArray, $literalBlock) { |
967 | 996 | foreach ($lineArray as $k => $_) { |
968 | 997 | if (is_array($_)) |
@@ -978,6 +1007,9 @@ discard block |
||
978 | 1007 | return substr ($line, $indent); |
979 | 1008 | } |
980 | 1009 | |
1010 | + /** |
|
1011 | + * @param integer $indent |
|
1012 | + */ |
|
981 | 1013 | private function getParentPathByIndent ($indent) { |
982 | 1014 | if ($indent == 0) return array(); |
983 | 1015 | $linePath = $this->path; |
@@ -1003,6 +1035,9 @@ discard block |
||
1003 | 1035 | } |
1004 | 1036 | |
1005 | 1037 | |
1038 | + /** |
|
1039 | + * @param string $line |
|
1040 | + */ |
|
1006 | 1041 | private static function isComment ($line) { |
1007 | 1042 | if (!$line) return false; |
1008 | 1043 | if ($line[0] == '#') return true; |
@@ -1010,6 +1045,9 @@ discard block |
||
1010 | 1045 | return false; |
1011 | 1046 | } |
1012 | 1047 | |
1048 | + /** |
|
1049 | + * @param string $line |
|
1050 | + */ |
|
1013 | 1051 | private static function isEmpty ($line) { |
1014 | 1052 | return (trim ($line) === ''); |
1015 | 1053 | } |
@@ -1035,6 +1073,9 @@ discard block |
||
1035 | 1073 | } |
1036 | 1074 | |
1037 | 1075 | |
1076 | + /** |
|
1077 | + * @param string $value |
|
1078 | + */ |
|
1038 | 1079 | private static function unquote ($value) { |
1039 | 1080 | if (!$value) return $value; |
1040 | 1081 | if (!is_string($value)) return $value; |
@@ -1043,10 +1084,16 @@ discard block |
||
1043 | 1084 | return $value; |
1044 | 1085 | } |
1045 | 1086 | |
1087 | + /** |
|
1088 | + * @param string $line |
|
1089 | + */ |
|
1046 | 1090 | private function startsMappedSequence ($line) { |
1047 | 1091 | return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':'); |
1048 | 1092 | } |
1049 | 1093 | |
1094 | + /** |
|
1095 | + * @param string $line |
|
1096 | + */ |
|
1050 | 1097 | private function returnMappedSequence ($line) { |
1051 | 1098 | $array = array(); |
1052 | 1099 | $key = self::unquote(trim(substr($line,1,-1))); |
@@ -1063,6 +1110,9 @@ discard block |
||
1063 | 1110 | } |
1064 | 1111 | } |
1065 | 1112 | |
1113 | + /** |
|
1114 | + * @param string $line |
|
1115 | + */ |
|
1066 | 1116 | private function returnMappedValue ($line) { |
1067 | 1117 | $this->checkKeysInValue($line); |
1068 | 1118 | $array = array(); |
@@ -1071,18 +1121,30 @@ discard block |
||
1071 | 1121 | return $array; |
1072 | 1122 | } |
1073 | 1123 | |
1124 | + /** |
|
1125 | + * @param string $line |
|
1126 | + */ |
|
1074 | 1127 | private function startsMappedValue ($line) { |
1075 | 1128 | return (substr ($line, -1, 1) == ':'); |
1076 | 1129 | } |
1077 | 1130 | |
1131 | + /** |
|
1132 | + * @param string $line |
|
1133 | + */ |
|
1078 | 1134 | private function isPlainArray ($line) { |
1079 | 1135 | return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
1080 | 1136 | } |
1081 | 1137 | |
1138 | + /** |
|
1139 | + * @param string $line |
|
1140 | + */ |
|
1082 | 1141 | private function returnPlainArray ($line) { |
1083 | 1142 | return $this->_toType($line); |
1084 | 1143 | } |
1085 | 1144 | |
1145 | + /** |
|
1146 | + * @param string $line |
|
1147 | + */ |
|
1086 | 1148 | private function returnKeyValuePair ($line) { |
1087 | 1149 | $array = array(); |
1088 | 1150 | $key = ''; |
@@ -1111,6 +1173,9 @@ discard block |
||
1111 | 1173 | } |
1112 | 1174 | |
1113 | 1175 | |
1176 | + /** |
|
1177 | + * @param string $line |
|
1178 | + */ |
|
1114 | 1179 | private function returnArrayElement ($line) { |
1115 | 1180 | if (strlen($line) <= 1) return array(array()); // Weird %) |
1116 | 1181 | $array = array(); |
@@ -1124,6 +1189,9 @@ discard block |
||
1124 | 1189 | } |
1125 | 1190 | |
1126 | 1191 | |
1192 | + /** |
|
1193 | + * @param string $line |
|
1194 | + */ |
|
1127 | 1195 | private function nodeContainsGroup ($line) { |
1128 | 1196 | $symbolsForReference = 'A-z0-9_\-'; |
1129 | 1197 | if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
@@ -1136,12 +1204,20 @@ discard block |
||
1136 | 1204 | |
1137 | 1205 | } |
1138 | 1206 | |
1207 | + /** |
|
1208 | + * @param string $line |
|
1209 | + * @param string $group |
|
1210 | + */ |
|
1139 | 1211 | private function addGroup ($line, $group) { |
1140 | 1212 | if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1); |
1141 | 1213 | if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1); |
1142 | 1214 | //print_r ($this->path); |
1143 | 1215 | } |
1144 | 1216 | |
1217 | + /** |
|
1218 | + * @param string $line |
|
1219 | + * @param string $group |
|
1220 | + */ |
|
1145 | 1221 | private function stripGroup ($line, $group) { |
1146 | 1222 | $line = trim(str_replace($group, '', $line)); |
1147 | 1223 | return $line; |
@@ -1,14 +1,14 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Spyc -- A Simple PHP YAML Class |
|
4 | - * @version 0.5.1 |
|
5 | - * @author Vlad Andersen <[email protected]> |
|
6 | - * @author Chris Wanstrath <[email protected]> |
|
7 | - * @link https://github.com/mustangostang/spyc/ |
|
8 | - * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2011 Vlad Andersen |
|
9 | - * @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
10 | - * @package Spyc |
|
11 | - */ |
|
3 | + * Spyc -- A Simple PHP YAML Class |
|
4 | + * @version 0.5.1 |
|
5 | + * @author Vlad Andersen <[email protected]> |
|
6 | + * @author Chris Wanstrath <[email protected]> |
|
7 | + * @link https://github.com/mustangostang/spyc/ |
|
8 | + * @copyright Copyright 2005-2006 Chris Wanstrath, 2006-2011 Vlad Andersen |
|
9 | + * @license http://www.opensource.org/licenses/mit-license.php MIT License |
|
10 | + * @package Spyc |
|
11 | + */ |
|
12 | 12 | |
13 | 13 | if (!function_exists('spyc_load')) { |
14 | 14 | /** |
@@ -17,7 +17,7 @@ discard block |
||
17 | 17 | * @return array |
18 | 18 | */ |
19 | 19 | function spyc_load ($string) { |
20 | - return Spyc::YAMLLoadString($string); |
|
20 | + return Spyc::YAMLLoadString($string); |
|
21 | 21 | } |
22 | 22 | } |
23 | 23 | |
@@ -28,7 +28,7 @@ discard block |
||
28 | 28 | * @return array |
29 | 29 | */ |
30 | 30 | function spyc_load_file ($file) { |
31 | - return Spyc::YAMLLoad($file); |
|
31 | + return Spyc::YAMLLoad($file); |
|
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
@@ -39,34 +39,34 @@ discard block |
||
39 | 39 | * @return string |
40 | 40 | */ |
41 | 41 | function spyc_dump ($data) { |
42 | - return Spyc::YAMLDump($data, false, false, true); |
|
42 | + return Spyc::YAMLDump($data, false, false, true); |
|
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
46 | 46 | if (!class_exists('Spyc')) { |
47 | 47 | |
48 | 48 | /** |
49 | - * The Simple PHP YAML Class. |
|
50 | - * |
|
51 | - * This class can be used to read a YAML file and convert its contents |
|
52 | - * into a PHP array. It currently supports a very limited subsection of |
|
53 | - * the YAML spec. |
|
54 | - * |
|
55 | - * Usage: |
|
56 | - * <code> |
|
57 | - * $Spyc = new Spyc; |
|
58 | - * $array = $Spyc->load($file); |
|
59 | - * </code> |
|
60 | - * or: |
|
61 | - * <code> |
|
62 | - * $array = Spyc::YAMLLoad($file); |
|
63 | - * </code> |
|
64 | - * or: |
|
65 | - * <code> |
|
66 | - * $array = spyc_load_file($file); |
|
67 | - * </code> |
|
68 | - * @package Spyc |
|
69 | - */ |
|
49 | + * The Simple PHP YAML Class. |
|
50 | + * |
|
51 | + * This class can be used to read a YAML file and convert its contents |
|
52 | + * into a PHP array. It currently supports a very limited subsection of |
|
53 | + * the YAML spec. |
|
54 | + * |
|
55 | + * Usage: |
|
56 | + * <code> |
|
57 | + * $Spyc = new Spyc; |
|
58 | + * $array = $Spyc->load($file); |
|
59 | + * </code> |
|
60 | + * or: |
|
61 | + * <code> |
|
62 | + * $array = Spyc::YAMLLoad($file); |
|
63 | + * </code> |
|
64 | + * or: |
|
65 | + * <code> |
|
66 | + * $array = spyc_load_file($file); |
|
67 | + * </code> |
|
68 | + * @package Spyc |
|
69 | + */ |
|
70 | 70 | class Spyc { |
71 | 71 | |
72 | 72 | // SETTINGS |
@@ -121,1030 +121,1030 @@ discard block |
||
121 | 121 | * @return array |
122 | 122 | */ |
123 | 123 | public function load ($input) { |
124 | - return $this->__loadString($input); |
|
124 | + return $this->__loadString($input); |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | /** |
128 | - * Load a valid YAML file to Spyc. |
|
129 | - * @param string $file |
|
130 | - * @return array |
|
131 | - */ |
|
128 | + * Load a valid YAML file to Spyc. |
|
129 | + * @param string $file |
|
130 | + * @return array |
|
131 | + */ |
|
132 | 132 | public function loadFile ($file) { |
133 | - return $this->__load($file); |
|
133 | + return $this->__load($file); |
|
134 | 134 | } |
135 | 135 | |
136 | 136 | /** |
137 | - * Load YAML into a PHP array statically |
|
138 | - * |
|
139 | - * The load method, when supplied with a YAML stream (string or file), |
|
140 | - * will do its best to convert YAML in a file into a PHP array. Pretty |
|
141 | - * simple. |
|
142 | - * Usage: |
|
143 | - * <code> |
|
144 | - * $array = Spyc::YAMLLoad('lucky.yaml'); |
|
145 | - * print_r($array); |
|
146 | - * </code> |
|
147 | - * @access public |
|
148 | - * @return array |
|
149 | - * @param string $input Path of YAML file or string containing YAML |
|
150 | - */ |
|
137 | + * Load YAML into a PHP array statically |
|
138 | + * |
|
139 | + * The load method, when supplied with a YAML stream (string or file), |
|
140 | + * will do its best to convert YAML in a file into a PHP array. Pretty |
|
141 | + * simple. |
|
142 | + * Usage: |
|
143 | + * <code> |
|
144 | + * $array = Spyc::YAMLLoad('lucky.yaml'); |
|
145 | + * print_r($array); |
|
146 | + * </code> |
|
147 | + * @access public |
|
148 | + * @return array |
|
149 | + * @param string $input Path of YAML file or string containing YAML |
|
150 | + */ |
|
151 | 151 | public static function YAMLLoad($input) { |
152 | - $Spyc = new Spyc; |
|
153 | - return $Spyc->__load($input); |
|
152 | + $Spyc = new Spyc; |
|
153 | + return $Spyc->__load($input); |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | /** |
157 | - * Load a string of YAML into a PHP array statically |
|
158 | - * |
|
159 | - * The load method, when supplied with a YAML string, will do its best |
|
160 | - * to convert YAML in a string into a PHP array. Pretty simple. |
|
161 | - * |
|
162 | - * Note: use this function if you don't want files from the file system |
|
163 | - * loaded and processed as YAML. This is of interest to people concerned |
|
164 | - * about security whose input is from a string. |
|
165 | - * |
|
166 | - * Usage: |
|
167 | - * <code> |
|
168 | - * $array = Spyc::YAMLLoadString("---\n0: hello world\n"); |
|
169 | - * print_r($array); |
|
170 | - * </code> |
|
171 | - * @access public |
|
172 | - * @return array |
|
173 | - * @param string $input String containing YAML |
|
174 | - */ |
|
157 | + * Load a string of YAML into a PHP array statically |
|
158 | + * |
|
159 | + * The load method, when supplied with a YAML string, will do its best |
|
160 | + * to convert YAML in a string into a PHP array. Pretty simple. |
|
161 | + * |
|
162 | + * Note: use this function if you don't want files from the file system |
|
163 | + * loaded and processed as YAML. This is of interest to people concerned |
|
164 | + * about security whose input is from a string. |
|
165 | + * |
|
166 | + * Usage: |
|
167 | + * <code> |
|
168 | + * $array = Spyc::YAMLLoadString("---\n0: hello world\n"); |
|
169 | + * print_r($array); |
|
170 | + * </code> |
|
171 | + * @access public |
|
172 | + * @return array |
|
173 | + * @param string $input String containing YAML |
|
174 | + */ |
|
175 | 175 | public static function YAMLLoadString($input) { |
176 | - $Spyc = new Spyc; |
|
177 | - return $Spyc->__loadString($input); |
|
176 | + $Spyc = new Spyc; |
|
177 | + return $Spyc->__loadString($input); |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | /** |
181 | - * Dump YAML from PHP array statically |
|
182 | - * |
|
183 | - * The dump method, when supplied with an array, will do its best |
|
184 | - * to convert the array into friendly YAML. Pretty simple. Feel free to |
|
185 | - * save the returned string as nothing.yaml and pass it around. |
|
186 | - * |
|
187 | - * Oh, and you can decide how big the indent is and what the wordwrap |
|
188 | - * for folding is. Pretty cool -- just pass in 'false' for either if |
|
189 | - * you want to use the default. |
|
190 | - * |
|
191 | - * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
|
192 | - * you can turn off wordwrap by passing in 0. |
|
193 | - * |
|
194 | - * @access public |
|
195 | - * @return string |
|
196 | - * @param array $array PHP array |
|
197 | - * @param int $indent Pass in false to use the default, which is 2 |
|
198 | - * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
|
199 | - * @param int $no_opening_dashes Do not start YAML file with "---\n" |
|
200 | - */ |
|
181 | + * Dump YAML from PHP array statically |
|
182 | + * |
|
183 | + * The dump method, when supplied with an array, will do its best |
|
184 | + * to convert the array into friendly YAML. Pretty simple. Feel free to |
|
185 | + * save the returned string as nothing.yaml and pass it around. |
|
186 | + * |
|
187 | + * Oh, and you can decide how big the indent is and what the wordwrap |
|
188 | + * for folding is. Pretty cool -- just pass in 'false' for either if |
|
189 | + * you want to use the default. |
|
190 | + * |
|
191 | + * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
|
192 | + * you can turn off wordwrap by passing in 0. |
|
193 | + * |
|
194 | + * @access public |
|
195 | + * @return string |
|
196 | + * @param array $array PHP array |
|
197 | + * @param int $indent Pass in false to use the default, which is 2 |
|
198 | + * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
|
199 | + * @param int $no_opening_dashes Do not start YAML file with "---\n" |
|
200 | + */ |
|
201 | 201 | public static function YAMLDump($array, $indent = false, $wordwrap = false, $no_opening_dashes = false) { |
202 | - $spyc = new Spyc; |
|
203 | - return $spyc->dump($array, $indent, $wordwrap, $no_opening_dashes); |
|
202 | + $spyc = new Spyc; |
|
203 | + return $spyc->dump($array, $indent, $wordwrap, $no_opening_dashes); |
|
204 | 204 | } |
205 | 205 | |
206 | 206 | |
207 | 207 | /** |
208 | - * Dump PHP array to YAML |
|
209 | - * |
|
210 | - * The dump method, when supplied with an array, will do its best |
|
211 | - * to convert the array into friendly YAML. Pretty simple. Feel free to |
|
212 | - * save the returned string as tasteful.yaml and pass it around. |
|
213 | - * |
|
214 | - * Oh, and you can decide how big the indent is and what the wordwrap |
|
215 | - * for folding is. Pretty cool -- just pass in 'false' for either if |
|
216 | - * you want to use the default. |
|
217 | - * |
|
218 | - * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
|
219 | - * you can turn off wordwrap by passing in 0. |
|
220 | - * |
|
221 | - * @access public |
|
222 | - * @return string |
|
223 | - * @param array $array PHP array |
|
224 | - * @param int $indent Pass in false to use the default, which is 2 |
|
225 | - * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
|
226 | - */ |
|
208 | + * Dump PHP array to YAML |
|
209 | + * |
|
210 | + * The dump method, when supplied with an array, will do its best |
|
211 | + * to convert the array into friendly YAML. Pretty simple. Feel free to |
|
212 | + * save the returned string as tasteful.yaml and pass it around. |
|
213 | + * |
|
214 | + * Oh, and you can decide how big the indent is and what the wordwrap |
|
215 | + * for folding is. Pretty cool -- just pass in 'false' for either if |
|
216 | + * you want to use the default. |
|
217 | + * |
|
218 | + * Indent's default is 2 spaces, wordwrap's default is 40 characters. And |
|
219 | + * you can turn off wordwrap by passing in 0. |
|
220 | + * |
|
221 | + * @access public |
|
222 | + * @return string |
|
223 | + * @param array $array PHP array |
|
224 | + * @param int $indent Pass in false to use the default, which is 2 |
|
225 | + * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
|
226 | + */ |
|
227 | 227 | public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashes = false) { |
228 | - // Dumps to some very clean YAML. We'll have to add some more features |
|
229 | - // and options soon. And better support for folding. |
|
230 | - |
|
231 | - // New features and options. |
|
232 | - if ($indent === false or !is_numeric($indent)) { |
|
233 | - $this->_dumpIndent = 2; |
|
234 | - } else { |
|
235 | - $this->_dumpIndent = $indent; |
|
236 | - } |
|
237 | - |
|
238 | - if ($wordwrap === false or !is_numeric($wordwrap)) { |
|
239 | - $this->_dumpWordWrap = 40; |
|
240 | - } else { |
|
241 | - $this->_dumpWordWrap = $wordwrap; |
|
242 | - } |
|
243 | - |
|
244 | - // New YAML document |
|
245 | - $string = ""; |
|
246 | - if (!$no_opening_dashes) $string = "---\n"; |
|
247 | - |
|
248 | - // Start at the base of the array and move through it. |
|
249 | - if ($array) { |
|
250 | - $array = (array)$array; |
|
251 | - $previous_key = -1; |
|
252 | - foreach ($array as $key => $value) { |
|
253 | - if (!isset($first_key)) $first_key = $key; |
|
254 | - $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array); |
|
255 | - $previous_key = $key; |
|
256 | - } |
|
257 | - } |
|
258 | - return $string; |
|
228 | + // Dumps to some very clean YAML. We'll have to add some more features |
|
229 | + // and options soon. And better support for folding. |
|
230 | + |
|
231 | + // New features and options. |
|
232 | + if ($indent === false or !is_numeric($indent)) { |
|
233 | + $this->_dumpIndent = 2; |
|
234 | + } else { |
|
235 | + $this->_dumpIndent = $indent; |
|
236 | + } |
|
237 | + |
|
238 | + if ($wordwrap === false or !is_numeric($wordwrap)) { |
|
239 | + $this->_dumpWordWrap = 40; |
|
240 | + } else { |
|
241 | + $this->_dumpWordWrap = $wordwrap; |
|
242 | + } |
|
243 | + |
|
244 | + // New YAML document |
|
245 | + $string = ""; |
|
246 | + if (!$no_opening_dashes) $string = "---\n"; |
|
247 | + |
|
248 | + // Start at the base of the array and move through it. |
|
249 | + if ($array) { |
|
250 | + $array = (array)$array; |
|
251 | + $previous_key = -1; |
|
252 | + foreach ($array as $key => $value) { |
|
253 | + if (!isset($first_key)) $first_key = $key; |
|
254 | + $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array); |
|
255 | + $previous_key = $key; |
|
256 | + } |
|
257 | + } |
|
258 | + return $string; |
|
259 | 259 | } |
260 | 260 | |
261 | 261 | /** |
262 | - * Attempts to convert a key / value array item to YAML |
|
263 | - * @access private |
|
264 | - * @return string |
|
265 | - * @param $key The name of the key |
|
266 | - * @param $value The value of the item |
|
267 | - * @param $indent The indent of the current node |
|
268 | - */ |
|
262 | + * Attempts to convert a key / value array item to YAML |
|
263 | + * @access private |
|
264 | + * @return string |
|
265 | + * @param $key The name of the key |
|
266 | + * @param $value The value of the item |
|
267 | + * @param $indent The indent of the current node |
|
268 | + */ |
|
269 | 269 | private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
270 | - if(is_object($value)) $value = (array)$value; |
|
271 | - if (is_array($value)) { |
|
272 | - if (empty ($value)) |
|
273 | - return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
|
274 | - // It has children. What to do? |
|
275 | - // Make it the right kind of item |
|
276 | - $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array); |
|
277 | - // Add the indent |
|
278 | - $indent += $this->_dumpIndent; |
|
279 | - // Yamlize the array |
|
280 | - $string .= $this->_yamlizeArray($value,$indent); |
|
281 | - } elseif (!is_array($value)) { |
|
282 | - // It doesn't have children. Yip. |
|
283 | - $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array); |
|
284 | - } |
|
285 | - return $string; |
|
270 | + if(is_object($value)) $value = (array)$value; |
|
271 | + if (is_array($value)) { |
|
272 | + if (empty ($value)) |
|
273 | + return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
|
274 | + // It has children. What to do? |
|
275 | + // Make it the right kind of item |
|
276 | + $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array); |
|
277 | + // Add the indent |
|
278 | + $indent += $this->_dumpIndent; |
|
279 | + // Yamlize the array |
|
280 | + $string .= $this->_yamlizeArray($value,$indent); |
|
281 | + } elseif (!is_array($value)) { |
|
282 | + // It doesn't have children. Yip. |
|
283 | + $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array); |
|
284 | + } |
|
285 | + return $string; |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | /** |
289 | - * Attempts to convert an array to YAML |
|
290 | - * @access private |
|
291 | - * @return string |
|
292 | - * @param $array The array you want to convert |
|
293 | - * @param $indent The indent of the current level |
|
294 | - */ |
|
289 | + * Attempts to convert an array to YAML |
|
290 | + * @access private |
|
291 | + * @return string |
|
292 | + * @param $array The array you want to convert |
|
293 | + * @param $indent The indent of the current level |
|
294 | + */ |
|
295 | 295 | private function _yamlizeArray($array,$indent) { |
296 | - if (is_array($array)) { |
|
297 | - $string = ''; |
|
298 | - $previous_key = -1; |
|
299 | - foreach ($array as $key => $value) { |
|
300 | - if (!isset($first_key)) $first_key = $key; |
|
301 | - $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array); |
|
302 | - $previous_key = $key; |
|
303 | - } |
|
304 | - return $string; |
|
305 | - } else { |
|
306 | - return false; |
|
307 | - } |
|
296 | + if (is_array($array)) { |
|
297 | + $string = ''; |
|
298 | + $previous_key = -1; |
|
299 | + foreach ($array as $key => $value) { |
|
300 | + if (!isset($first_key)) $first_key = $key; |
|
301 | + $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array); |
|
302 | + $previous_key = $key; |
|
303 | + } |
|
304 | + return $string; |
|
305 | + } else { |
|
306 | + return false; |
|
307 | + } |
|
308 | 308 | } |
309 | 309 | |
310 | 310 | /** |
311 | - * Returns YAML from a key and a value |
|
312 | - * @access private |
|
313 | - * @return string |
|
314 | - * @param $key The name of the key |
|
315 | - * @param $value The value of the item |
|
316 | - * @param $indent The indent of the current node |
|
317 | - */ |
|
311 | + * Returns YAML from a key and a value |
|
312 | + * @access private |
|
313 | + * @return string |
|
314 | + * @param $key The name of the key |
|
315 | + * @param $value The value of the item |
|
316 | + * @param $indent The indent of the current node |
|
317 | + */ |
|
318 | 318 | private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
319 | - // do some folding here, for blocks |
|
320 | - if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false || |
|
321 | - strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, '%') !== false || strpos ($value, ' ') !== false || |
|
322 | - strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 || |
|
323 | - substr ($value, -1, 1) == ':') |
|
324 | - ) { |
|
325 | - $value = $this->_doLiteralBlock($value,$indent); |
|
326 | - } else { |
|
327 | - $value = $this->_doFolding($value,$indent); |
|
328 | - } |
|
329 | - |
|
330 | - if ($value === array()) $value = '[ ]'; |
|
331 | - if ($value === "") $value = '""'; |
|
332 | - if (self::isTranslationWord($value)) { |
|
333 | - $value = $this->_doLiteralBlock($value, $indent); |
|
334 | - } |
|
335 | - if (trim ($value) != $value) |
|
336 | - $value = $this->_doLiteralBlock($value,$indent); |
|
337 | - |
|
338 | - if (is_bool($value)) { |
|
339 | - $value = $value ? "true" : "false"; |
|
340 | - } |
|
341 | - |
|
342 | - if ($value === null) $value = 'null'; |
|
343 | - if ($value === "'" . self::REMPTY . "'") $value = null; |
|
344 | - |
|
345 | - $spaces = str_repeat(' ',$indent); |
|
346 | - |
|
347 | - //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { |
|
348 | - if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) { |
|
349 | - // It's a sequence |
|
350 | - $string = $spaces.'- '.$value."\n"; |
|
351 | - } else { |
|
352 | - // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"'); |
|
353 | - // It's mapped |
|
354 | - if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; } |
|
355 | - $string = rtrim ($spaces.$key.': '.$value)."\n"; |
|
356 | - } |
|
357 | - return $string; |
|
319 | + // do some folding here, for blocks |
|
320 | + if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false || |
|
321 | + strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, '%') !== false || strpos ($value, ' ') !== false || |
|
322 | + strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 || |
|
323 | + substr ($value, -1, 1) == ':') |
|
324 | + ) { |
|
325 | + $value = $this->_doLiteralBlock($value,$indent); |
|
326 | + } else { |
|
327 | + $value = $this->_doFolding($value,$indent); |
|
328 | + } |
|
329 | + |
|
330 | + if ($value === array()) $value = '[ ]'; |
|
331 | + if ($value === "") $value = '""'; |
|
332 | + if (self::isTranslationWord($value)) { |
|
333 | + $value = $this->_doLiteralBlock($value, $indent); |
|
334 | + } |
|
335 | + if (trim ($value) != $value) |
|
336 | + $value = $this->_doLiteralBlock($value,$indent); |
|
337 | + |
|
338 | + if (is_bool($value)) { |
|
339 | + $value = $value ? "true" : "false"; |
|
340 | + } |
|
341 | + |
|
342 | + if ($value === null) $value = 'null'; |
|
343 | + if ($value === "'" . self::REMPTY . "'") $value = null; |
|
344 | + |
|
345 | + $spaces = str_repeat(' ',$indent); |
|
346 | + |
|
347 | + //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { |
|
348 | + if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) { |
|
349 | + // It's a sequence |
|
350 | + $string = $spaces.'- '.$value."\n"; |
|
351 | + } else { |
|
352 | + // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"'); |
|
353 | + // It's mapped |
|
354 | + if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; } |
|
355 | + $string = rtrim ($spaces.$key.': '.$value)."\n"; |
|
356 | + } |
|
357 | + return $string; |
|
358 | 358 | } |
359 | 359 | |
360 | 360 | /** |
361 | - * Creates a literal block for dumping |
|
362 | - * @access private |
|
363 | - * @return string |
|
364 | - * @param $value |
|
365 | - * @param $indent int The value of the indent |
|
366 | - */ |
|
361 | + * Creates a literal block for dumping |
|
362 | + * @access private |
|
363 | + * @return string |
|
364 | + * @param $value |
|
365 | + * @param $indent int The value of the indent |
|
366 | + */ |
|
367 | 367 | private function _doLiteralBlock($value,$indent) { |
368 | - if ($value === "\n") return '\n'; |
|
369 | - if (strpos($value, "\n") === false && strpos($value, "'") === false) { |
|
370 | - return sprintf ("'%s'", $value); |
|
371 | - } |
|
372 | - if (strpos($value, "\n") === false && strpos($value, '"') === false) { |
|
373 | - return sprintf ('"%s"', $value); |
|
374 | - } |
|
375 | - $exploded = explode("\n",$value); |
|
376 | - $newValue = '|'; |
|
377 | - if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) { |
|
378 | - $newValue = $exploded[0]; |
|
379 | - unset($exploded[0]); |
|
380 | - } |
|
381 | - $indent += $this->_dumpIndent; |
|
382 | - $spaces = str_repeat(' ',$indent); |
|
383 | - foreach ($exploded as $line) { |
|
384 | - $line = trim($line); |
|
385 | - if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line)-1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line)-1)) { |
|
386 | - $line = substr($line, 1, -1); |
|
387 | - } |
|
388 | - $newValue .= "\n" . $spaces . ($line); |
|
389 | - } |
|
390 | - return $newValue; |
|
368 | + if ($value === "\n") return '\n'; |
|
369 | + if (strpos($value, "\n") === false && strpos($value, "'") === false) { |
|
370 | + return sprintf ("'%s'", $value); |
|
371 | + } |
|
372 | + if (strpos($value, "\n") === false && strpos($value, '"') === false) { |
|
373 | + return sprintf ('"%s"', $value); |
|
374 | + } |
|
375 | + $exploded = explode("\n",$value); |
|
376 | + $newValue = '|'; |
|
377 | + if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) { |
|
378 | + $newValue = $exploded[0]; |
|
379 | + unset($exploded[0]); |
|
380 | + } |
|
381 | + $indent += $this->_dumpIndent; |
|
382 | + $spaces = str_repeat(' ',$indent); |
|
383 | + foreach ($exploded as $line) { |
|
384 | + $line = trim($line); |
|
385 | + if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line)-1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line)-1)) { |
|
386 | + $line = substr($line, 1, -1); |
|
387 | + } |
|
388 | + $newValue .= "\n" . $spaces . ($line); |
|
389 | + } |
|
390 | + return $newValue; |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | /** |
394 | - * Folds a string of text, if necessary |
|
395 | - * @access private |
|
396 | - * @return string |
|
397 | - * @param $value The string you wish to fold |
|
398 | - */ |
|
394 | + * Folds a string of text, if necessary |
|
395 | + * @access private |
|
396 | + * @return string |
|
397 | + * @param $value The string you wish to fold |
|
398 | + */ |
|
399 | 399 | private function _doFolding($value,$indent) { |
400 | - // Don't do anything if wordwrap is set to 0 |
|
401 | - |
|
402 | - if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) { |
|
403 | - $indent += $this->_dumpIndent; |
|
404 | - $indent = str_repeat(' ',$indent); |
|
405 | - $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); |
|
406 | - $value = ">\n".$indent.$wrapped; |
|
407 | - } else { |
|
408 | - if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) |
|
409 | - $value = '"' . $value . '"'; |
|
410 | - if (is_numeric($value) && is_string($value)) |
|
411 | - $value = '"' . $value . '"'; |
|
412 | - } |
|
413 | - |
|
414 | - |
|
415 | - return $value; |
|
400 | + // Don't do anything if wordwrap is set to 0 |
|
401 | + |
|
402 | + if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) { |
|
403 | + $indent += $this->_dumpIndent; |
|
404 | + $indent = str_repeat(' ',$indent); |
|
405 | + $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); |
|
406 | + $value = ">\n".$indent.$wrapped; |
|
407 | + } else { |
|
408 | + if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) |
|
409 | + $value = '"' . $value . '"'; |
|
410 | + if (is_numeric($value) && is_string($value)) |
|
411 | + $value = '"' . $value . '"'; |
|
412 | + } |
|
413 | + |
|
414 | + |
|
415 | + return $value; |
|
416 | 416 | } |
417 | 417 | |
418 | 418 | private function isTrueWord($value) { |
419 | - $words = self::getTranslations(array('true', 'on', 'yes', 'y')); |
|
420 | - return in_array($value, $words, true); |
|
419 | + $words = self::getTranslations(array('true', 'on', 'yes', 'y')); |
|
420 | + return in_array($value, $words, true); |
|
421 | 421 | } |
422 | 422 | |
423 | 423 | private function isFalseWord($value) { |
424 | - $words = self::getTranslations(array('false', 'off', 'no', 'n')); |
|
425 | - return in_array($value, $words, true); |
|
424 | + $words = self::getTranslations(array('false', 'off', 'no', 'n')); |
|
425 | + return in_array($value, $words, true); |
|
426 | 426 | } |
427 | 427 | |
428 | 428 | private function isNullWord($value) { |
429 | - $words = self::getTranslations(array('null', '~')); |
|
430 | - return in_array($value, $words, true); |
|
429 | + $words = self::getTranslations(array('null', '~')); |
|
430 | + return in_array($value, $words, true); |
|
431 | 431 | } |
432 | 432 | |
433 | 433 | private function isTranslationWord($value) { |
434 | - return ( |
|
435 | - self::isTrueWord($value) || |
|
436 | - self::isFalseWord($value) || |
|
437 | - self::isNullWord($value) |
|
438 | - ); |
|
434 | + return ( |
|
435 | + self::isTrueWord($value) || |
|
436 | + self::isFalseWord($value) || |
|
437 | + self::isNullWord($value) |
|
438 | + ); |
|
439 | 439 | } |
440 | 440 | |
441 | 441 | /** |
442 | - * Coerce a string into a native type |
|
443 | - * Reference: http://yaml.org/type/bool.html |
|
444 | - * TODO: Use only words from the YAML spec. |
|
445 | - * @access private |
|
446 | - * @param $value The value to coerce |
|
447 | - */ |
|
442 | + * Coerce a string into a native type |
|
443 | + * Reference: http://yaml.org/type/bool.html |
|
444 | + * TODO: Use only words from the YAML spec. |
|
445 | + * @access private |
|
446 | + * @param $value The value to coerce |
|
447 | + */ |
|
448 | 448 | private function coerceValue(&$value) { |
449 | - if (self::isTrueWord($value)) { |
|
450 | - $value = true; |
|
451 | - } else if (self::isFalseWord($value)) { |
|
452 | - $value = false; |
|
453 | - } else if (self::isNullWord($value)) { |
|
454 | - $value = null; |
|
455 | - } |
|
449 | + if (self::isTrueWord($value)) { |
|
450 | + $value = true; |
|
451 | + } else if (self::isFalseWord($value)) { |
|
452 | + $value = false; |
|
453 | + } else if (self::isNullWord($value)) { |
|
454 | + $value = null; |
|
455 | + } |
|
456 | 456 | } |
457 | 457 | |
458 | 458 | /** |
459 | - * Given a set of words, perform the appropriate translations on them to |
|
460 | - * match the YAML 1.1 specification for type coercing. |
|
461 | - * @param $words The words to translate |
|
462 | - * @access private |
|
463 | - */ |
|
459 | + * Given a set of words, perform the appropriate translations on them to |
|
460 | + * match the YAML 1.1 specification for type coercing. |
|
461 | + * @param $words The words to translate |
|
462 | + * @access private |
|
463 | + */ |
|
464 | 464 | private static function getTranslations(array $words) { |
465 | - $result = array(); |
|
466 | - foreach ($words as $i) { |
|
467 | - $result = array_merge($result, array(ucfirst($i), strtoupper($i), strtolower($i))); |
|
468 | - } |
|
469 | - return $result; |
|
465 | + $result = array(); |
|
466 | + foreach ($words as $i) { |
|
467 | + $result = array_merge($result, array(ucfirst($i), strtoupper($i), strtolower($i))); |
|
468 | + } |
|
469 | + return $result; |
|
470 | 470 | } |
471 | 471 | |
472 | 472 | // LOADING FUNCTIONS |
473 | 473 | |
474 | 474 | private function __load($input) { |
475 | - $Source = $this->loadFromSource($input); |
|
476 | - return $this->loadWithSource($Source); |
|
475 | + $Source = $this->loadFromSource($input); |
|
476 | + return $this->loadWithSource($Source); |
|
477 | 477 | } |
478 | 478 | |
479 | 479 | private function __loadString($input) { |
480 | - $Source = $this->loadFromString($input); |
|
481 | - return $this->loadWithSource($Source); |
|
480 | + $Source = $this->loadFromString($input); |
|
481 | + return $this->loadWithSource($Source); |
|
482 | 482 | } |
483 | 483 | |
484 | 484 | private function loadWithSource($Source) { |
485 | - if (empty ($Source)) return array(); |
|
486 | - if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { |
|
487 | - $array = syck_load (implode ("\n", $Source)); |
|
488 | - return is_array($array) ? $array : array(); |
|
489 | - } |
|
490 | - |
|
491 | - $this->path = array(); |
|
492 | - $this->result = array(); |
|
493 | - |
|
494 | - $cnt = count($Source); |
|
495 | - for ($i = 0; $i < $cnt; $i++) { |
|
496 | - $line = $Source[$i]; |
|
497 | - |
|
498 | - $this->indent = strlen($line) - strlen(ltrim($line)); |
|
499 | - $tempPath = $this->getParentPathByIndent($this->indent); |
|
500 | - $line = self::stripIndent($line, $this->indent); |
|
501 | - if (self::isComment($line)) continue; |
|
502 | - if (self::isEmpty($line)) continue; |
|
503 | - $this->path = $tempPath; |
|
504 | - |
|
505 | - $literalBlockStyle = self::startsLiteralBlock($line); |
|
506 | - if ($literalBlockStyle) { |
|
507 | - $line = rtrim ($line, $literalBlockStyle . " \n"); |
|
508 | - $literalBlock = ''; |
|
509 | - $line .= ' '.$this->LiteralPlaceHolder; |
|
510 | - $literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1])); |
|
511 | - while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) { |
|
512 | - $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent); |
|
513 | - } |
|
514 | - $i--; |
|
515 | - } |
|
516 | - |
|
517 | - // Strip out comments |
|
518 | - if (strpos ($line, '#')) { |
|
519 | - $line = preg_replace('/\s*#([^"\']+)$/','',$line); |
|
520 | - } |
|
521 | - |
|
522 | - while (++$i < $cnt && self::greedilyNeedNextLine($line)) { |
|
523 | - $line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t"); |
|
524 | - } |
|
525 | - $i--; |
|
526 | - |
|
527 | - $lineArray = $this->_parseLine($line); |
|
528 | - |
|
529 | - if ($literalBlockStyle) |
|
530 | - $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
|
531 | - |
|
532 | - $this->addArray($lineArray, $this->indent); |
|
533 | - |
|
534 | - foreach ($this->delayedPath as $indent => $delayedPath) |
|
535 | - $this->path[$indent] = $delayedPath; |
|
536 | - |
|
537 | - $this->delayedPath = array(); |
|
538 | - |
|
539 | - } |
|
540 | - return $this->result; |
|
485 | + if (empty ($Source)) return array(); |
|
486 | + if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { |
|
487 | + $array = syck_load (implode ("\n", $Source)); |
|
488 | + return is_array($array) ? $array : array(); |
|
489 | + } |
|
490 | + |
|
491 | + $this->path = array(); |
|
492 | + $this->result = array(); |
|
493 | + |
|
494 | + $cnt = count($Source); |
|
495 | + for ($i = 0; $i < $cnt; $i++) { |
|
496 | + $line = $Source[$i]; |
|
497 | + |
|
498 | + $this->indent = strlen($line) - strlen(ltrim($line)); |
|
499 | + $tempPath = $this->getParentPathByIndent($this->indent); |
|
500 | + $line = self::stripIndent($line, $this->indent); |
|
501 | + if (self::isComment($line)) continue; |
|
502 | + if (self::isEmpty($line)) continue; |
|
503 | + $this->path = $tempPath; |
|
504 | + |
|
505 | + $literalBlockStyle = self::startsLiteralBlock($line); |
|
506 | + if ($literalBlockStyle) { |
|
507 | + $line = rtrim ($line, $literalBlockStyle . " \n"); |
|
508 | + $literalBlock = ''; |
|
509 | + $line .= ' '.$this->LiteralPlaceHolder; |
|
510 | + $literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1])); |
|
511 | + while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) { |
|
512 | + $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent); |
|
513 | + } |
|
514 | + $i--; |
|
515 | + } |
|
516 | + |
|
517 | + // Strip out comments |
|
518 | + if (strpos ($line, '#')) { |
|
519 | + $line = preg_replace('/\s*#([^"\']+)$/','',$line); |
|
520 | + } |
|
521 | + |
|
522 | + while (++$i < $cnt && self::greedilyNeedNextLine($line)) { |
|
523 | + $line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t"); |
|
524 | + } |
|
525 | + $i--; |
|
526 | + |
|
527 | + $lineArray = $this->_parseLine($line); |
|
528 | + |
|
529 | + if ($literalBlockStyle) |
|
530 | + $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
|
531 | + |
|
532 | + $this->addArray($lineArray, $this->indent); |
|
533 | + |
|
534 | + foreach ($this->delayedPath as $indent => $delayedPath) |
|
535 | + $this->path[$indent] = $delayedPath; |
|
536 | + |
|
537 | + $this->delayedPath = array(); |
|
538 | + |
|
539 | + } |
|
540 | + return $this->result; |
|
541 | 541 | } |
542 | 542 | |
543 | 543 | private function loadFromSource ($input) { |
544 | - if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) |
|
545 | - $input = file_get_contents($input); |
|
544 | + if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) |
|
545 | + $input = file_get_contents($input); |
|
546 | 546 | |
547 | - return $this->loadFromString($input); |
|
547 | + return $this->loadFromString($input); |
|
548 | 548 | } |
549 | 549 | |
550 | 550 | private function loadFromString ($input) { |
551 | - $lines = explode("\n",$input); |
|
552 | - foreach ($lines as $k => $_) { |
|
553 | - $lines[$k] = rtrim ($_, "\r"); |
|
554 | - } |
|
555 | - return $lines; |
|
551 | + $lines = explode("\n",$input); |
|
552 | + foreach ($lines as $k => $_) { |
|
553 | + $lines[$k] = rtrim ($_, "\r"); |
|
554 | + } |
|
555 | + return $lines; |
|
556 | 556 | } |
557 | 557 | |
558 | 558 | /** |
559 | - * Parses YAML code and returns an array for a node |
|
560 | - * @access private |
|
561 | - * @return array |
|
562 | - * @param string $line A line from the YAML file |
|
563 | - */ |
|
559 | + * Parses YAML code and returns an array for a node |
|
560 | + * @access private |
|
561 | + * @return array |
|
562 | + * @param string $line A line from the YAML file |
|
563 | + */ |
|
564 | 564 | private function _parseLine($line) { |
565 | - if (!$line) return array(); |
|
566 | - $line = trim($line); |
|
567 | - if (!$line) return array(); |
|
565 | + if (!$line) return array(); |
|
566 | + $line = trim($line); |
|
567 | + if (!$line) return array(); |
|
568 | 568 | |
569 | - $array = array(); |
|
569 | + $array = array(); |
|
570 | 570 | |
571 | - $group = $this->nodeContainsGroup($line); |
|
572 | - if ($group) { |
|
573 | - $this->addGroup($line, $group); |
|
574 | - $line = $this->stripGroup ($line, $group); |
|
575 | - } |
|
571 | + $group = $this->nodeContainsGroup($line); |
|
572 | + if ($group) { |
|
573 | + $this->addGroup($line, $group); |
|
574 | + $line = $this->stripGroup ($line, $group); |
|
575 | + } |
|
576 | 576 | |
577 | - if ($this->startsMappedSequence($line)) |
|
578 | - return $this->returnMappedSequence($line); |
|
577 | + if ($this->startsMappedSequence($line)) |
|
578 | + return $this->returnMappedSequence($line); |
|
579 | 579 | |
580 | - if ($this->startsMappedValue($line)) |
|
581 | - return $this->returnMappedValue($line); |
|
580 | + if ($this->startsMappedValue($line)) |
|
581 | + return $this->returnMappedValue($line); |
|
582 | 582 | |
583 | - if ($this->isArrayElement($line)) |
|
584 | - return $this->returnArrayElement($line); |
|
583 | + if ($this->isArrayElement($line)) |
|
584 | + return $this->returnArrayElement($line); |
|
585 | 585 | |
586 | - if ($this->isPlainArray($line)) |
|
587 | - return $this->returnPlainArray($line); |
|
586 | + if ($this->isPlainArray($line)) |
|
587 | + return $this->returnPlainArray($line); |
|
588 | 588 | |
589 | 589 | |
590 | - return $this->returnKeyValuePair($line); |
|
590 | + return $this->returnKeyValuePair($line); |
|
591 | 591 | |
592 | 592 | } |
593 | 593 | |
594 | 594 | /** |
595 | - * Finds the type of the passed value, returns the value as the new type. |
|
596 | - * @access private |
|
597 | - * @param string $value |
|
598 | - * @return mixed |
|
599 | - */ |
|
595 | + * Finds the type of the passed value, returns the value as the new type. |
|
596 | + * @access private |
|
597 | + * @param string $value |
|
598 | + * @return mixed |
|
599 | + */ |
|
600 | 600 | private function _toType($value) { |
601 | - if ($value === '') return ""; |
|
602 | - $first_character = $value[0]; |
|
603 | - $last_character = substr($value, -1, 1); |
|
604 | - |
|
605 | - $is_quoted = false; |
|
606 | - do { |
|
607 | - if (!$value) break; |
|
608 | - if ($first_character != '"' && $first_character != "'") break; |
|
609 | - if ($last_character != '"' && $last_character != "'") break; |
|
610 | - $is_quoted = true; |
|
611 | - } while (0); |
|
612 | - |
|
613 | - if ($is_quoted) { |
|
614 | - $value = str_replace('\n', "\n", $value); |
|
615 | - if ($first_character == "'") |
|
616 | - return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\'')); |
|
617 | - return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\\\''=> '\'')); |
|
618 | - } |
|
619 | - |
|
620 | - if (strpos($value, ' #') !== false && !$is_quoted) |
|
621 | - $value = preg_replace('/\s+#(.+)$/','',$value); |
|
622 | - |
|
623 | - if ($first_character == '[' && $last_character == ']') { |
|
624 | - // Take out strings sequences and mappings |
|
625 | - $innerValue = trim(substr ($value, 1, -1)); |
|
626 | - if ($innerValue === '') return array(); |
|
627 | - $explode = $this->_inlineEscape($innerValue); |
|
628 | - // Propagate value array |
|
629 | - $value = array(); |
|
630 | - foreach ($explode as $v) { |
|
631 | - $value[] = $this->_toType($v); |
|
632 | - } |
|
633 | - return $value; |
|
634 | - } |
|
635 | - |
|
636 | - if (strpos($value,': ')!==false && $first_character != '{') { |
|
637 | - $array = explode(': ',$value); |
|
638 | - $key = trim($array[0]); |
|
639 | - array_shift($array); |
|
640 | - $value = trim(implode(': ',$array)); |
|
641 | - $value = $this->_toType($value); |
|
642 | - return array($key => $value); |
|
643 | - } |
|
644 | - |
|
645 | - if ($first_character == '{' && $last_character == '}') { |
|
646 | - $innerValue = trim(substr ($value, 1, -1)); |
|
647 | - if ($innerValue === '') return array(); |
|
648 | - // Inline Mapping |
|
649 | - // Take out strings sequences and mappings |
|
650 | - $explode = $this->_inlineEscape($innerValue); |
|
651 | - // Propagate value array |
|
652 | - $array = array(); |
|
653 | - foreach ($explode as $v) { |
|
654 | - $SubArr = $this->_toType($v); |
|
655 | - if (empty($SubArr)) continue; |
|
656 | - if (is_array ($SubArr)) { |
|
657 | - $array[key($SubArr)] = $SubArr[key($SubArr)]; continue; |
|
658 | - } |
|
659 | - $array[] = $SubArr; |
|
660 | - } |
|
661 | - return $array; |
|
662 | - } |
|
663 | - |
|
664 | - if ($value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~') { |
|
665 | - return null; |
|
666 | - } |
|
667 | - |
|
668 | - if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){ |
|
669 | - $intvalue = (int)$value; |
|
670 | - if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX) |
|
671 | - $value = $intvalue; |
|
672 | - return $value; |
|
673 | - } |
|
674 | - |
|
675 | - if (is_numeric($value) && preg_match('/^0[xX][0-9a-fA-F]+$/', $value)) { |
|
676 | - // Hexadecimal value. |
|
677 | - return hexdec($value); |
|
678 | - } |
|
679 | - |
|
680 | - $this->coerceValue($value); |
|
681 | - |
|
682 | - if (is_numeric($value)) { |
|
683 | - if ($value === '0') return 0; |
|
684 | - if (rtrim ($value, 0) === $value) |
|
685 | - $value = (float)$value; |
|
686 | - return $value; |
|
687 | - } |
|
688 | - |
|
689 | - return $value; |
|
601 | + if ($value === '') return ""; |
|
602 | + $first_character = $value[0]; |
|
603 | + $last_character = substr($value, -1, 1); |
|
604 | + |
|
605 | + $is_quoted = false; |
|
606 | + do { |
|
607 | + if (!$value) break; |
|
608 | + if ($first_character != '"' && $first_character != "'") break; |
|
609 | + if ($last_character != '"' && $last_character != "'") break; |
|
610 | + $is_quoted = true; |
|
611 | + } while (0); |
|
612 | + |
|
613 | + if ($is_quoted) { |
|
614 | + $value = str_replace('\n', "\n", $value); |
|
615 | + if ($first_character == "'") |
|
616 | + return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\'')); |
|
617 | + return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\\\''=> '\'')); |
|
618 | + } |
|
619 | + |
|
620 | + if (strpos($value, ' #') !== false && !$is_quoted) |
|
621 | + $value = preg_replace('/\s+#(.+)$/','',$value); |
|
622 | + |
|
623 | + if ($first_character == '[' && $last_character == ']') { |
|
624 | + // Take out strings sequences and mappings |
|
625 | + $innerValue = trim(substr ($value, 1, -1)); |
|
626 | + if ($innerValue === '') return array(); |
|
627 | + $explode = $this->_inlineEscape($innerValue); |
|
628 | + // Propagate value array |
|
629 | + $value = array(); |
|
630 | + foreach ($explode as $v) { |
|
631 | + $value[] = $this->_toType($v); |
|
632 | + } |
|
633 | + return $value; |
|
634 | + } |
|
635 | + |
|
636 | + if (strpos($value,': ')!==false && $first_character != '{') { |
|
637 | + $array = explode(': ',$value); |
|
638 | + $key = trim($array[0]); |
|
639 | + array_shift($array); |
|
640 | + $value = trim(implode(': ',$array)); |
|
641 | + $value = $this->_toType($value); |
|
642 | + return array($key => $value); |
|
643 | + } |
|
644 | + |
|
645 | + if ($first_character == '{' && $last_character == '}') { |
|
646 | + $innerValue = trim(substr ($value, 1, -1)); |
|
647 | + if ($innerValue === '') return array(); |
|
648 | + // Inline Mapping |
|
649 | + // Take out strings sequences and mappings |
|
650 | + $explode = $this->_inlineEscape($innerValue); |
|
651 | + // Propagate value array |
|
652 | + $array = array(); |
|
653 | + foreach ($explode as $v) { |
|
654 | + $SubArr = $this->_toType($v); |
|
655 | + if (empty($SubArr)) continue; |
|
656 | + if (is_array ($SubArr)) { |
|
657 | + $array[key($SubArr)] = $SubArr[key($SubArr)]; continue; |
|
658 | + } |
|
659 | + $array[] = $SubArr; |
|
660 | + } |
|
661 | + return $array; |
|
662 | + } |
|
663 | + |
|
664 | + if ($value == 'null' || $value == 'NULL' || $value == 'Null' || $value == '' || $value == '~') { |
|
665 | + return null; |
|
666 | + } |
|
667 | + |
|
668 | + if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){ |
|
669 | + $intvalue = (int)$value; |
|
670 | + if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX) |
|
671 | + $value = $intvalue; |
|
672 | + return $value; |
|
673 | + } |
|
674 | + |
|
675 | + if (is_numeric($value) && preg_match('/^0[xX][0-9a-fA-F]+$/', $value)) { |
|
676 | + // Hexadecimal value. |
|
677 | + return hexdec($value); |
|
678 | + } |
|
679 | + |
|
680 | + $this->coerceValue($value); |
|
681 | + |
|
682 | + if (is_numeric($value)) { |
|
683 | + if ($value === '0') return 0; |
|
684 | + if (rtrim ($value, 0) === $value) |
|
685 | + $value = (float)$value; |
|
686 | + return $value; |
|
687 | + } |
|
688 | + |
|
689 | + return $value; |
|
690 | 690 | } |
691 | 691 | |
692 | 692 | /** |
693 | - * Used in inlines to check for more inlines or quoted strings |
|
694 | - * @access private |
|
695 | - * @return array |
|
696 | - */ |
|
693 | + * Used in inlines to check for more inlines or quoted strings |
|
694 | + * @access private |
|
695 | + * @return array |
|
696 | + */ |
|
697 | 697 | private function _inlineEscape($inline) { |
698 | - // There's gotta be a cleaner way to do this... |
|
699 | - // While pure sequences seem to be nesting just fine, |
|
700 | - // pure mappings and mappings with sequences inside can't go very |
|
701 | - // deep. This needs to be fixed. |
|
702 | - |
|
703 | - $seqs = array(); |
|
704 | - $maps = array(); |
|
705 | - $saved_strings = array(); |
|
706 | - $saved_empties = array(); |
|
707 | - |
|
708 | - // Check for empty strings |
|
709 | - $regex = '/("")|(\'\')/'; |
|
710 | - if (preg_match_all($regex,$inline,$strings)) { |
|
711 | - $saved_empties = $strings[0]; |
|
712 | - $inline = preg_replace($regex,'YAMLEmpty',$inline); |
|
713 | - } |
|
714 | - unset($regex); |
|
715 | - |
|
716 | - // Check for strings |
|
717 | - $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/'; |
|
718 | - if (preg_match_all($regex,$inline,$strings)) { |
|
719 | - $saved_strings = $strings[0]; |
|
720 | - $inline = preg_replace($regex,'YAMLString',$inline); |
|
721 | - } |
|
722 | - unset($regex); |
|
723 | - |
|
724 | - // echo $inline; |
|
725 | - |
|
726 | - $i = 0; |
|
727 | - do { |
|
728 | - |
|
729 | - // Check for sequences |
|
730 | - while (preg_match('/\[([^{}\[\]]+)\]/U',$inline,$matchseqs)) { |
|
731 | - $seqs[] = $matchseqs[0]; |
|
732 | - $inline = preg_replace('/\[([^{}\[\]]+)\]/U', ('YAMLSeq' . (count($seqs) - 1) . 's'), $inline, 1); |
|
733 | - } |
|
734 | - |
|
735 | - // Check for mappings |
|
736 | - while (preg_match('/{([^\[\]{}]+)}/U',$inline,$matchmaps)) { |
|
737 | - $maps[] = $matchmaps[0]; |
|
738 | - $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1); |
|
739 | - } |
|
740 | - |
|
741 | - if ($i++ >= 10) break; |
|
742 | - |
|
743 | - } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
|
744 | - |
|
745 | - $explode = explode(',',$inline); |
|
746 | - $explode = array_map('trim', $explode); |
|
747 | - $stringi = 0; $i = 0; |
|
748 | - |
|
749 | - while (1) { |
|
750 | - |
|
751 | - // Re-add the sequences |
|
752 | - if (!empty($seqs)) { |
|
753 | - foreach ($explode as $key => $value) { |
|
754 | - if (strpos($value,'YAMLSeq') !== false) { |
|
755 | - foreach ($seqs as $seqk => $seq) { |
|
756 | - $explode[$key] = str_replace(('YAMLSeq'.$seqk.'s'),$seq,$value); |
|
757 | - $value = $explode[$key]; |
|
758 | - } |
|
759 | - } |
|
760 | - } |
|
761 | - } |
|
762 | - |
|
763 | - // Re-add the mappings |
|
764 | - if (!empty($maps)) { |
|
765 | - foreach ($explode as $key => $value) { |
|
766 | - if (strpos($value,'YAMLMap') !== false) { |
|
767 | - foreach ($maps as $mapk => $map) { |
|
768 | - $explode[$key] = str_replace(('YAMLMap'.$mapk.'s'), $map, $value); |
|
769 | - $value = $explode[$key]; |
|
770 | - } |
|
771 | - } |
|
772 | - } |
|
773 | - } |
|
774 | - |
|
775 | - |
|
776 | - // Re-add the strings |
|
777 | - if (!empty($saved_strings)) { |
|
778 | - foreach ($explode as $key => $value) { |
|
779 | - while (strpos($value,'YAMLString') !== false) { |
|
780 | - $explode[$key] = preg_replace('/YAMLString/',$saved_strings[$stringi],$value, 1); |
|
781 | - unset($saved_strings[$stringi]); |
|
782 | - ++$stringi; |
|
783 | - $value = $explode[$key]; |
|
784 | - } |
|
785 | - } |
|
786 | - } |
|
787 | - |
|
788 | - |
|
789 | - // Re-add the empties |
|
790 | - if (!empty($saved_empties)) { |
|
791 | - foreach ($explode as $key => $value) { |
|
792 | - while (strpos($value,'YAMLEmpty') !== false) { |
|
793 | - $explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1); |
|
794 | - $value = $explode[$key]; |
|
795 | - } |
|
796 | - } |
|
797 | - } |
|
798 | - |
|
799 | - $finished = true; |
|
800 | - foreach ($explode as $key => $value) { |
|
801 | - if (strpos($value,'YAMLSeq') !== false) { |
|
802 | - $finished = false; break; |
|
803 | - } |
|
804 | - if (strpos($value,'YAMLMap') !== false) { |
|
805 | - $finished = false; break; |
|
806 | - } |
|
807 | - if (strpos($value,'YAMLString') !== false) { |
|
808 | - $finished = false; break; |
|
809 | - } |
|
810 | - if (strpos($value,'YAMLEmpty') !== false) { |
|
811 | - $finished = false; break; |
|
812 | - } |
|
813 | - } |
|
814 | - if ($finished) break; |
|
815 | - |
|
816 | - $i++; |
|
817 | - if ($i > 10) |
|
818 | - break; // Prevent infinite loops. |
|
819 | - } |
|
820 | - |
|
821 | - |
|
822 | - return $explode; |
|
698 | + // There's gotta be a cleaner way to do this... |
|
699 | + // While pure sequences seem to be nesting just fine, |
|
700 | + // pure mappings and mappings with sequences inside can't go very |
|
701 | + // deep. This needs to be fixed. |
|
702 | + |
|
703 | + $seqs = array(); |
|
704 | + $maps = array(); |
|
705 | + $saved_strings = array(); |
|
706 | + $saved_empties = array(); |
|
707 | + |
|
708 | + // Check for empty strings |
|
709 | + $regex = '/("")|(\'\')/'; |
|
710 | + if (preg_match_all($regex,$inline,$strings)) { |
|
711 | + $saved_empties = $strings[0]; |
|
712 | + $inline = preg_replace($regex,'YAMLEmpty',$inline); |
|
713 | + } |
|
714 | + unset($regex); |
|
715 | + |
|
716 | + // Check for strings |
|
717 | + $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/'; |
|
718 | + if (preg_match_all($regex,$inline,$strings)) { |
|
719 | + $saved_strings = $strings[0]; |
|
720 | + $inline = preg_replace($regex,'YAMLString',$inline); |
|
721 | + } |
|
722 | + unset($regex); |
|
723 | + |
|
724 | + // echo $inline; |
|
725 | + |
|
726 | + $i = 0; |
|
727 | + do { |
|
728 | + |
|
729 | + // Check for sequences |
|
730 | + while (preg_match('/\[([^{}\[\]]+)\]/U',$inline,$matchseqs)) { |
|
731 | + $seqs[] = $matchseqs[0]; |
|
732 | + $inline = preg_replace('/\[([^{}\[\]]+)\]/U', ('YAMLSeq' . (count($seqs) - 1) . 's'), $inline, 1); |
|
733 | + } |
|
734 | + |
|
735 | + // Check for mappings |
|
736 | + while (preg_match('/{([^\[\]{}]+)}/U',$inline,$matchmaps)) { |
|
737 | + $maps[] = $matchmaps[0]; |
|
738 | + $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1); |
|
739 | + } |
|
740 | + |
|
741 | + if ($i++ >= 10) break; |
|
742 | + |
|
743 | + } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
|
744 | + |
|
745 | + $explode = explode(',',$inline); |
|
746 | + $explode = array_map('trim', $explode); |
|
747 | + $stringi = 0; $i = 0; |
|
748 | + |
|
749 | + while (1) { |
|
750 | + |
|
751 | + // Re-add the sequences |
|
752 | + if (!empty($seqs)) { |
|
753 | + foreach ($explode as $key => $value) { |
|
754 | + if (strpos($value,'YAMLSeq') !== false) { |
|
755 | + foreach ($seqs as $seqk => $seq) { |
|
756 | + $explode[$key] = str_replace(('YAMLSeq'.$seqk.'s'),$seq,$value); |
|
757 | + $value = $explode[$key]; |
|
758 | + } |
|
759 | + } |
|
760 | + } |
|
761 | + } |
|
762 | + |
|
763 | + // Re-add the mappings |
|
764 | + if (!empty($maps)) { |
|
765 | + foreach ($explode as $key => $value) { |
|
766 | + if (strpos($value,'YAMLMap') !== false) { |
|
767 | + foreach ($maps as $mapk => $map) { |
|
768 | + $explode[$key] = str_replace(('YAMLMap'.$mapk.'s'), $map, $value); |
|
769 | + $value = $explode[$key]; |
|
770 | + } |
|
771 | + } |
|
772 | + } |
|
773 | + } |
|
774 | + |
|
775 | + |
|
776 | + // Re-add the strings |
|
777 | + if (!empty($saved_strings)) { |
|
778 | + foreach ($explode as $key => $value) { |
|
779 | + while (strpos($value,'YAMLString') !== false) { |
|
780 | + $explode[$key] = preg_replace('/YAMLString/',$saved_strings[$stringi],$value, 1); |
|
781 | + unset($saved_strings[$stringi]); |
|
782 | + ++$stringi; |
|
783 | + $value = $explode[$key]; |
|
784 | + } |
|
785 | + } |
|
786 | + } |
|
787 | + |
|
788 | + |
|
789 | + // Re-add the empties |
|
790 | + if (!empty($saved_empties)) { |
|
791 | + foreach ($explode as $key => $value) { |
|
792 | + while (strpos($value,'YAMLEmpty') !== false) { |
|
793 | + $explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1); |
|
794 | + $value = $explode[$key]; |
|
795 | + } |
|
796 | + } |
|
797 | + } |
|
798 | + |
|
799 | + $finished = true; |
|
800 | + foreach ($explode as $key => $value) { |
|
801 | + if (strpos($value,'YAMLSeq') !== false) { |
|
802 | + $finished = false; break; |
|
803 | + } |
|
804 | + if (strpos($value,'YAMLMap') !== false) { |
|
805 | + $finished = false; break; |
|
806 | + } |
|
807 | + if (strpos($value,'YAMLString') !== false) { |
|
808 | + $finished = false; break; |
|
809 | + } |
|
810 | + if (strpos($value,'YAMLEmpty') !== false) { |
|
811 | + $finished = false; break; |
|
812 | + } |
|
813 | + } |
|
814 | + if ($finished) break; |
|
815 | + |
|
816 | + $i++; |
|
817 | + if ($i > 10) |
|
818 | + break; // Prevent infinite loops. |
|
819 | + } |
|
820 | + |
|
821 | + |
|
822 | + return $explode; |
|
823 | 823 | } |
824 | 824 | |
825 | 825 | private function literalBlockContinues ($line, $lineIndent) { |
826 | - if (!trim($line)) return true; |
|
827 | - if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true; |
|
828 | - return false; |
|
826 | + if (!trim($line)) return true; |
|
827 | + if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true; |
|
828 | + return false; |
|
829 | 829 | } |
830 | 830 | |
831 | 831 | private function referenceContentsByAlias ($alias) { |
832 | - do { |
|
833 | - if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; } |
|
834 | - $groupPath = $this->SavedGroups[$alias]; |
|
835 | - $value = $this->result; |
|
836 | - foreach ($groupPath as $k) { |
|
837 | - $value = $value[$k]; |
|
838 | - } |
|
839 | - } while (false); |
|
840 | - return $value; |
|
832 | + do { |
|
833 | + if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; } |
|
834 | + $groupPath = $this->SavedGroups[$alias]; |
|
835 | + $value = $this->result; |
|
836 | + foreach ($groupPath as $k) { |
|
837 | + $value = $value[$k]; |
|
838 | + } |
|
839 | + } while (false); |
|
840 | + return $value; |
|
841 | 841 | } |
842 | 842 | |
843 | 843 | private function addArrayInline ($array, $indent) { |
844 | - $CommonGroupPath = $this->path; |
|
845 | - if (empty ($array)) return false; |
|
846 | - |
|
847 | - foreach ($array as $k => $_) { |
|
848 | - $this->addArray(array($k => $_), $indent); |
|
849 | - $this->path = $CommonGroupPath; |
|
850 | - } |
|
851 | - return true; |
|
844 | + $CommonGroupPath = $this->path; |
|
845 | + if (empty ($array)) return false; |
|
846 | + |
|
847 | + foreach ($array as $k => $_) { |
|
848 | + $this->addArray(array($k => $_), $indent); |
|
849 | + $this->path = $CommonGroupPath; |
|
850 | + } |
|
851 | + return true; |
|
852 | 852 | } |
853 | 853 | |
854 | 854 | private function addArray ($incoming_data, $incoming_indent) { |
855 | 855 | |
856 | 856 | // print_r ($incoming_data); |
857 | 857 | |
858 | - if (count ($incoming_data) > 1) |
|
859 | - return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
860 | - |
|
861 | - $key = key ($incoming_data); |
|
862 | - $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
|
863 | - if ($key === '__!YAMLZero') $key = '0'; |
|
864 | - |
|
865 | - if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. |
|
866 | - if ($key || $key === '' || $key === '0') { |
|
867 | - $this->result[$key] = $value; |
|
868 | - } else { |
|
869 | - $this->result[] = $value; end ($this->result); $key = key ($this->result); |
|
870 | - } |
|
871 | - $this->path[$incoming_indent] = $key; |
|
872 | - return; |
|
873 | - } |
|
874 | - |
|
875 | - |
|
876 | - |
|
877 | - $history = array(); |
|
878 | - // Unfolding inner array tree. |
|
879 | - $history[] = $_arr = $this->result; |
|
880 | - foreach ($this->path as $k) { |
|
881 | - $history[] = $_arr = $_arr[$k]; |
|
882 | - } |
|
883 | - |
|
884 | - if ($this->_containsGroupAlias) { |
|
885 | - $value = $this->referenceContentsByAlias($this->_containsGroupAlias); |
|
886 | - $this->_containsGroupAlias = false; |
|
887 | - } |
|
888 | - |
|
889 | - |
|
890 | - // Adding string or numeric key to the innermost level or $this->arr. |
|
891 | - if (is_string($key) && $key == '<<') { |
|
892 | - if (!is_array ($_arr)) { $_arr = array (); } |
|
893 | - |
|
894 | - $_arr = array_merge ($_arr, $value); |
|
895 | - } else if ($key || $key === '' || $key === '0') { |
|
896 | - if (!is_array ($_arr)) |
|
897 | - $_arr = array ($key=>$value); |
|
898 | - else |
|
899 | - $_arr[$key] = $value; |
|
900 | - } else { |
|
901 | - if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } |
|
902 | - else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
|
903 | - } |
|
904 | - |
|
905 | - $reverse_path = array_reverse($this->path); |
|
906 | - $reverse_history = array_reverse ($history); |
|
907 | - $reverse_history[0] = $_arr; |
|
908 | - $cnt = count($reverse_history) - 1; |
|
909 | - for ($i = 0; $i < $cnt; $i++) { |
|
910 | - $reverse_history[$i+1][$reverse_path[$i]] = $reverse_history[$i]; |
|
911 | - } |
|
912 | - $this->result = $reverse_history[$cnt]; |
|
913 | - |
|
914 | - $this->path[$incoming_indent] = $key; |
|
915 | - |
|
916 | - if ($this->_containsGroupAnchor) { |
|
917 | - $this->SavedGroups[$this->_containsGroupAnchor] = $this->path; |
|
918 | - if (is_array ($value)) { |
|
919 | - $k = key ($value); |
|
920 | - if (!is_int ($k)) { |
|
921 | - $this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k; |
|
922 | - } |
|
923 | - } |
|
924 | - $this->_containsGroupAnchor = false; |
|
925 | - } |
|
858 | + if (count ($incoming_data) > 1) |
|
859 | + return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
860 | + |
|
861 | + $key = key ($incoming_data); |
|
862 | + $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
|
863 | + if ($key === '__!YAMLZero') $key = '0'; |
|
864 | + |
|
865 | + if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. |
|
866 | + if ($key || $key === '' || $key === '0') { |
|
867 | + $this->result[$key] = $value; |
|
868 | + } else { |
|
869 | + $this->result[] = $value; end ($this->result); $key = key ($this->result); |
|
870 | + } |
|
871 | + $this->path[$incoming_indent] = $key; |
|
872 | + return; |
|
873 | + } |
|
874 | + |
|
875 | + |
|
876 | + |
|
877 | + $history = array(); |
|
878 | + // Unfolding inner array tree. |
|
879 | + $history[] = $_arr = $this->result; |
|
880 | + foreach ($this->path as $k) { |
|
881 | + $history[] = $_arr = $_arr[$k]; |
|
882 | + } |
|
883 | + |
|
884 | + if ($this->_containsGroupAlias) { |
|
885 | + $value = $this->referenceContentsByAlias($this->_containsGroupAlias); |
|
886 | + $this->_containsGroupAlias = false; |
|
887 | + } |
|
888 | + |
|
889 | + |
|
890 | + // Adding string or numeric key to the innermost level or $this->arr. |
|
891 | + if (is_string($key) && $key == '<<') { |
|
892 | + if (!is_array ($_arr)) { $_arr = array (); } |
|
893 | + |
|
894 | + $_arr = array_merge ($_arr, $value); |
|
895 | + } else if ($key || $key === '' || $key === '0') { |
|
896 | + if (!is_array ($_arr)) |
|
897 | + $_arr = array ($key=>$value); |
|
898 | + else |
|
899 | + $_arr[$key] = $value; |
|
900 | + } else { |
|
901 | + if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } |
|
902 | + else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
|
903 | + } |
|
904 | + |
|
905 | + $reverse_path = array_reverse($this->path); |
|
906 | + $reverse_history = array_reverse ($history); |
|
907 | + $reverse_history[0] = $_arr; |
|
908 | + $cnt = count($reverse_history) - 1; |
|
909 | + for ($i = 0; $i < $cnt; $i++) { |
|
910 | + $reverse_history[$i+1][$reverse_path[$i]] = $reverse_history[$i]; |
|
911 | + } |
|
912 | + $this->result = $reverse_history[$cnt]; |
|
913 | + |
|
914 | + $this->path[$incoming_indent] = $key; |
|
915 | + |
|
916 | + if ($this->_containsGroupAnchor) { |
|
917 | + $this->SavedGroups[$this->_containsGroupAnchor] = $this->path; |
|
918 | + if (is_array ($value)) { |
|
919 | + $k = key ($value); |
|
920 | + if (!is_int ($k)) { |
|
921 | + $this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k; |
|
922 | + } |
|
923 | + } |
|
924 | + $this->_containsGroupAnchor = false; |
|
925 | + } |
|
926 | 926 | |
927 | 927 | } |
928 | 928 | |
929 | 929 | private static function startsLiteralBlock ($line) { |
930 | - $lastChar = substr (trim($line), -1); |
|
931 | - if ($lastChar != '>' && $lastChar != '|') return false; |
|
932 | - if ($lastChar == '|') return $lastChar; |
|
933 | - // HTML tags should not be counted as literal blocks. |
|
934 | - if (preg_match ('#<.*?>$#', $line)) return false; |
|
935 | - return $lastChar; |
|
930 | + $lastChar = substr (trim($line), -1); |
|
931 | + if ($lastChar != '>' && $lastChar != '|') return false; |
|
932 | + if ($lastChar == '|') return $lastChar; |
|
933 | + // HTML tags should not be counted as literal blocks. |
|
934 | + if (preg_match ('#<.*?>$#', $line)) return false; |
|
935 | + return $lastChar; |
|
936 | 936 | } |
937 | 937 | |
938 | 938 | private static function greedilyNeedNextLine($line) { |
939 | - $line = trim ($line); |
|
940 | - if (!strlen($line)) return false; |
|
941 | - if (substr ($line, -1, 1) == ']') return false; |
|
942 | - if ($line[0] == '[') return true; |
|
943 | - if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true; |
|
944 | - return false; |
|
939 | + $line = trim ($line); |
|
940 | + if (!strlen($line)) return false; |
|
941 | + if (substr ($line, -1, 1) == ']') return false; |
|
942 | + if ($line[0] == '[') return true; |
|
943 | + if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true; |
|
944 | + return false; |
|
945 | 945 | } |
946 | 946 | |
947 | 947 | private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) { |
948 | - $line = self::stripIndent($line, $indent); |
|
949 | - if ($literalBlockStyle !== '|') { |
|
950 | - $line = self::stripIndent($line); |
|
951 | - } |
|
952 | - $line = rtrim ($line, "\r\n\t ") . "\n"; |
|
953 | - if ($literalBlockStyle == '|') { |
|
954 | - return $literalBlock . $line; |
|
955 | - } |
|
956 | - if (strlen($line) == 0) |
|
957 | - return rtrim($literalBlock, ' ') . "\n"; |
|
958 | - if ($line == "\n" && $literalBlockStyle == '>') { |
|
959 | - return rtrim ($literalBlock, " \t") . "\n"; |
|
960 | - } |
|
961 | - if ($line != "\n") |
|
962 | - $line = trim ($line, "\r\n ") . " "; |
|
963 | - return $literalBlock . $line; |
|
948 | + $line = self::stripIndent($line, $indent); |
|
949 | + if ($literalBlockStyle !== '|') { |
|
950 | + $line = self::stripIndent($line); |
|
951 | + } |
|
952 | + $line = rtrim ($line, "\r\n\t ") . "\n"; |
|
953 | + if ($literalBlockStyle == '|') { |
|
954 | + return $literalBlock . $line; |
|
955 | + } |
|
956 | + if (strlen($line) == 0) |
|
957 | + return rtrim($literalBlock, ' ') . "\n"; |
|
958 | + if ($line == "\n" && $literalBlockStyle == '>') { |
|
959 | + return rtrim ($literalBlock, " \t") . "\n"; |
|
960 | + } |
|
961 | + if ($line != "\n") |
|
962 | + $line = trim ($line, "\r\n ") . " "; |
|
963 | + return $literalBlock . $line; |
|
964 | 964 | } |
965 | 965 | |
966 | 966 | function revertLiteralPlaceHolder ($lineArray, $literalBlock) { |
967 | - foreach ($lineArray as $k => $_) { |
|
968 | - if (is_array($_)) |
|
969 | - $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
|
970 | - else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
|
971 | - $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
|
972 | - } |
|
973 | - return $lineArray; |
|
967 | + foreach ($lineArray as $k => $_) { |
|
968 | + if (is_array($_)) |
|
969 | + $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
|
970 | + else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
|
971 | + $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
|
972 | + } |
|
973 | + return $lineArray; |
|
974 | 974 | } |
975 | 975 | |
976 | 976 | private static function stripIndent ($line, $indent = -1) { |
977 | - if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line)); |
|
978 | - return substr ($line, $indent); |
|
977 | + if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line)); |
|
978 | + return substr ($line, $indent); |
|
979 | 979 | } |
980 | 980 | |
981 | 981 | private function getParentPathByIndent ($indent) { |
982 | - if ($indent == 0) return array(); |
|
983 | - $linePath = $this->path; |
|
984 | - do { |
|
985 | - end($linePath); $lastIndentInParentPath = key($linePath); |
|
986 | - if ($indent <= $lastIndentInParentPath) array_pop ($linePath); |
|
987 | - } while ($indent <= $lastIndentInParentPath); |
|
988 | - return $linePath; |
|
982 | + if ($indent == 0) return array(); |
|
983 | + $linePath = $this->path; |
|
984 | + do { |
|
985 | + end($linePath); $lastIndentInParentPath = key($linePath); |
|
986 | + if ($indent <= $lastIndentInParentPath) array_pop ($linePath); |
|
987 | + } while ($indent <= $lastIndentInParentPath); |
|
988 | + return $linePath; |
|
989 | 989 | } |
990 | 990 | |
991 | 991 | |
992 | 992 | private function clearBiggerPathValues ($indent) { |
993 | 993 | |
994 | 994 | |
995 | - if ($indent == 0) $this->path = array(); |
|
996 | - if (empty ($this->path)) return true; |
|
995 | + if ($indent == 0) $this->path = array(); |
|
996 | + if (empty ($this->path)) return true; |
|
997 | 997 | |
998 | - foreach ($this->path as $k => $_) { |
|
999 | - if ($k > $indent) unset ($this->path[$k]); |
|
1000 | - } |
|
998 | + foreach ($this->path as $k => $_) { |
|
999 | + if ($k > $indent) unset ($this->path[$k]); |
|
1000 | + } |
|
1001 | 1001 | |
1002 | - return true; |
|
1002 | + return true; |
|
1003 | 1003 | } |
1004 | 1004 | |
1005 | 1005 | |
1006 | 1006 | private static function isComment ($line) { |
1007 | - if (!$line) return false; |
|
1008 | - if ($line[0] == '#') return true; |
|
1009 | - if (trim($line, " \r\n\t") == '---') return true; |
|
1010 | - return false; |
|
1007 | + if (!$line) return false; |
|
1008 | + if ($line[0] == '#') return true; |
|
1009 | + if (trim($line, " \r\n\t") == '---') return true; |
|
1010 | + return false; |
|
1011 | 1011 | } |
1012 | 1012 | |
1013 | 1013 | private static function isEmpty ($line) { |
1014 | - return (trim ($line) === ''); |
|
1014 | + return (trim ($line) === ''); |
|
1015 | 1015 | } |
1016 | 1016 | |
1017 | 1017 | |
1018 | 1018 | private function isArrayElement ($line) { |
1019 | - if (!$line || !is_scalar($line)) return false; |
|
1020 | - if (substr($line, 0, 2) != '- ') return false; |
|
1021 | - if (strlen ($line) > 3) |
|
1022 | - if (substr($line,0,3) == '---') return false; |
|
1019 | + if (!$line || !is_scalar($line)) return false; |
|
1020 | + if (substr($line, 0, 2) != '- ') return false; |
|
1021 | + if (strlen ($line) > 3) |
|
1022 | + if (substr($line,0,3) == '---') return false; |
|
1023 | 1023 | |
1024 | - return true; |
|
1024 | + return true; |
|
1025 | 1025 | } |
1026 | 1026 | |
1027 | 1027 | private function isHashElement ($line) { |
1028 | - return strpos($line, ':'); |
|
1028 | + return strpos($line, ':'); |
|
1029 | 1029 | } |
1030 | 1030 | |
1031 | 1031 | private function isLiteral ($line) { |
1032 | - if ($this->isArrayElement($line)) return false; |
|
1033 | - if ($this->isHashElement($line)) return false; |
|
1034 | - return true; |
|
1032 | + if ($this->isArrayElement($line)) return false; |
|
1033 | + if ($this->isHashElement($line)) return false; |
|
1034 | + return true; |
|
1035 | 1035 | } |
1036 | 1036 | |
1037 | 1037 | |
1038 | 1038 | private static function unquote ($value) { |
1039 | - if (!$value) return $value; |
|
1040 | - if (!is_string($value)) return $value; |
|
1041 | - if ($value[0] == '\'') return trim ($value, '\''); |
|
1042 | - if ($value[0] == '"') return trim ($value, '"'); |
|
1043 | - return $value; |
|
1039 | + if (!$value) return $value; |
|
1040 | + if (!is_string($value)) return $value; |
|
1041 | + if ($value[0] == '\'') return trim ($value, '\''); |
|
1042 | + if ($value[0] == '"') return trim ($value, '"'); |
|
1043 | + return $value; |
|
1044 | 1044 | } |
1045 | 1045 | |
1046 | 1046 | private function startsMappedSequence ($line) { |
1047 | - return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':'); |
|
1047 | + return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':'); |
|
1048 | 1048 | } |
1049 | 1049 | |
1050 | 1050 | private function returnMappedSequence ($line) { |
1051 | - $array = array(); |
|
1052 | - $key = self::unquote(trim(substr($line,1,-1))); |
|
1053 | - $array[$key] = array(); |
|
1054 | - $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key); |
|
1055 | - return array($array); |
|
1051 | + $array = array(); |
|
1052 | + $key = self::unquote(trim(substr($line,1,-1))); |
|
1053 | + $array[$key] = array(); |
|
1054 | + $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key); |
|
1055 | + return array($array); |
|
1056 | 1056 | } |
1057 | 1057 | |
1058 | 1058 | private function checkKeysInValue($value) { |
1059 | - if (strchr('[{"\'', $value[0]) === false) { |
|
1060 | - if (strchr($value, ': ') !== false) { |
|
1061 | - throw new Exception('Too many keys: '.$value); |
|
1062 | - } |
|
1063 | - } |
|
1059 | + if (strchr('[{"\'', $value[0]) === false) { |
|
1060 | + if (strchr($value, ': ') !== false) { |
|
1061 | + throw new Exception('Too many keys: '.$value); |
|
1062 | + } |
|
1063 | + } |
|
1064 | 1064 | } |
1065 | 1065 | |
1066 | 1066 | private function returnMappedValue ($line) { |
1067 | - $this->checkKeysInValue($line); |
|
1068 | - $array = array(); |
|
1069 | - $key = self::unquote (trim(substr($line,0,-1))); |
|
1070 | - $array[$key] = ''; |
|
1071 | - return $array; |
|
1067 | + $this->checkKeysInValue($line); |
|
1068 | + $array = array(); |
|
1069 | + $key = self::unquote (trim(substr($line,0,-1))); |
|
1070 | + $array[$key] = ''; |
|
1071 | + return $array; |
|
1072 | 1072 | } |
1073 | 1073 | |
1074 | 1074 | private function startsMappedValue ($line) { |
1075 | - return (substr ($line, -1, 1) == ':'); |
|
1075 | + return (substr ($line, -1, 1) == ':'); |
|
1076 | 1076 | } |
1077 | 1077 | |
1078 | 1078 | private function isPlainArray ($line) { |
1079 | - return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
|
1079 | + return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
|
1080 | 1080 | } |
1081 | 1081 | |
1082 | 1082 | private function returnPlainArray ($line) { |
1083 | - return $this->_toType($line); |
|
1083 | + return $this->_toType($line); |
|
1084 | 1084 | } |
1085 | 1085 | |
1086 | 1086 | private function returnKeyValuePair ($line) { |
1087 | - $array = array(); |
|
1088 | - $key = ''; |
|
1089 | - if (strpos ($line, ': ')) { |
|
1090 | - // It's a key/value pair most likely |
|
1091 | - // If the key is in double quotes pull it out |
|
1092 | - if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) { |
|
1093 | - $value = trim(str_replace($matches[1],'',$line)); |
|
1094 | - $key = $matches[2]; |
|
1095 | - } else { |
|
1096 | - // Do some guesswork as to the key and the value |
|
1097 | - $explode = explode(': ', $line); |
|
1098 | - $key = trim(array_shift($explode)); |
|
1099 | - $value = trim(implode(': ', $explode)); |
|
1100 | - $this->checkKeysInValue($value); |
|
1101 | - } |
|
1102 | - // Set the type of the value. Int, string, etc |
|
1103 | - $value = $this->_toType($value); |
|
1104 | - if ($key === '0') $key = '__!YAMLZero'; |
|
1105 | - $array[$key] = $value; |
|
1106 | - } else { |
|
1107 | - $array = array ($line); |
|
1108 | - } |
|
1109 | - return $array; |
|
1087 | + $array = array(); |
|
1088 | + $key = ''; |
|
1089 | + if (strpos ($line, ': ')) { |
|
1090 | + // It's a key/value pair most likely |
|
1091 | + // If the key is in double quotes pull it out |
|
1092 | + if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) { |
|
1093 | + $value = trim(str_replace($matches[1],'',$line)); |
|
1094 | + $key = $matches[2]; |
|
1095 | + } else { |
|
1096 | + // Do some guesswork as to the key and the value |
|
1097 | + $explode = explode(': ', $line); |
|
1098 | + $key = trim(array_shift($explode)); |
|
1099 | + $value = trim(implode(': ', $explode)); |
|
1100 | + $this->checkKeysInValue($value); |
|
1101 | + } |
|
1102 | + // Set the type of the value. Int, string, etc |
|
1103 | + $value = $this->_toType($value); |
|
1104 | + if ($key === '0') $key = '__!YAMLZero'; |
|
1105 | + $array[$key] = $value; |
|
1106 | + } else { |
|
1107 | + $array = array ($line); |
|
1108 | + } |
|
1109 | + return $array; |
|
1110 | 1110 | |
1111 | 1111 | } |
1112 | 1112 | |
1113 | 1113 | |
1114 | 1114 | private function returnArrayElement ($line) { |
1115 | - if (strlen($line) <= 1) return array(array()); // Weird %) |
|
1116 | - $array = array(); |
|
1117 | - $value = trim(substr($line,1)); |
|
1118 | - $value = $this->_toType($value); |
|
1119 | - if ($this->isArrayElement($value)) { |
|
1120 | - $value = $this->returnArrayElement($value); |
|
1121 | - } |
|
1122 | - $array[] = $value; |
|
1123 | - return $array; |
|
1115 | + if (strlen($line) <= 1) return array(array()); // Weird %) |
|
1116 | + $array = array(); |
|
1117 | + $value = trim(substr($line,1)); |
|
1118 | + $value = $this->_toType($value); |
|
1119 | + if ($this->isArrayElement($value)) { |
|
1120 | + $value = $this->returnArrayElement($value); |
|
1121 | + } |
|
1122 | + $array[] = $value; |
|
1123 | + return $array; |
|
1124 | 1124 | } |
1125 | 1125 | |
1126 | 1126 | |
1127 | 1127 | private function nodeContainsGroup ($line) { |
1128 | - $symbolsForReference = 'A-z0-9_\-'; |
|
1129 | - if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
|
1130 | - if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1131 | - if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1132 | - if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1]; |
|
1133 | - if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1]; |
|
1134 | - if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; |
|
1135 | - return false; |
|
1128 | + $symbolsForReference = 'A-z0-9_\-'; |
|
1129 | + if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
|
1130 | + if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1131 | + if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1132 | + if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1]; |
|
1133 | + if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1]; |
|
1134 | + if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; |
|
1135 | + return false; |
|
1136 | 1136 | |
1137 | 1137 | } |
1138 | 1138 | |
1139 | 1139 | private function addGroup ($line, $group) { |
1140 | - if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1); |
|
1141 | - if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1); |
|
1142 | - //print_r ($this->path); |
|
1140 | + if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1); |
|
1141 | + if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1); |
|
1142 | + //print_r ($this->path); |
|
1143 | 1143 | } |
1144 | 1144 | |
1145 | 1145 | private function stripGroup ($line, $group) { |
1146 | - $line = trim(str_replace($group, '', $line)); |
|
1147 | - return $line; |
|
1146 | + $line = trim(str_replace($group, '', $line)); |
|
1147 | + return $line; |
|
1148 | 1148 | } |
1149 | 1149 | } |
1150 | 1150 | } |
@@ -10,40 +10,40 @@ discard block |
||
10 | 10 | * @package Spyc |
11 | 11 | */ |
12 | 12 | |
13 | -if (!function_exists('spyc_load')) { |
|
13 | +if ( ! function_exists('spyc_load')) { |
|
14 | 14 | /** |
15 | 15 | * Parses YAML to array. |
16 | 16 | * @param string $string YAML string. |
17 | 17 | * @return array |
18 | 18 | */ |
19 | - function spyc_load ($string) { |
|
19 | + function spyc_load($string) { |
|
20 | 20 | return Spyc::YAMLLoadString($string); |
21 | 21 | } |
22 | 22 | } |
23 | 23 | |
24 | -if (!function_exists('spyc_load_file')) { |
|
24 | +if ( ! function_exists('spyc_load_file')) { |
|
25 | 25 | /** |
26 | 26 | * Parses YAML to array. |
27 | 27 | * @param string $file Path to YAML file. |
28 | 28 | * @return array |
29 | 29 | */ |
30 | - function spyc_load_file ($file) { |
|
30 | + function spyc_load_file($file) { |
|
31 | 31 | return Spyc::YAMLLoad($file); |
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
35 | -if (!function_exists('spyc_dump')) { |
|
35 | +if ( ! function_exists('spyc_dump')) { |
|
36 | 36 | /** |
37 | 37 | * Dumps array to YAML. |
38 | 38 | * @param array $data Array. |
39 | 39 | * @return string |
40 | 40 | */ |
41 | - function spyc_dump ($data) { |
|
41 | + function spyc_dump($data) { |
|
42 | 42 | return Spyc::YAMLDump($data, false, false, true); |
43 | 43 | } |
44 | 44 | } |
45 | 45 | |
46 | -if (!class_exists('Spyc')) { |
|
46 | +if ( ! class_exists('Spyc')) { |
|
47 | 47 | |
48 | 48 | /** |
49 | 49 | * The Simple PHP YAML Class. |
@@ -120,7 +120,7 @@ discard block |
||
120 | 120 | * @param string $input |
121 | 121 | * @return array |
122 | 122 | */ |
123 | - public function load ($input) { |
|
123 | + public function load($input) { |
|
124 | 124 | return $this->__loadString($input); |
125 | 125 | } |
126 | 126 | |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | * @param string $file |
130 | 130 | * @return array |
131 | 131 | */ |
132 | - public function loadFile ($file) { |
|
132 | + public function loadFile($file) { |
|
133 | 133 | return $this->__load($file); |
134 | 134 | } |
135 | 135 | |
@@ -224,18 +224,18 @@ discard block |
||
224 | 224 | * @param int $indent Pass in false to use the default, which is 2 |
225 | 225 | * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40) |
226 | 226 | */ |
227 | - public function dump($array,$indent = false,$wordwrap = false, $no_opening_dashes = false) { |
|
227 | + public function dump($array, $indent = false, $wordwrap = false, $no_opening_dashes = false) { |
|
228 | 228 | // Dumps to some very clean YAML. We'll have to add some more features |
229 | 229 | // and options soon. And better support for folding. |
230 | 230 | |
231 | 231 | // New features and options. |
232 | - if ($indent === false or !is_numeric($indent)) { |
|
232 | + if ($indent === false or ! is_numeric($indent)) { |
|
233 | 233 | $this->_dumpIndent = 2; |
234 | 234 | } else { |
235 | 235 | $this->_dumpIndent = $indent; |
236 | 236 | } |
237 | 237 | |
238 | - if ($wordwrap === false or !is_numeric($wordwrap)) { |
|
238 | + if ($wordwrap === false or ! is_numeric($wordwrap)) { |
|
239 | 239 | $this->_dumpWordWrap = 40; |
240 | 240 | } else { |
241 | 241 | $this->_dumpWordWrap = $wordwrap; |
@@ -243,15 +243,15 @@ discard block |
||
243 | 243 | |
244 | 244 | // New YAML document |
245 | 245 | $string = ""; |
246 | - if (!$no_opening_dashes) $string = "---\n"; |
|
246 | + if ( ! $no_opening_dashes) $string = "---\n"; |
|
247 | 247 | |
248 | 248 | // Start at the base of the array and move through it. |
249 | 249 | if ($array) { |
250 | - $array = (array)$array; |
|
250 | + $array = (array) $array; |
|
251 | 251 | $previous_key = -1; |
252 | 252 | foreach ($array as $key => $value) { |
253 | - if (!isset($first_key)) $first_key = $key; |
|
254 | - $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array); |
|
253 | + if ( ! isset($first_key)) $first_key = $key; |
|
254 | + $string .= $this->_yamlize($key, $value, 0, $previous_key, $first_key, $array); |
|
255 | 255 | $previous_key = $key; |
256 | 256 | } |
257 | 257 | } |
@@ -266,8 +266,8 @@ discard block |
||
266 | 266 | * @param $value The value of the item |
267 | 267 | * @param $indent The indent of the current node |
268 | 268 | */ |
269 | - private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
|
270 | - if(is_object($value)) $value = (array)$value; |
|
269 | + private function _yamlize($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
|
270 | + if (is_object($value)) $value = (array) $value; |
|
271 | 271 | if (is_array($value)) { |
272 | 272 | if (empty ($value)) |
273 | 273 | return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
@@ -277,8 +277,8 @@ discard block |
||
277 | 277 | // Add the indent |
278 | 278 | $indent += $this->_dumpIndent; |
279 | 279 | // Yamlize the array |
280 | - $string .= $this->_yamlizeArray($value,$indent); |
|
281 | - } elseif (!is_array($value)) { |
|
280 | + $string .= $this->_yamlizeArray($value, $indent); |
|
281 | + } elseif ( ! is_array($value)) { |
|
282 | 282 | // It doesn't have children. Yip. |
283 | 283 | $string = $this->_dumpNode($key, $value, $indent, $previous_key, $first_key, $source_array); |
284 | 284 | } |
@@ -292,12 +292,12 @@ discard block |
||
292 | 292 | * @param $array The array you want to convert |
293 | 293 | * @param $indent The indent of the current level |
294 | 294 | */ |
295 | - private function _yamlizeArray($array,$indent) { |
|
295 | + private function _yamlizeArray($array, $indent) { |
|
296 | 296 | if (is_array($array)) { |
297 | 297 | $string = ''; |
298 | 298 | $previous_key = -1; |
299 | 299 | foreach ($array as $key => $value) { |
300 | - if (!isset($first_key)) $first_key = $key; |
|
300 | + if ( ! isset($first_key)) $first_key = $key; |
|
301 | 301 | $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array); |
302 | 302 | $previous_key = $key; |
303 | 303 | } |
@@ -317,14 +317,14 @@ discard block |
||
317 | 317 | */ |
318 | 318 | private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
319 | 319 | // do some folding here, for blocks |
320 | - if (is_string ($value) && ((strpos($value,"\n") !== false || strpos($value,": ") !== false || strpos($value,"- ") !== false || |
|
321 | - strpos($value,"*") !== false || strpos($value,"#") !== false || strpos($value,"<") !== false || strpos($value,">") !== false || strpos ($value, '%') !== false || strpos ($value, ' ') !== false || |
|
322 | - strpos($value,"[") !== false || strpos($value,"]") !== false || strpos($value,"{") !== false || strpos($value,"}") !== false) || strpos($value,"&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 || |
|
323 | - substr ($value, -1, 1) == ':') |
|
320 | + if (is_string($value) && ((strpos($value, "\n") !== false || strpos($value, ": ") !== false || strpos($value, "- ") !== false || |
|
321 | + strpos($value, "*") !== false || strpos($value, "#") !== false || strpos($value, "<") !== false || strpos($value, ">") !== false || strpos($value, '%') !== false || strpos($value, ' ') !== false || |
|
322 | + strpos($value, "[") !== false || strpos($value, "]") !== false || strpos($value, "{") !== false || strpos($value, "}") !== false) || strpos($value, "&") !== false || strpos($value, "'") !== false || strpos($value, "!") === 0 || |
|
323 | + substr($value, -1, 1) == ':') |
|
324 | 324 | ) { |
325 | - $value = $this->_doLiteralBlock($value,$indent); |
|
325 | + $value = $this->_doLiteralBlock($value, $indent); |
|
326 | 326 | } else { |
327 | - $value = $this->_doFolding($value,$indent); |
|
327 | + $value = $this->_doFolding($value, $indent); |
|
328 | 328 | } |
329 | 329 | |
330 | 330 | if ($value === array()) $value = '[ ]'; |
@@ -332,8 +332,8 @@ discard block |
||
332 | 332 | if (self::isTranslationWord($value)) { |
333 | 333 | $value = $this->_doLiteralBlock($value, $indent); |
334 | 334 | } |
335 | - if (trim ($value) != $value) |
|
336 | - $value = $this->_doLiteralBlock($value,$indent); |
|
335 | + if (trim($value) != $value) |
|
336 | + $value = $this->_doLiteralBlock($value, $indent); |
|
337 | 337 | |
338 | 338 | if (is_bool($value)) { |
339 | 339 | $value = $value ? "true" : "false"; |
@@ -342,17 +342,17 @@ discard block |
||
342 | 342 | if ($value === null) $value = 'null'; |
343 | 343 | if ($value === "'" . self::REMPTY . "'") $value = null; |
344 | 344 | |
345 | - $spaces = str_repeat(' ',$indent); |
|
345 | + $spaces = str_repeat(' ', $indent); |
|
346 | 346 | |
347 | 347 | //if (is_int($key) && $key - 1 == $previous_key && $first_key===0) { |
348 | - if (is_array ($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) { |
|
348 | + if (is_array($source_array) && array_keys($source_array) === range(0, count($source_array) - 1)) { |
|
349 | 349 | // It's a sequence |
350 | - $string = $spaces.'- '.$value."\n"; |
|
350 | + $string = $spaces . '- ' . $value . "\n"; |
|
351 | 351 | } else { |
352 | 352 | // if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"'); |
353 | 353 | // It's mapped |
354 | 354 | if (strpos($key, ":") !== false || strpos($key, "#") !== false) { $key = '"' . $key . '"'; } |
355 | - $string = rtrim ($spaces.$key.': '.$value)."\n"; |
|
355 | + $string = rtrim($spaces . $key . ': ' . $value) . "\n"; |
|
356 | 356 | } |
357 | 357 | return $string; |
358 | 358 | } |
@@ -364,25 +364,25 @@ discard block |
||
364 | 364 | * @param $value |
365 | 365 | * @param $indent int The value of the indent |
366 | 366 | */ |
367 | - private function _doLiteralBlock($value,$indent) { |
|
367 | + private function _doLiteralBlock($value, $indent) { |
|
368 | 368 | if ($value === "\n") return '\n'; |
369 | 369 | if (strpos($value, "\n") === false && strpos($value, "'") === false) { |
370 | - return sprintf ("'%s'", $value); |
|
370 | + return sprintf("'%s'", $value); |
|
371 | 371 | } |
372 | 372 | if (strpos($value, "\n") === false && strpos($value, '"') === false) { |
373 | - return sprintf ('"%s"', $value); |
|
373 | + return sprintf('"%s"', $value); |
|
374 | 374 | } |
375 | - $exploded = explode("\n",$value); |
|
375 | + $exploded = explode("\n", $value); |
|
376 | 376 | $newValue = '|'; |
377 | 377 | if (isset($exploded[0]) && ($exploded[0] == "|" || $exploded[0] == "|-" || $exploded[0] == ">")) { |
378 | 378 | $newValue = $exploded[0]; |
379 | 379 | unset($exploded[0]); |
380 | 380 | } |
381 | 381 | $indent += $this->_dumpIndent; |
382 | - $spaces = str_repeat(' ',$indent); |
|
382 | + $spaces = str_repeat(' ', $indent); |
|
383 | 383 | foreach ($exploded as $line) { |
384 | 384 | $line = trim($line); |
385 | - if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line)-1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line)-1)) { |
|
385 | + if (strpos($line, '"') === 0 && strrpos($line, '"') == (strlen($line) - 1) || strpos($line, "'") === 0 && strrpos($line, "'") == (strlen($line) - 1)) { |
|
386 | 386 | $line = substr($line, 1, -1); |
387 | 387 | } |
388 | 388 | $newValue .= "\n" . $spaces . ($line); |
@@ -396,16 +396,16 @@ discard block |
||
396 | 396 | * @return string |
397 | 397 | * @param $value The string you wish to fold |
398 | 398 | */ |
399 | - private function _doFolding($value,$indent) { |
|
399 | + private function _doFolding($value, $indent) { |
|
400 | 400 | // Don't do anything if wordwrap is set to 0 |
401 | 401 | |
402 | - if ($this->_dumpWordWrap !== 0 && is_string ($value) && strlen($value) > $this->_dumpWordWrap) { |
|
402 | + if ($this->_dumpWordWrap !== 0 && is_string($value) && strlen($value) > $this->_dumpWordWrap) { |
|
403 | 403 | $indent += $this->_dumpIndent; |
404 | - $indent = str_repeat(' ',$indent); |
|
405 | - $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); |
|
406 | - $value = ">\n".$indent.$wrapped; |
|
404 | + $indent = str_repeat(' ', $indent); |
|
405 | + $wrapped = wordwrap($value, $this->_dumpWordWrap, "\n$indent"); |
|
406 | + $value = ">\n" . $indent . $wrapped; |
|
407 | 407 | } else { |
408 | - if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) |
|
408 | + if ($this->setting_dump_force_quotes && is_string($value) && $value !== self::REMPTY) |
|
409 | 409 | $value = '"' . $value . '"'; |
410 | 410 | if (is_numeric($value) && is_string($value)) |
411 | 411 | $value = '"' . $value . '"'; |
@@ -432,7 +432,7 @@ discard block |
||
432 | 432 | |
433 | 433 | private function isTranslationWord($value) { |
434 | 434 | return ( |
435 | - self::isTrueWord($value) || |
|
435 | + self::isTrueWord($value) || |
|
436 | 436 | self::isFalseWord($value) || |
437 | 437 | self::isNullWord($value) |
438 | 438 | ); |
@@ -483,8 +483,8 @@ discard block |
||
483 | 483 | |
484 | 484 | private function loadWithSource($Source) { |
485 | 485 | if (empty ($Source)) return array(); |
486 | - if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { |
|
487 | - $array = syck_load (implode ("\n", $Source)); |
|
486 | + if ($this->setting_use_syck_is_possible && function_exists('syck_load')) { |
|
487 | + $array = syck_load(implode("\n", $Source)); |
|
488 | 488 | return is_array($array) ? $array : array(); |
489 | 489 | } |
490 | 490 | |
@@ -504,10 +504,10 @@ discard block |
||
504 | 504 | |
505 | 505 | $literalBlockStyle = self::startsLiteralBlock($line); |
506 | 506 | if ($literalBlockStyle) { |
507 | - $line = rtrim ($line, $literalBlockStyle . " \n"); |
|
507 | + $line = rtrim($line, $literalBlockStyle . " \n"); |
|
508 | 508 | $literalBlock = ''; |
509 | - $line .= ' '.$this->LiteralPlaceHolder; |
|
510 | - $literal_block_indent = strlen($Source[$i+1]) - strlen(ltrim($Source[$i+1])); |
|
509 | + $line .= ' ' . $this->LiteralPlaceHolder; |
|
510 | + $literal_block_indent = strlen($Source[$i + 1]) - strlen(ltrim($Source[$i + 1])); |
|
511 | 511 | while (++$i < $cnt && $this->literalBlockContinues($Source[$i], $this->indent)) { |
512 | 512 | $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], $literalBlockStyle, $literal_block_indent); |
513 | 513 | } |
@@ -515,19 +515,19 @@ discard block |
||
515 | 515 | } |
516 | 516 | |
517 | 517 | // Strip out comments |
518 | - if (strpos ($line, '#')) { |
|
519 | - $line = preg_replace('/\s*#([^"\']+)$/','',$line); |
|
518 | + if (strpos($line, '#')) { |
|
519 | + $line = preg_replace('/\s*#([^"\']+)$/', '', $line); |
|
520 | 520 | } |
521 | 521 | |
522 | 522 | while (++$i < $cnt && self::greedilyNeedNextLine($line)) { |
523 | - $line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t"); |
|
523 | + $line = rtrim($line, " \n\t\r") . ' ' . ltrim($Source[$i], " \t"); |
|
524 | 524 | } |
525 | 525 | $i--; |
526 | 526 | |
527 | 527 | $lineArray = $this->_parseLine($line); |
528 | 528 | |
529 | 529 | if ($literalBlockStyle) |
530 | - $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
|
530 | + $lineArray = $this->revertLiteralPlaceHolder($lineArray, $literalBlock); |
|
531 | 531 | |
532 | 532 | $this->addArray($lineArray, $this->indent); |
533 | 533 | |
@@ -540,17 +540,17 @@ discard block |
||
540 | 540 | return $this->result; |
541 | 541 | } |
542 | 542 | |
543 | - private function loadFromSource ($input) { |
|
544 | - if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) |
|
543 | + private function loadFromSource($input) { |
|
544 | + if ( ! empty($input) && strpos($input, "\n") === false && file_exists($input)) |
|
545 | 545 | $input = file_get_contents($input); |
546 | 546 | |
547 | 547 | return $this->loadFromString($input); |
548 | 548 | } |
549 | 549 | |
550 | - private function loadFromString ($input) { |
|
551 | - $lines = explode("\n",$input); |
|
550 | + private function loadFromString($input) { |
|
551 | + $lines = explode("\n", $input); |
|
552 | 552 | foreach ($lines as $k => $_) { |
553 | - $lines[$k] = rtrim ($_, "\r"); |
|
553 | + $lines[$k] = rtrim($_, "\r"); |
|
554 | 554 | } |
555 | 555 | return $lines; |
556 | 556 | } |
@@ -562,16 +562,16 @@ discard block |
||
562 | 562 | * @param string $line A line from the YAML file |
563 | 563 | */ |
564 | 564 | private function _parseLine($line) { |
565 | - if (!$line) return array(); |
|
565 | + if ( ! $line) return array(); |
|
566 | 566 | $line = trim($line); |
567 | - if (!$line) return array(); |
|
567 | + if ( ! $line) return array(); |
|
568 | 568 | |
569 | 569 | $array = array(); |
570 | 570 | |
571 | 571 | $group = $this->nodeContainsGroup($line); |
572 | 572 | if ($group) { |
573 | 573 | $this->addGroup($line, $group); |
574 | - $line = $this->stripGroup ($line, $group); |
|
574 | + $line = $this->stripGroup($line, $group); |
|
575 | 575 | } |
576 | 576 | |
577 | 577 | if ($this->startsMappedSequence($line)) |
@@ -604,7 +604,7 @@ discard block |
||
604 | 604 | |
605 | 605 | $is_quoted = false; |
606 | 606 | do { |
607 | - if (!$value) break; |
|
607 | + if ( ! $value) break; |
|
608 | 608 | if ($first_character != '"' && $first_character != "'") break; |
609 | 609 | if ($last_character != '"' && $last_character != "'") break; |
610 | 610 | $is_quoted = true; |
@@ -613,37 +613,37 @@ discard block |
||
613 | 613 | if ($is_quoted) { |
614 | 614 | $value = str_replace('\n', "\n", $value); |
615 | 615 | if ($first_character == "'") |
616 | - return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\'')); |
|
617 | - return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\\\''=> '\'')); |
|
616 | + return strtr(substr($value, 1, -1), array('\'\'' => '\'', '\\\''=> '\'')); |
|
617 | + return strtr(substr($value, 1, -1), array('\\"' => '"', '\\\''=> '\'')); |
|
618 | 618 | } |
619 | 619 | |
620 | - if (strpos($value, ' #') !== false && !$is_quoted) |
|
621 | - $value = preg_replace('/\s+#(.+)$/','',$value); |
|
620 | + if (strpos($value, ' #') !== false && ! $is_quoted) |
|
621 | + $value = preg_replace('/\s+#(.+)$/', '', $value); |
|
622 | 622 | |
623 | 623 | if ($first_character == '[' && $last_character == ']') { |
624 | 624 | // Take out strings sequences and mappings |
625 | - $innerValue = trim(substr ($value, 1, -1)); |
|
625 | + $innerValue = trim(substr($value, 1, -1)); |
|
626 | 626 | if ($innerValue === '') return array(); |
627 | 627 | $explode = $this->_inlineEscape($innerValue); |
628 | 628 | // Propagate value array |
629 | - $value = array(); |
|
629 | + $value = array(); |
|
630 | 630 | foreach ($explode as $v) { |
631 | 631 | $value[] = $this->_toType($v); |
632 | 632 | } |
633 | 633 | return $value; |
634 | 634 | } |
635 | 635 | |
636 | - if (strpos($value,': ')!==false && $first_character != '{') { |
|
637 | - $array = explode(': ',$value); |
|
636 | + if (strpos($value, ': ') !== false && $first_character != '{') { |
|
637 | + $array = explode(': ', $value); |
|
638 | 638 | $key = trim($array[0]); |
639 | 639 | array_shift($array); |
640 | - $value = trim(implode(': ',$array)); |
|
640 | + $value = trim(implode(': ', $array)); |
|
641 | 641 | $value = $this->_toType($value); |
642 | 642 | return array($key => $value); |
643 | 643 | } |
644 | 644 | |
645 | 645 | if ($first_character == '{' && $last_character == '}') { |
646 | - $innerValue = trim(substr ($value, 1, -1)); |
|
646 | + $innerValue = trim(substr($value, 1, -1)); |
|
647 | 647 | if ($innerValue === '') return array(); |
648 | 648 | // Inline Mapping |
649 | 649 | // Take out strings sequences and mappings |
@@ -653,7 +653,7 @@ discard block |
||
653 | 653 | foreach ($explode as $v) { |
654 | 654 | $SubArr = $this->_toType($v); |
655 | 655 | if (empty($SubArr)) continue; |
656 | - if (is_array ($SubArr)) { |
|
656 | + if (is_array($SubArr)) { |
|
657 | 657 | $array[key($SubArr)] = $SubArr[key($SubArr)]; continue; |
658 | 658 | } |
659 | 659 | $array[] = $SubArr; |
@@ -665,8 +665,8 @@ discard block |
||
665 | 665 | return null; |
666 | 666 | } |
667 | 667 | |
668 | - if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){ |
|
669 | - $intvalue = (int)$value; |
|
668 | + if (is_numeric($value) && preg_match('/^(-|)[1-9]+[0-9]*$/', $value)) { |
|
669 | + $intvalue = (int) $value; |
|
670 | 670 | if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX) |
671 | 671 | $value = $intvalue; |
672 | 672 | return $value; |
@@ -681,8 +681,8 @@ discard block |
||
681 | 681 | |
682 | 682 | if (is_numeric($value)) { |
683 | 683 | if ($value === '0') return 0; |
684 | - if (rtrim ($value, 0) === $value) |
|
685 | - $value = (float)$value; |
|
684 | + if (rtrim($value, 0) === $value) |
|
685 | + $value = (float) $value; |
|
686 | 686 | return $value; |
687 | 687 | } |
688 | 688 | |
@@ -707,17 +707,17 @@ discard block |
||
707 | 707 | |
708 | 708 | // Check for empty strings |
709 | 709 | $regex = '/("")|(\'\')/'; |
710 | - if (preg_match_all($regex,$inline,$strings)) { |
|
710 | + if (preg_match_all($regex, $inline, $strings)) { |
|
711 | 711 | $saved_empties = $strings[0]; |
712 | - $inline = preg_replace($regex,'YAMLEmpty',$inline); |
|
712 | + $inline = preg_replace($regex, 'YAMLEmpty', $inline); |
|
713 | 713 | } |
714 | 714 | unset($regex); |
715 | 715 | |
716 | 716 | // Check for strings |
717 | 717 | $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/'; |
718 | - if (preg_match_all($regex,$inline,$strings)) { |
|
718 | + if (preg_match_all($regex, $inline, $strings)) { |
|
719 | 719 | $saved_strings = $strings[0]; |
720 | - $inline = preg_replace($regex,'YAMLString',$inline); |
|
720 | + $inline = preg_replace($regex, 'YAMLString', $inline); |
|
721 | 721 | } |
722 | 722 | unset($regex); |
723 | 723 | |
@@ -727,33 +727,33 @@ discard block |
||
727 | 727 | do { |
728 | 728 | |
729 | 729 | // Check for sequences |
730 | - while (preg_match('/\[([^{}\[\]]+)\]/U',$inline,$matchseqs)) { |
|
730 | + while (preg_match('/\[([^{}\[\]]+)\]/U', $inline, $matchseqs)) { |
|
731 | 731 | $seqs[] = $matchseqs[0]; |
732 | 732 | $inline = preg_replace('/\[([^{}\[\]]+)\]/U', ('YAMLSeq' . (count($seqs) - 1) . 's'), $inline, 1); |
733 | 733 | } |
734 | 734 | |
735 | 735 | // Check for mappings |
736 | - while (preg_match('/{([^\[\]{}]+)}/U',$inline,$matchmaps)) { |
|
736 | + while (preg_match('/{([^\[\]{}]+)}/U', $inline, $matchmaps)) { |
|
737 | 737 | $maps[] = $matchmaps[0]; |
738 | 738 | $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1); |
739 | 739 | } |
740 | 740 | |
741 | 741 | if ($i++ >= 10) break; |
742 | 742 | |
743 | - } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
|
743 | + } while (strpos($inline, '[') !== false || strpos($inline, '{') !== false); |
|
744 | 744 | |
745 | - $explode = explode(',',$inline); |
|
745 | + $explode = explode(',', $inline); |
|
746 | 746 | $explode = array_map('trim', $explode); |
747 | 747 | $stringi = 0; $i = 0; |
748 | 748 | |
749 | 749 | while (1) { |
750 | 750 | |
751 | 751 | // Re-add the sequences |
752 | - if (!empty($seqs)) { |
|
752 | + if ( ! empty($seqs)) { |
|
753 | 753 | foreach ($explode as $key => $value) { |
754 | - if (strpos($value,'YAMLSeq') !== false) { |
|
754 | + if (strpos($value, 'YAMLSeq') !== false) { |
|
755 | 755 | foreach ($seqs as $seqk => $seq) { |
756 | - $explode[$key] = str_replace(('YAMLSeq'.$seqk.'s'),$seq,$value); |
|
756 | + $explode[$key] = str_replace(('YAMLSeq' . $seqk . 's'), $seq, $value); |
|
757 | 757 | $value = $explode[$key]; |
758 | 758 | } |
759 | 759 | } |
@@ -761,11 +761,11 @@ discard block |
||
761 | 761 | } |
762 | 762 | |
763 | 763 | // Re-add the mappings |
764 | - if (!empty($maps)) { |
|
764 | + if ( ! empty($maps)) { |
|
765 | 765 | foreach ($explode as $key => $value) { |
766 | - if (strpos($value,'YAMLMap') !== false) { |
|
766 | + if (strpos($value, 'YAMLMap') !== false) { |
|
767 | 767 | foreach ($maps as $mapk => $map) { |
768 | - $explode[$key] = str_replace(('YAMLMap'.$mapk.'s'), $map, $value); |
|
768 | + $explode[$key] = str_replace(('YAMLMap' . $mapk . 's'), $map, $value); |
|
769 | 769 | $value = $explode[$key]; |
770 | 770 | } |
771 | 771 | } |
@@ -774,10 +774,10 @@ discard block |
||
774 | 774 | |
775 | 775 | |
776 | 776 | // Re-add the strings |
777 | - if (!empty($saved_strings)) { |
|
777 | + if ( ! empty($saved_strings)) { |
|
778 | 778 | foreach ($explode as $key => $value) { |
779 | - while (strpos($value,'YAMLString') !== false) { |
|
780 | - $explode[$key] = preg_replace('/YAMLString/',$saved_strings[$stringi],$value, 1); |
|
779 | + while (strpos($value, 'YAMLString') !== false) { |
|
780 | + $explode[$key] = preg_replace('/YAMLString/', $saved_strings[$stringi], $value, 1); |
|
781 | 781 | unset($saved_strings[$stringi]); |
782 | 782 | ++$stringi; |
783 | 783 | $value = $explode[$key]; |
@@ -787,9 +787,9 @@ discard block |
||
787 | 787 | |
788 | 788 | |
789 | 789 | // Re-add the empties |
790 | - if (!empty($saved_empties)) { |
|
790 | + if ( ! empty($saved_empties)) { |
|
791 | 791 | foreach ($explode as $key => $value) { |
792 | - while (strpos($value,'YAMLEmpty') !== false) { |
|
792 | + while (strpos($value, 'YAMLEmpty') !== false) { |
|
793 | 793 | $explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1); |
794 | 794 | $value = $explode[$key]; |
795 | 795 | } |
@@ -798,16 +798,16 @@ discard block |
||
798 | 798 | |
799 | 799 | $finished = true; |
800 | 800 | foreach ($explode as $key => $value) { |
801 | - if (strpos($value,'YAMLSeq') !== false) { |
|
801 | + if (strpos($value, 'YAMLSeq') !== false) { |
|
802 | 802 | $finished = false; break; |
803 | 803 | } |
804 | - if (strpos($value,'YAMLMap') !== false) { |
|
804 | + if (strpos($value, 'YAMLMap') !== false) { |
|
805 | 805 | $finished = false; break; |
806 | 806 | } |
807 | - if (strpos($value,'YAMLString') !== false) { |
|
807 | + if (strpos($value, 'YAMLString') !== false) { |
|
808 | 808 | $finished = false; break; |
809 | 809 | } |
810 | - if (strpos($value,'YAMLEmpty') !== false) { |
|
810 | + if (strpos($value, 'YAMLEmpty') !== false) { |
|
811 | 811 | $finished = false; break; |
812 | 812 | } |
813 | 813 | } |
@@ -822,15 +822,15 @@ discard block |
||
822 | 822 | return $explode; |
823 | 823 | } |
824 | 824 | |
825 | - private function literalBlockContinues ($line, $lineIndent) { |
|
826 | - if (!trim($line)) return true; |
|
825 | + private function literalBlockContinues($line, $lineIndent) { |
|
826 | + if ( ! trim($line)) return true; |
|
827 | 827 | if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true; |
828 | 828 | return false; |
829 | 829 | } |
830 | 830 | |
831 | - private function referenceContentsByAlias ($alias) { |
|
831 | + private function referenceContentsByAlias($alias) { |
|
832 | 832 | do { |
833 | - if (!isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; } |
|
833 | + if ( ! isset($this->SavedGroups[$alias])) { echo "Bad group name: $alias."; break; } |
|
834 | 834 | $groupPath = $this->SavedGroups[$alias]; |
835 | 835 | $value = $this->result; |
836 | 836 | foreach ($groupPath as $k) { |
@@ -840,7 +840,7 @@ discard block |
||
840 | 840 | return $value; |
841 | 841 | } |
842 | 842 | |
843 | - private function addArrayInline ($array, $indent) { |
|
843 | + private function addArrayInline($array, $indent) { |
|
844 | 844 | $CommonGroupPath = $this->path; |
845 | 845 | if (empty ($array)) return false; |
846 | 846 | |
@@ -851,22 +851,22 @@ discard block |
||
851 | 851 | return true; |
852 | 852 | } |
853 | 853 | |
854 | - private function addArray ($incoming_data, $incoming_indent) { |
|
854 | + private function addArray($incoming_data, $incoming_indent) { |
|
855 | 855 | |
856 | 856 | // print_r ($incoming_data); |
857 | 857 | |
858 | - if (count ($incoming_data) > 1) |
|
859 | - return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
858 | + if (count($incoming_data) > 1) |
|
859 | + return $this->addArrayInline($incoming_data, $incoming_indent); |
|
860 | 860 | |
861 | - $key = key ($incoming_data); |
|
861 | + $key = key($incoming_data); |
|
862 | 862 | $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
863 | 863 | if ($key === '__!YAMLZero') $key = '0'; |
864 | 864 | |
865 | - if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. |
|
865 | + if ($incoming_indent == 0 && ! $this->_containsGroupAlias && ! $this->_containsGroupAnchor) { // Shortcut for root-level values. |
|
866 | 866 | if ($key || $key === '' || $key === '0') { |
867 | 867 | $this->result[$key] = $value; |
868 | 868 | } else { |
869 | - $this->result[] = $value; end ($this->result); $key = key ($this->result); |
|
869 | + $this->result[] = $value; end($this->result); $key = key($this->result); |
|
870 | 870 | } |
871 | 871 | $this->path[$incoming_indent] = $key; |
872 | 872 | return; |
@@ -889,25 +889,25 @@ discard block |
||
889 | 889 | |
890 | 890 | // Adding string or numeric key to the innermost level or $this->arr. |
891 | 891 | if (is_string($key) && $key == '<<') { |
892 | - if (!is_array ($_arr)) { $_arr = array (); } |
|
892 | + if ( ! is_array($_arr)) { $_arr = array(); } |
|
893 | 893 | |
894 | - $_arr = array_merge ($_arr, $value); |
|
894 | + $_arr = array_merge($_arr, $value); |
|
895 | 895 | } else if ($key || $key === '' || $key === '0') { |
896 | - if (!is_array ($_arr)) |
|
897 | - $_arr = array ($key=>$value); |
|
896 | + if ( ! is_array($_arr)) |
|
897 | + $_arr = array($key=>$value); |
|
898 | 898 | else |
899 | 899 | $_arr[$key] = $value; |
900 | 900 | } else { |
901 | - if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } |
|
902 | - else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
|
901 | + if ( ! is_array($_arr)) { $_arr = array($value); $key = 0; } |
|
902 | + else { $_arr[] = $value; end($_arr); $key = key($_arr); } |
|
903 | 903 | } |
904 | 904 | |
905 | 905 | $reverse_path = array_reverse($this->path); |
906 | - $reverse_history = array_reverse ($history); |
|
906 | + $reverse_history = array_reverse($history); |
|
907 | 907 | $reverse_history[0] = $_arr; |
908 | 908 | $cnt = count($reverse_history) - 1; |
909 | 909 | for ($i = 0; $i < $cnt; $i++) { |
910 | - $reverse_history[$i+1][$reverse_path[$i]] = $reverse_history[$i]; |
|
910 | + $reverse_history[$i + 1][$reverse_path[$i]] = $reverse_history[$i]; |
|
911 | 911 | } |
912 | 912 | $this->result = $reverse_history[$cnt]; |
913 | 913 | |
@@ -915,9 +915,9 @@ discard block |
||
915 | 915 | |
916 | 916 | if ($this->_containsGroupAnchor) { |
917 | 917 | $this->SavedGroups[$this->_containsGroupAnchor] = $this->path; |
918 | - if (is_array ($value)) { |
|
919 | - $k = key ($value); |
|
920 | - if (!is_int ($k)) { |
|
918 | + if (is_array($value)) { |
|
919 | + $k = key($value); |
|
920 | + if ( ! is_int($k)) { |
|
921 | 921 | $this->SavedGroups[$this->_containsGroupAnchor][$incoming_indent + 2] = $k; |
922 | 922 | } |
923 | 923 | } |
@@ -926,70 +926,70 @@ discard block |
||
926 | 926 | |
927 | 927 | } |
928 | 928 | |
929 | - private static function startsLiteralBlock ($line) { |
|
930 | - $lastChar = substr (trim($line), -1); |
|
929 | + private static function startsLiteralBlock($line) { |
|
930 | + $lastChar = substr(trim($line), -1); |
|
931 | 931 | if ($lastChar != '>' && $lastChar != '|') return false; |
932 | 932 | if ($lastChar == '|') return $lastChar; |
933 | 933 | // HTML tags should not be counted as literal blocks. |
934 | - if (preg_match ('#<.*?>$#', $line)) return false; |
|
934 | + if (preg_match('#<.*?>$#', $line)) return false; |
|
935 | 935 | return $lastChar; |
936 | 936 | } |
937 | 937 | |
938 | 938 | private static function greedilyNeedNextLine($line) { |
939 | - $line = trim ($line); |
|
940 | - if (!strlen($line)) return false; |
|
941 | - if (substr ($line, -1, 1) == ']') return false; |
|
939 | + $line = trim($line); |
|
940 | + if ( ! strlen($line)) return false; |
|
941 | + if (substr($line, -1, 1) == ']') return false; |
|
942 | 942 | if ($line[0] == '[') return true; |
943 | - if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true; |
|
943 | + if (preg_match('#^[^:]+?:\s*\[#', $line)) return true; |
|
944 | 944 | return false; |
945 | 945 | } |
946 | 946 | |
947 | - private function addLiteralLine ($literalBlock, $line, $literalBlockStyle, $indent = -1) { |
|
947 | + private function addLiteralLine($literalBlock, $line, $literalBlockStyle, $indent = -1) { |
|
948 | 948 | $line = self::stripIndent($line, $indent); |
949 | 949 | if ($literalBlockStyle !== '|') { |
950 | 950 | $line = self::stripIndent($line); |
951 | 951 | } |
952 | - $line = rtrim ($line, "\r\n\t ") . "\n"; |
|
952 | + $line = rtrim($line, "\r\n\t ") . "\n"; |
|
953 | 953 | if ($literalBlockStyle == '|') { |
954 | 954 | return $literalBlock . $line; |
955 | 955 | } |
956 | 956 | if (strlen($line) == 0) |
957 | 957 | return rtrim($literalBlock, ' ') . "\n"; |
958 | 958 | if ($line == "\n" && $literalBlockStyle == '>') { |
959 | - return rtrim ($literalBlock, " \t") . "\n"; |
|
959 | + return rtrim($literalBlock, " \t") . "\n"; |
|
960 | 960 | } |
961 | 961 | if ($line != "\n") |
962 | - $line = trim ($line, "\r\n ") . " "; |
|
962 | + $line = trim($line, "\r\n ") . " "; |
|
963 | 963 | return $literalBlock . $line; |
964 | 964 | } |
965 | 965 | |
966 | - function revertLiteralPlaceHolder ($lineArray, $literalBlock) { |
|
966 | + function revertLiteralPlaceHolder($lineArray, $literalBlock) { |
|
967 | 967 | foreach ($lineArray as $k => $_) { |
968 | 968 | if (is_array($_)) |
969 | - $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
|
970 | - else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
|
971 | - $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
|
969 | + $lineArray[$k] = $this->revertLiteralPlaceHolder($_, $literalBlock); |
|
970 | + else if (substr($_, -1 * strlen($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
|
971 | + $lineArray[$k] = rtrim($literalBlock, " \r\n"); |
|
972 | 972 | } |
973 | 973 | return $lineArray; |
974 | 974 | } |
975 | 975 | |
976 | - private static function stripIndent ($line, $indent = -1) { |
|
976 | + private static function stripIndent($line, $indent = -1) { |
|
977 | 977 | if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line)); |
978 | - return substr ($line, $indent); |
|
978 | + return substr($line, $indent); |
|
979 | 979 | } |
980 | 980 | |
981 | - private function getParentPathByIndent ($indent) { |
|
981 | + private function getParentPathByIndent($indent) { |
|
982 | 982 | if ($indent == 0) return array(); |
983 | 983 | $linePath = $this->path; |
984 | 984 | do { |
985 | 985 | end($linePath); $lastIndentInParentPath = key($linePath); |
986 | - if ($indent <= $lastIndentInParentPath) array_pop ($linePath); |
|
986 | + if ($indent <= $lastIndentInParentPath) array_pop($linePath); |
|
987 | 987 | } while ($indent <= $lastIndentInParentPath); |
988 | 988 | return $linePath; |
989 | 989 | } |
990 | 990 | |
991 | 991 | |
992 | - private function clearBiggerPathValues ($indent) { |
|
992 | + private function clearBiggerPathValues($indent) { |
|
993 | 993 | |
994 | 994 | |
995 | 995 | if ($indent == 0) $this->path = array(); |
@@ -1003,94 +1003,94 @@ discard block |
||
1003 | 1003 | } |
1004 | 1004 | |
1005 | 1005 | |
1006 | - private static function isComment ($line) { |
|
1007 | - if (!$line) return false; |
|
1006 | + private static function isComment($line) { |
|
1007 | + if ( ! $line) return false; |
|
1008 | 1008 | if ($line[0] == '#') return true; |
1009 | 1009 | if (trim($line, " \r\n\t") == '---') return true; |
1010 | 1010 | return false; |
1011 | 1011 | } |
1012 | 1012 | |
1013 | - private static function isEmpty ($line) { |
|
1014 | - return (trim ($line) === ''); |
|
1013 | + private static function isEmpty($line) { |
|
1014 | + return (trim($line) === ''); |
|
1015 | 1015 | } |
1016 | 1016 | |
1017 | 1017 | |
1018 | - private function isArrayElement ($line) { |
|
1019 | - if (!$line || !is_scalar($line)) return false; |
|
1018 | + private function isArrayElement($line) { |
|
1019 | + if ( ! $line || ! is_scalar($line)) return false; |
|
1020 | 1020 | if (substr($line, 0, 2) != '- ') return false; |
1021 | - if (strlen ($line) > 3) |
|
1022 | - if (substr($line,0,3) == '---') return false; |
|
1021 | + if (strlen($line) > 3) |
|
1022 | + if (substr($line, 0, 3) == '---') return false; |
|
1023 | 1023 | |
1024 | 1024 | return true; |
1025 | 1025 | } |
1026 | 1026 | |
1027 | - private function isHashElement ($line) { |
|
1027 | + private function isHashElement($line) { |
|
1028 | 1028 | return strpos($line, ':'); |
1029 | 1029 | } |
1030 | 1030 | |
1031 | - private function isLiteral ($line) { |
|
1031 | + private function isLiteral($line) { |
|
1032 | 1032 | if ($this->isArrayElement($line)) return false; |
1033 | 1033 | if ($this->isHashElement($line)) return false; |
1034 | 1034 | return true; |
1035 | 1035 | } |
1036 | 1036 | |
1037 | 1037 | |
1038 | - private static function unquote ($value) { |
|
1039 | - if (!$value) return $value; |
|
1040 | - if (!is_string($value)) return $value; |
|
1041 | - if ($value[0] == '\'') return trim ($value, '\''); |
|
1042 | - if ($value[0] == '"') return trim ($value, '"'); |
|
1038 | + private static function unquote($value) { |
|
1039 | + if ( ! $value) return $value; |
|
1040 | + if ( ! is_string($value)) return $value; |
|
1041 | + if ($value[0] == '\'') return trim($value, '\''); |
|
1042 | + if ($value[0] == '"') return trim($value, '"'); |
|
1043 | 1043 | return $value; |
1044 | 1044 | } |
1045 | 1045 | |
1046 | - private function startsMappedSequence ($line) { |
|
1047 | - return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':'); |
|
1046 | + private function startsMappedSequence($line) { |
|
1047 | + return (substr($line, 0, 2) == '- ' && substr($line, -1, 1) == ':'); |
|
1048 | 1048 | } |
1049 | 1049 | |
1050 | - private function returnMappedSequence ($line) { |
|
1050 | + private function returnMappedSequence($line) { |
|
1051 | 1051 | $array = array(); |
1052 | - $key = self::unquote(trim(substr($line,1,-1))); |
|
1052 | + $key = self::unquote(trim(substr($line, 1, -1))); |
|
1053 | 1053 | $array[$key] = array(); |
1054 | - $this->delayedPath = array(strpos ($line, $key) + $this->indent => $key); |
|
1054 | + $this->delayedPath = array(strpos($line, $key) + $this->indent => $key); |
|
1055 | 1055 | return array($array); |
1056 | 1056 | } |
1057 | 1057 | |
1058 | 1058 | private function checkKeysInValue($value) { |
1059 | 1059 | if (strchr('[{"\'', $value[0]) === false) { |
1060 | 1060 | if (strchr($value, ': ') !== false) { |
1061 | - throw new Exception('Too many keys: '.$value); |
|
1061 | + throw new Exception('Too many keys: ' . $value); |
|
1062 | 1062 | } |
1063 | 1063 | } |
1064 | 1064 | } |
1065 | 1065 | |
1066 | - private function returnMappedValue ($line) { |
|
1066 | + private function returnMappedValue($line) { |
|
1067 | 1067 | $this->checkKeysInValue($line); |
1068 | 1068 | $array = array(); |
1069 | - $key = self::unquote (trim(substr($line,0,-1))); |
|
1069 | + $key = self::unquote(trim(substr($line, 0, -1))); |
|
1070 | 1070 | $array[$key] = ''; |
1071 | 1071 | return $array; |
1072 | 1072 | } |
1073 | 1073 | |
1074 | - private function startsMappedValue ($line) { |
|
1075 | - return (substr ($line, -1, 1) == ':'); |
|
1074 | + private function startsMappedValue($line) { |
|
1075 | + return (substr($line, -1, 1) == ':'); |
|
1076 | 1076 | } |
1077 | 1077 | |
1078 | - private function isPlainArray ($line) { |
|
1079 | - return ($line[0] == '[' && substr ($line, -1, 1) == ']'); |
|
1078 | + private function isPlainArray($line) { |
|
1079 | + return ($line[0] == '[' && substr($line, -1, 1) == ']'); |
|
1080 | 1080 | } |
1081 | 1081 | |
1082 | - private function returnPlainArray ($line) { |
|
1082 | + private function returnPlainArray($line) { |
|
1083 | 1083 | return $this->_toType($line); |
1084 | 1084 | } |
1085 | 1085 | |
1086 | - private function returnKeyValuePair ($line) { |
|
1086 | + private function returnKeyValuePair($line) { |
|
1087 | 1087 | $array = array(); |
1088 | 1088 | $key = ''; |
1089 | - if (strpos ($line, ': ')) { |
|
1089 | + if (strpos($line, ': ')) { |
|
1090 | 1090 | // It's a key/value pair most likely |
1091 | 1091 | // If the key is in double quotes pull it out |
1092 | - if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) { |
|
1093 | - $value = trim(str_replace($matches[1],'',$line)); |
|
1092 | + if (($line[0] == '"' || $line[0] == "'") && preg_match('/^(["\'](.*)["\'](\s)*:)/', $line, $matches)) { |
|
1093 | + $value = trim(str_replace($matches[1], '', $line)); |
|
1094 | 1094 | $key = $matches[2]; |
1095 | 1095 | } else { |
1096 | 1096 | // Do some guesswork as to the key and the value |
@@ -1104,17 +1104,17 @@ discard block |
||
1104 | 1104 | if ($key === '0') $key = '__!YAMLZero'; |
1105 | 1105 | $array[$key] = $value; |
1106 | 1106 | } else { |
1107 | - $array = array ($line); |
|
1107 | + $array = array($line); |
|
1108 | 1108 | } |
1109 | 1109 | return $array; |
1110 | 1110 | |
1111 | 1111 | } |
1112 | 1112 | |
1113 | 1113 | |
1114 | - private function returnArrayElement ($line) { |
|
1114 | + private function returnArrayElement($line) { |
|
1115 | 1115 | if (strlen($line) <= 1) return array(array()); // Weird %) |
1116 | 1116 | $array = array(); |
1117 | - $value = trim(substr($line,1)); |
|
1117 | + $value = trim(substr($line, 1)); |
|
1118 | 1118 | $value = $this->_toType($value); |
1119 | 1119 | if ($this->isArrayElement($value)) { |
1120 | 1120 | $value = $this->returnArrayElement($value); |
@@ -1124,25 +1124,25 @@ discard block |
||
1124 | 1124 | } |
1125 | 1125 | |
1126 | 1126 | |
1127 | - private function nodeContainsGroup ($line) { |
|
1127 | + private function nodeContainsGroup($line) { |
|
1128 | 1128 | $symbolsForReference = 'A-z0-9_\-'; |
1129 | 1129 | if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
1130 | - if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1131 | - if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1132 | - if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1]; |
|
1133 | - if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1]; |
|
1134 | - if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; |
|
1130 | + if ($line[0] == '&' && preg_match('/^(&[' . $symbolsForReference . ']+)/', $line, $matches)) return $matches[1]; |
|
1131 | + if ($line[0] == '*' && preg_match('/^(\*[' . $symbolsForReference . ']+)/', $line, $matches)) return $matches[1]; |
|
1132 | + if (preg_match('/(&[' . $symbolsForReference . ']+)$/', $line, $matches)) return $matches[1]; |
|
1133 | + if (preg_match('/(\*[' . $symbolsForReference . ']+$)/', $line, $matches)) return $matches[1]; |
|
1134 | + if (preg_match('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; |
|
1135 | 1135 | return false; |
1136 | 1136 | |
1137 | 1137 | } |
1138 | 1138 | |
1139 | - private function addGroup ($line, $group) { |
|
1140 | - if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1); |
|
1141 | - if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1); |
|
1139 | + private function addGroup($line, $group) { |
|
1140 | + if ($group[0] == '&') $this->_containsGroupAnchor = substr($group, 1); |
|
1141 | + if ($group[0] == '*') $this->_containsGroupAlias = substr($group, 1); |
|
1142 | 1142 | //print_r ($this->path); |
1143 | 1143 | } |
1144 | 1144 | |
1145 | - private function stripGroup ($line, $group) { |
|
1145 | + private function stripGroup($line, $group) { |
|
1146 | 1146 | $line = trim(str_replace($group, '', $line)); |
1147 | 1147 | return $line; |
1148 | 1148 | } |
@@ -1155,7 +1155,7 @@ discard block |
||
1155 | 1155 | do { |
1156 | 1156 | if (PHP_SAPI != 'cli') break; |
1157 | 1157 | if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; |
1158 | - if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) break; |
|
1158 | + if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos($_SERVER['PHP_SELF'], 'Spyc.php')) break; |
|
1159 | 1159 | $file = $argv[1]; |
1160 | - echo json_encode (spyc_load_file ($file)); |
|
1160 | + echo json_encode(spyc_load_file($file)); |
|
1161 | 1161 | } while (0); |
@@ -243,14 +243,18 @@ discard block |
||
243 | 243 | |
244 | 244 | // New YAML document |
245 | 245 | $string = ""; |
246 | - if (!$no_opening_dashes) $string = "---\n"; |
|
246 | + if (!$no_opening_dashes) { |
|
247 | + $string = "---\n"; |
|
248 | + } |
|
247 | 249 | |
248 | 250 | // Start at the base of the array and move through it. |
249 | 251 | if ($array) { |
250 | 252 | $array = (array)$array; |
251 | 253 | $previous_key = -1; |
252 | 254 | foreach ($array as $key => $value) { |
253 | - if (!isset($first_key)) $first_key = $key; |
|
255 | + if (!isset($first_key)) { |
|
256 | + $first_key = $key; |
|
257 | + } |
|
254 | 258 | $string .= $this->_yamlize($key,$value,0,$previous_key, $first_key, $array); |
255 | 259 | $previous_key = $key; |
256 | 260 | } |
@@ -267,10 +271,13 @@ discard block |
||
267 | 271 | * @param $indent The indent of the current node |
268 | 272 | */ |
269 | 273 | private function _yamlize($key,$value,$indent, $previous_key = -1, $first_key = 0, $source_array = null) { |
270 | - if(is_object($value)) $value = (array)$value; |
|
274 | + if(is_object($value)) { |
|
275 | + $value = (array)$value; |
|
276 | + } |
|
271 | 277 | if (is_array($value)) { |
272 | - if (empty ($value)) |
|
273 | - return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
|
278 | + if (empty ($value)) { |
|
279 | + return $this->_dumpNode($key, array(), $indent, $previous_key, $first_key, $source_array); |
|
280 | + } |
|
274 | 281 | // It has children. What to do? |
275 | 282 | // Make it the right kind of item |
276 | 283 | $string = $this->_dumpNode($key, self::REMPTY, $indent, $previous_key, $first_key, $source_array); |
@@ -297,7 +304,9 @@ discard block |
||
297 | 304 | $string = ''; |
298 | 305 | $previous_key = -1; |
299 | 306 | foreach ($array as $key => $value) { |
300 | - if (!isset($first_key)) $first_key = $key; |
|
307 | + if (!isset($first_key)) { |
|
308 | + $first_key = $key; |
|
309 | + } |
|
301 | 310 | $string .= $this->_yamlize($key, $value, $indent, $previous_key, $first_key, $array); |
302 | 311 | $previous_key = $key; |
303 | 312 | } |
@@ -327,20 +336,29 @@ discard block |
||
327 | 336 | $value = $this->_doFolding($value,$indent); |
328 | 337 | } |
329 | 338 | |
330 | - if ($value === array()) $value = '[ ]'; |
|
331 | - if ($value === "") $value = '""'; |
|
339 | + if ($value === array()) { |
|
340 | + $value = '[ ]'; |
|
341 | + } |
|
342 | + if ($value === "") { |
|
343 | + $value = '""'; |
|
344 | + } |
|
332 | 345 | if (self::isTranslationWord($value)) { |
333 | 346 | $value = $this->_doLiteralBlock($value, $indent); |
334 | 347 | } |
335 | - if (trim ($value) != $value) |
|
336 | - $value = $this->_doLiteralBlock($value,$indent); |
|
348 | + if (trim ($value) != $value) { |
|
349 | + $value = $this->_doLiteralBlock($value,$indent); |
|
350 | + } |
|
337 | 351 | |
338 | 352 | if (is_bool($value)) { |
339 | 353 | $value = $value ? "true" : "false"; |
340 | 354 | } |
341 | 355 | |
342 | - if ($value === null) $value = 'null'; |
|
343 | - if ($value === "'" . self::REMPTY . "'") $value = null; |
|
356 | + if ($value === null) { |
|
357 | + $value = 'null'; |
|
358 | + } |
|
359 | + if ($value === "'" . self::REMPTY . "'") { |
|
360 | + $value = null; |
|
361 | + } |
|
344 | 362 | |
345 | 363 | $spaces = str_repeat(' ',$indent); |
346 | 364 | |
@@ -365,7 +383,9 @@ discard block |
||
365 | 383 | * @param $indent int The value of the indent |
366 | 384 | */ |
367 | 385 | private function _doLiteralBlock($value,$indent) { |
368 | - if ($value === "\n") return '\n'; |
|
386 | + if ($value === "\n") { |
|
387 | + return '\n'; |
|
388 | + } |
|
369 | 389 | if (strpos($value, "\n") === false && strpos($value, "'") === false) { |
370 | 390 | return sprintf ("'%s'", $value); |
371 | 391 | } |
@@ -405,10 +425,12 @@ discard block |
||
405 | 425 | $wrapped = wordwrap($value,$this->_dumpWordWrap,"\n$indent"); |
406 | 426 | $value = ">\n".$indent.$wrapped; |
407 | 427 | } else { |
408 | - if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) |
|
409 | - $value = '"' . $value . '"'; |
|
410 | - if (is_numeric($value) && is_string($value)) |
|
411 | - $value = '"' . $value . '"'; |
|
428 | + if ($this->setting_dump_force_quotes && is_string ($value) && $value !== self::REMPTY) { |
|
429 | + $value = '"' . $value . '"'; |
|
430 | + } |
|
431 | + if (is_numeric($value) && is_string($value)) { |
|
432 | + $value = '"' . $value . '"'; |
|
433 | + } |
|
412 | 434 | } |
413 | 435 | |
414 | 436 | |
@@ -482,7 +504,9 @@ discard block |
||
482 | 504 | } |
483 | 505 | |
484 | 506 | private function loadWithSource($Source) { |
485 | - if (empty ($Source)) return array(); |
|
507 | + if (empty ($Source)) { |
|
508 | + return array(); |
|
509 | + } |
|
486 | 510 | if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) { |
487 | 511 | $array = syck_load (implode ("\n", $Source)); |
488 | 512 | return is_array($array) ? $array : array(); |
@@ -498,8 +522,12 @@ discard block |
||
498 | 522 | $this->indent = strlen($line) - strlen(ltrim($line)); |
499 | 523 | $tempPath = $this->getParentPathByIndent($this->indent); |
500 | 524 | $line = self::stripIndent($line, $this->indent); |
501 | - if (self::isComment($line)) continue; |
|
502 | - if (self::isEmpty($line)) continue; |
|
525 | + if (self::isComment($line)) { |
|
526 | + continue; |
|
527 | + } |
|
528 | + if (self::isEmpty($line)) { |
|
529 | + continue; |
|
530 | + } |
|
503 | 531 | $this->path = $tempPath; |
504 | 532 | |
505 | 533 | $literalBlockStyle = self::startsLiteralBlock($line); |
@@ -526,13 +554,15 @@ discard block |
||
526 | 554 | |
527 | 555 | $lineArray = $this->_parseLine($line); |
528 | 556 | |
529 | - if ($literalBlockStyle) |
|
530 | - $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
|
557 | + if ($literalBlockStyle) { |
|
558 | + $lineArray = $this->revertLiteralPlaceHolder ($lineArray, $literalBlock); |
|
559 | + } |
|
531 | 560 | |
532 | 561 | $this->addArray($lineArray, $this->indent); |
533 | 562 | |
534 | - foreach ($this->delayedPath as $indent => $delayedPath) |
|
535 | - $this->path[$indent] = $delayedPath; |
|
563 | + foreach ($this->delayedPath as $indent => $delayedPath) { |
|
564 | + $this->path[$indent] = $delayedPath; |
|
565 | + } |
|
536 | 566 | |
537 | 567 | $this->delayedPath = array(); |
538 | 568 | |
@@ -541,8 +571,9 @@ discard block |
||
541 | 571 | } |
542 | 572 | |
543 | 573 | private function loadFromSource ($input) { |
544 | - if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) |
|
545 | - $input = file_get_contents($input); |
|
574 | + if (!empty($input) && strpos($input, "\n") === false && file_exists($input)) { |
|
575 | + $input = file_get_contents($input); |
|
576 | + } |
|
546 | 577 | |
547 | 578 | return $this->loadFromString($input); |
548 | 579 | } |
@@ -562,9 +593,13 @@ discard block |
||
562 | 593 | * @param string $line A line from the YAML file |
563 | 594 | */ |
564 | 595 | private function _parseLine($line) { |
565 | - if (!$line) return array(); |
|
596 | + if (!$line) { |
|
597 | + return array(); |
|
598 | + } |
|
566 | 599 | $line = trim($line); |
567 | - if (!$line) return array(); |
|
600 | + if (!$line) { |
|
601 | + return array(); |
|
602 | + } |
|
568 | 603 | |
569 | 604 | $array = array(); |
570 | 605 | |
@@ -574,17 +609,21 @@ discard block |
||
574 | 609 | $line = $this->stripGroup ($line, $group); |
575 | 610 | } |
576 | 611 | |
577 | - if ($this->startsMappedSequence($line)) |
|
578 | - return $this->returnMappedSequence($line); |
|
612 | + if ($this->startsMappedSequence($line)) { |
|
613 | + return $this->returnMappedSequence($line); |
|
614 | + } |
|
579 | 615 | |
580 | - if ($this->startsMappedValue($line)) |
|
581 | - return $this->returnMappedValue($line); |
|
616 | + if ($this->startsMappedValue($line)) { |
|
617 | + return $this->returnMappedValue($line); |
|
618 | + } |
|
582 | 619 | |
583 | - if ($this->isArrayElement($line)) |
|
584 | - return $this->returnArrayElement($line); |
|
620 | + if ($this->isArrayElement($line)) { |
|
621 | + return $this->returnArrayElement($line); |
|
622 | + } |
|
585 | 623 | |
586 | - if ($this->isPlainArray($line)) |
|
587 | - return $this->returnPlainArray($line); |
|
624 | + if ($this->isPlainArray($line)) { |
|
625 | + return $this->returnPlainArray($line); |
|
626 | + } |
|
588 | 627 | |
589 | 628 | |
590 | 629 | return $this->returnKeyValuePair($line); |
@@ -598,32 +637,44 @@ discard block |
||
598 | 637 | * @return mixed |
599 | 638 | */ |
600 | 639 | private function _toType($value) { |
601 | - if ($value === '') return ""; |
|
640 | + if ($value === '') { |
|
641 | + return ""; |
|
642 | + } |
|
602 | 643 | $first_character = $value[0]; |
603 | 644 | $last_character = substr($value, -1, 1); |
604 | 645 | |
605 | 646 | $is_quoted = false; |
606 | 647 | do { |
607 | - if (!$value) break; |
|
608 | - if ($first_character != '"' && $first_character != "'") break; |
|
609 | - if ($last_character != '"' && $last_character != "'") break; |
|
648 | + if (!$value) { |
|
649 | + break; |
|
650 | + } |
|
651 | + if ($first_character != '"' && $first_character != "'") { |
|
652 | + break; |
|
653 | + } |
|
654 | + if ($last_character != '"' && $last_character != "'") { |
|
655 | + break; |
|
656 | + } |
|
610 | 657 | $is_quoted = true; |
611 | 658 | } while (0); |
612 | 659 | |
613 | 660 | if ($is_quoted) { |
614 | 661 | $value = str_replace('\n', "\n", $value); |
615 | - if ($first_character == "'") |
|
616 | - return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\'')); |
|
662 | + if ($first_character == "'") { |
|
663 | + return strtr(substr ($value, 1, -1), array ('\'\'' => '\'', '\\\''=> '\'')); |
|
664 | + } |
|
617 | 665 | return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\\\''=> '\'')); |
618 | 666 | } |
619 | 667 | |
620 | - if (strpos($value, ' #') !== false && !$is_quoted) |
|
621 | - $value = preg_replace('/\s+#(.+)$/','',$value); |
|
668 | + if (strpos($value, ' #') !== false && !$is_quoted) { |
|
669 | + $value = preg_replace('/\s+#(.+)$/','',$value); |
|
670 | + } |
|
622 | 671 | |
623 | 672 | if ($first_character == '[' && $last_character == ']') { |
624 | 673 | // Take out strings sequences and mappings |
625 | 674 | $innerValue = trim(substr ($value, 1, -1)); |
626 | - if ($innerValue === '') return array(); |
|
675 | + if ($innerValue === '') { |
|
676 | + return array(); |
|
677 | + } |
|
627 | 678 | $explode = $this->_inlineEscape($innerValue); |
628 | 679 | // Propagate value array |
629 | 680 | $value = array(); |
@@ -644,7 +695,9 @@ discard block |
||
644 | 695 | |
645 | 696 | if ($first_character == '{' && $last_character == '}') { |
646 | 697 | $innerValue = trim(substr ($value, 1, -1)); |
647 | - if ($innerValue === '') return array(); |
|
698 | + if ($innerValue === '') { |
|
699 | + return array(); |
|
700 | + } |
|
648 | 701 | // Inline Mapping |
649 | 702 | // Take out strings sequences and mappings |
650 | 703 | $explode = $this->_inlineEscape($innerValue); |
@@ -652,7 +705,9 @@ discard block |
||
652 | 705 | $array = array(); |
653 | 706 | foreach ($explode as $v) { |
654 | 707 | $SubArr = $this->_toType($v); |
655 | - if (empty($SubArr)) continue; |
|
708 | + if (empty($SubArr)) { |
|
709 | + continue; |
|
710 | + } |
|
656 | 711 | if (is_array ($SubArr)) { |
657 | 712 | $array[key($SubArr)] = $SubArr[key($SubArr)]; continue; |
658 | 713 | } |
@@ -667,8 +722,9 @@ discard block |
||
667 | 722 | |
668 | 723 | if ( is_numeric($value) && preg_match ('/^(-|)[1-9]+[0-9]*$/', $value) ){ |
669 | 724 | $intvalue = (int)$value; |
670 | - if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX) |
|
671 | - $value = $intvalue; |
|
725 | + if ($intvalue != PHP_INT_MAX && $intvalue != ~PHP_INT_MAX) { |
|
726 | + $value = $intvalue; |
|
727 | + } |
|
672 | 728 | return $value; |
673 | 729 | } |
674 | 730 | |
@@ -680,9 +736,12 @@ discard block |
||
680 | 736 | $this->coerceValue($value); |
681 | 737 | |
682 | 738 | if (is_numeric($value)) { |
683 | - if ($value === '0') return 0; |
|
684 | - if (rtrim ($value, 0) === $value) |
|
685 | - $value = (float)$value; |
|
739 | + if ($value === '0') { |
|
740 | + return 0; |
|
741 | + } |
|
742 | + if (rtrim ($value, 0) === $value) { |
|
743 | + $value = (float)$value; |
|
744 | + } |
|
686 | 745 | return $value; |
687 | 746 | } |
688 | 747 | |
@@ -738,7 +797,9 @@ discard block |
||
738 | 797 | $inline = preg_replace('/{([^\[\]{}]+)}/U', ('YAMLMap' . (count($maps) - 1) . 's'), $inline, 1); |
739 | 798 | } |
740 | 799 | |
741 | - if ($i++ >= 10) break; |
|
800 | + if ($i++ >= 10) { |
|
801 | + break; |
|
802 | + } |
|
742 | 803 | |
743 | 804 | } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== false); |
744 | 805 | |
@@ -811,11 +872,15 @@ discard block |
||
811 | 872 | $finished = false; break; |
812 | 873 | } |
813 | 874 | } |
814 | - if ($finished) break; |
|
875 | + if ($finished) { |
|
876 | + break; |
|
877 | + } |
|
815 | 878 | |
816 | 879 | $i++; |
817 | - if ($i > 10) |
|
818 | - break; // Prevent infinite loops. |
|
880 | + if ($i > 10) { |
|
881 | + break; |
|
882 | + } |
|
883 | + // Prevent infinite loops. |
|
819 | 884 | } |
820 | 885 | |
821 | 886 | |
@@ -823,8 +888,12 @@ discard block |
||
823 | 888 | } |
824 | 889 | |
825 | 890 | private function literalBlockContinues ($line, $lineIndent) { |
826 | - if (!trim($line)) return true; |
|
827 | - if (strlen($line) - strlen(ltrim($line)) > $lineIndent) return true; |
|
891 | + if (!trim($line)) { |
|
892 | + return true; |
|
893 | + } |
|
894 | + if (strlen($line) - strlen(ltrim($line)) > $lineIndent) { |
|
895 | + return true; |
|
896 | + } |
|
828 | 897 | return false; |
829 | 898 | } |
830 | 899 | |
@@ -842,7 +911,9 @@ discard block |
||
842 | 911 | |
843 | 912 | private function addArrayInline ($array, $indent) { |
844 | 913 | $CommonGroupPath = $this->path; |
845 | - if (empty ($array)) return false; |
|
914 | + if (empty ($array)) { |
|
915 | + return false; |
|
916 | + } |
|
846 | 917 | |
847 | 918 | foreach ($array as $k => $_) { |
848 | 919 | $this->addArray(array($k => $_), $indent); |
@@ -855,12 +926,15 @@ discard block |
||
855 | 926 | |
856 | 927 | // print_r ($incoming_data); |
857 | 928 | |
858 | - if (count ($incoming_data) > 1) |
|
859 | - return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
929 | + if (count ($incoming_data) > 1) { |
|
930 | + return $this->addArrayInline ($incoming_data, $incoming_indent); |
|
931 | + } |
|
860 | 932 | |
861 | 933 | $key = key ($incoming_data); |
862 | 934 | $value = isset($incoming_data[$key]) ? $incoming_data[$key] : null; |
863 | - if ($key === '__!YAMLZero') $key = '0'; |
|
935 | + if ($key === '__!YAMLZero') { |
|
936 | + $key = '0'; |
|
937 | + } |
|
864 | 938 | |
865 | 939 | if ($incoming_indent == 0 && !$this->_containsGroupAlias && !$this->_containsGroupAnchor) { // Shortcut for root-level values. |
866 | 940 | if ($key || $key === '' || $key === '0') { |
@@ -893,13 +967,13 @@ discard block |
||
893 | 967 | |
894 | 968 | $_arr = array_merge ($_arr, $value); |
895 | 969 | } else if ($key || $key === '' || $key === '0') { |
896 | - if (!is_array ($_arr)) |
|
897 | - $_arr = array ($key=>$value); |
|
898 | - else |
|
899 | - $_arr[$key] = $value; |
|
970 | + if (!is_array ($_arr)) { |
|
971 | + $_arr = array ($key=>$value); |
|
972 | + } else { |
|
973 | + $_arr[$key] = $value; |
|
974 | + } |
|
900 | 975 | } else { |
901 | - if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } |
|
902 | - else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
|
976 | + if (!is_array ($_arr)) { $_arr = array ($value); $key = 0; } else { $_arr[] = $value; end ($_arr); $key = key ($_arr); } |
|
903 | 977 | } |
904 | 978 | |
905 | 979 | $reverse_path = array_reverse($this->path); |
@@ -928,19 +1002,33 @@ discard block |
||
928 | 1002 | |
929 | 1003 | private static function startsLiteralBlock ($line) { |
930 | 1004 | $lastChar = substr (trim($line), -1); |
931 | - if ($lastChar != '>' && $lastChar != '|') return false; |
|
932 | - if ($lastChar == '|') return $lastChar; |
|
1005 | + if ($lastChar != '>' && $lastChar != '|') { |
|
1006 | + return false; |
|
1007 | + } |
|
1008 | + if ($lastChar == '|') { |
|
1009 | + return $lastChar; |
|
1010 | + } |
|
933 | 1011 | // HTML tags should not be counted as literal blocks. |
934 | - if (preg_match ('#<.*?>$#', $line)) return false; |
|
1012 | + if (preg_match ('#<.*?>$#', $line)) { |
|
1013 | + return false; |
|
1014 | + } |
|
935 | 1015 | return $lastChar; |
936 | 1016 | } |
937 | 1017 | |
938 | 1018 | private static function greedilyNeedNextLine($line) { |
939 | 1019 | $line = trim ($line); |
940 | - if (!strlen($line)) return false; |
|
941 | - if (substr ($line, -1, 1) == ']') return false; |
|
942 | - if ($line[0] == '[') return true; |
|
943 | - if (preg_match ('#^[^:]+?:\s*\[#', $line)) return true; |
|
1020 | + if (!strlen($line)) { |
|
1021 | + return false; |
|
1022 | + } |
|
1023 | + if (substr ($line, -1, 1) == ']') { |
|
1024 | + return false; |
|
1025 | + } |
|
1026 | + if ($line[0] == '[') { |
|
1027 | + return true; |
|
1028 | + } |
|
1029 | + if (preg_match ('#^[^:]+?:\s*\[#', $line)) { |
|
1030 | + return true; |
|
1031 | + } |
|
944 | 1032 | return false; |
945 | 1033 | } |
946 | 1034 | |
@@ -953,37 +1041,46 @@ discard block |
||
953 | 1041 | if ($literalBlockStyle == '|') { |
954 | 1042 | return $literalBlock . $line; |
955 | 1043 | } |
956 | - if (strlen($line) == 0) |
|
957 | - return rtrim($literalBlock, ' ') . "\n"; |
|
1044 | + if (strlen($line) == 0) { |
|
1045 | + return rtrim($literalBlock, ' ') . "\n"; |
|
1046 | + } |
|
958 | 1047 | if ($line == "\n" && $literalBlockStyle == '>') { |
959 | 1048 | return rtrim ($literalBlock, " \t") . "\n"; |
960 | 1049 | } |
961 | - if ($line != "\n") |
|
962 | - $line = trim ($line, "\r\n ") . " "; |
|
1050 | + if ($line != "\n") { |
|
1051 | + $line = trim ($line, "\r\n ") . " "; |
|
1052 | + } |
|
963 | 1053 | return $literalBlock . $line; |
964 | 1054 | } |
965 | 1055 | |
966 | 1056 | function revertLiteralPlaceHolder ($lineArray, $literalBlock) { |
967 | 1057 | foreach ($lineArray as $k => $_) { |
968 | - if (is_array($_)) |
|
969 | - $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
|
970 | - else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) |
|
971 | - $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
|
1058 | + if (is_array($_)) { |
|
1059 | + $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock); |
|
1060 | + } else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == $this->LiteralPlaceHolder) { |
|
1061 | + $lineArray[$k] = rtrim ($literalBlock, " \r\n"); |
|
1062 | + } |
|
972 | 1063 | } |
973 | 1064 | return $lineArray; |
974 | 1065 | } |
975 | 1066 | |
976 | 1067 | private static function stripIndent ($line, $indent = -1) { |
977 | - if ($indent == -1) $indent = strlen($line) - strlen(ltrim($line)); |
|
1068 | + if ($indent == -1) { |
|
1069 | + $indent = strlen($line) - strlen(ltrim($line)); |
|
1070 | + } |
|
978 | 1071 | return substr ($line, $indent); |
979 | 1072 | } |
980 | 1073 | |
981 | 1074 | private function getParentPathByIndent ($indent) { |
982 | - if ($indent == 0) return array(); |
|
1075 | + if ($indent == 0) { |
|
1076 | + return array(); |
|
1077 | + } |
|
983 | 1078 | $linePath = $this->path; |
984 | 1079 | do { |
985 | 1080 | end($linePath); $lastIndentInParentPath = key($linePath); |
986 | - if ($indent <= $lastIndentInParentPath) array_pop ($linePath); |
|
1081 | + if ($indent <= $lastIndentInParentPath) { |
|
1082 | + array_pop ($linePath); |
|
1083 | + } |
|
987 | 1084 | } while ($indent <= $lastIndentInParentPath); |
988 | 1085 | return $linePath; |
989 | 1086 | } |
@@ -992,11 +1089,17 @@ discard block |
||
992 | 1089 | private function clearBiggerPathValues ($indent) { |
993 | 1090 | |
994 | 1091 | |
995 | - if ($indent == 0) $this->path = array(); |
|
996 | - if (empty ($this->path)) return true; |
|
1092 | + if ($indent == 0) { |
|
1093 | + $this->path = array(); |
|
1094 | + } |
|
1095 | + if (empty ($this->path)) { |
|
1096 | + return true; |
|
1097 | + } |
|
997 | 1098 | |
998 | 1099 | foreach ($this->path as $k => $_) { |
999 | - if ($k > $indent) unset ($this->path[$k]); |
|
1100 | + if ($k > $indent) { |
|
1101 | + unset ($this->path[$k]); |
|
1102 | + } |
|
1000 | 1103 | } |
1001 | 1104 | |
1002 | 1105 | return true; |
@@ -1004,9 +1107,15 @@ discard block |
||
1004 | 1107 | |
1005 | 1108 | |
1006 | 1109 | private static function isComment ($line) { |
1007 | - if (!$line) return false; |
|
1008 | - if ($line[0] == '#') return true; |
|
1009 | - if (trim($line, " \r\n\t") == '---') return true; |
|
1110 | + if (!$line) { |
|
1111 | + return false; |
|
1112 | + } |
|
1113 | + if ($line[0] == '#') { |
|
1114 | + return true; |
|
1115 | + } |
|
1116 | + if (trim($line, " \r\n\t") == '---') { |
|
1117 | + return true; |
|
1118 | + } |
|
1010 | 1119 | return false; |
1011 | 1120 | } |
1012 | 1121 | |
@@ -1016,10 +1125,15 @@ discard block |
||
1016 | 1125 | |
1017 | 1126 | |
1018 | 1127 | private function isArrayElement ($line) { |
1019 | - if (!$line || !is_scalar($line)) return false; |
|
1020 | - if (substr($line, 0, 2) != '- ') return false; |
|
1021 | - if (strlen ($line) > 3) |
|
1022 | - if (substr($line,0,3) == '---') return false; |
|
1128 | + if (!$line || !is_scalar($line)) { |
|
1129 | + return false; |
|
1130 | + } |
|
1131 | + if (substr($line, 0, 2) != '- ') { |
|
1132 | + return false; |
|
1133 | + } |
|
1134 | + if (strlen ($line) > 3) { |
|
1135 | + if (substr($line,0,3) == '---') return false; |
|
1136 | + } |
|
1023 | 1137 | |
1024 | 1138 | return true; |
1025 | 1139 | } |
@@ -1029,17 +1143,29 @@ discard block |
||
1029 | 1143 | } |
1030 | 1144 | |
1031 | 1145 | private function isLiteral ($line) { |
1032 | - if ($this->isArrayElement($line)) return false; |
|
1033 | - if ($this->isHashElement($line)) return false; |
|
1146 | + if ($this->isArrayElement($line)) { |
|
1147 | + return false; |
|
1148 | + } |
|
1149 | + if ($this->isHashElement($line)) { |
|
1150 | + return false; |
|
1151 | + } |
|
1034 | 1152 | return true; |
1035 | 1153 | } |
1036 | 1154 | |
1037 | 1155 | |
1038 | 1156 | private static function unquote ($value) { |
1039 | - if (!$value) return $value; |
|
1040 | - if (!is_string($value)) return $value; |
|
1041 | - if ($value[0] == '\'') return trim ($value, '\''); |
|
1042 | - if ($value[0] == '"') return trim ($value, '"'); |
|
1157 | + if (!$value) { |
|
1158 | + return $value; |
|
1159 | + } |
|
1160 | + if (!is_string($value)) { |
|
1161 | + return $value; |
|
1162 | + } |
|
1163 | + if ($value[0] == '\'') { |
|
1164 | + return trim ($value, '\''); |
|
1165 | + } |
|
1166 | + if ($value[0] == '"') { |
|
1167 | + return trim ($value, '"'); |
|
1168 | + } |
|
1043 | 1169 | return $value; |
1044 | 1170 | } |
1045 | 1171 | |
@@ -1101,7 +1227,9 @@ discard block |
||
1101 | 1227 | } |
1102 | 1228 | // Set the type of the value. Int, string, etc |
1103 | 1229 | $value = $this->_toType($value); |
1104 | - if ($key === '0') $key = '__!YAMLZero'; |
|
1230 | + if ($key === '0') { |
|
1231 | + $key = '__!YAMLZero'; |
|
1232 | + } |
|
1105 | 1233 | $array[$key] = $value; |
1106 | 1234 | } else { |
1107 | 1235 | $array = array ($line); |
@@ -1112,7 +1240,10 @@ discard block |
||
1112 | 1240 | |
1113 | 1241 | |
1114 | 1242 | private function returnArrayElement ($line) { |
1115 | - if (strlen($line) <= 1) return array(array()); // Weird %) |
|
1243 | + if (strlen($line) <= 1) { |
|
1244 | + return array(array()); |
|
1245 | + } |
|
1246 | + // Weird %) |
|
1116 | 1247 | $array = array(); |
1117 | 1248 | $value = trim(substr($line,1)); |
1118 | 1249 | $value = $this->_toType($value); |
@@ -1126,19 +1257,36 @@ discard block |
||
1126 | 1257 | |
1127 | 1258 | private function nodeContainsGroup ($line) { |
1128 | 1259 | $symbolsForReference = 'A-z0-9_\-'; |
1129 | - if (strpos($line, '&') === false && strpos($line, '*') === false) return false; // Please die fast ;-) |
|
1130 | - if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1131 | - if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) return $matches[1]; |
|
1132 | - if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) return $matches[1]; |
|
1133 | - if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) return $matches[1]; |
|
1134 | - if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) return $matches[1]; |
|
1260 | + if (strpos($line, '&') === false && strpos($line, '*') === false) { |
|
1261 | + return false; |
|
1262 | + } |
|
1263 | + // Please die fast ;-) |
|
1264 | + if ($line[0] == '&' && preg_match('/^(&['.$symbolsForReference.']+)/', $line, $matches)) { |
|
1265 | + return $matches[1]; |
|
1266 | + } |
|
1267 | + if ($line[0] == '*' && preg_match('/^(\*['.$symbolsForReference.']+)/', $line, $matches)) { |
|
1268 | + return $matches[1]; |
|
1269 | + } |
|
1270 | + if (preg_match('/(&['.$symbolsForReference.']+)$/', $line, $matches)) { |
|
1271 | + return $matches[1]; |
|
1272 | + } |
|
1273 | + if (preg_match('/(\*['.$symbolsForReference.']+$)/', $line, $matches)) { |
|
1274 | + return $matches[1]; |
|
1275 | + } |
|
1276 | + if (preg_match ('#^\s*<<\s*:\s*(\*[^\s]+).*$#', $line, $matches)) { |
|
1277 | + return $matches[1]; |
|
1278 | + } |
|
1135 | 1279 | return false; |
1136 | 1280 | |
1137 | 1281 | } |
1138 | 1282 | |
1139 | 1283 | private function addGroup ($line, $group) { |
1140 | - if ($group[0] == '&') $this->_containsGroupAnchor = substr ($group, 1); |
|
1141 | - if ($group[0] == '*') $this->_containsGroupAlias = substr ($group, 1); |
|
1284 | + if ($group[0] == '&') { |
|
1285 | + $this->_containsGroupAnchor = substr ($group, 1); |
|
1286 | + } |
|
1287 | + if ($group[0] == '*') { |
|
1288 | + $this->_containsGroupAlias = substr ($group, 1); |
|
1289 | + } |
|
1142 | 1290 | //print_r ($this->path); |
1143 | 1291 | } |
1144 | 1292 | |
@@ -1153,9 +1301,15 @@ discard block |
||
1153 | 1301 | // The syntax is the following: php Spyc.php spyc.yaml |
1154 | 1302 | |
1155 | 1303 | do { |
1156 | - if (PHP_SAPI != 'cli') break; |
|
1157 | - if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break; |
|
1158 | - if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) break; |
|
1304 | + if (PHP_SAPI != 'cli') { |
|
1305 | + break; |
|
1306 | + } |
|
1307 | + if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) { |
|
1308 | + break; |
|
1309 | + } |
|
1310 | + if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 'Spyc.php') ) { |
|
1311 | + break; |
|
1312 | + } |
|
1159 | 1313 | $file = $argv[1]; |
1160 | 1314 | echo json_encode (spyc_load_file ($file)); |
1161 | 1315 | } while (0); |
@@ -36,8 +36,8 @@ discard block |
||
36 | 36 | */ |
37 | 37 | public function __construct() { |
38 | 38 | // Clear out previously saved information. |
39 | - if ( get_option( '_wogh_api_cache' ) ) { |
|
40 | - delete_option( '_wogh_api_cache' ); |
|
39 | + if (get_option('_wogh_api_cache')) { |
|
40 | + delete_option('_wogh_api_cache'); |
|
41 | 41 | } |
42 | 42 | } |
43 | 43 | |
@@ -48,10 +48,10 @@ discard block |
||
48 | 48 | * |
49 | 49 | * @return false|Writing_On_GitHub_Blob |
50 | 50 | */ |
51 | - public function fetch_blob( $sha ) { |
|
52 | - $blob = $this->get( 'blobs', $sha ); |
|
51 | + public function fetch_blob($sha) { |
|
52 | + $blob = $this->get('blobs', $sha); |
|
53 | 53 | |
54 | - if ( $blob instanceof Writing_On_GitHub_Blob ) { |
|
54 | + if ($blob instanceof Writing_On_GitHub_Blob) { |
|
55 | 55 | return $blob; |
56 | 56 | } |
57 | 57 | |
@@ -66,8 +66,8 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @return Writing_On_GitHub_Blob |
68 | 68 | */ |
69 | - public function set_blob( $sha, Writing_On_GitHub_Blob $blob ) { |
|
70 | - return $this->save( 'blobs', $sha, $blob, 3 * DAY_IN_SECONDS ); |
|
69 | + public function set_blob($sha, Writing_On_GitHub_Blob $blob) { |
|
70 | + return $this->save('blobs', $sha, $blob, 3 * DAY_IN_SECONDS); |
|
71 | 71 | } |
72 | 72 | |
73 | 73 | /** |
@@ -78,13 +78,13 @@ discard block |
||
78 | 78 | * |
79 | 79 | * @return stdClass|false response object if cached, false if not |
80 | 80 | */ |
81 | - protected function get( $type, $sha ) { |
|
82 | - if ( isset( $this->{$type}[ $sha ] ) ) { |
|
83 | - return $this->{$type}[ $sha ]; |
|
81 | + protected function get($type, $sha) { |
|
82 | + if (isset($this->{$type}[$sha])) { |
|
83 | + return $this->{$type}[$sha]; |
|
84 | 84 | } |
85 | 85 | |
86 | - if ( $data = get_transient( $this->cache_id( $type, $sha ) ) ) { |
|
87 | - return $this->{$type}[ $sha ] = $data; |
|
86 | + if ($data = get_transient($this->cache_id($type, $sha))) { |
|
87 | + return $this->{$type}[$sha] = $data; |
|
88 | 88 | } |
89 | 89 | |
90 | 90 | return false; |
@@ -100,10 +100,10 @@ discard block |
||
100 | 100 | * |
101 | 101 | * @return mixed |
102 | 102 | */ |
103 | - protected function save( $type, $sha, $data, $time ) { |
|
104 | - $this->{$type}[ $sha ] = $data; |
|
103 | + protected function save($type, $sha, $data, $time) { |
|
104 | + $this->{$type}[$sha] = $data; |
|
105 | 105 | |
106 | - set_transient( $this->cache_id( $type, $sha ), $data, $time ); |
|
106 | + set_transient($this->cache_id($type, $sha), $data, $time); |
|
107 | 107 | |
108 | 108 | return $data; |
109 | 109 | } |
@@ -116,7 +116,7 @@ discard block |
||
116 | 116 | * |
117 | 117 | * @return string |
118 | 118 | */ |
119 | - protected function cache_id( $type, $sha ) { |
|
120 | - return 'wogh_' . md5( $type . '_' . $sha ); |
|
119 | + protected function cache_id($type, $sha) { |
|
120 | + return 'wogh_' . md5($type . '_' . $sha); |
|
121 | 121 | } |
122 | 122 | } |