ObjectAliases   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 64
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A aliasGet() 0 7 2
A aliasSet() 0 10 3
A expandAlias() 0 3 1
1
<?php
2
3
namespace Silk\Type;
4
5
trait ObjectAliases
6
{
7
    /**
8
     * Get all object aliases as a dictionary.
9
     *
10
     * Eg. ['aliasName' => 'propertyNameOnObject']
11
     *
12
     * @return array
13
     */
14
    abstract protected function objectAliases();
15
16
    /**
17
     * Get the aliased object instance.
18
     *
19
     * @return object
20
     */
21
    abstract protected function getAliasedObject();
22
23
    /**
24
     * Get a property from the aliased object by the model's key.
25
     *
26
     * @param $key
27
     *
28
     * @return mixed|null
29
     */
30
    protected function aliasGet($key)
31
    {
32
        if (! $expanded = $this->expandAlias($key)) {
33
            return null;
34
        }
35
36
        return data_get($this->getAliasedObject(), $expanded);
37
    }
38
39
    /**
40
     * Set a property on the aliased object.
41
     *
42
     * @param string $key   The alias name on the model
43
     * @param mixed  $value The value to set on the aliased object
44
     *
45
     * @return bool          True if the alias was resolved and set; otherwise false
46
     */
47
    protected function aliasSet($key, $value)
48
    {
49
        $expanded = $this->expandAlias($key);
50
51
        if ($expanded && is_object($aliased = $this->getAliasedObject())) {
52
            $aliased->$expanded = $value;
53
            return true;
54
        }
55
56
        return false;
57
    }
58
59
    /**
60
     * Expands an alias into its respective object property name.
61
     *
62
     * @param string $key  Alias key
63
     *
64
     * @return string|false  The expanded alias, or false no alias exists for the key.
65
     */
66
    protected function expandAlias($key)
67
    {
68
        return data_get($this->objectAliases(), $key, false);
0 ignored issues
show
Bug Best Practice introduced by
The expression return data_get($this->o...Aliases(), $key, false) also could return the type array which is incompatible with the documented return type false|string.
Loading history...
69
    }
70
}
71