Failed Conditions
Pull Request — master (#4141)
by Owen
16:19 queued 01:34
created

StringValueBinder::setSetIgnoredErrors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
rs 10
ccs 2
cts 2
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Cell;
4
5
use DateTimeInterface;
6
use PhpOffice\PhpSpreadsheet\Exception as SpreadsheetException;
7
use PhpOffice\PhpSpreadsheet\RichText\RichText;
8
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
9
use Stringable;
10
11
class StringValueBinder extends DefaultValueBinder implements IValueBinder
12
{
13
    protected bool $convertNull = true;
14
15
    protected bool $convertBoolean = true;
16
17
    protected bool $convertNumeric = true;
18
19
    protected bool $convertFormula = true;
20
21 3
    protected bool $setIgnoredErrors = false;
22
23 3
    public function setSetIgnoredErrors(bool $setIgnoredErrors = false): self
24
    {
25 3
        $this->setIgnoredErrors = $setIgnoredErrors;
26
27
        return $this;
28 8
    }
29
30 8
    public function setNullConversion(bool $suppressConversion = false): self
31
    {
32 8
        $this->convertNull = $suppressConversion;
33
34
        return $this;
35 6
    }
36
37 6
    public function setBooleanConversion(bool $suppressConversion = false): self
38
    {
39
        $this->convertBoolean = $suppressConversion;
40 11
41
        return $this;
42 11
    }
43
44 11
    public function getBooleanConversion(): bool
45
    {
46
        return $this->convertBoolean;
47 2
    }
48
49 2
    public function setNumericConversion(bool $suppressConversion = false): self
50
    {
51 2
        $this->convertNumeric = $suppressConversion;
52
53
        return $this;
54 22
    }
55
56 22
    public function setFormulaConversion(bool $suppressConversion = false): self
57 22
    {
58 22
        $this->convertFormula = $suppressConversion;
59 22
60
        return $this;
61 22
    }
62
63
    public function setConversionForAllValueTypes(bool $suppressConversion = false): self
64
    {
65
        $this->convertNull = $suppressConversion;
66
        $this->convertBoolean = $suppressConversion;
67
        $this->convertNumeric = $suppressConversion;
68
        $this->convertFormula = $suppressConversion;
69
70 72
        return $this;
71
    }
72 72
73 3
    /**
74
     * Bind value to a cell.
75 70
     *
76 1
     * @param Cell $cell Cell to bind value to
77
     * @param mixed $value Value to bind in cell
78
     */
79
    public function bindValue(Cell $cell, mixed $value): bool
80 69
    {
81 29
        if (is_object($value)) {
82
            return $this->bindObjectValue($cell, $value);
83
        }
84 69
        if ($value !== null && !is_scalar($value)) {
85 2
            throw new SpreadsheetException('Unable to bind unstringable ' . gettype($value));
86 67
        }
87 6
88 63
        // sanitize UTF-8 strings
89 16
        if (is_string($value)) {
90 47
            $value = StringHelper::sanitizeUTF8($value);
91 2
        }
92
93 45
        if ($value === null && $this->convertNull === false) {
94
            $cell->setValueExplicit($value, DataType::TYPE_NULL);
95
        } elseif (is_bool($value) && $this->convertBoolean === false) {
96 69
            $cell->setValueExplicit($value, DataType::TYPE_BOOL);
97
        } elseif ((is_int($value) || is_float($value)) && $this->convertNumeric === false) {
98
            $cell->setValueExplicit($value, DataType::TYPE_NUMERIC);
99 3
        } elseif (is_string($value) && strlen($value) > 1 && $value[0] === '=' && $this->convertFormula === false && parent::dataTypeForValue($value) === DataType::TYPE_FORMULA) {
100
            $cell->setValueExplicit($value, DataType::TYPE_FORMULA);
101
        } else {
102 3
            if ($this->setIgnoredErrors && is_numeric($value)) {
103 1
                $cell->getIgnoredErrors()->setNumberStoredAsText(true);
104 1
            }
105 2
            $cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
106 1
        }
107 1
108 1
        return true;
109
    }
110 1
111
    protected function bindObjectValue(Cell $cell, object $value): bool
112
    {
113 3
        // Handle any objects that might be injected
114
        if ($value instanceof DateTimeInterface) {
115
            $value = $value->format('Y-m-d H:i:s');
116
            $cell->setValueExplicit($value, DataType::TYPE_STRING);
117
        } elseif ($value instanceof RichText) {
118
            $cell->setValueExplicit($value, DataType::TYPE_INLINE);
119
        } elseif ($value instanceof Stringable) {
120
            $cell->setValueExplicit((string) $value, DataType::TYPE_STRING);
121
        } else {
122
            throw new SpreadsheetException('Unable to bind unstringable object of type ' . get_class($value));
123
        }
124
125
        return true;
126
    }
127
}
128