AbstractWrite::updateProperty()
last analyzed

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 1
1
<?php
2
3
namespace Saxulum\Accessor\Accessors;
4
5
use Saxulum\Accessor\CallbackBag;
6
use Saxulum\Hint\Hint;
7
use Saxulum\Accessor\Prop;
8
9
abstract class AbstractWrite extends AbstractAccessor
10
{
11
    /**
12
     * @var array
13
     */
14
    protected static $remoteToPrefixMapping = array();
15
16
    /**
17
     * @param  CallbackBag $callbackBag
18
     * @return mixed
19
     * @throws \Exception
20
     */
21
    public function callback(CallbackBag $callbackBag)
22
    {
23
        if (!$callbackBag->argumentExists(0)) {
24
            throw new \InvalidArgumentException($this->getPrefix() . ' accessor needs at least a value!');
25
        }
26
27
        Hint::validateOrException(
28
            $callbackBag->getName(),
29
            $callbackBag->getArgument(0),
30
            $callbackBag->getHint(),
31
            $callbackBag->getNullable()
32
        );
33
34
        $this->propertyDefault($callbackBag);
35
        $this->updateProperty($callbackBag);
36
37
        return $callbackBag->getObject();
38
    }
39
40
    /**
41
     * @param  CallbackBag $callbackBag
42
     * @return void
43
     */
44
    abstract protected function propertyDefault(CallbackBag $callbackBag);
45
46
    /**
47
     * @param  CallbackBag $callbackBag
48
     * @return void
49
     */
50
    abstract protected function updateProperty(CallbackBag $callbackBag);
51
52
    /**
53
     * @param  Prop        $prop
54
     * @return string|null
55
     */
56
    protected function getPrefixByProp(Prop $prop)
57
    {
58
        if (null !== $mappedType = $prop->getMappedType()) {
59
            if (isset(static::$remoteToPrefixMapping[$prop->getMappedType()])) {
60
                return static::$remoteToPrefixMapping[$prop->getMappedType()];
61
            }
62
        }
63
64
        return null;
65
    }
66
67
    /**
68
     * @param  Prop   $prop
69
     * @param  string $namespace
70
     * @return string
71
     */
72
    public static function generatePhpDoc(Prop $prop, $namespace)
73
    {
74
        $name = $prop->getName();
75
76
        return '* @method $this ' . static::PREFIX . ucfirst($name) . '(' .  static::getPhpDocHint($prop, $namespace) . '$' . $name . ')';
77
    }
78
}
79