Commit 88ae3c83 authored by Micheal Mand's avatar Micheal Mand

Use type hints instead of using PHPDoc, where possible

parent 5a3ebbcb
...@@ -6,21 +6,16 @@ trait NamespacedEntity ...@@ -6,21 +6,16 @@ trait NamespacedEntity
{ {
/** /**
* Returns the entity namespace. * Returns the entity namespace.
*
* @return string
*/ */
public static function getEntityNamespace() public static function getEntityNamespace(): string
{ {
return isset(static::$entityNamespace) ? static::$entityNamespace : get_called_class(); return isset(static::$entityNamespace) ? static::$entityNamespace : get_called_class();
} }
/** /**
* Sets the entity namespace. * Sets the entity namespace.
*
* @param string $namespace
* @return void
*/ */
public static function setEntityNamespace($namespace) public static function setEntityNamespace(string $namespace)
{ {
static::$entityNamespace = $namespace; static::$entityNamespace = $namespace;
} }
......
...@@ -3,102 +3,88 @@ ...@@ -3,102 +3,88 @@
namespace Modules\Tag\Contracts; namespace Modules\Tag\Contracts;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
interface TaggableInterface interface TaggableInterface
{ {
/** /**
* The Eloquent tag entity name. * The Eloquent tag entity name.
* @var string
*/ */
public static function getEntityNamespace(); public static function getEntityNamespace(): string;
/** /**
* Returns the Eloquent tags entity name. * Returns the Eloquent tags entity name.
* @return string
*/ */
public static function getTagsModel(); public static function getTagsModel(): string;
/** /**
* Sets the Eloquent tags entity name. * Sets the Eloquent tags entity name.
* @param string $model
* @return void
*/ */
public static function setTagsModel($model); public static function setTagsModel(string $model);
/** /**
* Get all the entities with the given tag(s) * Get all the entities with the given tag(s)
* Optionally specify the column on which * Optionally specify the column on which
* to perform the search operation. * to perform the search operation.
* @param \Illuminate\Database\Eloquent\Builder $query *
* @param string|array $tags * @param string|array $tags
* @param string $type
* @return \Illuminate\Database\Eloquent\Builder
*/ */
public function scopeWhereTag(Builder $query, $tags, $type = 'slug'); public function scopeWhereTag(Builder $query, $tags, string $type = 'slug'): Builder;
/** /**
* Get all the entities with one of the given tag(s) * Get all the entities with one of the given tag(s)
* Optionally specify the column on which * Optionally specify the column on which
* to perform the search operation. * to perform the search operation.
* @param \Illuminate\Database\Eloquent\Builder $query *
* @param string|array $tags * @param string|array $tags
* @param string $type
* @return \Illuminate\Database\Eloquent\Builder
*/ */
public function scopeWithTag(Builder $query, $tags, $type = 'slug'); public function scopeWithTag(Builder $query, $tags, string $type = 'slug'): Builder;
/** /**
* Define the eloquent morphMany relationship * Define the eloquent morphMany relationship
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/ */
public function tags(); public function tags(): MorphToMany;
/** /**
* Returns all the tags under the current entity namespace. * Returns all the tags under the current entity namespace.
* @return \Illuminate\Database\Eloquent\Builder
*/ */
public static function allTags(); public static function allTags(): Builder;
/** /**
* Creates a new model instance. * Creates a new model instance.
* @return \Illuminate\Database\Eloquent\Model
*/ */
public static function createTagsModel(); public static function createTagsModel(): Model;
/** /**
* Syncs the given tags. * Syncs the given tags.
*
* @param string|array $tags * @param string|array $tags
* @param string $type
* @return bool
*/ */
public function setTags($tags, $type = 'name'); public function setTags($tags, string $type = 'name'): bool;
/** /**
* Detaches multiple tags from the entity or if no tags are * Detaches multiple tags from the entity or if no tags are
* passed, removes all the attached tags from the entity. * passed, removes all the attached tags from the entity.
*
* @param string|array|null $tags * @param string|array|null $tags
* @return bool
*/ */
public function untag($tags = null); public function untag($tags = null): bool;
/** /**
* Detaches the given tag from the entity. * Detaches the given tag from the entity.
* @param string $name
* @return void
*/ */
public function removeTag($name); public function removeTag(string $name);
/** /**
* Attaches multiple tags to the entity. * Attaches multiple tags to the entity.
* @param string|array $tags *
* @return bool * @param string|array $tags
*/ */
public function tag($tags); public function tag($tags): bool;
/** /**
* Attaches the given tag to the entity. * Attaches the given tag to the entity.
* @param string $name
* @return void
*/ */
public function addTag($name); public function addTag(string $name);
} }
...@@ -3,35 +3,25 @@ ...@@ -3,35 +3,25 @@
namespace Modules\Tag\Traits; namespace Modules\Tag\Traits;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Modules\Tag\Entities\Tag; use Modules\Tag\Entities\Tag;
trait TaggableTrait trait TaggableTrait
{ {
/**
* {@inheritdoc}
*/
protected static $tagsModel = Tag::class; protected static $tagsModel = Tag::class;
/** public static function getTagsModel(): string
* {@inheritdoc}
*/
public static function getTagsModel()
{ {
return static::$tagsModel; return static::$tagsModel;
} }
/**
* {@inheritdoc}
*/
public static function setTagsModel($model) public static function setTagsModel($model)
{ {
static::$tagsModel = $model; static::$tagsModel = $model;
} }
/** public function scopeWhereTag(Builder $query, $tags, string $type = 'slug'): Builder
* {@inheritdoc}
*/
public function scopeWhereTag(Builder $query, $tags, $type = 'slug')
{ {
if (is_string($tags) === true) { if (is_string($tags) === true) {
$tags = [$tags]; $tags = [$tags];
...@@ -49,10 +39,7 @@ trait TaggableTrait ...@@ -49,10 +39,7 @@ trait TaggableTrait
return $query; return $query;
} }
/** public function scopeWithTag(Builder $query, $tags, string $type = 'slug'): Builder
* {@inheritdoc}
*/
public function scopeWithTag(Builder $query, $tags, $type = 'slug')
{ {
if (is_string($tags) === true) { if (is_string($tags) === true) {
$tags = [$tags]; $tags = [$tags];
...@@ -66,36 +53,24 @@ trait TaggableTrait ...@@ -66,36 +53,24 @@ trait TaggableTrait
}); });
} }
/** public function tags(): MorphToMany
* {@inheritdoc}
*/
public function tags()
{ {
return $this->morphToMany(static::$tagsModel, 'taggable', 'tag__tagged', 'taggable_id', 'tag_id'); return $this->morphToMany(static::$tagsModel, 'taggable', 'tag__tagged', 'taggable_id', 'tag_id');
} }
/** public static function createTagsModel(): Model
* {@inheritdoc}
*/
public static function createTagsModel()
{ {
return new static::$tagsModel; return new static::$tagsModel;
} }
/** public static function allTags(): Builder
* {@inheritdoc}
*/
public static function allTags()
{ {
$instance = new static; $instance = new static;
return $instance->createTagsModel()->with('translations')->whereNamespace($instance->getEntityClassName()); return $instance->createTagsModel()->with('translations')->whereNamespace($instance->getEntityClassName());
} }
/** public function setTags($tags, string $type = 'slug'): bool
* {@inheritdoc}
*/
public function setTags($tags, $type = 'slug')
{ {
if (empty($tags)) { if (empty($tags)) {
$tags = []; $tags = [];
...@@ -121,10 +96,7 @@ trait TaggableTrait ...@@ -121,10 +96,7 @@ trait TaggableTrait
return true; return true;
} }
/** public function tag($tags): bool
* {@inheritdoc}
*/
public function tag($tags)
{ {
foreach ($tags as $tag) { foreach ($tags as $tag) {
$this->addTag($tag); $this->addTag($tag);
...@@ -133,10 +105,7 @@ trait TaggableTrait ...@@ -133,10 +105,7 @@ trait TaggableTrait
return true; return true;
} }
/** public function addTag(string $name)
* {@inheritdoc}
*/
public function addTag($name)
{ {
$tag = $this->createTagsModel()->where('namespace', $this->getEntityClassName()) $tag = $this->createTagsModel()->where('namespace', $this->getEntityClassName())
->with('translations') ->with('translations')
...@@ -162,10 +131,7 @@ trait TaggableTrait ...@@ -162,10 +131,7 @@ trait TaggableTrait
} }
} }
/** public function untag($tags = null): bool
* {@inheritdoc}
*/
public function untag($tags = null)
{ {
$tags = $tags ?: $this->tags()->get()->pluck('name')->all(); $tags = $tags ?: $this->tags()->get()->pluck('name')->all();
...@@ -176,10 +142,7 @@ trait TaggableTrait ...@@ -176,10 +142,7 @@ trait TaggableTrait
return true; return true;
} }
/** public function removeTag(string $name)
* {@inheritdoc}
*/
public function removeTag($name)
{ {
$tag = $this->createTagsModel() $tag = $this->createTagsModel()
->where('namespace', $this->getEntityClassName()) ->where('namespace', $this->getEntityClassName())
...@@ -193,10 +156,7 @@ trait TaggableTrait ...@@ -193,10 +156,7 @@ trait TaggableTrait
} }
} }
/** protected function getEntityClassName(): string
* {@inheritdoc}
*/
protected function getEntityClassName()
{ {
if (isset(static::$entityNamespace)) { if (isset(static::$entityNamespace)) {
return static::$entityNamespace; return static::$entityNamespace;
...@@ -205,10 +165,7 @@ trait TaggableTrait ...@@ -205,10 +165,7 @@ trait TaggableTrait
return $this->tags()->getMorphClass(); return $this->tags()->getMorphClass();
} }
/** public function generateTagSlug($name, $separator = '-'): string
* {@inheritdoc}
*/
public function generateTagSlug($name, $separator = '-')
{ {
// Convert all dashes/underscores into separator // Convert all dashes/underscores into separator
$flip = $separator == '-' ? '_' : '-'; $flip = $separator == '-' ? '_' : '-';
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment