• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

diego-ninja / granite / 16608963009

29 Jul 2025 10:37PM UTC coverage: 50.423%. First build
16608963009

Pull #5

github

web-flow
Merge 43d8840d7 into 6a6caca51
Pull Request #5: code: adds phpstan level max, pint with per and github actions

321 of 632 new or added lines in 77 files covered. (50.79%)

1132 of 2245 relevant lines covered (50.42%)

9.88 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

57.14
/src/Mapping/PropertyMapping.php
1
<?php
2

3
namespace Ninja\Granite\Mapping;
4

5
use Ninja\Granite\Mapping\Contracts\Transformer;
6

7
class PropertyMapping
8
{
9
    private ?string $sourceProperty = null;
10
    private mixed $transformer = null;
11
    private bool $ignore = false;
12
    /**
13
     * @var callable|null Function that receives source data and returns boolean
14
     */
15
    private mixed $condition = null;
16
    private mixed $defaultValue = null;
17
    private bool $hasDefaultValue = false;
18

19
    /**
20
     * Map from specific source property.
21
     */
22
    public function mapFrom(string $sourceProperty): self
15✔
23
    {
24
        $this->sourceProperty = $sourceProperty;
15✔
25
        return $this;
15✔
26
    }
27

28
    /**
29
     * Set transformer for this property mapping.
30
     */
31
    public function using(callable|Transformer $transformer): self
26✔
32
    {
33
        $this->transformer = $transformer;
26✔
34
        return $this;
26✔
35
    }
36

37
    /**
38
     * Ignore this property during mapping.
39
     */
40
    public function ignore(): self
3✔
41
    {
42
        $this->ignore = true;
3✔
43
        return $this;
3✔
44
    }
45

46
    /**
47
     * Only apply this mapping if the condition is true.
48
     *
49
     * @param callable $condition Function that receives source data and returns boolean
50
     * @return $this
51
     */
52
    public function onlyIf(callable $condition): self
×
53
    {
54
        $this->condition = $condition;
×
55
        return $this;
×
56
    }
57

58
    /**
59
     * @param class-string $itemType
60
     */
61
    public function asCollection(
×
62
        string $itemType,
63
        bool $preserveKeys = false,
64
        bool $recursive = false,
65
        mixed $itemTransformer = null,
66
    ): self {
67
        $this->transformer = new Transformers\CollectionTransformer(
×
68
            destinationType: $itemType,
×
NEW
69
            mapper: null,
×
70
            preserveKeys: $preserveKeys,
×
71
            recursive: $recursive,
×
NEW
72
            itemTransformer: $itemTransformer,
×
73
        );
×
74

75
        return $this;
×
76
    }
77

78
    /**
79
     * Set the default value to use when condition fails or source is null.
80
     *
81
     * @param mixed $value Default value
82
     * @return $this
83
     */
84
    public function defaultValue(mixed $value): self
×
85
    {
86
        $this->defaultValue = $value;
×
87
        $this->hasDefaultValue = true;
×
88
        return $this;
×
89
    }
90

91
    /**
92
     * Transform value with context.
93
     */
94
    public function transform(mixed $value, array $sourceData = []): mixed
17✔
95
    {
96
        // Skip if explicitly ignored
97
        if ($this->ignore) {
17✔
98
            return null;
2✔
99
        }
100

101
        // Check condition if set
102
        if (null !== $this->condition && ! ($this->condition)($sourceData)) {
15✔
103
            return $this->hasDefaultValue ? $this->defaultValue : null;
×
104
        }
105

106
        // Apply transformer if set
107
        if (null !== $this->transformer) {
15✔
108
            if (is_callable($this->transformer)) {
14✔
109
                $value = ($this->transformer)($value, $sourceData);
12✔
110
            } elseif ($this->transformer instanceof Transformer) {
2✔
111
                $value = $this->transformer->transform($value, $sourceData);
2✔
112
            }
113
        }
114

115
        // Use default value if the value is null and default is set
116
        if (null === $value && $this->hasDefaultValue) {
15✔
117
            return $this->defaultValue;
×
118
        }
119

120
        return $value;
15✔
121
    }
122

123
    public function getSourceProperty(): ?string
16✔
124
    {
125
        return $this->sourceProperty;
16✔
126
    }
127

128
    public function isIgnored(): bool
11✔
129
    {
130
        return $this->ignore;
11✔
131
    }
132

133
    public function hasCondition(): bool
×
134
    {
NEW
135
        return null !== $this->condition;
×
136
    }
137

138
    /**
139
     * Get the condition callable.
140
     */
141
    public function getCondition(): mixed
9✔
142
    {
143
        return $this->condition;
9✔
144
    }
145

146
    public function getDefaultValue(): mixed
9✔
147
    {
148
        return $this->defaultValue;
9✔
149
    }
150

151
    public function hasDefaultValue(): bool
9✔
152
    {
153
        return $this->hasDefaultValue;
9✔
154
    }
155

156
    /**
157
     * Get the transformer.
158
     */
159
    public function getTransformer(): mixed
9✔
160
    {
161
        return $this->transformer;
9✔
162
    }
163

164
    /**
165
     * Set a reference to the mapper.
166
     */
167
    public function setMapper(mixed $mapper): self
×
168
    {
NEW
169
        if ($this->transformer instanceof Transformers\CollectionTransformer && $mapper instanceof Contracts\Mapper) {
×
170
            $this->transformer->setMapper($mapper);
×
171
        }
172

173
        return $this;
×
174
    }
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc