Completed
Pull Request — 1.0 (#53)
by Harald
05:33
created

FieldDefinitionObject::identifierToReadable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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\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 $parent;
63
64
    /**
65
     * @var FieldDefinitionMapper
66
     */
67
    private $mapper;
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 13
    public function __construct($identifier, ContentTypeObject $parent, $data = array())
73
    {
74 13
        $data['identifier'] = $identifier;
75 13
        $this->parent = &$parent;
76 13
        parent::__construct($data);
77 13
        $this->setMissingDefaults();
78 13
    }
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
    protected function identifierToReadable($string)
90
    {
91
        return ucwords(str_replace('_', ' ', $string));
92
    }
93
94
    /**
95
     * @return FieldDefinitionMapper
96
     */
97 13
    public function getMapper()
98
    {
99 13
        if (!$this->mapper) {
100 13
            $this->mapper = new FieldDefinitionMapper($this);
101 13
        }
102
103 13
        return $this->mapper;
104
    }
105
106 13
    private function setMissingDefaults()
107
    {
108 13
        if (!isset($this->data['names']) || empty($this->data['names'])) {
109
            $this->data['names'] = array(
110
                $this->parent->data['main_language_code'] => $this->identifierToReadable($this->data['identifier']),
111
            );
112
        }
113 13
    }
114
}
115