Completed
Push — 1.0 ( df18cb...d1b573 )
by Valentin
01:34
created

FieldDefinitionObject::setStructCallback()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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