Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 23 | abstract class Document implements DocumentInterface, \JsonSerializable { |
||
| 24 | use AtMemberManager, HttpStatusCodeManager, LinksManager; |
||
| 25 | |||
| 26 | const JSONAPI_VERSION_1_0 = '1.0'; |
||
| 27 | const JSONAPI_VERSION_1_1 = '1.1'; |
||
| 28 | const JSONAPI_VERSION_LATEST = Document::JSONAPI_VERSION_1_1; |
||
| 29 | |||
| 30 | const CONTENT_TYPE_OFFICIAL = 'application/vnd.api+json'; |
||
| 31 | const CONTENT_TYPE_DEBUG = 'application/json'; |
||
| 32 | const CONTENT_TYPE_JSONP = 'application/javascript'; |
||
| 33 | |||
| 34 | const LEVEL_ROOT = 'root'; |
||
| 35 | const LEVEL_JSONAPI = 'jsonapi'; |
||
| 36 | const LEVEL_RESOURCE = 'resource'; |
||
| 37 | |||
| 38 | /** @var MetaObject */ |
||
| 39 | protected $meta; |
||
| 40 | /** @var JsonapiObject */ |
||
| 41 | protected $jsonapi; |
||
| 42 | /** @var ProfileInterface[] */ |
||
| 43 | protected $profiles = []; |
||
| 44 | /** @var array */ |
||
| 45 | protected static $defaults = [ |
||
| 46 | /** |
||
| 47 | * encode to json with these default options |
||
| 48 | */ |
||
| 49 | 'encodeOptions' => JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE, |
||
| 50 | |||
| 51 | /** |
||
| 52 | * encode to human-readable json, useful when debugging |
||
| 53 | */ |
||
| 54 | 'prettyPrint' => false, |
||
| 55 | |||
| 56 | /** |
||
| 57 | * send out the official jsonapi content-type header |
||
| 58 | * overwrite for jsonp or if clients don't support it |
||
| 59 | */ |
||
| 60 | 'contentType' => Document::CONTENT_TYPE_OFFICIAL, |
||
| 61 | |||
| 62 | /** |
||
| 63 | * overwrite the array to encode to json |
||
| 64 | */ |
||
| 65 | 'array' => null, |
||
| 66 | |||
| 67 | /** |
||
| 68 | * overwrite the json to send as response |
||
| 69 | */ |
||
| 70 | 'json' => null, |
||
| 71 | |||
| 72 | /** |
||
| 73 | * set the callback for jsonp responses |
||
| 74 | */ |
||
| 75 | 'jsonpCallback' => null, |
||
| 76 | ]; |
||
| 77 | |||
| 78 | public function __construct() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * human api |
||
| 85 | */ |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $key |
||
| 89 | * @param string $href |
||
| 90 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
| 91 | * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT |
||
| 92 | * |
||
| 93 | * @throws InputException if the $level is Document::LEVEL_JSONAPI, Document::LEVEL_RESOURCE, or unknown |
||
| 94 | */ |
||
| 95 | public function addLink($key, $href, array $meta=[], $level=Document::LEVEL_ROOT) { |
||
| 113 | |||
| 114 | /** |
||
| 115 | * set the self link on the document |
||
| 116 | * |
||
| 117 | * @param string $href |
||
| 118 | * @param array $meta optional, if given a LinkObject is added, otherwise a link string is added |
||
| 119 | * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT |
||
| 120 | */ |
||
| 121 | public function setSelfLink($href, array $meta=[], $level=Document::LEVEL_ROOT) { |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param string $key |
||
| 127 | * @param mixed $value |
||
| 128 | * @param string $level one of the Document::LEVEL_* constants, optional, defaults to Document::LEVEL_ROOT |
||
| 129 | * |
||
| 130 | * @throws InputException if the $level is unknown |
||
| 131 | * @throws InputException if the $level is Document::LEVEL_RESOURCE |
||
| 132 | */ |
||
| 133 | public function addMeta($key, $value, $level=Document::LEVEL_ROOT) { |
||
| 155 | |||
| 156 | /** |
||
| 157 | * spec api |
||
| 158 | */ |
||
| 159 | |||
| 160 | /** |
||
| 161 | * @param MetaObject $metaObject |
||
| 162 | */ |
||
| 163 | public function setMetaObject(MetaObject $metaObject) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * @param JsonapiObject $jsonapiObject |
||
| 169 | */ |
||
| 170 | public function setJsonapiObject(JsonapiObject $jsonapiObject) { |
||
| 173 | |||
| 174 | /** |
||
| 175 | * hide that this api supports jsonapi, or which version it is using |
||
| 176 | */ |
||
| 177 | public function unsetJsonapiObject() { |
||
| 180 | |||
| 181 | /** |
||
| 182 | * apply a profile which adds the link and sets a correct content-type |
||
| 183 | * |
||
| 184 | * note that the rules from the profile are not automatically enforced |
||
| 185 | * applying the rules, and applying them correctly, is manual |
||
| 186 | * however the $profile could have custom methods to help |
||
| 187 | * |
||
| 188 | * @see https://jsonapi.org/format/1.1/#profiles |
||
| 189 | * |
||
| 190 | * @param ProfileInterface $profile |
||
| 191 | */ |
||
| 192 | public function applyProfile(ProfileInterface $profile) { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * DocumentInterface |
||
| 210 | */ |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @inheritDoc |
||
| 214 | */ |
||
| 215 | View Code Duplication | public function toArray() { |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @inheritDoc |
||
| 233 | */ |
||
| 234 | public function toJson(array $options=[]) { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @inheritDoc |
||
| 257 | */ |
||
| 258 | public function sendResponse(array $options=[]) { |
||
| 275 | |||
| 276 | /** |
||
| 277 | * JsonSerializable |
||
| 278 | */ |
||
| 279 | |||
| 280 | public function jsonSerialize() { |
||
| 283 | } |
||
| 284 |