diff options
author | Marvin Borner | 2018-05-23 22:23:28 +0200 |
---|---|---|
committer | Marvin Borner | 2018-05-23 22:23:28 +0200 |
commit | b66a61addb6c8e66cb26fcf74b532d68891267e4 (patch) | |
tree | 05e9449ff25bdc98f68105f41923ccb9f6ef5095 /main/app/sprinkles/core/src/Database | |
parent | 1d4ef435177a5f9b6d1a289800d933e49be0c550 (diff) |
Refactored code, many fixes and improvements in chat backend+frontend
Diffstat (limited to 'main/app/sprinkles/core/src/Database')
15 files changed, 207 insertions, 261 deletions
diff --git a/main/app/sprinkles/core/src/Database/Builder.php b/main/app/sprinkles/core/src/Database/Builder.php index 8e27b7c..cebc318 100644 --- a/main/app/sprinkles/core/src/Database/Builder.php +++ b/main/app/sprinkles/core/src/Database/Builder.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database; use Illuminate\Database\Capsule\Manager as DB; @@ -18,7 +19,7 @@ use Illuminate\Database\Query\Builder as LaravelBuilder; */ class Builder extends LaravelBuilder { - protected $excludedColumns = null; + protected $excludedColumns = NULL; /** * Perform a "begins with" pattern match on a specified column in a query. @@ -27,8 +28,7 @@ class Builder extends LaravelBuilder * @param $field string The column to match * @param $value string The value to match */ - public function beginsWith($field, $value) - { + public function beginsWith($field, $value) { return $this->where($field, 'LIKE', "$value%"); } @@ -39,8 +39,7 @@ class Builder extends LaravelBuilder * @param $field string The column to match * @param $value string The value to match */ - public function endsWith($field, $value) - { + public function endsWith($field, $value) { return $this->where($field, 'LIKE', "%$value"); } @@ -50,11 +49,10 @@ class Builder extends LaravelBuilder * @param $value array|string The column(s) to exclude * @return $this */ - public function exclude($column) - { + public function exclude($column) { $column = is_array($column) ? $column : func_get_args(); - $this->excludedColumns = array_merge((array) $this->excludedColumns, $column); + $this->excludedColumns = array_merge((array)$this->excludedColumns, $column); return $this; } @@ -65,8 +63,7 @@ class Builder extends LaravelBuilder * @param $field string The column to match * @param $value string The value to match */ - public function like($field, $value) - { + public function like($field, $value) { return $this->where($field, 'LIKE', "%$value%"); } @@ -76,19 +73,17 @@ class Builder extends LaravelBuilder * @param $field string The column to match * @param $value string The value to match */ - public function orLike($field, $value) - { + public function orLike($field, $value) { return $this->orWhere($field, 'LIKE', "%$value%"); } /** * Execute the query as a "select" statement. * - * @param array $columns + * @param array $columns * @return \Illuminate\Support\Collection */ - public function get($columns = ['*']) - { + public function get($columns = ['*']) { $original = $this->columns; if (is_null($original)) { @@ -110,8 +105,7 @@ class Builder extends LaravelBuilder /** * Remove excluded columns from the select column list. */ - protected function removeExcludedSelectColumns() - { + protected function removeExcludedSelectColumns() { // Convert current column list and excluded column list to fully-qualified list $this->columns = $this->convertColumnsToFullyQualified($this->columns); $excludedColumns = $this->convertColumnsToFullyQualified($this->excludedColumns); @@ -132,8 +126,7 @@ class Builder extends LaravelBuilder * @param array $columns * @return array */ - protected function replaceWildcardColumns(array $columns) - { + protected function replaceWildcardColumns(array $columns) { $wildcardTables = $this->findWildcardTables($columns); foreach ($wildcardTables as $wildColumn => $table) { @@ -153,8 +146,7 @@ class Builder extends LaravelBuilder * @param array $columns * @return array */ - protected function findWildcardTables(array $columns) - { + protected function findWildcardTables(array $columns) { $tables = []; foreach ($columns as $column) { @@ -180,8 +172,7 @@ class Builder extends LaravelBuilder * @param string $table * @return array */ - protected function getQualifiedColumnNames($table = null) - { + protected function getQualifiedColumnNames($table = NULL) { $schema = $this->getConnection()->getSchemaBuilder(); return $this->convertColumnsToFullyQualified($schema->getColumnListing($table), $table); @@ -193,14 +184,13 @@ class Builder extends LaravelBuilder * @param array $columns * @return array */ - protected function convertColumnsToFullyQualified($columns, $table = null) - { + protected function convertColumnsToFullyQualified($columns, $table = NULL) { if (is_null($table)) { $table = $this->from; } array_walk($columns, function (&$item, $key) use ($table) { - if (strpos($item, '.') === false) { + if (strpos($item, '.') === FALSE) { $item = "$table.$item"; } }); diff --git a/main/app/sprinkles/core/src/Database/DatabaseInvalidException.php b/main/app/sprinkles/core/src/Database/DatabaseInvalidException.php index 08f8a31..0eba67b 100644 --- a/main/app/sprinkles/core/src/Database/DatabaseInvalidException.php +++ b/main/app/sprinkles/core/src/Database/DatabaseInvalidException.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database; use UserFrosting\Support\Exception\ForbiddenException; diff --git a/main/app/sprinkles/core/src/Database/Migrations/v400/SessionsTable.php b/main/app/sprinkles/core/src/Database/Migrations/v400/SessionsTable.php index ac86ceb..82d6534 100644 --- a/main/app/sprinkles/core/src/Database/Migrations/v400/SessionsTable.php +++ b/main/app/sprinkles/core/src/Database/Migrations/v400/SessionsTable.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Migrations\v400; use Illuminate\Database\Schema\Blueprint; @@ -24,8 +25,7 @@ class SessionsTable extends Migration /** * {@inheritDoc} */ - public function up() - { + public function up() { if (!$this->schema->hasTable('sessions')) { $this->schema->create('sessions', function (Blueprint $table) { $table->string('id')->unique(); @@ -41,8 +41,7 @@ class SessionsTable extends Migration /** * {@inheritDoc} */ - public function down() - { + public function down() { $this->schema->drop('sessions'); } } diff --git a/main/app/sprinkles/core/src/Database/Migrations/v400/ThrottlesTable.php b/main/app/sprinkles/core/src/Database/Migrations/v400/ThrottlesTable.php index 1c742f7..f74fee8 100644 --- a/main/app/sprinkles/core/src/Database/Migrations/v400/ThrottlesTable.php +++ b/main/app/sprinkles/core/src/Database/Migrations/v400/ThrottlesTable.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Migrations\v400; use Illuminate\Database\Schema\Blueprint; @@ -23,8 +24,7 @@ class ThrottlesTable extends Migration /** * {@inheritDoc} */ - public function up() - { + public function up() { if (!$this->schema->hasTable('throttles')) { $this->schema->create('throttles', function (Blueprint $table) { $table->increments('id'); @@ -45,8 +45,7 @@ class ThrottlesTable extends Migration /** * {@inheritDoc} */ - public function down() - { + public function down() { $this->schema->drop('throttles'); } } diff --git a/main/app/sprinkles/core/src/Database/Models/Concerns/HasRelationships.php b/main/app/sprinkles/core/src/Database/Models/Concerns/HasRelationships.php index 4fe9a30..919c108 100644 --- a/main/app/sprinkles/core/src/Database/Models/Concerns/HasRelationships.php +++ b/main/app/sprinkles/core/src/Database/Models/Concerns/HasRelationships.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Models\Concerns; use Illuminate\Support\Arr; @@ -41,8 +42,7 @@ trait HasRelationships * {@inheritDoc} * @return \UserFrosting\Sprinkle\Core\Database\Relations\HasManySyncable */ - public function hasMany($related, $foreignKey = null, $localKey = null) - { + public function hasMany($related, $foreignKey = NULL, $localKey = NULL) { $instance = $this->newRelatedInstance($related); $foreignKey = $foreignKey ?: $this->getForeignKey(); @@ -50,7 +50,7 @@ trait HasRelationships $localKey = $localKey ?: $this->getKeyName(); return new HasManySyncable( - $instance->newQuery(), $this, $instance->getTable().'.'.$foreignKey, $localKey + $instance->newQuery(), $this, $instance->getTable() . '.' . $foreignKey, $localKey ); } @@ -60,8 +60,7 @@ trait HasRelationships * {@inheritDoc} * @return \UserFrosting\Sprinkle\Core\Database\Relations\MorphManySyncable */ - public function morphMany($related, $name, $type = null, $id = null, $localKey = null) - { + public function morphMany($related, $name, $type = NULL, $id = NULL, $localKey = NULL) { $instance = $this->newRelatedInstance($related); // Here we will gather up the morph type and ID for the relationship so that we @@ -71,38 +70,37 @@ trait HasRelationships $table = $instance->getTable(); $localKey = $localKey ?: $this->getKeyName(); - return new MorphManySyncable($instance->newQuery(), $this, $table.'.'.$type, $table.'.'.$id, $localKey); + return new MorphManySyncable($instance->newQuery(), $this, $table . '.' . $type, $table . '.' . $id, $localKey); } /** * Define a many-to-many 'through' relationship. * This is basically hasManyThrough for many-to-many relationships. * - * @param string $related - * @param string $through - * @param string $firstJoiningTable - * @param string $firstForeignKey - * @param string $firstRelatedKey - * @param string $secondJoiningTable - * @param string $secondForeignKey - * @param string $secondRelatedKey - * @param string $throughRelation - * @param string $relation + * @param string $related + * @param string $through + * @param string $firstJoiningTable + * @param string $firstForeignKey + * @param string $firstRelatedKey + * @param string $secondJoiningTable + * @param string $secondForeignKey + * @param string $secondRelatedKey + * @param string $throughRelation + * @param string $relation * @return \UserFrosting\Sprinkle\Core\Database\Relations\BelongsToManyThrough */ public function belongsToManyThrough( $related, $through, - $firstJoiningTable = null, - $firstForeignKey = null, - $firstRelatedKey = null, - $secondJoiningTable = null, - $secondForeignKey = null, - $secondRelatedKey = null, - $throughRelation = null, - $relation = null - ) - { + $firstJoiningTable = NULL, + $firstForeignKey = NULL, + $firstRelatedKey = NULL, + $secondJoiningTable = NULL, + $secondForeignKey = NULL, + $secondRelatedKey = NULL, + $throughRelation = NULL, + $relation = NULL + ) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. @@ -153,8 +151,7 @@ trait HasRelationships * {@inheritDoc} * @return \UserFrosting\Sprinkle\Core\Database\Relations\BelongsToManyUnique */ - public function belongsToManyUnique($related, $table = null, $foreignKey = null, $relatedKey = null, $relation = null) - { + public function belongsToManyUnique($related, $table = NULL, $foreignKey = NULL, $relatedKey = NULL, $relation = NULL) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. @@ -189,14 +186,13 @@ trait HasRelationships * {@inheritDoc} * @return \UserFrosting\Sprinkle\Core\Database\Relations\MorphToManyUnique */ - public function morphToManyUnique($related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false) - { + public function morphToManyUnique($related, $name, $table = NULL, $foreignKey = NULL, $otherKey = NULL, $inverse = FALSE) { $caller = $this->getBelongsToManyCaller(); // First, we will need to determine the foreign key and "other key" for the // relationship. Once we have determined the keys we will make the query // instances, as well as the relationship instances we need for these. - $foreignKey = $foreignKey ?: $name.'_id'; + $foreignKey = $foreignKey ?: $name . '_id'; $instance = new $related; @@ -221,16 +217,15 @@ trait HasRelationships * This has been superseded by the belongsToManyUnique relationship's `withTernary` method since 4.1.7. * * @deprecated since 4.1.6 - * @param string $related - * @param string $constraintKey - * @param string $table - * @param string $foreignKey - * @param string $relatedKey - * @param string $relation + * @param string $related + * @param string $constraintKey + * @param string $table + * @param string $foreignKey + * @param string $relatedKey + * @param string $relation * @return \UserFrosting\Sprinkle\Core\Database\Relations\BelongsToManyConstrained */ - public function belongsToManyConstrained($related, $constraintKey, $table = null, $foreignKey = null, $relatedKey = null, $relation = null) - { + public function belongsToManyConstrained($related, $constraintKey, $table = NULL, $foreignKey = NULL, $relatedKey = NULL, $relation = NULL) { // If no relationship name was passed, we will pull backtraces to get the // name of the calling function. We will use that function name as the // title of this relation since that is a great convention to apply. @@ -264,15 +259,14 @@ trait HasRelationships * * @return string */ - protected function getBelongsToManyCaller() - { + protected function getBelongsToManyCaller() { $self = __FUNCTION__; $caller = Arr::first(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), function ($key, $trace) use ($self) { $caller = $trace['function']; - return ! in_array($caller, HasRelationships::$manyMethodsExtended) && $caller != $self; + return !in_array($caller, HasRelationships::$manyMethodsExtended) && $caller != $self; }); - return ! is_null($caller) ? $caller['function'] : null; + return !is_null($caller) ? $caller['function'] : NULL; } } diff --git a/main/app/sprinkles/core/src/Database/Models/Model.php b/main/app/sprinkles/core/src/Database/Models/Model.php index 1c18c2c..28b6be0 100644 --- a/main/app/sprinkles/core/src/Database/Models/Model.php +++ b/main/app/sprinkles/core/src/Database/Models/Model.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Models; use Illuminate\Database\Capsule\Manager as DB; @@ -29,10 +30,9 @@ abstract class Model extends LaravelModel /** * @var bool Disable timestamps for now. */ - public $timestamps = false; + public $timestamps = FALSE; - public function __construct(array $attributes = []) - { + public function __construct(array $attributes = []) { // Hacky way to force the DB service to load before attempting to use the model static::$ci['db']; @@ -42,24 +42,22 @@ abstract class Model extends LaravelModel /** * Determine if an attribute exists on the model - even if it is null. * - * @param string $key + * @param string $key * @return bool */ - public function attributeExists($key) - { + public function attributeExists($key) { return array_key_exists($key, $this->attributes); } /** * Determines whether a model exists by checking a unique column, including checking soft-deleted records * - * @param mixed $value + * @param mixed $value * @param string $identifier - * @param bool $checkDeleted set to true to include soft-deleted records + * @param bool $checkDeleted set to true to include soft-deleted records * @return \UserFrosting\Sprinkle\Core\Database\Models\Model|null */ - public static function findUnique($value, $identifier, $checkDeleted = true) - { + public static function findUnique($value, $identifier, $checkDeleted = TRUE) { $query = static::where($identifier, $value); if ($checkDeleted) { @@ -72,11 +70,10 @@ abstract class Model extends LaravelModel /** * Determine if an relation exists on the model - even if it is null. * - * @param string $key + * @param string $key * @return bool */ - public function relationExists($key) - { + public function relationExists($key) { return array_key_exists($key, $this->relations); } @@ -86,8 +83,7 @@ abstract class Model extends LaravelModel * Calls save(), then returns the id of the new record in the database. * @return int the id of this object. */ - public function store() - { + public function store() { $this->save(); // Store function should always return the id of the object @@ -99,8 +95,7 @@ abstract class Model extends LaravelModel * * @return \UserFrosting\Sprinkles\Core\Database\Builder */ - protected function newBaseQueryBuilder() - { + protected function newBaseQueryBuilder() { /** @var UserFrosting\Sprinkle\Core\Util\ClassMapper $classMapper */ $classMapper = static::$ci->classMapper; @@ -120,8 +115,7 @@ abstract class Model extends LaravelModel * @deprecated since 4.1.8 There is no point in having this alias. * @return array */ - public function export() - { + public function export() { return $this->toArray(); } @@ -131,8 +125,7 @@ abstract class Model extends LaravelModel * @deprecated since 4.1.8 setFetchMode is no longer available as of Laravel 5.4. * @link https://github.com/laravel/framework/issues/17728 */ - public static function queryBuilder() - { + public static function queryBuilder() { // Set query builder to fetch result sets as associative arrays (instead of creating stdClass objects) DB::connection()->setFetchMode(\PDO::FETCH_ASSOC); return DB::table(static::$table); diff --git a/main/app/sprinkles/core/src/Database/Models/Throttle.php b/main/app/sprinkles/core/src/Database/Models/Throttle.php index d13a7c1..82d2c87 100644 --- a/main/app/sprinkles/core/src/Database/Models/Throttle.php +++ b/main/app/sprinkles/core/src/Database/Models/Throttle.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Models; /** @@ -17,10 +18,10 @@ namespace UserFrosting\Sprinkle\Core\Database\Models; * @property string request_data */ class Throttle extends Model -{ +{ /** * @var string The name of the table for the current model. - */ + */ protected $table = "throttles"; protected $fillable = [ @@ -31,6 +32,6 @@ class Throttle extends Model /** * @var bool Enable timestamps for Throttles. - */ - public $timestamps = true; + */ + public $timestamps = TRUE; } diff --git a/main/app/sprinkles/core/src/Database/Relations/BelongsToManyConstrained.php b/main/app/sprinkles/core/src/Database/Relations/BelongsToManyConstrained.php index d652b56..cf79223 100644 --- a/main/app/sprinkles/core/src/Database/Relations/BelongsToManyConstrained.php +++ b/main/app/sprinkles/core/src/Database/Relations/BelongsToManyConstrained.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations; use Illuminate\Database\Eloquent\Model; @@ -30,17 +31,16 @@ class BelongsToManyConstrained extends BelongsToMany /** * Create a new belongs to many constrained relationship instance. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent - * @param string $constraintKey - * @param string $table - * @param string $foreignKey - * @param string $relatedKey - * @param string $relationName + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent + * @param string $constraintKey + * @param string $table + * @param string $foreignKey + * @param string $relatedKey + * @param string $relationName * @return void */ - public function __construct(Builder $query, Model $parent, $constraintKey, $table, $foreignKey, $relatedKey, $relationName = null) - { + public function __construct(Builder $query, Model $parent, $constraintKey, $table, $foreignKey, $relatedKey, $relationName = NULL) { $this->constraintKey = $constraintKey; parent::__construct($query, $parent, $table, $foreignKey, $relatedKey, $relationName); } @@ -48,11 +48,10 @@ class BelongsToManyConstrained extends BelongsToMany /** * Set the constraints for an eager load of the relation. * - * @param array $models + * @param array $models * @return void */ - public function addEagerConstraints(array $models) - { + public function addEagerConstraints(array $models) { // To make the query more efficient, we only bother querying related models if their pivot key value // matches the pivot key value of one of the parent models. $pivotKeys = $this->getPivotKeys($models, $this->constraintKey); @@ -63,8 +62,7 @@ class BelongsToManyConstrained extends BelongsToMany /** * Gets a list of unique pivot key values from an array of models. */ - protected function getPivotKeys(array $models, $pivotKey) - { + protected function getPivotKeys(array $models, $pivotKey) { $pivotKeys = []; foreach ($models as $model) { $pivotKeys[] = $model->getRelation('pivot')->{$pivotKey}; @@ -77,13 +75,12 @@ class BelongsToManyConstrained extends BelongsToMany * in the parent object to the child objects. * * @link Called in https://github.com/laravel/framework/blob/2f4135d8db5ded851d1f4f611124c53b768a3c08/src/Illuminate/Database/Eloquent/Builder.php - * @param array $models - * @param \Illuminate\Database\Eloquent\Collection $results - * @param string $relation + * @param array $models + * @param \Illuminate\Database\Eloquent\Collection $results + * @param string $relation * @return array */ - public function match(array $models, Collection $results, $relation) - { + public function match(array $models, Collection $results, $relation) { $dictionary = $this->buildDictionary($results); // Once we have an array dictionary of child objects we can easily match the @@ -109,8 +106,7 @@ class BelongsToManyConstrained extends BelongsToMany * @param mixed $pivotValue * @return array */ - protected function findMatchingPivots($items, $pivotValue) - { + protected function findMatchingPivots($items, $pivotValue) { $result = []; foreach ($items as $item) { if ($item->getRelation('pivot')->{$this->constraintKey} == $pivotValue) { diff --git a/main/app/sprinkles/core/src/Database/Relations/BelongsToManyThrough.php b/main/app/sprinkles/core/src/Database/Relations/BelongsToManyThrough.php index 33be507..67304be 100644 --- a/main/app/sprinkles/core/src/Database/Relations/BelongsToManyThrough.php +++ b/main/app/sprinkles/core/src/Database/Relations/BelongsToManyThrough.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations; use Illuminate\Database\Eloquent\Model; @@ -34,17 +35,16 @@ class BelongsToManyThrough extends BelongsToMany /** * Create a new belongs to many relationship instance. * - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Model $parent + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Model $parent * @param \Illuminate\Database\Eloquent\Relations\Relation $intermediateRelation - * @param string $table - * @param string $foreignKey - * @param string $relatedKey - * @param string $relationName + * @param string $table + * @param string $foreignKey + * @param string $relatedKey + * @param string $relationName * @return void */ - public function __construct(Builder $query, Model $parent, Relation $intermediateRelation, $table, $foreignKey, $relatedKey, $relationName = null) - { + public function __construct(Builder $query, Model $parent, Relation $intermediateRelation, $table, $foreignKey, $relatedKey, $relationName = NULL) { $this->intermediateRelation = $intermediateRelation; parent::__construct($query, $parent, $table, $foreignKey, $relatedKey, $relationName); @@ -57,8 +57,7 @@ class BelongsToManyThrough extends BelongsToMany * It would be better if BelongsToMany had a simple accessor for its foreign key. * @return string */ - public function getParentKeyName() - { + public function getParentKeyName() { return $this->intermediateRelation->newExistingPivot()->getForeignKey(); } @@ -68,20 +67,18 @@ class BelongsToManyThrough extends BelongsToMany * @see \Illuminate\Database\Eloquent\Relations\BelongsToMany * @return string */ - public function getExistenceCompareKey() - { + public function getExistenceCompareKey() { return $this->intermediateRelation->getQualifiedForeignKeyName(); } /** * Add a "via" query to load the intermediate models through which the child models are related. * - * @param string $viaRelationName + * @param string $viaRelationName * @param callable $viaCallback * @return $this */ - public function withVia($viaRelationName = null, $viaCallback = null) - { + public function withVia($viaRelationName = NULL, $viaCallback = NULL) { $this->tertiaryRelated = $this->intermediateRelation->getRelated(); // Set tertiary key and related model @@ -90,10 +87,10 @@ class BelongsToManyThrough extends BelongsToMany $this->tertiaryRelationName = is_null($viaRelationName) ? $this->intermediateRelation->getRelationName() . '_via' : $viaRelationName; $this->tertiaryCallback = is_null($viaCallback) - ? function () { - // - } - : $viaCallback; + ? function () { + // + } + : $viaCallback; return $this; } @@ -101,11 +98,10 @@ class BelongsToManyThrough extends BelongsToMany /** * Set the constraints for an eager load of the relation. * - * @param array $models + * @param array $models * @return void */ - public function addEagerConstraints(array $models) - { + public function addEagerConstraints(array $models) { // Constraint to only load models where the intermediate relation's foreign key matches the parent model $intermediateForeignKeyName = $this->intermediateRelation->getQualifiedForeignKeyName(); @@ -117,8 +113,7 @@ class BelongsToManyThrough extends BelongsToMany * * @return $this */ - protected function addWhereConstraints() - { + protected function addWhereConstraints() { $parentKeyName = $this->getParentKeyName(); $this->query->where( @@ -131,13 +126,12 @@ class BelongsToManyThrough extends BelongsToMany /** * Match the eagerly loaded results to their parents * - * @param array $models - * @param \Illuminate\Database\Eloquent\Collection $results - * @param string $relation + * @param array $models + * @param \Illuminate\Database\Eloquent\Collection $results + * @param string $relation * @return array */ - public function match(array $models, Collection $results, $relation) - { + public function match(array $models, Collection $results, $relation) { // Build dictionary of parent (e.g. user) to related (e.g. permission) models list($dictionary, $nestedViaDictionary) = $this->buildDictionary($results, $this->getParentKeyName()); @@ -177,8 +171,7 @@ class BelongsToManyThrough extends BelongsToMany * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - protected function unsetTertiaryPivots(Collection $models) - { + protected function unsetTertiaryPivots(Collection $models) { foreach ($models as $model) { unset($model->pivot->{$this->foreignKey}); } @@ -187,11 +180,10 @@ class BelongsToManyThrough extends BelongsToMany /** * Set the join clause for the relation query. * - * @param \Illuminate\Database\Eloquent\Builder|null $query + * @param \Illuminate\Database\Eloquent\Builder|null $query * @return $this */ - protected function performJoin($query = null) - { + protected function performJoin($query = NULL) { $query = $query ?: $this->query; parent::performJoin($query); @@ -215,11 +207,10 @@ class BelongsToManyThrough extends BelongsToMany * * @return array */ - protected function aliasedPivotColumns() - { + protected function aliasedPivotColumns() { $defaults = [$this->foreignKey, $this->relatedKey]; $aliasedPivotColumns = collect(array_merge($defaults, $this->pivotColumns))->map(function ($column) { - return $this->table.'.'.$column.' as pivot_'.$column; + return $this->table . '.' . $column . ' as pivot_' . $column; }); $parentKeyName = $this->getParentKeyName(); diff --git a/main/app/sprinkles/core/src/Database/Relations/BelongsToManyUnique.php b/main/app/sprinkles/core/src/Database/Relations/BelongsToManyUnique.php index f256f17..d5d473d 100644 --- a/main/app/sprinkles/core/src/Database/Relations/BelongsToManyUnique.php +++ b/main/app/sprinkles/core/src/Database/Relations/BelongsToManyUnique.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations; use Illuminate\Database\Eloquent\Relations\BelongsToMany; diff --git a/main/app/sprinkles/core/src/Database/Relations/Concerns/Syncable.php b/main/app/sprinkles/core/src/Database/Relations/Concerns/Syncable.php index 278b762..cb32d52 100644 --- a/main/app/sprinkles/core/src/Database/Relations/Concerns/Syncable.php +++ b/main/app/sprinkles/core/src/Database/Relations/Concerns/Syncable.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations\Concerns; use Illuminate\Database\Eloquent\Model; @@ -22,12 +23,11 @@ trait Syncable * Synchronizes an array of data for related models with a parent model. * * @param mixed[] $data - * @param bool $deleting Delete models from the database that are not represented in the input data. - * @param bool $forceCreate Ignore mass assignment restrictions on child models. - * @param string $relatedKeyName The primary key used to determine which child models are new, updated, or deleted. + * @param bool $deleting Delete models from the database that are not represented in the input data. + * @param bool $forceCreate Ignore mass assignment restrictions on child models. + * @param string $relatedKeyName The primary key used to determine which child models are new, updated, or deleted. */ - public function sync($data, $deleting = true, $forceCreate = false, $relatedKeyName = null) - { + public function sync($data, $deleting = TRUE, $forceCreate = FALSE, $relatedKeyName = NULL) { $changes = [ 'created' => [], 'deleted' => [], 'updated' => [], ]; @@ -71,9 +71,9 @@ trait Syncable if ($deleting && count($deleteIds) > 0) { // Remove global scopes to avoid ambiguous keys $this->getRelated() - ->withoutGlobalScopes() - ->whereIn($relatedKeyName, $deleteIds) - ->delete(); + ->withoutGlobalScopes() + ->whereIn($relatedKeyName, $deleteIds) + ->delete(); $changes['deleted'] = $this->castKeys($deleteIds); } @@ -82,11 +82,11 @@ trait Syncable foreach ($updateRows as $id => $row) { // Remove global scopes to avoid ambiguous keys $this->getRelated() - ->withoutGlobalScopes() - ->where($relatedKeyName, $id) - ->update($row); + ->withoutGlobalScopes() + ->where($relatedKeyName, $id) + ->update($row); } - + $changes['updated'] = $this->castKeys($updateIds); // Insert the new rows @@ -109,24 +109,22 @@ trait Syncable /** * Cast the given keys to integers if they are numeric and string otherwise. * - * @param array $keys + * @param array $keys * @return array */ - protected function castKeys(array $keys) - { - return (array) array_map(function ($v) { + protected function castKeys(array $keys) { + return (array)array_map(function ($v) { return $this->castKey($v); }, $keys); } - + /** * Cast the given key to an integer if it is numeric. * - * @param mixed $key + * @param mixed $key * @return mixed */ - protected function castKey($key) - { - return is_numeric($key) ? (int) $key : (string) $key; + protected function castKey($key) { + return is_numeric($key) ? (int)$key : (string)$key; } } diff --git a/main/app/sprinkles/core/src/Database/Relations/Concerns/Unique.php b/main/app/sprinkles/core/src/Database/Relations/Concerns/Unique.php index 4b529bb..71c1c4c 100644 --- a/main/app/sprinkles/core/src/Database/Relations/Concerns/Unique.php +++ b/main/app/sprinkles/core/src/Database/Relations/Concerns/Unique.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations\Concerns; use Illuminate\Database\Eloquent\Builder; @@ -23,14 +24,14 @@ trait Unique * * @var \Illuminate\Database\Eloquent\Model */ - protected $tertiaryRelated = null; + protected $tertiaryRelated = NULL; /** * The name to use for the tertiary relation (e.g. 'roles_via', etc) * * @var string */ - protected $tertiaryRelationName = null; + protected $tertiaryRelationName = NULL; /** * The foreign key to the related tertiary model instance. @@ -44,30 +45,29 @@ trait Unique * * @var callable|null */ - protected $tertiaryCallback = null; + protected $tertiaryCallback = NULL; /** * The limit to apply on the number of related models retrieved. * * @var int|null */ - protected $limit = null; + protected $limit = NULL; /** * The offset to apply on the related models retrieved. * * @var int|null */ - protected $offset = null; + protected $offset = NULL; /** * Alias to set the "offset" value of the query. * - * @param int $value + * @param int $value * @return $this */ - public function skip($value) - { + public function skip($value) { return $this->offset($value); } @@ -76,11 +76,10 @@ trait Unique * * @todo Implement for 'unionOffset' as well? (By checking the value of $this->query->getQuery()->unions) * @see \Illuminate\Database\Query\Builder - * @param int $value + * @param int $value * @return $this */ - public function offset($value) - { + public function offset($value) { $this->offset = max(0, $value); return $this; @@ -89,11 +88,10 @@ trait Unique /** * Alias to set the "limit" value of the query. * - * @param int $value + * @param int $value * @return $this */ - public function take($value) - { + public function take($value) { return $this->limit($value); } @@ -102,11 +100,10 @@ trait Unique * * @todo Implement for 'unionLimit' as well? (By checking the value of $this->query->getQuery()->unions) * @see \Illuminate\Database\Query\Builder - * @param int $value + * @param int $value * @return $this */ - public function limit($value) - { + public function limit($value) { if ($value >= 0) { $this->limit = $value; } @@ -121,8 +118,7 @@ trait Unique * @param int $value * @return $this */ - public function withLimit($value) - { + public function withLimit($value) { return $this->limit($value); } @@ -133,22 +129,20 @@ trait Unique * @param int $value * @return $this */ - public function withOffset($value) - { + public function withOffset($value) { return $this->offset($value); } /** * Add a query to load the nested tertiary models for this relationship. * - * @param \Illuminate\Database\Eloquent\Model $tertiaryRelated - * @param string $tertiaryRelationName - * @param string $tertiaryKey - * @param callable $tertiaryCallback + * @param \Illuminate\Database\Eloquent\Model $tertiaryRelated + * @param string $tertiaryRelationName + * @param string $tertiaryKey + * @param callable $tertiaryCallback * @return $this */ - public function withTertiary($tertiaryRelated, $tertiaryRelationName = null, $tertiaryKey = null, $tertiaryCallback = null) - { + public function withTertiary($tertiaryRelated, $tertiaryRelationName = NULL, $tertiaryKey = NULL, $tertiaryCallback = NULL) { $this->tertiaryRelated = new $tertiaryRelated; // Try to guess the tertiary related key from the tertiaryRelated model. @@ -160,10 +154,10 @@ trait Unique $this->tertiaryRelationName = is_null($tertiaryRelationName) ? $this->tertiaryRelated->getTable() : $tertiaryRelationName; $this->tertiaryCallback = is_null($tertiaryCallback) - ? function () { - // - } - : $tertiaryCallback; + ? function () { + // + } + : $tertiaryCallback; return $this; } @@ -174,8 +168,7 @@ trait Unique * @see http://stackoverflow.com/a/29728129/2970321 * @return int */ - public function count() - { + public function count() { $constrainedBuilder = clone $this->query; $constrainedBuilder = $constrainedBuilder->distinct(); @@ -187,12 +180,11 @@ trait Unique * Add the constraints for a relationship count query. * * @see \Illuminate\Database\Eloquent\Relations\Relation - * @param \Illuminate\Database\Eloquent\Builder $query - * @param \Illuminate\Database\Eloquent\Builder $parentQuery + * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $parentQuery * @return \Illuminate\Database\Eloquent\Builder */ - public function getRelationExistenceCountQuery(Builder $query, Builder $parentQuery) - { + public function getRelationExistenceCountQuery(Builder $query, Builder $parentQuery) { return $this->getRelationExistenceQuery( $query, $parentQuery, new Expression("count(distinct {$this->relatedKey})") ); @@ -201,13 +193,12 @@ trait Unique /** * Match the eagerly loaded results to their parents * - * @param array $models - * @param \Illuminate\Database\Eloquent\Collection $results - * @param string $relation + * @param array $models + * @param \Illuminate\Database\Eloquent\Collection $results + * @param string $relation * @return array */ - public function match(array $models, Collection $results, $relation) - { + public function match(array $models, Collection $results, $relation) { // Build dictionary of parent (e.g. user) to related (e.g. permission) models list($dictionary, $nestedTertiaryDictionary) = $this->buildDictionary($results, $this->foreignKey); @@ -240,13 +231,12 @@ trait Unique * Execute the query as a "select" statement, getting all requested models * and matching up any tertiary models. * - * @param array $columns + * @param array $columns * @return \Illuminate\Database\Eloquent\Collection */ - public function get($columns = ['*']) - { + public function get($columns = ['*']) { // Get models and condense the result set - $models = $this->getModels($columns, true); + $models = $this->getModels($columns, TRUE); // Remove the tertiary pivot key from the condensed models $this->unsetTertiaryPivots($models); @@ -258,13 +248,12 @@ trait Unique * If we are applying either a limit or offset, we'll first determine a limited/offset list of model ids * to select from in the final query. * - * @param \Illuminate\Database\Eloquent\Builder $query + * @param \Illuminate\Database\Eloquent\Builder $query * @param int $limit * @param int $offset * @return \Illuminate\Database\Eloquent\Builder */ - public function getPaginatedQuery(Builder $query, $limit = null, $offset = null) - { + public function getPaginatedQuery(Builder $query, $limit = NULL, $offset = NULL) { $constrainedBuilder = clone $query; // Since some unique models will be represented by more than one row in the database, @@ -278,7 +267,7 @@ trait Unique // Apply an additional scope to override any selected columns in other global scopes $uniqueIdScope = function ($subQuery) use ($relatedKeyName) { $subQuery->select($relatedKeyName) - ->groupBy($relatedKeyName); + ->groupBy($relatedKeyName); }; $identifier = spl_object_hash($uniqueIdScope); @@ -308,21 +297,19 @@ trait Unique * * @return \Illuminate\Database\Eloquent\Collection */ - public function getEager() - { - return $this->getModels(['*'], false); + public function getEager() { + return $this->getModels(['*'], FALSE); } /** * Get the hydrated models and eager load their relations, optionally * condensing the set of models before performing the eager loads. * - * @param array $columns - * @param bool $condenseModels + * @param array $columns + * @param bool $condenseModels * @return \Illuminate\Database\Eloquent\Collection */ - public function getModels($columns = ['*'], $condenseModels = true) - { + public function getModels($columns = ['*'], $condenseModels = TRUE) { // First we'll add the proper select columns onto the query so it is run with // the proper columns. Then, we will get the results and hydrate out pivot // models with the result of those columns as a separate model relation. @@ -367,10 +354,9 @@ trait Unique * @param array $models * @return array */ - protected function condenseModels(array $models) - { + protected function condenseModels(array $models) { // Build dictionary of tertiary models, if `withTertiary` was called - $dictionary = null; + $dictionary = NULL; if ($this->tertiaryRelated) { $dictionary = $this->buildTertiaryDictionary($models); } @@ -392,12 +378,11 @@ trait Unique * that maps parent ids to arrays of related ids, which in turn map to arrays * of tertiary models corresponding to each relationship. * - * @param \Illuminate\Database\Eloquent\Collection $results + * @param \Illuminate\Database\Eloquent\Collection $results * @param string $parentKey * @return array */ - protected function buildDictionary(Collection $results, $parentKey = null) - { + protected function buildDictionary(Collection $results, $parentKey = NULL) { // First we will build a dictionary of child models keyed by the "parent key" (foreign key // of the intermediate relation) so that we will easily and quickly match them to their // parents without having a possibly slow inner loops for every models. @@ -416,8 +401,8 @@ trait Unique // ], // ... //] - $nestedTertiaryDictionary = null; - $tertiaryModels = null; + $nestedTertiaryDictionary = NULL; + $tertiaryModels = NULL; if ($this->tertiaryRelationName) { // Get all tertiary models from the result set matching any of the parent models. @@ -439,11 +424,11 @@ trait Unique if (!is_null($tertiaryKeyValue)) { $tertiaryModel = clone $tertiaryModels[$tertiaryKeyValue]; - + // We also transfer the pivot relation at this point, since we have already coalesced // any tertiary models into the nested dictionary. $this->transferPivotsToTertiary($result, $tertiaryModel); - + $nestedTertiaryDictionary[$parentKeyValue][$result->getKey()][] = $tertiaryModel; } } @@ -455,11 +440,10 @@ trait Unique /** * Build dictionary of tertiary models keyed by the corresponding related model keys. * - * @param array $models + * @param array $models * @return array */ - protected function buildTertiaryDictionary(array $models) - { + protected function buildTertiaryDictionary(array $models) { $dictionary = []; // Find the related tertiary entities (e.g. tasks) for all related models (e.g. locations) @@ -479,8 +463,7 @@ trait Unique return $dictionary; } - protected function transferPivotsToTertiary($model, $tertiaryModel) - { + protected function transferPivotsToTertiary($model, $tertiaryModel) { $pivotAttributes = []; foreach ($this->pivotColumns as $column) { $pivotAttributes[$column] = $model->pivot->$column; @@ -499,11 +482,10 @@ trait Unique /** * Get the tertiary models for the relationship. * - * @param array $models + * @param array $models * @return \Illuminate\Database\Eloquent\Collection */ - protected function getTertiaryModels(array $models) - { + protected function getTertiaryModels(array $models) { $tertiaryClass = $this->tertiaryRelated; $keys = []; @@ -529,11 +511,10 @@ trait Unique * Match a collection of child models into a collection of parent models using a dictionary. * * @param array $dictionary - * @param \Illuminate\Database\Eloquent\Collection $results + * @param \Illuminate\Database\Eloquent\Collection $results * @return void */ - protected function matchTertiaryModels(array $dictionary, Collection $results) - { + protected function matchTertiaryModels(array $dictionary, Collection $results) { // Now go through and set the tertiary relation on each child model foreach ($results as $model) { if (isset($dictionary[$key = $model->getKey()])) { @@ -552,8 +533,7 @@ trait Unique * @param \Illuminate\Database\Eloquent\Collection $models * @return void */ - protected function unsetTertiaryPivots(Collection $models) - { + protected function unsetTertiaryPivots(Collection $models) { foreach ($models as $model) { foreach ($this->pivotColumns as $column) { unset($model->pivot->$column); diff --git a/main/app/sprinkles/core/src/Database/Relations/HasManySyncable.php b/main/app/sprinkles/core/src/Database/Relations/HasManySyncable.php index bcf2a9d..9d85b1f 100644 --- a/main/app/sprinkles/core/src/Database/Relations/HasManySyncable.php +++ b/main/app/sprinkles/core/src/Database/Relations/HasManySyncable.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations; use Illuminate\Database\Eloquent\Relations\HasMany; diff --git a/main/app/sprinkles/core/src/Database/Relations/MorphManySyncable.php b/main/app/sprinkles/core/src/Database/Relations/MorphManySyncable.php index 2786193..bb90257 100644 --- a/main/app/sprinkles/core/src/Database/Relations/MorphManySyncable.php +++ b/main/app/sprinkles/core/src/Database/Relations/MorphManySyncable.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations; use Illuminate\Database\Eloquent\Relations\MorphMany; diff --git a/main/app/sprinkles/core/src/Database/Relations/MorphToManyUnique.php b/main/app/sprinkles/core/src/Database/Relations/MorphToManyUnique.php index cc9a03f..0920d1c 100644 --- a/main/app/sprinkles/core/src/Database/Relations/MorphToManyUnique.php +++ b/main/app/sprinkles/core/src/Database/Relations/MorphToManyUnique.php @@ -5,6 +5,7 @@ * @link https://github.com/userfrosting/UserFrosting * @license https://github.com/userfrosting/UserFrosting/blob/master/licenses/UserFrosting.md (MIT License) */ + namespace UserFrosting\Sprinkle\Core\Database\Relations; use Illuminate\Database\Eloquent\Relations\MorphToMany; |