Completed
Push — 1.0 ( 19d976...2156a0 )
by Valentin
05:35
created

FieldDefinitionObject::getMapper()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
3
/*
4
 * This file is part of Transfer.
5
 *
6
 * For the full copyright and license information, please view the LICENSE file located
7
 * in the root directory.
8
 */
9
10
namespace Transfer\EzPlatform\Data;
11
12
use Transfer\Data\ValueObject;
13
use Transfer\EzPlatform\Repository\Content\FieldDefinitionMapper;
14
15
/**
16
 * Content type object.
17
 */
18
class FieldDefinitionObject extends ValueObject
19
{
20
    /**
21
     * @var ContentTypeObject
22
     */
23
    private $parent;
24
25
    /**
26
     * @var FieldDefinitionMapper
27
     */
28
    private $mapper;
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 15
    public function __construct($identifier, ContentTypeObject $parent, $data = array())
34
    {
35 15
        $data['identifier'] = $identifier;
36 15
        $this->parent = &$parent;
37 15
        parent::__construct($data);
38 15
        $this->setMissingDefaults();
39 15
    }
40
41
    /**
42
     * Converts a string to one or more words
43
     *  'name' -> 'Name'
44
     *  'short_description' -> 'Short Description'.
45
     *
46
     * @param string $string
47
     *
48
     * @return string
49
     */
50 3
    protected function identifierToReadable($string)
51
    {
52 3
        return ucwords(str_replace('_', ' ', $string));
53
    }
54
55
    /**
56
     * @return FieldDefinitionMapper
57
     */
58 9
    public function getMapper()
59
    {
60 9
        if (!$this->mapper) {
61 9
            $this->mapper = new FieldDefinitionMapper($this);
62 9
        }
63
64 9
        return $this->mapper;
65
    }
66
67 15
    private function setMissingDefaults()
68
    {
69 15
        if (!isset($this->data['names']) || empty($this->data['names'])) {
70 3
            $this->data['names'] = array(
71 3
                $this->parent->data['main_language_code'] => $this->identifierToReadable($this->data['identifier']),
72
            );
73 3
        }
74 15
    }
75
}
76