Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
Platform
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Administrator
Platform
Commits
f6df9b81
Unverified
Commit
f6df9b81
authored
Nov 02, 2018
by
Nicolas Widart
Committed by
GitHub
Nov 02, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #642 from mikemand/feature/update-tags-with-typehits
Update Tags trait with typehints
parents
a475153c
e2bb9578
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
53 additions
and
122 deletions
+53
-122
NamespacedEntity.php
Modules/Core/Traits/NamespacedEntity.php
+2
-7
TaggableInterface.php
Modules/Tag/Contracts/TaggableInterface.php
+21
-35
TaggableTrait.php
Modules/Tag/Traits/TaggableTrait.php
+30
-80
No files found.
Modules/Core/Traits/NamespacedEntity.php
View file @
f6df9b81
...
...
@@ -6,21 +6,16 @@ trait NamespacedEntity
{
/**
* Returns the entity namespace.
*
* @return string
*/
public
static
function
getEntityNamespace
()
public
static
function
getEntityNamespace
()
:
string
{
return
isset
(
static
::
$entityNamespace
)
?
static
::
$entityNamespace
:
get_called_class
();
}
/**
* Sets the entity namespace.
*
* @param string $namespace
* @return void
*/
public
static
function
setEntityNamespace
(
$namespace
)
public
static
function
setEntityNamespace
(
string
$namespace
)
{
static
::
$entityNamespace
=
$namespace
;
}
...
...
Modules/Tag/Contracts/TaggableInterface.php
View file @
f6df9b81
...
...
@@ -3,102 +3,88 @@
namespace
Modules\Tag\Contracts
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Relations\MorphToMany
;
interface
TaggableInterface
{
/**
* The Eloquent tag entity name.
* @var string
*/
public
static
function
getEntityNamespace
();
public
static
function
getEntityNamespace
()
:
string
;
/**
* Returns the Eloquent tags entity name.
* @return string
*/
public
static
function
getTagsModel
();
public
static
function
getTagsModel
()
:
string
;
/**
* 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)
* Optionally specify the column on which
* to perform the search operation.
*
@param \Illuminate\Database\Eloquent\Builder $query
*
* @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)
* Optionally specify the column on which
* to perform the search operation.
*
@param \Illuminate\Database\Eloquent\Builder $query
*
* @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
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public
function
tags
();
public
function
tags
()
:
MorphToMany
;
/**
* 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.
* @return \Illuminate\Database\Eloquent\Model
*/
public
static
function
createTagsModel
();
public
static
function
createTagsModel
()
:
Model
;
/**
* Syncs the given 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
* passed, removes all the attached tags from the entity.
*
* @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.
* @param string $name
* @return void
*/
public
function
removeTag
(
$name
);
public
function
removeTag
(
string
$name
);
/**
* 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.
* @param string $name
* @return void
*/
public
function
addTag
(
$name
);
public
function
addTag
(
string
$name
);
}
Modules/Tag/Traits/TaggableTrait.php
View file @
f6df9b81
...
...
@@ -3,42 +3,29 @@
namespace
Modules\Tag\Traits
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Relations\MorphToMany
;
use
Modules\Tag\Entities\Tag
;
trait
TaggableTrait
{
/**
* {@inheritdoc}
*/
protected
static
$tagsModel
=
Tag
::
class
;
/**
* {@inheritdoc}
*/
public
static
function
getTagsModel
()
public
static
function
getTagsModel
()
:
string
{
return
static
::
$tagsModel
;
}
/**
* {@inheritdoc}
*/
public
static
function
setTagsModel
(
$model
)
public
static
function
setTagsModel
(
string
$model
)
{
static
::
$tagsModel
=
$model
;
}
/**
* {@inheritdoc}
*/
public
function
scopeWhereTag
(
Builder
$query
,
$tags
,
$type
=
'slug'
)
public
function
scopeWhereTag
(
Builder
$query
,
$tags
,
string
$type
=
'slug'
)
:
Builder
{
if
(
is_string
(
$tags
)
===
true
)
{
$tags
=
[
$tags
];
}
$query
->
with
(
'translations'
);
foreach
(
$tags
as
$tag
)
{
foreach
(
array_wrap
(
$tags
)
as
$tag
)
{
$query
->
whereHas
(
'tags'
,
function
(
Builder
$query
)
use
(
$type
,
$tag
)
{
$query
->
whereHas
(
'translations'
,
function
(
Builder
$query
)
use
(
$type
,
$tag
)
{
$query
->
where
(
$type
,
$tag
);
...
...
@@ -49,14 +36,10 @@ trait TaggableTrait
return
$query
;
}
/**
* {@inheritdoc}
*/
public
function
scopeWithTag
(
Builder
$query
,
$tags
,
$type
=
'slug'
)
public
function
scopeWithTag
(
Builder
$query
,
$tags
,
string
$type
=
'slug'
)
:
Builder
{
if
(
is_string
(
$tags
)
===
true
)
{
$tags
=
[
$tags
];
}
$tags
=
array_wrap
(
$tags
);
$query
->
with
(
'translations'
);
return
$query
->
whereHas
(
'tags'
,
function
(
Builder
$query
)
use
(
$type
,
$tags
)
{
...
...
@@ -66,43 +49,31 @@ trait TaggableTrait
});
}
/**
* {@inheritdoc}
*/
public
function
tags
()
public
function
tags
()
:
MorphToMany
{
return
$this
->
morphToMany
(
static
::
$tagsModel
,
'taggable'
,
'tag__tagged'
,
'taggable_id'
,
'tag_id'
);
}
/**
* {@inheritdoc}
*/
public
static
function
createTagsModel
()
public
static
function
createTagsModel
()
:
Model
{
return
new
static
::
$tagsModel
;
}
/**
* {@inheritdoc}
*/
public
static
function
allTags
()
public
static
function
allTags
()
:
Builder
{
$instance
=
new
static
;
return
$instance
->
createTagsModel
()
->
with
(
'translations'
)
->
whereNamespace
(
$instance
->
getEntityClassName
());
return
self
::
createTagsModel
()
->
with
(
'translations'
)
->
where
(
'namespace'
,
$instance
->
getEntityClassName
());
}
/**
* {@inheritdoc}
*/
public
function
setTags
(
$tags
,
$type
=
'slug'
)
public
function
setTags
(
$tags
,
string
$type
=
'slug'
)
:
bool
{
if
(
empty
(
$tags
))
{
$tags
=
[];
}
// Get the current entity tags
$entityTags
=
$this
->
tags
->
pluck
(
$type
)
->
all
();
$entityTags
=
$this
->
tags
()
->
get
()
->
pluck
(
$type
)
->
all
();
// Prepare the tags to be added and removed
$tagsToAdd
=
array_diff
(
$tags
,
$entityTags
);
...
...
@@ -121,25 +92,18 @@ trait TaggableTrait
return
true
;
}
/**
* {@inheritdoc}
*/
public
function
tag
(
$tags
)
public
function
tag
(
$tags
)
:
bool
{
foreach
(
$tags
as
$tag
)
{
foreach
(
array_wrap
(
$tags
)
as
$tag
)
{
$this
->
addTag
(
$tag
);
}
return
true
;
}
/**
* {@inheritdoc}
*/
public
function
addTag
(
$name
)
public
function
addTag
(
string
$name
)
{
$tag
=
$this
->
createTagsModel
()
->
where
(
'namespace'
,
$this
->
getEntityClassName
())
->
with
(
'translations'
)
$tag
=
self
::
allTags
()
->
whereHas
(
'translations'
,
function
(
Builder
$q
)
use
(
$name
)
{
$q
->
where
(
'slug'
,
$this
->
generateTagSlug
(
$name
));
})
->
first
();
...
...
@@ -157,17 +121,14 @@ trait TaggableTrait
$tag
->
save
();
}
if
(
$this
->
tags
->
contains
(
$tag
->
id
)
===
false
)
{
if
(
$this
->
tags
()
->
get
()
->
contains
(
$tag
->
id
)
===
false
)
{
$this
->
tags
()
->
attach
(
$tag
);
}
}
/**
* {@inheritdoc}
*/
public
function
untag
(
$tags
=
null
)
public
function
untag
(
$tags
=
null
)
:
bool
{
$tags
=
$tags
?:
$this
->
tags
->
pluck
(
'name'
)
->
all
();
$tags
=
$tags
?:
$this
->
tags
()
->
get
()
->
pluck
(
'name'
)
->
all
();
foreach
(
$tags
as
$tag
)
{
$this
->
removeTag
(
$tag
);
...
...
@@ -176,14 +137,9 @@ trait TaggableTrait
return
true
;
}
/**
* {@inheritdoc}
*/
public
function
removeTag
(
$name
)
public
function
removeTag
(
string
$name
)
{
$tag
=
$this
->
createTagsModel
()
->
where
(
'namespace'
,
$this
->
getEntityClassName
())
->
with
(
'translations'
)
$tag
=
self
::
allTags
()
->
whereHas
(
'translations'
,
function
(
Builder
$q
)
use
(
$name
)
{
$q
->
where
(
'slug'
,
$this
->
generateTagSlug
(
$name
));
})
->
first
();
...
...
@@ -193,10 +149,7 @@ trait TaggableTrait
}
}
/**
* {@inheritdoc}
*/
protected
function
getEntityClassName
()
protected
function
getEntityClassName
()
:
string
{
if
(
isset
(
static
::
$entityNamespace
))
{
return
static
::
$entityNamespace
;
...
...
@@ -205,24 +158,21 @@ trait TaggableTrait
return
$this
->
tags
()
->
getMorphClass
();
}
/**
* {@inheritdoc}
*/
public
function
generateTagSlug
(
$name
,
$separator
=
'-'
)
public
function
generateTagSlug
(
$name
,
$separator
=
'-'
)
:
string
{
// Convert all dashes/underscores into separator
$flip
=
$separator
==
'-'
?
'_'
:
'-'
;
$name
=
preg_replace
(
'!['
.
preg_quote
(
$flip
)
.
']+!u'
,
$separator
,
$name
);
$name
=
preg_replace
(
'!['
.
preg_quote
(
$flip
,
'!'
)
.
']+!u'
,
$separator
,
$name
);
// Replace @ with the word 'at'
$name
=
str_replace
(
'@'
,
$separator
.
'at'
.
$separator
,
$name
);
$name
=
str_replace
(
'@'
,
$separator
.
'at'
.
$separator
,
$name
);
// Remove all characters that are not the separator, letters, numbers, or whitespace.
$name
=
preg_replace
(
'![^'
.
preg_quote
(
$separator
)
.
'\pL\pN\s]+!u'
,
''
,
mb_strtolower
(
$name
));
$name
=
preg_replace
(
'![^'
.
preg_quote
(
$separator
,
'!'
)
.
'\pL\pN\s]+!u'
,
''
,
mb_strtolower
(
$name
));
// Replace all separator characters and whitespace by a single separator
$name
=
preg_replace
(
'!['
.
preg_quote
(
$separator
)
.
'\s]+!u'
,
$separator
,
$name
);
$name
=
preg_replace
(
'!['
.
preg_quote
(
$separator
,
'!'
)
.
'\s]+!u'
,
$separator
,
$name
);
return
trim
(
$name
,
$separator
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment