Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.82% covered (warning)
81.82%
9 / 11
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ManyToOneAttributeConverter
81.82% covered (warning)
81.82%
9 / 11
33.33% covered (danger)
33.33%
1 / 3
6.22
0.00% covered (danger)
0.00%
0 / 1
 applyToDomain
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 applyToProperty
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 applyToStorage
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
1<?php
2namespace Apie\StorageMetadata\PropertyConverters;
3
4use Apie\Core\TypeUtils;
5use Apie\StorageMetadata\Attributes\ManyToOneAttribute;
6use Apie\StorageMetadata\Interfaces\PropertyConverterInterface;
7use Apie\StorageMetadata\Mediators\DomainToStorageContext;
8use ReflectionProperty;
9
10class ManyToOneAttributeConverter implements PropertyConverterInterface
11{
12    public function applyToDomain(
13        DomainToStorageContext $context
14    ): void {
15        // no-op
16    }
17
18    public static function applyToProperty(ReflectionProperty $property, object $object, object $parentObject): void
19    {
20        foreach ($property->getAttributes(ManyToOneAttribute::class) as $propertyAttribute) {
21            if (TypeUtils::matchesType(
22                $property->getType(),
23                $parentObject
24            )) {
25                $property->setValue($object, $parentObject);
26            } else {
27                $property->setValue($object, null);
28            }
29        }
30    }
31
32    public function applyToStorage(
33        DomainToStorageContext $context
34    ): void {
35        if (!isset($context->parentContext)) {
36            return;
37        }
38        self::applyToProperty($context->storageProperty, $context->storageObject, $context->parentContext->storageObject);
39    }
40}