Completed
Push — master ( 13b77a...1dbff4 )
by Bukashk0zzz
02:00
created

LiipImagineSerializableField::checkOption()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 3 Features 0
Metric Value
c 3
b 3
f 0
dl 0
loc 14
rs 8.8571
cc 6
eloc 8
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Bukashk0zzzLiipImagineSerializationBundle
5
 *
6
 * (c) Denis Golubovskiy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Bukashk0zzz\LiipImagineSerializationBundle\Annotation;
13
14
use Doctrine\ORM\Mapping\Annotation;
15
16
/**
17
 * LiipImagineSerializableField
18
 *
19
 * @Annotation
20
 * @Target({"PROPERTY", "METHOD"})
21
 *
22
 * @author Denis Golubovskiy <[email protected]>
23
 */
24
final class LiipImagineSerializableField implements Annotation
25
{
26
    /**
27
     * @var string $filter LiipImagine Filter
28
     */
29
    private $filter;
30
31
    /**
32
     * @var string $vichUploaderField Field
33
     */
34
    private $vichUploaderField;
35
36
    /**
37
     * @var string $virtualField Virtual Field
38
     */
39
    private $virtualField;
40
41
    /**
42
     * @var array $options Options
43
     */
44
    private $options;
45
46
47
    /**
48
     * Constructor
49
     *
50
     * @param array $options Options
51
     *
52
     * @throws \Exception
53
     */
54
    public function __construct(array $options)
55
    {
56
        $this->options = $options;
57
58
        if (!array_key_exists('value', $this->options) && !array_key_exists('filter', $this->options)) {
59
            throw new \LogicException(sprintf('Either "value" or "filter" option must be set.'));
60
        }
61
62
        if ($this->checkOption('value', true)) {
63
            $this->setFilter($options['value']);
64
        } elseif ($this->checkOption('filter', true)) {
65
            $this->setFilter($this->options['filter']);
66
        }
67
68
        if ($this->checkOption('vichUploaderField', false)) {
69
            $this->setVichUploaderField($this->options['vichUploaderField']);
70
        }
71
72
        if ($this->checkOption('virtualField', false)) {
73
            $this->setVirtualField($this->options['virtualField']);
74
        }
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    public function getFilter()
81
    {
82
        return $this->filter;
83
    }
84
85
    /**
86
     * @param string $filter
87
     * @return $this
88
     */
89
    public function setFilter($filter)
90
    {
91
        $this->filter = $filter;
92
93
        return $this;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function getVichUploaderField()
100
    {
101
        return $this->vichUploaderField;
102
    }
103
104
    /**
105
     * @param string $vichUploaderField
106
     * @return $this
107
     */
108
    public function setVichUploaderField($vichUploaderField)
109
    {
110
        $this->vichUploaderField = $vichUploaderField;
111
112
        return $this;
113
    }
114
115
    /**
116
     * @return string
117
     */
118
    public function getVirtualField()
119
    {
120
        return $this->virtualField;
121
    }
122
123
    /**
124
     * @param string $virtualField
125
     * @return $this
126
     */
127
    public function setVirtualField($virtualField)
128
    {
129
        $this->virtualField = $virtualField;
130
131
        return $this;
132
    }
133
134
    /**
135
     * @param string $optionName
136
     * @param bool   $canBeArray
137
     * @return bool
138
     * @throws \Exception
139
     */
140
    private function checkOption($optionName, $canBeArray)
141
    {
142
        if (array_key_exists($optionName, $this->options)) {
143
            if ($canBeArray && !is_array($this->options[$optionName]) && !is_string($this->options[$optionName])) {
144
                throw new \InvalidArgumentException(sprintf('Option "'.$optionName.'" must be a array or string.'));
145
            } elseif (!is_string($this->options[$optionName])) {
146
                throw new \InvalidArgumentException(sprintf('Option "'.$optionName.'" must be a string.'));
147
            }
148
149
            return true;
150
        }
151
152
        return false;
153
    }
154
}
155