public function json(\Opine\Collection\Collection $collection) { $method = $collection->methodGet(); $value = $collection->valueGet(); $head = ''; $tail = ''; if (isset($_GET['callback'])) { if ($_GET['callback'] == '?') { $_GET['callback'] = 'callback'; } $head = $_GET['callback'] . '('; $tail = ');'; } $options = null; $name = $collection->collection(); if ($method == 'byEmbeddedField') { $name = $collection->name; } if (isset($_GET) && isset($_GET['pretty'])) { $options = JSON_PRETTY_PRINT; $head = '<html><head></head><body style="margin:0; border:0; padding: 0"><textarea wrap="off" style="overflow: auto; margin:0; border:0; padding: 0; width:100%; height: 100%">'; $tail = '</textarea></body></html>'; } if (in_array($method, ['byId', 'bySlug'])) { $name = $collection->singularGet(); echo $head . json_encode([$name => $collection->{$method}($value)], $options) . $tail; } else { echo $head . json_encode([$name => $collection->{$method}($value), 'pagination' => ['limit' => $collection->limitGet(), 'total' => $collection->totalGet(), 'page' => $collection->pageGet(), 'pageCount' => ceil($collection->totalGet() / $collection->limitGet())], 'metadata' => array_merge(['display' => ['collection' => ucwords(str_replace('_', ' ', $collection->collection())), 'document' => ucwords(str_replace('_', ' ', $collection->singularGet()))], 'method' => $method], get_object_vars($collection))], $options) . $tail; } }