Test Setup Failed
Pull Request — master (#20)
by
unknown
02:00
created

SettableModel   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 29
rs 10
c 0
b 0
f 0
wmc 3
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
A __set() 0 20 3
1
<?php
2
3
namespace Scriptotek\Alma\Model;
4
5
use Scriptotek\Alma\Model\Model;
6
7
abstract class SettableModel extends Model
8
{
9
    /**
10
     * Magic setter function to set properties on the underlying data object
11
     * 
12
     * @param string $key
13
     * @param mixed $value
14
     */
15
    public function __set($key, $value)
16
    {
17
        // Convert electronic_collections to ElectronicCollections
18
        $key_s = implode('', array_map(function ($x) {
19
            return ucfirst($x);
20
        }, explode('_', $key)));
21
22
        // If there's a setter method, call it.
23
        $method = 'set' . ucfirst($key_s);
24
        if (method_exists($this, $method)) {
25
            return $this->$method($value);
26
        }
27
        $method = 'set' . ucfirst($key);
28
        if (method_exists($this, $method)) {
29
            return $this->$method($value);
30
        }
31
32
        // Otherwise set the key on the data object
33
        $this->data->{$key} = $value;
34
    }
35
}
36