diff --git a/app/Entities/BaseRepository.php b/app/Entities/BaseRepository.php index 72b9994..2b861fe 100755 --- a/app/Entities/BaseRepository.php +++ b/app/Entities/BaseRepository.php @@ -15,11 +15,21 @@ use App\Entities\Partners\Customer; */ abstract class BaseRepository extends EloquentRepository { + /** + * Get collection of customers. + * + * @return \Illuminate\Database\Eloquent\Collection + */ public function getCustomersList() { return Customer::orderBy('name')->pluck('name', 'id'); } + /** + * Get list of customers and vendors. + * + * @return array + */ public function getCustomersAndVendorsList() { $partners = [ @@ -30,21 +40,42 @@ abstract class BaseRepository extends EloquentRepository return $partners; } + /** + * Get collection of workers. + * + * @return \Illuminate\Database\Eloquent\Collection + */ public function getWorkersList() { return User::orderBy('name')->pluck('name', 'id'); } + /** + * Get collection of vendors. + * + * @return \Illuminate\Database\Eloquent\Collection + */ public function getVendorsList() { return Vendor::orderBy('name')->pluck('name', 'id'); } + /** + * Get collection of projects. + * + * @return \Illuminate\Database\Eloquent\Collection + */ public function getProjectsList() { return Project::orderBy('name')->pluck('name', 'id'); } + /** + * Get Job by it's id. + * + * @param int $jobId + * @return \App\Entities\Projects\Job + */ public function requireJobById($jobId) { return Job::findOrFail($jobId); diff --git a/app/Entities/EloquentRepository.php b/app/Entities/EloquentRepository.php index 029a57d..39da770 100755 --- a/app/Entities/EloquentRepository.php +++ b/app/Entities/EloquentRepository.php @@ -12,19 +12,47 @@ use App\Exceptions\EntityNotFoundException; */ abstract class EloquentRepository { + /** + * Default paginated items on each page. + * + * @var int + */ protected $_paginate = 25; + + /** + * Corresponding model of repository. + * + * @var \Illuminate\Database\Eloquent\Model + */ protected $model; + /** + * Create repository instance. + * + * @param \Illuminate\Database\Eloquent\Model $model + */ public function __construct(Model $model) { $this->model = $model; } + /** + * Set model of repository. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return void + */ public function setModel(Model $model) { $this->model = $model; } + /** + * Get all records filtered by $q (search query). + * + * @param string $q + * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator + */ public function getAll($q) { return $this->model->latest() @@ -32,11 +60,24 @@ abstract class EloquentRepository ->paginate($this->_paginate); } + /** + * Get model record by id. + * + * @param int $id + * @return \Illuminate\Database\Eloquent\Model + */ public function getById($id) { return $this->getBy($this->model->getKeyName(), $id); } + /** + * Get model record by column and value. + * + * @param string $column + * @param string $value + * @return \Illuminate\Database\Eloquent\Model + */ public function getBy($column, $value) { $model = $this->model->newQuery()->where($column, $value)->get(); @@ -47,6 +88,11 @@ abstract class EloquentRepository return $model->first(); } + /** + * Get model record by Id and throws exception if not found. + * @param int $id + * @return \Illuminate\Database\Eloquent\Model + */ public function requireById($id) { $model = $this->getById($id); @@ -57,11 +103,23 @@ abstract class EloquentRepository return $model; } + /** + * Create new instance of model with given attributes. + * + * @param array $attributes + * @return \Illuminate\Database\Eloquent\Model + */ public function getNewInstance($attributes = []) { return $this->model->newInstance($attributes); } + /** + * Create new record on database based on given data attributes. + * + * @param array $data + * @return \Illuminate\Database\Eloquent\Model + */ public function create($data) { if ($data instanceof Model) { @@ -77,6 +135,13 @@ abstract class EloquentRepository } } + /** + * Update $data attributes on database based on given $modelId/ + * + * @param array $data + * @param int $modelId + * @return \Illuminate\Database\Eloquent\Model + */ public function update($data, $modelId) { foreach ($data as $key => $value) { @@ -91,6 +156,12 @@ abstract class EloquentRepository return $model; } + /** + * Delete record based on given id. + * + * @param int $modelId + * @return bool + */ public function delete($modelId) { $model = $this->requireById($modelId); @@ -98,6 +169,12 @@ abstract class EloquentRepository return $model->delete(); } + /** + * Save instance of eloquent model data to database. + * + * @param \Illuminate\Database\Eloquent\Model $model + * @return bool + */ protected function storeEloquentModel(Model $model) { if ($model->getDirty()) { @@ -107,6 +184,12 @@ abstract class EloquentRepository } } + /** + * Store instance of model to database with given data. + * + * @param array $data + * @return \Illuminate\Database\Eloquent\Model + */ protected function storeArray($data) { $model = $this->getNewInstance($data); diff --git a/app/Entities/ReferenceAbstract.php b/app/Entities/ReferenceAbstract.php index ead9895..7aa0ebe 100644 --- a/app/Entities/ReferenceAbstract.php +++ b/app/Entities/ReferenceAbstract.php @@ -12,25 +12,56 @@ use App\Exceptions\ReferenceKeyNotFoundException; */ abstract class ReferenceAbstract { + /** + * List of reference items. + * + * @var array + */ protected static $lists = []; + /** + * List of color of reference items. + * + * @var array + */ protected static $colors = []; + /** + * Get collection of items. + * + * @return \Illuminate\Support\Collection + */ public static function get() { return collect(static::$lists); } + /** + * Get array of items. + * + * @return array + */ public static function toArray() { return static::$lists; } + /** + * Get array of items. + * + * @return array + */ public static function all() { return static::toArray(); } + /** + * Get item value by id (index). + * + * @param int|string $singleId + * @return string + */ public static function getById($singleId) { if (isset(static::$lists[$singleId])) { @@ -40,21 +71,44 @@ abstract class ReferenceAbstract new ReferenceKeyNotFoundException('Reference key: '.$singleId.' not found for '.get_called_class().'::lists'); } + /** + * Return array of items by array of ids (indexes). + * + * @param array $singleIds + * @return array + */ public static function only(array $singleIds) { return Arr::only(static::$lists, $singleIds); } + /** + * Return array of items except given ids (indexes). + * + * @param array $singleIds + * @return array + */ public static function except(array $singleIds) { return Arr::except(static::$lists, $singleIds); } + /** + * List of item colors. + * + * @return array + */ public static function colors() { return static::$colors; } + /** + * Get color name by item id (index). + * + * @param int|string $colorId + * @return string + */ public static function getColorById($colorId) { if ((bool) static::getById($colorId) && isset(static::$colors[$colorId])) { @@ -64,11 +118,23 @@ abstract class ReferenceAbstract throw new ReferenceKeyNotFoundException('Reference key: '.$colorId.' not found for '.get_called_class().'::colors'); } + /** + * Return array of item colors except given for ids (indexes). + * + * @param array $colorIds + * @return array + */ public static function colorsExcept(array $colorIds) { return Arr::except(static::$colors, $colorIds); } + /** + * Return array of item colorss by array of ids (indexes). + * + * @param array $colorIds + * @return array + */ public static function colorsOnly(array $colorIds) { return Arr::only(static::$colors, $colorIds);