FieldDefinitionObject::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 1
rs 9.4285
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 39
     */
72
    public function __construct($identifier, ContentTypeObject $parent, array $data = array())
73 39
    {
74 39
        $data['identifier'] = $identifier;
75 39
        $this->contentType = &$parent;
76 39
        parent::__construct($data);
77 39
        $this->setMissingDefaults();
78
    }
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 4
     */
89
    protected function identifierToReadable($string)
90 4
    {
91
        return ucwords(str_replace('_', ' ', $string));
92
    }
93
94
    /**
95
     * @return FieldDefinitionMapper
96 38
     */
97
    public function getMapper()
98 38
    {
99 38
        if (!$this->mapper) {
100 38
            $this->mapper = new FieldDefinitionMapper($this);
101
        }
102 38
103
        return $this->mapper;
104
    }
105 39
106
    private function setMissingDefaults()
107 39
    {
108 4
        if (!isset($this->contentType->data['main_language_code'])) {
109 4
            $this->contentType->data['main_language_code'] = 'eng-GB';
110
        }
111 39
112 4
        if (!isset($this->data['names']) || empty($this->data['names'])) {
113 4
            $this->data['names'] = array(
114
                $this->contentType->data['main_language_code'] => $this->identifierToReadable($this->data['identifier']),
115 4
            );
116 39
        }
117
    }
118
119
    /**
120
     * Allows direct control in FieldDefintiionCreateStruct and FieldDefintiionUpdateStruct.
121
     *
122
     * @param \Closure $callback
123 1
     */
124
    public function setStructCallback(\Closure $callback)
125 1
    {
126 1
        $this->setProperty('struct_callback', $callback);
127
    }
128
}
129