@@ -64,7 +64,7 @@ |
||
| 64 | 64 | public function scopeSearch($query, $keyword = null) |
| 65 | 65 | { |
| 66 | 66 | $searchCols = $this->getSearchCols(null, static::$labelCol); |
| 67 | - $query->select('*', implode(' ', static::$labelCol) . ' as label', 'url'); |
|
| 67 | + $query->select('*', implode(' ', static::$labelCol) . ' as label', 'url'); |
|
| 68 | 68 | $match = ''; |
| 69 | 69 | |
| 70 | 70 | // make search results use the "and" clause |
@@ -20,7 +20,7 @@ |
||
| 20 | 20 | |
| 21 | 21 | public function fire() |
| 22 | 22 | { |
| 23 | - $request = Request::create((string)$this->option('uri'), 'GET'); |
|
| 23 | + $request = Request::create((string) $this->option('uri'), 'GET'); |
|
| 24 | 24 | $request->headers->set('X-Authorization', $this->option('token')); |
| 25 | 25 | $this->info(strip_tags( |
| 26 | 26 | app()['Illuminate\Contracts\Http\Kernel']->handle($request) |
@@ -7,154 +7,154 @@ |
||
| 7 | 7 | class Sftp |
| 8 | 8 | { |
| 9 | 9 | |
| 10 | - protected $connectionName; |
|
| 11 | - |
|
| 12 | - /** |
|
| 13 | - * Constructor |
|
| 14 | - * |
|
| 15 | - * @param string $connectionName |
|
| 16 | - */ |
|
| 17 | - public function __construct($connectionName) |
|
| 18 | - { |
|
| 19 | - $this->connectionName = $connectionName; |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * Gets all XML files from a remote folder |
|
| 24 | - * |
|
| 25 | - * @param string $remoteDir |
|
| 26 | - * @param string $localDir |
|
| 27 | - * @return array |
|
| 28 | - */ |
|
| 10 | + protected $connectionName; |
|
| 11 | + |
|
| 12 | + /** |
|
| 13 | + * Constructor |
|
| 14 | + * |
|
| 15 | + * @param string $connectionName |
|
| 16 | + */ |
|
| 17 | + public function __construct($connectionName) |
|
| 18 | + { |
|
| 19 | + $this->connectionName = $connectionName; |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * Gets all XML files from a remote folder |
|
| 24 | + * |
|
| 25 | + * @param string $remoteDir |
|
| 26 | + * @param string $localDir |
|
| 27 | + * @return array |
|
| 28 | + */ |
|
| 29 | 29 | public function getFiles($remoteDir, $localDir) |
| 30 | 30 | { |
| 31 | - $files = $this->remoteListPackaged($remoteDir); |
|
| 31 | + $files = $this->remoteListPackaged($remoteDir); |
|
| 32 | 32 | |
| 33 | - foreach ($files as $file) { |
|
| 33 | + foreach ($files as $file) { |
|
| 34 | 34 | $this->getFile($file['fullFileName'], $localDir . '/' . $file['fileName']); |
| 35 | 35 | } |
| 36 | 36 | |
| 37 | - return $files; |
|
| 37 | + return $files; |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - /** |
|
| 41 | - * Gets all XML files from a remote folder |
|
| 42 | - * |
|
| 43 | - * @param string $dir |
|
| 44 | - * @param bool $sorted |
|
| 45 | - * @param string $sortOrder |
|
| 46 | - * @return array |
|
| 47 | - */ |
|
| 40 | + /** |
|
| 41 | + * Gets all XML files from a remote folder |
|
| 42 | + * |
|
| 43 | + * @param string $dir |
|
| 44 | + * @param bool $sorted |
|
| 45 | + * @param string $sortOrder |
|
| 46 | + * @return array |
|
| 47 | + */ |
|
| 48 | 48 | public function remoteListPackaged($dir, $sorted = true, $sortOrder = 'desc') |
| 49 | 49 | { |
| 50 | - $files = []; |
|
| 50 | + $files = []; |
|
| 51 | 51 | $all = $this->remoteList($dir, $sorted, $sortOrder); |
| 52 | 52 | |
| 53 | - foreach ($all as $file=>$val) { |
|
| 53 | + foreach ($all as $file=>$val) { |
|
| 54 | 54 | if ($this->isValidExtension($val['filename'])) { |
| 55 | 55 | $files[] = [ |
| 56 | - 'fileName' => $val['filename'], |
|
| 57 | - 'fullFileName' => $dir . '/' . $val['filename'], |
|
| 58 | - 'size' => $val['size'], |
|
| 59 | - 'modified' => gmdate("m/d/Y H:i:s", $val['mtime']), |
|
| 60 | - ]; |
|
| 56 | + 'fileName' => $val['filename'], |
|
| 57 | + 'fullFileName' => $dir . '/' . $val['filename'], |
|
| 58 | + 'size' => $val['size'], |
|
| 59 | + 'modified' => gmdate("m/d/Y H:i:s", $val['mtime']), |
|
| 60 | + ]; |
|
| 61 | 61 | } |
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | return $files; |
| 65 | - } |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - /** |
|
| 68 | - * Retrieves a file from a SSH/SFTP server |
|
| 69 | - * |
|
| 70 | - * @param string $remoteFile |
|
| 71 | - * @param string $localFile |
|
| 67 | + /** |
|
| 68 | + * Retrieves a file from a SSH/SFTP server |
|
| 69 | + * |
|
| 70 | + * @param string $remoteFile |
|
| 71 | + * @param string $localFile |
|
| 72 | 72 | * @return \Symfony\Component\Console\Output\OutputInterface |
| 73 | - */ |
|
| 74 | - public function getFile($remoteFile, $localFile) |
|
| 75 | - { |
|
| 76 | - return \SSH::into($this->connectionName)->get($remoteFile, $localFile); |
|
| 77 | - } |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Places a file on a remote SSH/SFTP server |
|
| 81 | - * |
|
| 82 | - * @param string $remoteFile |
|
| 83 | - * @param string $localFile |
|
| 73 | + */ |
|
| 74 | + public function getFile($remoteFile, $localFile) |
|
| 75 | + { |
|
| 76 | + return \SSH::into($this->connectionName)->get($remoteFile, $localFile); |
|
| 77 | + } |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Places a file on a remote SSH/SFTP server |
|
| 81 | + * |
|
| 82 | + * @param string $remoteFile |
|
| 83 | + * @param string $localFile |
|
| 84 | 84 | * @return \Symfony\Component\Console\Output\OutputInterface |
| 85 | - */ |
|
| 86 | - public function putFile($remoteFile, $localFile) |
|
| 87 | - { |
|
| 88 | - return \SSH::into($this->connectionName)->put($localFile, $remoteFile); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * Retrieves a listing from directory |
|
| 93 | - * |
|
| 94 | - * @param string $dir |
|
| 95 | - * @param bool $sorted |
|
| 96 | - * @param string $sortOrder |
|
| 85 | + */ |
|
| 86 | + public function putFile($remoteFile, $localFile) |
|
| 87 | + { |
|
| 88 | + return \SSH::into($this->connectionName)->put($localFile, $remoteFile); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * Retrieves a listing from directory |
|
| 93 | + * |
|
| 94 | + * @param string $dir |
|
| 95 | + * @param bool $sorted |
|
| 96 | + * @param string $sortOrder |
|
| 97 | 97 | * @return array array of files and folders |
| 98 | - */ |
|
| 99 | - public function remoteList($dir, $sorted = true, $sortOrder = 'desc') |
|
| 100 | - { |
|
| 101 | - $conn = $this->getConnection(); |
|
| 102 | - $conn->chdir($dir); |
|
| 103 | - $list = $conn->rawlist(); |
|
| 104 | - |
|
| 105 | - if ($sorted) { |
|
| 106 | - $list = $this->sortList($list, $sortOrder); |
|
| 107 | - } |
|
| 98 | + */ |
|
| 99 | + public function remoteList($dir, $sorted = true, $sortOrder = 'desc') |
|
| 100 | + { |
|
| 101 | + $conn = $this->getConnection(); |
|
| 102 | + $conn->chdir($dir); |
|
| 103 | + $list = $conn->rawlist(); |
|
| 104 | + |
|
| 105 | + if ($sorted) { |
|
| 106 | + $list = $this->sortList($list, $sortOrder); |
|
| 107 | + } |
|
| 108 | 108 | |
| 109 | - return $list; |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - /** |
|
| 113 | - * Retrieves a listing from directory |
|
| 114 | - * |
|
| 115 | - * @param array $list |
|
| 116 | - * @param string $sortOrder |
|
| 117 | - * @return array sorted array of a folder |
|
| 118 | - */ |
|
| 119 | - public function sortList($list, $sortOrder = 'desc') |
|
| 120 | - { |
|
| 121 | - usort($list, function($a, $b) { return $a['mtime'] - $b['mtime']; }); |
|
| 122 | - |
|
| 123 | - return $sortOrder == 'asc' ? $list : array_reverse($list); |
|
| 124 | - } |
|
| 125 | - |
|
| 126 | - /** |
|
| 127 | - * Retrieves a listing from directory |
|
| 128 | - * |
|
| 129 | - * @param string $remoteFileFrom |
|
| 130 | - * @param string $remoteFileTo |
|
| 131 | - * @return boolean success |
|
| 132 | - */ |
|
| 133 | - public function rename($remoteFileFrom, $remoteFileTo) |
|
| 134 | - { |
|
| 135 | - $conn = $this->getConnection(); |
|
| 136 | - //print $conn->getSFTPLog(); |
|
| 137 | - return $conn->rename($remoteFileFrom, $remoteFileTo); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - /** |
|
| 141 | - * Gets a Net_SFTP connection as Laravels SSH doesn't have all the functionality |
|
| 142 | - * |
|
| 143 | - * @return Net_SFTP connection |
|
| 144 | - */ |
|
| 145 | - protected function getConnection() |
|
| 146 | - { |
|
| 147 | - return \SSH::into($this->connectionName)->getGateway()->getConnection(); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Gets a Net_SFTP connection as Laravels SSH doesn't have all the functionality |
|
| 152 | - * |
|
| 153 | - * @param string $fileName |
|
| 154 | - * @return bool |
|
| 155 | - */ |
|
| 156 | - protected function isValidExtension($fileName) |
|
| 157 | - { |
|
| 158 | - return substr(strrchr($fileName, '.'), 1) === 'xml' || substr(strrchr($fileName, '.'), 1) === 'err'; |
|
| 159 | - } |
|
| 109 | + return $list; |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + /** |
|
| 113 | + * Retrieves a listing from directory |
|
| 114 | + * |
|
| 115 | + * @param array $list |
|
| 116 | + * @param string $sortOrder |
|
| 117 | + * @return array sorted array of a folder |
|
| 118 | + */ |
|
| 119 | + public function sortList($list, $sortOrder = 'desc') |
|
| 120 | + { |
|
| 121 | + usort($list, function($a, $b) { return $a['mtime'] - $b['mtime']; }); |
|
| 122 | + |
|
| 123 | + return $sortOrder == 'asc' ? $list : array_reverse($list); |
|
| 124 | + } |
|
| 125 | + |
|
| 126 | + /** |
|
| 127 | + * Retrieves a listing from directory |
|
| 128 | + * |
|
| 129 | + * @param string $remoteFileFrom |
|
| 130 | + * @param string $remoteFileTo |
|
| 131 | + * @return boolean success |
|
| 132 | + */ |
|
| 133 | + public function rename($remoteFileFrom, $remoteFileTo) |
|
| 134 | + { |
|
| 135 | + $conn = $this->getConnection(); |
|
| 136 | + //print $conn->getSFTPLog(); |
|
| 137 | + return $conn->rename($remoteFileFrom, $remoteFileTo); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + /** |
|
| 141 | + * Gets a Net_SFTP connection as Laravels SSH doesn't have all the functionality |
|
| 142 | + * |
|
| 143 | + * @return Net_SFTP connection |
|
| 144 | + */ |
|
| 145 | + protected function getConnection() |
|
| 146 | + { |
|
| 147 | + return \SSH::into($this->connectionName)->getGateway()->getConnection(); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Gets a Net_SFTP connection as Laravels SSH doesn't have all the functionality |
|
| 152 | + * |
|
| 153 | + * @param string $fileName |
|
| 154 | + * @return bool |
|
| 155 | + */ |
|
| 156 | + protected function isValidExtension($fileName) |
|
| 157 | + { |
|
| 158 | + return substr(strrchr($fileName, '.'), 1) === 'xml' || substr(strrchr($fileName, '.'), 1) === 'err'; |
|
| 159 | + } |
|
| 160 | 160 | } |
| 161 | 161 | \ No newline at end of file |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | { |
| 37 | 37 | $xml = $xml ?: $this->xml; |
| 38 | 38 | //$array = json_decode(str_replace('{"0":" "}','null', json_encode(simplexml_load_string($xml),JSON_NUMERIC_CHECK)), true); |
| 39 | - $array = json_decode(str_replace('{"0":" "}','null', json_encode(simplexml_load_string($xml))), true); |
|
| 39 | + $array = json_decode(str_replace('{"0":" "}', 'null', json_encode(simplexml_load_string($xml))), true); |
|
| 40 | 40 | $this->filterEmptyArray($array); |
| 41 | 41 | |
| 42 | 42 | return $array; |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | */ |
| 54 | 54 | public function replaceInNode($xml, $node, $find, $replace) |
| 55 | 55 | { |
| 56 | - return preg_replace_callback('@(<' . $node . '>)' . $find . '(</' . $node . '>)@s', function ($matches) use ($replace) { |
|
| 56 | + return preg_replace_callback('@(<' . $node . '>)' . $find . '(</' . $node . '>)@s', function($matches) use ($replace) { |
|
| 57 | 57 | return str_replace(' ', $replace, $matches[0]); |
| 58 | 58 | }, $xml); |
| 59 | 59 | } |
@@ -63,7 +63,7 @@ |
||
| 63 | 63 | * |
| 64 | 64 | * @param string $xml |
| 65 | 65 | * @return \SimpleXMLElement |
| 66 | - */ |
|
| 66 | + */ |
|
| 67 | 67 | public function toXml($xml, $namespace = null) |
| 68 | 68 | { |
| 69 | 69 | $xml = $xml ?: $this->xml; |
@@ -121,7 +121,7 @@ |
||
| 121 | 121 | /** |
| 122 | 122 | * Returns a single item |
| 123 | 123 | * |
| 124 | - * @param object $object |
|
| 124 | + * @param \Illuminate\Database\Eloquent\Model $object |
|
| 125 | 125 | * @return \Illuminate\Http\JsonResponse |
| 126 | 126 | */ |
| 127 | 127 | public function showByObject($object) |
@@ -4,9 +4,6 @@ |
||
| 4 | 4 | |
| 5 | 5 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
| 6 | 6 | use Illuminate\Contracts\Validation\ValidationException; |
| 7 | -use Illuminate\Foundation\Bus\DispatchesCommands; |
|
| 8 | -use Illuminate\Foundation\Validation\ValidatesRequests; |
|
| 9 | -use Illuminate\Routing\Controller as BaseController; |
|
| 10 | 7 | |
| 11 | 8 | use \Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController; |
| 12 | 9 | |
@@ -14,182 +14,182 @@ |
||
| 14 | 14 | |
| 15 | 15 | abstract class ApiController extends ApiGuardController implements ApiInterface |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Laraponse object, which abstracts Fractal |
|
| 19 | - * |
|
| 20 | - * @var \EllipseSynergie\ApiResponse\Laravel\Response |
|
| 21 | - */ |
|
| 22 | - public $response; |
|
| 17 | + /** |
|
| 18 | + * Laraponse object, which abstracts Fractal |
|
| 19 | + * |
|
| 20 | + * @var \EllipseSynergie\ApiResponse\Laravel\Response |
|
| 21 | + */ |
|
| 22 | + public $response; |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * The string name of the Eloquent Model |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $modelName; |
|
| 24 | + /** |
|
| 25 | + * The string name of the Eloquent Model |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $modelName; |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * The Eloquent Model |
|
| 33 | - * |
|
| 34 | - * @var \Illuminate\Database\Eloquent\Model |
|
| 35 | - */ |
|
| 36 | - protected $model; |
|
| 31 | + /** |
|
| 32 | + * The Eloquent Model |
|
| 33 | + * |
|
| 34 | + * @var \Illuminate\Database\Eloquent\Model |
|
| 35 | + */ |
|
| 36 | + protected $model; |
|
| 37 | 37 | |
| 38 | - /** |
|
| 39 | - * The string name of the Transformer class |
|
| 40 | - * |
|
| 41 | - * @var string |
|
| 42 | - */ |
|
| 43 | - protected $transformerName; |
|
| 38 | + /** |
|
| 39 | + * The string name of the Transformer class |
|
| 40 | + * |
|
| 41 | + * @var string |
|
| 42 | + */ |
|
| 43 | + protected $transformerName; |
|
| 44 | 44 | |
| 45 | - /** |
|
| 46 | - * The name of the collection that is returned in the JSON response |
|
| 47 | - * |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - protected $collectionName; |
|
| 45 | + /** |
|
| 46 | + * The name of the collection that is returned in the JSON response |
|
| 47 | + * |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + protected $collectionName; |
|
| 51 | 51 | |
| 52 | - /** |
|
| 53 | - * The Fractal Manager |
|
| 54 | - * |
|
| 55 | - * @var \League\Fractal\Manager |
|
| 56 | - */ |
|
| 57 | - public $manager; |
|
| 58 | - |
|
| 59 | - |
|
| 60 | - /** |
|
| 61 | - * Constructor |
|
| 62 | - */ |
|
| 63 | - public function __construct() |
|
| 64 | - { |
|
| 65 | - $this->response = \Response::api(); |
|
| 52 | + /** |
|
| 53 | + * The Fractal Manager |
|
| 54 | + * |
|
| 55 | + * @var \League\Fractal\Manager |
|
| 56 | + */ |
|
| 57 | + public $manager; |
|
| 58 | + |
|
| 59 | + |
|
| 60 | + /** |
|
| 61 | + * Constructor |
|
| 62 | + */ |
|
| 63 | + public function __construct() |
|
| 64 | + { |
|
| 65 | + $this->response = \Response::api(); |
|
| 66 | 66 | |
| 67 | - if (!empty($this->modelName)) { |
|
| 68 | - $this->model = new $this->modelName; |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - // if no collection name provided, use the model's table name |
|
| 72 | - if (empty($this->collectionName)) { |
|
| 73 | - $this->collectionName = $this->model->getTable(); |
|
| 74 | - } |
|
| 67 | + if (!empty($this->modelName)) { |
|
| 68 | + $this->model = new $this->modelName; |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + // if no collection name provided, use the model's table name |
|
| 72 | + if (empty($this->collectionName)) { |
|
| 73 | + $this->collectionName = $this->model->getTable(); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - // parse includes |
|
| 77 | - if (\Input::get('include') != '') { |
|
| 78 | - $this->manager = new \League\Fractal\Manager; |
|
| 79 | - $this->manager->parseIncludes(explode(',', \Input::get('include'))); |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - parent::__construct(); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - /** |
|
| 86 | - * Creates an item |
|
| 87 | - * |
|
| 88 | - * @param \Illuminate\Http\Request $data |
|
| 89 | - * @return \Illuminate\Http\JsonResponse |
|
| 90 | - */ |
|
| 91 | - public function create(Request $data = null) |
|
| 92 | - { |
|
| 93 | - $data = $data ?: \Input::json(); |
|
| 94 | - $json = $data->all(); |
|
| 95 | - $json = $this->addAttribution($json); |
|
| 96 | - |
|
| 97 | - try { |
|
| 98 | - $this->model->validate($json); |
|
| 99 | - $item = $this->model->create($json); |
|
| 100 | - return $this->showByObject($item); |
|
| 76 | + // parse includes |
|
| 77 | + if (\Input::get('include') != '') { |
|
| 78 | + $this->manager = new \League\Fractal\Manager; |
|
| 79 | + $this->manager->parseIncludes(explode(',', \Input::get('include'))); |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + parent::__construct(); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + /** |
|
| 86 | + * Creates an item |
|
| 87 | + * |
|
| 88 | + * @param \Illuminate\Http\Request $data |
|
| 89 | + * @return \Illuminate\Http\JsonResponse |
|
| 90 | + */ |
|
| 91 | + public function create(Request $data = null) |
|
| 92 | + { |
|
| 93 | + $data = $data ?: \Input::json(); |
|
| 94 | + $json = $data->all(); |
|
| 95 | + $json = $this->addAttribution($json); |
|
| 96 | + |
|
| 97 | + try { |
|
| 98 | + $this->model->validate($json); |
|
| 99 | + $item = $this->model->create($json); |
|
| 100 | + return $this->showByObject($item); |
|
| 101 | 101 | |
| 102 | - } catch (ValidationException $e) { |
|
| 103 | - return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 104 | - } catch (MassAssignmentException $e) { |
|
| 105 | - return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 106 | - } catch (\Exception $e) { |
|
| 107 | - return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 108 | - } |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * Adds created_by and updated_by to the array if the model supports it |
|
| 113 | - * |
|
| 114 | - * @param array $data |
|
| 115 | - * @return array $data |
|
| 116 | - */ |
|
| 117 | - protected function addAttribution(array $data) |
|
| 118 | - { |
|
| 119 | - if ($this->model->attirbution) { |
|
| 120 | - $data['created_by'] = $this->apiKey->user_id; |
|
| 121 | - $data['updated_by'] = $this->apiKey->user_id; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - return $data; |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns a single item |
|
| 129 | - * |
|
| 130 | - * @param int $id |
|
| 131 | - * @return \Illuminate\Http\JsonResponse |
|
| 132 | - */ |
|
| 133 | - public function show($id) |
|
| 134 | - { |
|
| 135 | - try { |
|
| 102 | + } catch (ValidationException $e) { |
|
| 103 | + return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 104 | + } catch (MassAssignmentException $e) { |
|
| 105 | + return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 106 | + } catch (\Exception $e) { |
|
| 107 | + return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
|
| 108 | + } |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * Adds created_by and updated_by to the array if the model supports it |
|
| 113 | + * |
|
| 114 | + * @param array $data |
|
| 115 | + * @return array $data |
|
| 116 | + */ |
|
| 117 | + protected function addAttribution(array $data) |
|
| 118 | + { |
|
| 119 | + if ($this->model->attirbution) { |
|
| 120 | + $data['created_by'] = $this->apiKey->user_id; |
|
| 121 | + $data['updated_by'] = $this->apiKey->user_id; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + return $data; |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns a single item |
|
| 129 | + * |
|
| 130 | + * @param int $id |
|
| 131 | + * @return \Illuminate\Http\JsonResponse |
|
| 132 | + */ |
|
| 133 | + public function show($id) |
|
| 134 | + { |
|
| 135 | + try { |
|
| 136 | 136 | |
| 137 | - return $this->response->withItem($this->model->findOrFail($id), new $this->transformerName); |
|
| 137 | + return $this->response->withItem($this->model->findOrFail($id), new $this->transformerName); |
|
| 138 | 138 | |
| 139 | - } catch (ModelNotFoundException $e) { |
|
| 140 | - |
|
| 141 | - return $this->respondNotFound(); |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - /** |
|
| 146 | - * Returns a single item |
|
| 147 | - * |
|
| 148 | - * @param object $object |
|
| 149 | - * @return \Illuminate\Http\JsonResponse |
|
| 150 | - */ |
|
| 151 | - public function showByObject($object) |
|
| 152 | - { |
|
| 153 | - try { |
|
| 154 | - return $this->response->withItem($object, new $this->transformerName); |
|
| 139 | + } catch (ModelNotFoundException $e) { |
|
| 140 | + |
|
| 141 | + return $this->respondNotFound(); |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + /** |
|
| 146 | + * Returns a single item |
|
| 147 | + * |
|
| 148 | + * @param object $object |
|
| 149 | + * @return \Illuminate\Http\JsonResponse |
|
| 150 | + */ |
|
| 151 | + public function showByObject($object) |
|
| 152 | + { |
|
| 153 | + try { |
|
| 154 | + return $this->response->withItem($object, new $this->transformerName); |
|
| 155 | 155 | |
| 156 | - } catch (ModelNotFoundException $e) { |
|
| 157 | - |
|
| 158 | - return $this->respondNotFound(); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns a paginated collection |
|
| 164 | - * |
|
| 165 | - * @return \Illuminate\Http\JsonResponse |
|
| 166 | - */ |
|
| 167 | - public function collection() |
|
| 168 | - { |
|
| 169 | - $limit = \Input::get('limit') ?: 10; |
|
| 170 | - $model = $this->model; |
|
| 156 | + } catch (ModelNotFoundException $e) { |
|
| 157 | + |
|
| 158 | + return $this->respondNotFound(); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns a paginated collection |
|
| 164 | + * |
|
| 165 | + * @return \Illuminate\Http\JsonResponse |
|
| 166 | + */ |
|
| 167 | + public function collection() |
|
| 168 | + { |
|
| 169 | + $limit = \Input::get('limit') ?: 10; |
|
| 170 | + $model = $this->model; |
|
| 171 | 171 | |
| 172 | - if (\Request::has('order')) { |
|
| 173 | - list($orderCol, $orderBy) = explode('|', \Input::get('order')); |
|
| 174 | - $model = $model->orderBy($orderCol, $orderBy); |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - return $this->response->withPaginator($model->paginate($limit), new $this->transformerName, $this->collectionName); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Handles 404 errors |
|
| 182 | - * |
|
| 183 | - * @param string $msg |
|
| 184 | - * @return \Illuminate\Http\JsonResponse |
|
| 185 | - */ |
|
| 186 | - public function respondNotFound($msg = 'Not found!') |
|
| 187 | - { |
|
| 188 | - return \Response::json([ |
|
| 189 | - 'error' => [ |
|
| 190 | - 'message' => $msg, |
|
| 191 | - 'status_code' => 404 |
|
| 192 | - ] |
|
| 193 | - ], 404); |
|
| 194 | - } |
|
| 172 | + if (\Request::has('order')) { |
|
| 173 | + list($orderCol, $orderBy) = explode('|', \Input::get('order')); |
|
| 174 | + $model = $model->orderBy($orderCol, $orderBy); |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + return $this->response->withPaginator($model->paginate($limit), new $this->transformerName, $this->collectionName); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Handles 404 errors |
|
| 182 | + * |
|
| 183 | + * @param string $msg |
|
| 184 | + * @return \Illuminate\Http\JsonResponse |
|
| 185 | + */ |
|
| 186 | + public function respondNotFound($msg = 'Not found!') |
|
| 187 | + { |
|
| 188 | + return \Response::json([ |
|
| 189 | + 'error' => [ |
|
| 190 | + 'message' => $msg, |
|
| 191 | + 'status_code' => 404 |
|
| 192 | + ] |
|
| 193 | + ], 404); |
|
| 194 | + } |
|
| 195 | 195 | } |
| 196 | 196 | \ No newline at end of file |
@@ -103,7 +103,7 @@ |
||
| 103 | 103 | return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 104 | 104 | } catch (MassAssignmentException $e) { |
| 105 | 105 | return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 106 | - } catch (\Exception $e) { |
|
| 106 | + } catch (\Exception $e) { |
|
| 107 | 107 | return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -103,7 +103,7 @@ |
||
| 103 | 103 | return $this->response->setStatusCode(422)->withError($e->errors()->all(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 104 | 104 | } catch (MassAssignmentException $e) { |
| 105 | 105 | return $this->response->setStatusCode(422)->withError("Cannot mass assign " . $e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 106 | - } catch (\Exception $e) { |
|
| 106 | + } catch (\Exception $e) { |
|
| 107 | 107 | return $this->response->setStatusCode(422)->withError($e->getMessage(), 'GEN-UNPROCESSABLE-ENTITY'); |
| 108 | 108 | } |
| 109 | 109 | } |
@@ -19,7 +19,7 @@ |
||
| 19 | 19 | /** |
| 20 | 20 | * Include Template Detail |
| 21 | 21 | * |
| 22 | - * @return \League\Fractal\ItemResource |
|
| 22 | + * @return \League\Fractal\Resource\Collection |
|
| 23 | 23 | */ |
| 24 | 24 | public function includeTemplateDetail(PageDetail $pageDetail) |
| 25 | 25 | { |
@@ -16,13 +16,13 @@ |
||
| 16 | 16 | 'template_detail' |
| 17 | 17 | ]; |
| 18 | 18 | |
| 19 | - /** |
|
| 20 | - * Include Template Detail |
|
| 21 | - * |
|
| 22 | - * @return \League\Fractal\ItemResource |
|
| 23 | - */ |
|
| 24 | - public function includeTemplateDetail(PageDetail $pageDetail) |
|
| 25 | - { |
|
| 26 | - return $this->collection($pageDetail->templateDetail, new TemplateDetailTransformer); |
|
| 27 | - } |
|
| 19 | + /** |
|
| 20 | + * Include Template Detail |
|
| 21 | + * |
|
| 22 | + * @return \League\Fractal\ItemResource |
|
| 23 | + */ |
|
| 24 | + public function includeTemplateDetail(PageDetail $pageDetail) |
|
| 25 | + { |
|
| 26 | + return $this->collection($pageDetail->templateDetail, new TemplateDetailTransformer); |
|
| 27 | + } |
|
| 28 | 28 | } |
| 29 | 29 | \ No newline at end of file |
@@ -1,6 +1,5 @@ |
||
| 1 | 1 | <?php namespace App\LaravelRestCms\Page; |
| 2 | 2 | |
| 3 | -use App\LaravelRestCms\BaseModel; |
|
| 4 | 3 | use App\LaravelRestCms\BaseTransformer; |
| 5 | 4 | use App\LaravelRestCms\Page\Page; |
| 6 | 5 | use App\LaravelRestCms\Page\PageDetailTransformer; |
@@ -22,7 +22,7 @@ discard block |
||
| 22 | 22 | * Include Page Detail |
| 23 | 23 | * |
| 24 | 24 | * @param \App\LaravelRestCms\Page\Page |
| 25 | - * @return \League\Fractal\ItemResource |
|
| 25 | + * @return \League\Fractal\Resource\Collection |
|
| 26 | 26 | */ |
| 27 | 27 | public function includeDetail(Page $page) |
| 28 | 28 | { |
@@ -33,7 +33,7 @@ discard block |
||
| 33 | 33 | * Include Template |
| 34 | 34 | * |
| 35 | 35 | * @param \App\LaravelRestCms\Page\Page |
| 36 | - * @return \League\Fractal\ItemResource |
|
| 36 | + * @return \League\Fractal\Resource\Collection |
|
| 37 | 37 | */ |
| 38 | 38 | public function includeTemplate(Page $page) |
| 39 | 39 | { |
@@ -1,6 +1,5 @@ |
||
| 1 | 1 | <?php namespace App\LaravelRestCms\Page; |
| 2 | 2 | |
| 3 | -use App\LaravelRestCms\BaseModel; |
|
| 4 | 3 | use App\LaravelRestCms\BaseTransformer; |
| 5 | 4 | use App\LaravelRestCms\Page\Page; |
| 6 | 5 | use App\LaravelRestCms\Page\PageDetailTransformer; |
@@ -18,25 +18,25 @@ |
||
| 18 | 18 | 'template', |
| 19 | 19 | ]; |
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * Include Page Detail |
|
| 23 | - * |
|
| 24 | - * @param \App\LaravelRestCms\Page\Page |
|
| 25 | - * @return \League\Fractal\ItemResource |
|
| 26 | - */ |
|
| 27 | - public function includeDetail(Page $page) |
|
| 28 | - { |
|
| 29 | - return $this->collection($page->detail, new PageDetailTransformer); |
|
| 30 | - } |
|
| 21 | + /** |
|
| 22 | + * Include Page Detail |
|
| 23 | + * |
|
| 24 | + * @param \App\LaravelRestCms\Page\Page |
|
| 25 | + * @return \League\Fractal\ItemResource |
|
| 26 | + */ |
|
| 27 | + public function includeDetail(Page $page) |
|
| 28 | + { |
|
| 29 | + return $this->collection($page->detail, new PageDetailTransformer); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - /** |
|
| 33 | - * Include Template |
|
| 34 | - * |
|
| 35 | - * @param \App\LaravelRestCms\Page\Page |
|
| 36 | - * @return \League\Fractal\ItemResource |
|
| 37 | - */ |
|
| 38 | - public function includeTemplate(Page $page) |
|
| 39 | - { |
|
| 40 | - return $this->collection($page->template, new TemplateTransformer); |
|
| 41 | - } |
|
| 32 | + /** |
|
| 33 | + * Include Template |
|
| 34 | + * |
|
| 35 | + * @param \App\LaravelRestCms\Page\Page |
|
| 36 | + * @return \League\Fractal\ItemResource |
|
| 37 | + */ |
|
| 38 | + public function includeTemplate(Page $page) |
|
| 39 | + { |
|
| 40 | + return $this->collection($page->template, new TemplateTransformer); |
|
| 41 | + } |
|
| 42 | 42 | } |
| 43 | 43 | \ No newline at end of file |
@@ -34,8 +34,8 @@ |
||
| 34 | 34 | * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 35 | 35 | */ |
| 36 | 36 | public function detail() |
| 37 | - { |
|
| 38 | - return $this->hasMany(TemplateDetail::class, 'template_id', 'id'); |
|
| 39 | - } |
|
| 37 | + { |
|
| 38 | + return $this->hasMany(TemplateDetail::class, 'template_id', 'id'); |
|
| 39 | + } |
|
| 40 | 40 | |
| 41 | 41 | } |
@@ -5,7 +5,6 @@ |
||
| 5 | 5 | use Illuminate\Foundation\Validation\ValidatesRequests; |
| 6 | 6 | use Illuminate\Support\MessageBag; |
| 7 | 7 | use Illuminate\Support\Pluralizer; |
| 8 | -use Illuminate\Support\Singular; |
|
| 9 | 8 | |
| 10 | 9 | abstract class BaseModel extends Model { |
| 11 | 10 | |
@@ -74,21 +74,21 @@ discard block |
||
| 74 | 74 | public $attirbution = false; |
| 75 | 75 | |
| 76 | 76 | /** |
| 77 | - * The "booting" method of the model. |
|
| 78 | - * |
|
| 79 | - * @return void |
|
| 80 | - */ |
|
| 81 | - protected static function boot() |
|
| 82 | - { |
|
| 83 | - if (!is_null(static::$updateRules) && !is_null(static::$updateRulesConcat) && sizeof(static::$updateRulesConcat)) { |
|
| 84 | - static::$updateRules = static::$createRules + static::$updateRulesConcat; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - parent::boot(); |
|
| 88 | - |
|
| 89 | - static::savedEvent(); |
|
| 90 | - static::deletingEvent(); |
|
| 91 | - } |
|
| 77 | + * The "booting" method of the model. |
|
| 78 | + * |
|
| 79 | + * @return void |
|
| 80 | + */ |
|
| 81 | + protected static function boot() |
|
| 82 | + { |
|
| 83 | + if (!is_null(static::$updateRules) && !is_null(static::$updateRulesConcat) && sizeof(static::$updateRulesConcat)) { |
|
| 84 | + static::$updateRules = static::$createRules + static::$updateRulesConcat; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + parent::boot(); |
|
| 88 | + |
|
| 89 | + static::savedEvent(); |
|
| 90 | + static::deletingEvent(); |
|
| 91 | + } |
|
| 92 | 92 | |
| 93 | 93 | /** |
| 94 | 94 | * Gets the table name of the model. |
@@ -101,35 +101,35 @@ discard block |
||
| 101 | 101 | return $this->table; |
| 102 | 102 | } |
| 103 | 103 | |
| 104 | - /** |
|
| 105 | - * Hooks into the "saved" event |
|
| 106 | - * |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - protected static function savedEvent() |
|
| 110 | - { |
|
| 111 | - static::saved(function($model) |
|
| 112 | - { |
|
| 113 | - static::addToCache($model); |
|
| 114 | - |
|
| 115 | - return true; |
|
| 116 | - }); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Hooks into the "deleting" event |
|
| 121 | - * |
|
| 122 | - * @return void |
|
| 123 | - */ |
|
| 124 | - protected static function deletingEvent() |
|
| 125 | - { |
|
| 126 | - static::deleting(function($model) |
|
| 127 | - { |
|
| 128 | - static::removeFromCache($model); |
|
| 129 | - |
|
| 130 | - return true; |
|
| 131 | - }); |
|
| 132 | - } |
|
| 104 | + /** |
|
| 105 | + * Hooks into the "saved" event |
|
| 106 | + * |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + protected static function savedEvent() |
|
| 110 | + { |
|
| 111 | + static::saved(function($model) |
|
| 112 | + { |
|
| 113 | + static::addToCache($model); |
|
| 114 | + |
|
| 115 | + return true; |
|
| 116 | + }); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Hooks into the "deleting" event |
|
| 121 | + * |
|
| 122 | + * @return void |
|
| 123 | + */ |
|
| 124 | + protected static function deletingEvent() |
|
| 125 | + { |
|
| 126 | + static::deleting(function($model) |
|
| 127 | + { |
|
| 128 | + static::removeFromCache($model); |
|
| 129 | + |
|
| 130 | + return true; |
|
| 131 | + }); |
|
| 132 | + } |
|
| 133 | 133 | |
| 134 | 134 | /** |
| 135 | 135 | * Retrieves the singular name of the table |