Skip to content

Commit

Permalink
Rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
szepeviktor committed Oct 21, 2023
1 parent 8a59bdd commit a9dddb9
Show file tree
Hide file tree
Showing 18 changed files with 254 additions and 2 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/back-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,12 @@ jobs:
name: "Search for $this->$this typo 🐌"
run: |
! git grep --line-number -e '\$this\s*->\s*\$this' -- ':!:*/back-end\.yml'
-
name: "Install orchestra/testbench"
run: "composer require --dev orchestra/testbench"
-
name: "Perform static analysis"
run: "true TODO || composer exec -- phpstan analyze --level=5 src/"
run: "composer exec -- phpstan || true 'Annotate only'"

coding_standards:
name: "4️⃣ Coding Standards"
Expand Down
22 changes: 21 additions & 1 deletion phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,28 @@ parameters:
paths:
- src/
- config/
- database/
# TODO
# - database/
- routes/
level: 5
checkAlwaysTrueCheckTypeFunctionCall: true
checkAlwaysTrueInstanceof: true
checkAlwaysTrueStrictComparison: true
checkAlwaysTrueLooseComparison: true
checkClassCaseSensitivity: false
checkDynamicProperties: true
checkExplicitMixed: false
checkImplicitMixed: false
checkExplicitMixedMissingReturn: true
checkFunctionNameCase: true
checkInternalClassCaseSensitivity: true
ignoreErrors:
- '#^Unsafe usage of new static#'
# --- TODO-s ---
# Tricky readonlys
- '#Assign it in the constructor\.$#'
- '#is assigned outside of the constructor\.$#'
# X vs. X contract
- '#but returns Illuminate\\Contracts\\#'
# SoftDeletes
- '#(\$forceDeleting|::withTrashed|::onlyTrashed|::trashed|::restore)#'
5 changes: 5 additions & 0 deletions src/Fields/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo as EloquentRelation;
use Illuminate\Http\Request;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsTo
*
* @extends \Cone\Root\Fields\Relation<TRelation>
*/
class BelongsTo extends Relation
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\BelongsToMany
*
* @extends \Cone\Root\Fields\Relation<TRelation>
*/
class BelongsToMany extends Relation
{
use ResolvesFields;
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany as EloquentRelation;

/**
* @extends \Cone\Root\Fields\HasOneOrMany<\Illuminate\Database\Eloquent\Relations\HasMany>
*/
class HasMany extends HasOneOrMany
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/HasOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne as EloquentRelation;

/**
* @extends \Cone\Root\Fields\HasOneOrMany<\Illuminate\Database\Eloquent\Relations\HasOne>
*/
class HasOne extends HasOneOrMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\HasOneOrMany
*
* @extends \Cone\Root\Fields\Relation<TRelation>
*/
abstract class HasOneOrMany extends Relation
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation;
use Illuminate\Http\Request;

/**
* @extends \Cone\Root\Fields\MorphOne<\Illuminate\Database\Eloquent\Relations\MorphOne>
*/
class Meta extends MorphOne
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/MorphMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany as EloquentRelation;

/**
* @extends \Cone\Root\Fields\MorphOneOrMany<\Illuminate\Database\Eloquent\Relations\MorphMany>
*/
class MorphMany extends MorphOneOrMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/MorphOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOne as EloquentRelation;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOne
*
* @extends \Cone\Root\Fields\MorphOneOrMany<TRelation>
*/
class MorphOne extends MorphOneOrMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphOneOrMany as EloquentRelation;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\MorphOneOrMany
*
* @extends \Cone\Root\Fields\HasOneOrMany<TRelation>
*/
abstract class MorphOneOrMany extends HasOneOrMany
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo as EloquentRelation;

/**
* @extends \Cone\Root\Fields\BelongsTo<\Illuminate\Database\Eloquent\Relations\MorphTo>
*/
class MorphTo extends BelongsTo
{
/**
Expand Down
3 changes: 3 additions & 0 deletions src/Fields/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany as EloquentRelation;

/**
* @extends \Cone\Root\Fields\BelongsToMany<\Illuminate\Database\Eloquent\Relations\MorphToMany>
*/
class MorphToMany extends BelongsToMany
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
* @template TRelation of \Illuminate\Database\Eloquent\Relations\Relation
*/
abstract class Relation extends Field
{
/**
Expand Down Expand Up @@ -72,6 +75,8 @@ public static function scopeQuery(Closure $callback): void

/**
* Get the relation instance.
*
* @phpstan-return TRelation
*/
public function getRelation(Model $model): EloquentRelation
{
Expand Down
2 changes: 2 additions & 0 deletions src/Models/Medium.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public function user(): BelongsTo

/**
* Determine if the file is image.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute<bool, never>
*/
protected function isImage(): Attribute
{
Expand Down
6 changes: 6 additions & 0 deletions src/Navigation/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
use Cone\Root\Traits\Makeable;
use Illuminate\Support\Facades\URL;

/**
* @property string $url
* @property string $label
* @property string $icon
* @property string $group
*/
class Item
{
use HasAttributes;
Expand Down
170 changes: 170 additions & 0 deletions src/Support/Slug.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php

namespace Cone\Root\Support;

use Closure;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
use Stringable;

class Slug implements Stringable
{
/**
* The model instance.
*/
public readonly Model $model;

/**
* The attributes that the slug is created from.
*/
public readonly array $from;

/**
* The attribute that the slug is saved into.
*/
public readonly string $to;

/**
* The slug separator.
*/
public readonly string $separator;

/**
* Indicates if the slug should be unique.
*/
public readonly bool $unique;

/**
* Indicates if generate always a fresh slug.
*/
public readonly bool $fresh;

/**
* The slug resolver.
*/
protected ?Closure $resolver = null;

/**
* Create a new slug instance.
*/
public function __construct(Model $model)
{
$this->model = $model;
}

/**
* Set the "from" property.
*/
public function from(array|string $attributes): static
{
$this->from = (array) $attributes;

return $this;
}

/**
* Set the "to" property
*/
public function to(string $attribute): static
{
$this->to = $attribute;

return $this;
}

/**
* Set the "unique" property.
*/
public function unique(bool $value = true): static
{
$this->unique = $value;

return $this;
}

/**
* Set the "fresh" property.
*/
public function fresh(bool $value = true): static
{
$this->fresh = $value;

return $this;
}

/**
* Set the "resolver" property.
*/
public function generateUsing(Closure $callback): static
{
$this->resolver = $callback;

return $this;
}

/**
* Generate the slug.
*/
public function generate(): string
{
$this->from ??= ['id'];
$this->to ??= 'slug';
$this->separator ??= '-';
$this->unique ??= false;
$this->fresh ??= false;

if (! is_null($this->model->getAttribute($this->to)) && ! $this->fresh) {
return $this->model->getAttribute($this->to);
}

$value = Str::of(implode($this->separator, $this->model->only($this->from)))
->slug($this->separator)
->value();

if (! is_null($this->resolver)) {
return call_user_func_array($this->resolver, [$this, $value]);
}

if (! $this->unique) {
return $value;
}

$match = $this->model
->newQuery()
->when(
in_array(SoftDeletes::class, class_uses_recursive($this->model)),
static function (Builder $query): Builder {
return $query->withTrashed();
}
)
->whereRaw(sprintf(
"`%s` regexp '^%s(%s[\\\\d]+)?$'",
$this->to,
preg_quote($value),
preg_quote($this->separator)
))
->orderByDesc($this->to)
->limit(1)
->value($this->to);

$value = is_null($match) ? $value : preg_replace_callback(
sprintf('/%s([\d]+)?$/', preg_quote($this->separator)),
static function (array $match): string {
return str_replace($match[1], (string) (((int) $match[1]) + 1), $match[0]);
},
$match
);

return $value === $match ? sprintf('%s%s1', $value, $this->separator) : $value;
}

/**
* Get the string representation of the slug.
*/
public function __toString(): string
{
return $this->generate();
}
}
3 changes: 3 additions & 0 deletions src/Widgets/Widgets.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\ForwardsCalls;

/**
* @mixin \Illuminate\Support\Collection
*/
class Widgets
{
use ForwardsCalls;
Expand Down

0 comments on commit a9dddb9

Please sign in to comment.