PropertyBuilder::createProperty()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 25
rs 9.5555
c 0
b 0
f 0
cc 5
nc 4
nop 4
1
<?php
2
declare(strict_types=1);
3
4
namespace MisterIcy\ExcelWriter\Properties;
5
6
use MisterIcy\ExcelWriter\Exceptions\PropertyException;
7
8
/**
9
 * Class PropertyBuilder
10
 * @package MisterIcy\ExcelWriter\Properties
11
 */
12
final class PropertyBuilder
13
{
14
    const BOOLEAN = BoolProperty::class;
15
    const CURRENCY = CurrencyProperty::class;
16
    const DATE = DateProperty::class;
17
    const TIME = TimeProperty::class;
18
    const DATETIME = DateTimeProperty::class;
19
    const FLOAT = FloatProperty::class;
20
    const INT = IntProperty::class;
21
    const STRING = StringProperty::class;
22
23
    /**
24
     * PropertyBuilder constructor.
25
     */
26
    private function __construct()
27
    {
28
    }
29
30
    /**
31
     * @param string $propertyType
32
     * @param string|null $propertyPath
33
     * @param bool $isFormula
34
     * @param string|null $formula
35
     * @return PropertyInterface
36
     * @throws PropertyException
37
     * @throws \ReflectionException
38
     */
39
    public static function createProperty(
40
        string $propertyType = self::STRING,
41
        string $propertyPath = null,
42
        bool $isFormula = false,
43
        string $formula = null
44
    ) : AbstractProperty {
45
46
47
        $propertyReflector = new \ReflectionClass($propertyType);
48
        /** @var AbstractProperty $property */
49
        $property = $propertyReflector->newInstance();
50
51
        if ($isFormula || $property->isFormula()) {
52
            if (is_null($formula)) {
53
                throw new PropertyException("A property marked as formula, should contain a formula");
54
            }
55
            $property->setIsFormula(true)
56
                ->setFormula($formula);
57
        } else {
58
            if (is_null($propertyPath)) {
59
                throw new PropertyException("A property's path cannot be empty");
60
            }
61
            $property->setPath($propertyPath);
62
        }
63
        return $property;
64
    }
65
}
66