Metadata::setAlias()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of Sulu.
5
 *
6
 * (c) MASSIVE ART WebServices GmbH
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Sulu\Component\DocumentManager;
13
14
class Metadata
15
{
16
    /**
17
     * @var string
18
     */
19
    private $class;
20
21
    /**
22
     * @var string
23
     */
24
    private $alias;
25
26
    /**
27
     * @var string
28
     */
29
    private $phpcrType;
30
31
    /**
32
     * @var \ReflectionClass
33
     */
34
    private $reflection;
35
36
    /**
37
     * @var array
38
     */
39
    private $fieldMappings;
40
41
    /**
42
     * Add a field mapping for field with given name, for example:.
43
     *
44
     * ````
45
     * $metadata->addFieldMapping(array(
46
     *     'encoding' => 'content',
47
     *     'property' => 'phpcr_property_name',
48
     * ));
49
     * ````
50
     *
51
     * @param string $name Name of field/property in the mapped class.
52
     * @param array $mapping {
53
     *
54
     *   @var string Encoding type to use, @see \Sulu\Component\DocumentManager\PropertyEncoder::encode()
55
     *   @var string PHPCR property name (excluding the prefix)
56
     *   @var string Type of field (leave blank to determine automatically)
57
     *   @var bool If the field should be mapped. Set to false to manually persist and hydrate the data.
58
     * }
59
     */
60
    public function addFieldMapping($name, $mapping)
61
    {
62
        $mapping = array_merge([
63
            'encoding' => 'content',
64
            'property' => $name,
65
            'type' => null,
66
            'mapped' => true,
67
            'multiple' => false,
68
            'default' => null,
69
        ], $mapping);
70
71
        $this->fieldMappings[$name] = $mapping;
72
    }
73
74
    /**
75
     * Return all field mappings.
76
     *
77
     * @return array
78
     */
79
    public function getFieldMappings()
80
    {
81
        return $this->fieldMappings;
82
    }
83
84
    /**
85
     * @return string
86
     */
87
    public function getClass()
88
    {
89
        return $this->class;
90
    }
91
92
    /**
93
     * @param string $class
94
     */
95
    public function setClass($class)
96
    {
97
        $this->class = $class;
98
        $this->reflection = null;
99
    }
100
101
    /**
102
     * @return ReflectionClass
103
     */
104
    public function getReflectionClass()
105
    {
106
        if ($this->reflection) {
107
            return $this->reflection;
108
        }
109
110
        if (!$this->class) {
111
            throw new \InvalidArgumentException(
112
                'Cannot retrieve ReflectionClass on metadata which has no class attribute'
113
            );
114
        }
115
116
        $this->reflection = new \ReflectionClass($this->class);
117
118
        return $this->reflection;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getAlias()
125
    {
126
        return $this->alias;
127
    }
128
129
    /**
130
     * @param string $alias
131
     */
132
    public function setAlias($alias)
133
    {
134
        $this->alias = $alias;
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    public function getPhpcrType()
141
    {
142
        return $this->phpcrType;
143
    }
144
145
    /**
146
     * @param string $phpcrType
147
     */
148
    public function setPhpcrType($phpcrType)
149
    {
150
        $this->phpcrType = $phpcrType;
151
    }
152
}
153