Passed
Push — master ( c0f43d...08ed67 )
by Julien
05:01
created

Slug::getSlugBehavior()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Zemit\Mvc\Model;
15
use Zemit\Mvc\Model\AbstractTrait\AbstractBehavior;
16
use Zemit\Mvc\Model\AbstractTrait\AbstractInjectable;
17
use Zemit\Mvc\Model\Behavior\Transformable;
18
19
trait Slug
20
{
21
    use AbstractInjectable;
22
    use AbstractBehavior;
23
    use Options;
24
    
25
    /**
26
     * Initializing Slug
27
     */
28 2
    public function initializeSlug(?array $options = null): void
29
    {
30 2
        $options ??= $this->getOptionsManager()->get('slug') ?? [];
31
        
32 2
        $field = $options['field'] ?? 'slug';
33
        
34 2
        $this->setSlugBehavior(new Transformable([
35 2
            'beforeValidation' => [
36 2
                $field => function (Model $model, string $field) {
37
                    $value = $model->readAttribute($field);
38
                    return $value && is_string($value) ? \Zemit\Support\Slug::generate($value) : $value;
39 2
                },
40 2
            ],
41 2
        ]));
42
    }
43
    
44
    /**
45
     * Set Slug Behavior
46
     */
47 2
    public function setSlugBehavior(Transformable $slugBehavior): void
48
    {
49 2
        $this->setBehavior('slug', $slugBehavior);
0 ignored issues
show
Bug introduced by
The method setBehavior() does not exist on Zemit\Mvc\Model\Slug. Did you maybe mean setSlugBehavior()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->/** @scrutinizer ignore-call */ 
50
               setBehavior('slug', $slugBehavior);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
    }
51
    
52
    /**
53
     * Get Slug Behavior
54
     */
55
    public function getSlugBehavior(): Transformable
56
    {
57
        $slugBehavior = $this->getBehavior('slug');
0 ignored issues
show
Bug introduced by
The method getBehavior() does not exist on Zemit\Mvc\Model\Slug. Did you maybe mean getSlugBehavior()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        /** @scrutinizer ignore-call */ 
58
        $slugBehavior = $this->getBehavior('slug');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
        assert($slugBehavior instanceof Transformable);
59
        return $slugBehavior;
60
    }
61
}
62