Skip to content

Commit

Permalink
Style updates
Browse files Browse the repository at this point in the history
  • Loading branch information
BlazOrazem committed Sep 6, 2021
1 parent 2fa4674 commit c849bb7
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ protected function insertI18n(array $values, $key)
* Update values in base table.
*
* @param array $values
* @param $ids
* @param array $ids
*
* @return mixed
*/
Expand Down
18 changes: 11 additions & 7 deletions src/QueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function setModel(Eloquent $model)
* @param array|mixed $columns
*
* @return $this
*
* @throws \Exception
*/
public function select($columns = ['*'])
Expand All @@ -54,6 +55,7 @@ public function select($columns = ['*'])
* @param array|mixed $column
*
* @return $this
*
* @throws \Exception
*/
public function addSelect($column)
Expand All @@ -71,12 +73,13 @@ public function addSelect($column)
* @param $columns
*
* @return mixed
*
* @throws \Exception
*/
protected function qualifyColumns($columns)
{
foreach ($columns as &$column) {
if (! in_array($column, $this->model->translatableAttributes())) {
if (!in_array($column, $this->model->translatableAttributes())) {
continue;
}

Expand All @@ -100,10 +103,10 @@ protected function qualifyColumns($columns)
* @param string $operator
* @param mixed $value
* @param string $boolean
*
* @return Builder|QueryBuilder
*
* @throws \InvalidArgumentException
*
* @throws \Exception
*/
public function where($column, $operator = null, $value = null, $boolean = 'and')
Expand Down Expand Up @@ -132,6 +135,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
* @param string $operator
* @param mixed $value
* @param string $boolean
*
* @return Builder|QueryBuilder
*
* @throws \InvalidArgumentException
Expand Down Expand Up @@ -166,19 +170,19 @@ public function whereTranslated($column, $operator = null, $value = null, $boole
}

// If the given operator is not found in the list of valid operators we will
// assume that the developer is just short-cutting the '=' operators and
// assume that the developer is just short-cutting the '=' operators, and
// we will set the operators to '=' and set the values appropriately.
if (! in_array(strtolower($operator), $this->operators, true)) {
if (!in_array(strtolower($operator), $this->operators, true)) {
[$value, $operator] = [$operator, '='];
}

$fallbackColumn = $this->qualifyTranslationColumn($column, true);
$column = $this->qualifyTranslationColumn($column);

// Finally we'll check whether we need to consider fallback translations. In
// Finally, we'll check whether we need to consider fallback translations. In
// that case we need to create a complex "ifnull" clause, otherwise we can
// just prepend the translation alias and add the where clause normally.
if (! $this->model->shouldFallback() || $column instanceof Closure) {
if (!$this->model->shouldFallback() || $column instanceof Closure) {
return $this->where($column, $operator, $value, $boolean);
}

Expand Down Expand Up @@ -257,7 +261,7 @@ public function orderByTranslated($column, $direction = 'asc')
$fallbackColumn = $this->qualifyTranslationColumn($column, true);
$column = $this->qualifyTranslationColumn($column);

if (! $this->model->shouldFallback()) {
if (!$this->model->shouldFallback()) {
return $this->orderBy($column, $direction);
}

Expand Down
37 changes: 20 additions & 17 deletions src/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function create(array $attributes = [], $translations = [])
*
* @return Translatable|string
*/
public static function createInLocale($locale, array $attributes = [], $translations = [])
public static function createInLocale(string $locale, array $attributes = [], $translations = [])
{
$model = (new static($attributes))->setLocale($locale);

Expand Down Expand Up @@ -90,6 +90,7 @@ public static function forceCreate(array $attributes, $translations = [])
/**
* Save a new model in provided locale and return the instance. Allow mass-assignment.
*
* @param string $locale
* @param array $attributes
* @param array|string $translations
*
Expand All @@ -113,7 +114,7 @@ public static function forceCreateInLocale($locale, array $attributes, $translat
*/
public function fresh($with = [])
{
if (! $this->exists) {
if (!$this->exists) {
return null;
}

Expand Down Expand Up @@ -205,7 +206,7 @@ public function forceSaveTranslation($locale, array $attributes)
*/
public function fill(array $attributes)
{
if (! isset(static::$i18nAttributes[$this->getTable()])) {
if (!isset(static::$i18nAttributes[$this->getTable()])) {
$this->initTranslatableAttributes();
}

Expand Down Expand Up @@ -233,7 +234,7 @@ protected function initTranslatableAttributes()
*/
protected function getTranslatableAttributesFromSchema()
{
if ((! $con = $this->getConnection()) || (! $builder = $con->getSchemaBuilder())) {
if ((!$con = $this->getConnection()) || (!$builder = $con->getSchemaBuilder())) {
return [];
}

Expand All @@ -254,13 +255,14 @@ protected function getTranslatableAttributesFromSchema()
* Return a collection of translated attributes in a given locale.
*
* @param $locale
*
* @return \Laraplus\Data\TranslationModel|null
*/
public function translate($locale)
{
$found = $this->translations->where($this->getLocaleKey(), $locale)->first();

if (! $found && $this->shouldFallback($locale)) {
if (!$found && $this->shouldFallback($locale)) {
return $this->translate($this->getFallbackLocale());
}

Expand Down Expand Up @@ -325,7 +327,7 @@ public function translationModel()
*/
public function translatableAttributes()
{
if (! isset(static::$i18nAttributes[$this->getTable()])) {
if (!isset(static::$i18nAttributes[$this->getTable()])) {
return [];
}

Expand All @@ -347,7 +349,7 @@ public function getLocaleKey()
*
* @param $locale
*
* @return string
* @return $this
*/
public function setLocale($locale)
{
Expand Down Expand Up @@ -380,7 +382,7 @@ public function getLocale()
*
* @param $locale
*
* @return string
* @return $this
*/
public function setFallbackLocale($locale)
{
Expand Down Expand Up @@ -428,7 +430,7 @@ public function setOnlyTranslated($onlyTranslated)
*/
public function getOnlyTranslated()
{
if (! is_null($this->overrideOnlyTranslated)) {
if (!is_null($this->overrideOnlyTranslated)) {
return $this->overrideOnlyTranslated;
}

Expand Down Expand Up @@ -460,7 +462,7 @@ public function setWithFallback($withFallback)
*/
public function getWithFallback()
{
if (! is_null($this->overrideWithFallback)) {
if (!is_null($this->overrideWithFallback)) {
return $this->overrideWithFallback;
}

Expand Down Expand Up @@ -502,15 +504,15 @@ public function getTranslationTableSuffix()
}

/**
* Should fallback to a primary translation.
* Should fall back to a primary translation.
*
* @param string|null $locale
*
* @return bool
*/
public function shouldFallback($locale = null)
{
if (! $this->getWithFallback() || ! $this->getFallbackLocale()) {
if (!$this->getWithFallback() || !$this->getFallbackLocale()) {
return false;
}

Expand Down Expand Up @@ -554,7 +556,7 @@ public function getDirty()
{
$dirty = parent::getDirty();

if (! $this->localeChanged) {
if (!$this->localeChanged) {
return $dirty;
}

Expand All @@ -580,17 +582,18 @@ public function syncOriginal()
}

/**
* Prefix column names with translation table instead of model table
* if the given column is translated.
* Prefix column names with the translation table instead of the model table if the given column is translated.
*
* @param $column
*
* @return string
*/
public function qualifyColumn($column)
{
if (\in_array($column, $this->translatableAttributes(), true)) {
if (in_array($column, $this->translatableAttributes(), true)) {
return sprintf('%s.%s', $this->getI18nTable(), $column);
}

return parent::qualifyColumn($column);
}

}
6 changes: 3 additions & 3 deletions src/TranslatableScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function apply(EloquentBuilder $builder, Eloquent $model)
$this->i18nTable = $model->getI18nTable();
$this->fallback = $model->getFallbackLocale();

if (! Str::startsWith($this->table, 'laravel_reserved_')) {
if (!Str::startsWith($this->table, 'laravel_reserved_')) {
$this->createJoin($builder, $model);
$this->createWhere($builder, $model);
$this->createSelect($builder, $model);
Expand Down Expand Up @@ -74,7 +74,7 @@ protected function createJoin(EloquentBuilder $builder, Eloquent $model)
*/
protected function getJoinType(Eloquent $model)
{
$innerJoin = ! $model->shouldFallback() && $model->getOnlyTranslated();
$innerJoin = !$model->shouldFallback() && $model->getOnlyTranslated();

return $innerJoin ? 'join' : 'leftJoin';
}
Expand Down Expand Up @@ -146,7 +146,7 @@ protected function createSelect(EloquentBuilder $builder, Eloquent $model)
protected function formatColumns(EloquentBuilder $builder, Eloquent $model)
{
$map = function ($field) use ($builder, $model) {
if (! $model->shouldFallback()) {
if (!$model->shouldFallback()) {
return "{$this->i18nTable}.{$field}";
}

Expand Down

0 comments on commit c849bb7

Please sign in to comment.