|
| 1 | +<?php namespace Neomerx\Samples\JsonApi\Application; |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright 2015 [email protected] (www.neomerx.com) |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +use \Closure; |
| 20 | +use \Neomerx\JsonApi\Schema\Link; |
| 21 | +use \Neomerx\JsonApi\Encoder\Encoder; |
| 22 | +use \Neomerx\Samples\JsonApi\Models\Post; |
| 23 | +use \Neomerx\Samples\JsonApi\Models\Site; |
| 24 | +use \Neomerx\Samples\JsonApi\Models\Author; |
| 25 | +use \Neomerx\Samples\JsonApi\Models\Comment; |
| 26 | +use \Neomerx\JsonApi\Encoder\EncoderOptions; |
| 27 | +use \Neomerx\Samples\JsonApi\Schemas\PostSchema; |
| 28 | +use \Neomerx\Samples\JsonApi\Schemas\SiteSchema; |
| 29 | +use \Neomerx\Samples\JsonApi\Schemas\AuthorSchema; |
| 30 | +use \Neomerx\JsonApi\Parameters\EncodingParameters; |
| 31 | +use \Neomerx\Samples\JsonApi\Schemas\CommentSchema; |
| 32 | + |
| 33 | +/** |
| 34 | + * @package Neomerx\Samples\JsonApi |
| 35 | + */ |
| 36 | +class EncodeSamples |
| 37 | +{ |
| 38 | + /** |
| 39 | + * Get basic usage. |
| 40 | + * |
| 41 | + * @return string |
| 42 | + */ |
| 43 | + public function getBasicExample() |
| 44 | + { |
| 45 | + $author = Author::instance('123', 'John', 'Dow'); |
| 46 | + |
| 47 | + $encoder = Encoder::instance([ |
| 48 | + Author::class => AuthorSchema::class, |
| 49 | + ], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com/api/v1')); |
| 50 | + |
| 51 | + $result = $encoder->encodeData($author); |
| 52 | + |
| 53 | + return $result; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Get how objects are put to 'included'. |
| 58 | + * |
| 59 | + * @return string |
| 60 | + */ |
| 61 | + public function getIncludedObjectsExample() |
| 62 | + { |
| 63 | + $author = Author::instance('123', 'John', 'Dow'); |
| 64 | + $comments = [ |
| 65 | + Comment::instance('456', 'Included objects work as easy as basic ones', $author), |
| 66 | + Comment::instance('789', 'Let\'s try!', $author), |
| 67 | + ]; |
| 68 | + $post = Post::instance('321', 'Included objects', 'Yes, it is supported', $author, $comments); |
| 69 | + $site = Site::instance('1', 'JSON API Samples', [$post]); |
| 70 | + |
| 71 | + $encoder = Encoder::instance([ |
| 72 | + Author::class => AuthorSchema::class, |
| 73 | + Comment::class => CommentSchema::class, |
| 74 | + Post::class => PostSchema::class, |
| 75 | + Site::class => SiteSchema::class |
| 76 | + ], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com')); |
| 77 | + |
| 78 | + $result = $encoder->encodeData($site); |
| 79 | + |
| 80 | + return $result; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get sparse and field set filters. |
| 85 | + * |
| 86 | + * @return string |
| 87 | + */ |
| 88 | + public function getSparseAndFieldSetsExample() |
| 89 | + { |
| 90 | + $author = Author::instance('123', 'John', 'Dow'); |
| 91 | + $comments = [ |
| 92 | + Comment::instance('456', 'Included objects work as easy as basic ones', $author), |
| 93 | + Comment::instance('789', 'Let\'s try!', $author), |
| 94 | + ]; |
| 95 | + $post = Post::instance('321', 'Included objects', 'Yes, it is supported', $author, $comments); |
| 96 | + $site = Site::instance('1', 'JSON API Samples', [$post]); |
| 97 | + |
| 98 | + $options = new EncodingParameters([ |
| 99 | + // Paths to be included. Note 'posts.comments' will not be shown. |
| 100 | + 'posts.author' |
| 101 | + ], [ |
| 102 | + // Attributes and relationships that should be shown |
| 103 | + 'sites' => ['name', 'posts'], |
| 104 | + 'posts' => ['author'], |
| 105 | + 'people' => ['first_name'], |
| 106 | + ]); |
| 107 | + |
| 108 | + SiteSchema::$isShowCustomLinks = false; |
| 109 | + $encoder = Encoder::instance([ |
| 110 | + Author::class => AuthorSchema::class, |
| 111 | + Comment::class => CommentSchema::class, |
| 112 | + Post::class => PostSchema::class, |
| 113 | + Site::class => SiteSchema::class |
| 114 | + ], new EncoderOptions(JSON_PRETTY_PRINT)); |
| 115 | + |
| 116 | + $result = $encoder->encodeData($site, $options); |
| 117 | + |
| 118 | + return $result; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Get sparse and field set filters. |
| 123 | + * |
| 124 | + * @return string |
| 125 | + */ |
| 126 | + public function getTopLevelMetaAndLinksExample() |
| 127 | + { |
| 128 | + $author = Author::instance('123', 'John', 'Dow'); |
| 129 | + $meta = [ |
| 130 | + "copyright" => "Copyright 2015 Example Corp.", |
| 131 | + "authors" => [ |
| 132 | + "Yehuda Katz", |
| 133 | + "Steve Klabnik", |
| 134 | + "Dan Gebhardt" |
| 135 | + ] |
| 136 | + ]; |
| 137 | + $links = [ |
| 138 | + Link::FIRST => new Link('http://example.com/people?first', null, true), |
| 139 | + Link::LAST => new Link('http://example.com/people?last', null, true), |
| 140 | + Link::PREV => new Link('http://example.com/people?prev', null, true), |
| 141 | + Link::NEXT => new Link('http://example.com/people?next', null, true), |
| 142 | + ]; |
| 143 | + |
| 144 | + $encoder = Encoder::instance([ |
| 145 | + Author::class => AuthorSchema::class, |
| 146 | + Comment::class => CommentSchema::class, |
| 147 | + Post::class => PostSchema::class, |
| 148 | + Site::class => SiteSchema::class |
| 149 | + ], new EncoderOptions(JSON_PRETTY_PRINT, 'http://example.com')); |
| 150 | + |
| 151 | + $result = $encoder->withLinks($links)->withMeta($meta)->encodeData($author); |
| 152 | + |
| 153 | + return $result; |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Get how schema could change dynamically. |
| 158 | + * |
| 159 | + * @return array |
| 160 | + */ |
| 161 | + public function getDynamicSchemaExample() |
| 162 | + { |
| 163 | + $site = Site::instance('1', 'JSON API Samples', []); |
| 164 | + |
| 165 | + $encoder = Encoder::instance([ |
| 166 | + Site::class => SiteSchema::class, |
| 167 | + ], new EncoderOptions(JSON_PRETTY_PRINT)); |
| 168 | + |
| 169 | + SiteSchema::$isShowCustomLinks = false; |
| 170 | + $noLinksResult = $encoder->encodeData($site); |
| 171 | + |
| 172 | + SiteSchema::$isShowCustomLinks = true; |
| 173 | + $withLinksResult = $encoder->encodeData($site); |
| 174 | + |
| 175 | + return [ |
| 176 | + $noLinksResult, |
| 177 | + $withLinksResult, |
| 178 | + ]; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Run performance test for many times for small nested resources. |
| 183 | + * |
| 184 | + * @param int $iterations |
| 185 | + * |
| 186 | + * @return mixed |
| 187 | + */ |
| 188 | + public function runPerformanceTestForSmallNestedResources($iterations) |
| 189 | + { |
| 190 | + $closure = function () use ($iterations) { |
| 191 | + $options = new EncodingParameters( |
| 192 | + ['posts.author'], |
| 193 | + ['sites' => ['name'], 'people' => ['first_name']] |
| 194 | + ); |
| 195 | + $encoder = Encoder::instance([ |
| 196 | + Author::class => AuthorSchema::class, |
| 197 | + Comment::class => CommentSchema::class, |
| 198 | + Post::class => PostSchema::class, |
| 199 | + Site::class => SiteSchema::class |
| 200 | + ]); |
| 201 | + |
| 202 | + for ($index = 0; $index < $iterations; ++$index) { |
| 203 | + $rand = rand(); |
| 204 | + |
| 205 | + $author = Author::instance('123', 'John' . $rand, 'Dow' . $rand); |
| 206 | + $comments = [ |
| 207 | + Comment::instance('456', 'Included objects work as easy as basic ones' . $rand, $author), |
| 208 | + Comment::instance('789', 'Let\'s try!' . $rand, $author), |
| 209 | + ]; |
| 210 | + $post = Post::instance('321', 'Included objects' . $rand, 'Yes, it is supported', $author, $comments); |
| 211 | + $site = Site::instance('1', 'JSON API Samples' . $rand, [$post]); |
| 212 | + |
| 213 | + $encoder |
| 214 | + ->withLinks([Link::SELF => new Link('http://example.com/sites/1?' . $rand, null, true)]) |
| 215 | + ->withMeta(['some' => ['meta' => 'information' . $rand]]) |
| 216 | + ->encodeData($site, $options); |
| 217 | + } |
| 218 | + }; |
| 219 | + |
| 220 | + $timeSpent = null; |
| 221 | + $this->getTime($closure, $timeSpent); |
| 222 | + |
| 223 | + return $timeSpent; |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Run performance test one time for big collection of resources. |
| 228 | + * |
| 229 | + * @param int $numberOfItems |
| 230 | + * |
| 231 | + * @return mixed |
| 232 | + */ |
| 233 | + public function runPerformanceTestForBigCollection($numberOfItems) |
| 234 | + { |
| 235 | + $closure = function() use ($numberOfItems) { |
| 236 | + $sites = []; |
| 237 | + for ($index = 0; $index < $numberOfItems; ++$index) { |
| 238 | + $rand = rand(); |
| 239 | + |
| 240 | + $author = Author::instance('123', 'John' . $rand, 'Dow' . $rand); |
| 241 | + $comments = [ |
| 242 | + Comment::instance('456', 'Included objects work as easy as basic ones' . $rand, $author), |
| 243 | + Comment::instance('789', 'Let\'s try!' . $rand, $author), |
| 244 | + ]; |
| 245 | + $post = Post::instance('321', 'Included objects' . $rand, 'Yes, it is supported', $author, $comments); |
| 246 | + $site = Site::instance('1', 'JSON API Samples' . $rand, [$post]); |
| 247 | + |
| 248 | + $sites[] = $site; |
| 249 | + } |
| 250 | + |
| 251 | + $options = new EncodingParameters( |
| 252 | + ['posts.author', 'posts.comments'], |
| 253 | + ['sites' => ['name'], 'people' => ['first_name']] |
| 254 | + ); |
| 255 | + $encoder = Encoder::instance([ |
| 256 | + Author::class => AuthorSchema::class, |
| 257 | + Comment::class => CommentSchema::class, |
| 258 | + Post::class => PostSchema::class, |
| 259 | + Site::class => SiteSchema::class |
| 260 | + ]); |
| 261 | + |
| 262 | + $encoder->encodeData($sites, $options); |
| 263 | + }; |
| 264 | + |
| 265 | + $timeSpent = null; |
| 266 | + $this->getTime($closure, $timeSpent); |
| 267 | + |
| 268 | + return $timeSpent; |
| 269 | + } |
| 270 | + |
| 271 | + /** |
| 272 | + * @param Closure $closure |
| 273 | + * @param float &$time |
| 274 | + * |
| 275 | + * @return mixed |
| 276 | + */ |
| 277 | + private function getTime(Closure $closure, &$time) |
| 278 | + { |
| 279 | + $time_start = microtime(true); |
| 280 | + try { |
| 281 | + return $closure(); |
| 282 | + } finally { |
| 283 | + $time_end = microtime(true); |
| 284 | + $time = $time_end - $time_start; |
| 285 | + } |
| 286 | + } |
| 287 | +} |
0 commit comments