Code Duplication    Length = 77-88 lines in 3 locations

src/Models/ContentTraits/HasBlocks.php 1 location

@@ 7-83 (lines=77) @@
4
5
use Larafolio\Models\TextBlock;
6
7
trait HasBlocks
8
{
9
    /**
10
     * Get a model from a relationship by model name.
11
     *
12
     * @param string $relationship Name of relationship.
13
     * @param string $name         Name of model to get.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Model|null
16
     */
17
    abstract protected function getFromRelationshipByName($relationship, $name);
18
19
    /**
20
     * Define a polymorphic one-to-many relationship.
21
     *
22
     * @param string $related
23
     * @param string $name
24
     * @param string $type
25
     * @param string $id
26
     * @param string $localKey
27
     *
28
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
29
     */
30
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
31
32
    /**
33
     * A resource has many text blocks.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
36
     */
37
    public function blocks()
38
    {
39
        return $this->morphMany(TextBlock::class, 'resource');
40
    }
41
42
    /**
43
     * Return true if project has blocks.
44
     *
45
     * @return bool
46
     */
47
    public function hasBlocks()
48
    {
49
        return !$this->blocks->isEmpty();
50
    }
51
52
    /**
53
     * Get a text block by name, if exists.
54
     *
55
     * @param string $name Name of text block to get.
56
     *
57
     * @return \Larafolio\Models\TextBlock|null
58
     */
59
    public function block($name)
60
    {
61
        return $this->getFromRelationshipByName('blocks', $name);
62
    }
63
64
    /**
65
     * Get block text by block name, if block exists.
66
     *
67
     * @param string $name      Name of text block to get.
68
     * @param bool   $formatted If true, return formmated text.
69
     *
70
     * @return string|null
71
     */
72
    public function blockText($name, $formatted = true)
73
    {
74
        if (!$block = $this->block($name)) {
75
            return;
76
        }
77
78
        if ($formatted) {
79
            return $block->formattedText();
80
        }
81
82
        return $block->text();
83
    }
84
}
85

src/Models/ContentTraits/HasLinks.php 1 location

@@ 7-94 (lines=88) @@
4
5
use Larafolio\Models\Link;
6
7
trait HasLinks
8
{
9
    /**
10
     * Get a model from a relationship by model name.
11
     *
12
     * @param string $relationship Name of relationship.
13
     * @param string $name         Name of model to get.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Model|null
16
     */
17
    abstract protected function getFromRelationshipByName($relationship, $name);
18
19
    /**
20
     * Define a polymorphic one-to-many relationship.
21
     *
22
     * @param string $related
23
     * @param string $name
24
     * @param string $type
25
     * @param string $id
26
     * @param string $localKey
27
     *
28
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
29
     */
30
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
31
32
    /**
33
     * A resource has many text blocks.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
36
     */
37
    public function links()
38
    {
39
        return $this->morphMany(Link::class, 'resource');
40
    }
41
42
    /**
43
     * Return true if project has links.
44
     *
45
     * @return bool
46
     */
47
    public function hasLinks()
48
    {
49
        return !$this->links->isEmpty();
50
    }
51
52
    /**
53
     * Get link by name, if exists.
54
     *
55
     * @param string $name Name of link to get.
56
     *
57
     * @return \Larafolio\Models\Link|null
58
     */
59
    public function link($name)
60
    {
61
        return $this->getFromRelationshipByName('links', $name);
62
    }
63
64
    /**
65
     * Get link url.
66
     *
67
     * @param string $name Name of link.
68
     *
69
     * @return string|null
70
     */
71
    public function linkUrl($name)
72
    {
73
        if (!$link = $this->link($name)) {
74
            return;
75
        }
76
77
        return $link->url();
78
    }
79
80
    /**
81
     * Get link text.
82
     *
83
     * @param string $name Name of link.
84
     *
85
     * @return string|null
86
     */
87
    public function linkText($name)
88
    {
89
        if (!$link = $this->link($name)) {
90
            return;
91
        }
92
93
        return $link->text();
94
    }
95
}
96

src/Models/ContentTraits/HasLines.php 1 location

@@ 7-84 (lines=78) @@
4
5
use Larafolio\Models\TextLine;
6
7
trait HasLines
8
{
9
    /**
10
     * Get a model from a relationship by model name.
11
     *
12
     * @param string $relationship Name of relationship.
13
     * @param string $name         Name of model to get.
14
     *
15
     * @return \Illuminate\Database\Eloquent\Model|null
16
     */
17
    abstract protected function getFromRelationshipByName($relationship, $name);
18
19
    /**
20
     * Define a polymorphic one-to-many relationship.
21
     *
22
     * @param string $related
23
     * @param string $name
24
     * @param string $type
25
     * @param string $id
26
     * @param string $localKey
27
     *
28
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
29
     */
30
    abstract public function morphMany($related, $name, $type = null, $id = null, $localKey = null);
31
32
    /**
33
     * A resource has many text lines.
34
     *
35
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
36
     */
37
    public function lines()
38
    {
39
        return $this->morphMany(TextLine::class, 'resource');
40
    }
41
42
    /**
43
     * Return true if project has lines.
44
     *
45
     * @return bool
46
     */
47
    public function hasLines()
48
    {
49
        return !$this->lines->isEmpty();
50
    }
51
52
    /**
53
     * Get a text line by name, if exists.
54
     *
55
     * @param string $name Name of text line to get.
56
     *
57
     * @return \Larafolio\Models\TextBlock|null
58
     */
59
    public function line($name)
60
    {
61
        return $this->getFromRelationshipByName('lines', $name);
62
    }
63
64
    /**
65
     * Get line text by line name, if line exists.
66
     *
67
     * @param string $name      Name of text line to get.
68
     * @param bool   $formatted If true, return formmated text.
69
     *
70
     * @return string|null
71
     */
72
    public function lineText($name, $formatted = true)
73
    {
74
        if (!$line = $this->line($name)) {
75
            return;
76
        }
77
78
        if ($formatted) {
79
            return $line->formattedText();
80
        }
81
82
        return $line->text();
83
    }
84
}
85