Passed
Branch 1.0 (733517)
by Harald
06:28
created

FieldDefinitionObject   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 58
ccs 19
cts 19
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getMapper() 0 8 2
A identifierToReadable() 0 4 1
A setMissingDefaults() 0 8 3
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\Repository\Values;
11
12
use Transfer\EzPlatform\Repository\Values\Mapper\FieldDefinitionMapper;
13
14
/*
15
16
** Available keys: **
17
18
    $parent = Transfer\EzPlatform\Data\ContentTypeObject
19
20
    $data = [
21
        identifer           => string
22
        type                => string
23
        names               => string[]
24
        descriptions        => string[]
25
        field_group         => string
26
        position            => int
27
        is_translatable     => bool
28
        is_required         => bool
29
        is_info_collector   => bool
30
        is_searchable       => bool
31
    ],
32
    $properties = [
33
        <none>
34
    ]
35
36
37
** Required on `create`:
38
**** Required by transfer:
39
    An `identifier` unique to its ContentType(Object)
40
41
**** Required by eZ:
42
    An `identifier` unique to its ContentType
43
    A type, transfer defaults to ezstring
44
45
** Required on `update`:
46
**** Required by transfer:
47
    An `identifier`
48
49
**** Required by eZ:
50
    An `identifier`
51
52
*/
53
54
/**
55
 * Content type object.
56
 */
57
class FieldDefinitionObject extends EzPlatformObject
58
{
59
    /**
60
     * @var ContentTypeObject
61
     */
62
    private $contentType;
63
64
    /**
65
     * @var FieldDefinitionMapper
66
     */
67
    private $mapper;
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 38
    public function __construct($identifier, ContentTypeObject $parent, $data = array())
73
    {
74 38
        $data['identifier'] = $identifier;
75 38
        $this->contentType = &$parent;
76 38
        parent::__construct($data);
77 38
        $this->setMissingDefaults();
78 38
    }
79
80
    /**
81
     * Converts a string to one or more words
82
     *  'name' -> 'Name'
83
     *  'short_description' -> 'Short Description'.
84
     *
85
     * @param string $string
86
     *
87
     * @return string
88
     */
89 3
    protected function identifierToReadable($string)
90
    {
91 3
        return ucwords(str_replace('_', ' ', $string));
92
    }
93
94
    /**
95
     * @return FieldDefinitionMapper
96
     */
97 33
    public function getMapper()
98
    {
99 33
        if (!$this->mapper) {
100 33
            $this->mapper = new FieldDefinitionMapper($this);
101 33
        }
102
103 33
        return $this->mapper;
104
    }
105
106 38
    private function setMissingDefaults()
107
    {
108 38
        if (!isset($this->data['names']) || empty($this->data['names'])) {
109 3
            $this->data['names'] = array(
110 3
                $this->contentType->data['main_language_code'] => $this->identifierToReadable($this->data['identifier']),
111
            );
112 3
        }
113 38
    }
114
}
115