Completed
Push — master ( f24189...ede2db )
by Samuel
26:59 queued 11:50
created

Uuid4Behaviour::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleMapper\Behaviour;
6
7
use Ramsey\Uuid\Uuid;
8
9
class Uuid4Behaviour extends AbstractBehaviour
10
{
11
    /** @var string */
12
    private $field;
13
14
    /**
15
     * @param string $field
16
     */
17
    public function __construct(string $field = 'id')
18
    {
19
        $this->field = $field;
20
    }
21
22
    /**
23
     * @param array $data
24
     * @return array
25
     */
26
    public function beforeInsert(array $data): array
27
    {
28
        if (!array_key_exists($this->field, $data) || !$data[$this->field]) {
29
            $data[$this->field] = Uuid::uuid4();
30
        }
31
        return $data;
32
    }
33
}
34