Passed
Push — master ( a5b307...a707f9 )
by Ondřej
02:16
created

StrictComposite::fromMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
namespace Ivory\Value;
4
5
use Ivory\Exception\NotImplementedException;
6
7
/**
8
 * Base for type-strict composite values.
9
 *
10
 * Specializes the general {@link Composite} class such that:
11
 * - the constructor expects a map of values of all attributes the composite defines;
12
 * - the attribute getter emits a warning upon accessing an undefined attribute.
13
 */
14
abstract class StrictComposite extends Composite
15
{
16
    public static function fromMap(array $valueMap): Composite
17
    {
18
        throw new NotImplementedException(
19
            __METHOD__ . ' must be re-implemented by ' . static::class . ' - base ' . Composite::class .
20
            ' implementation cannot be used'
21
        );
22
    }
23
24
    public function __get($name)
25
    {
26
        $val = parent::__get($name);
27
28
        if ($val === null && !array_key_exists($name, $this->toMap())) {
29
            trigger_error(
30
                sprintf('Accessing an undefined attribute "%s" of %s', $name, get_class($this)),
31
                E_USER_WARNING
32
            );
33
        }
34
35
        return $val;
36
    }
37
}
38