Passed
Push — main ( 2bf33d...adc87c )
by Gaetano
08:33
created

ReferenceSetterTrait::isValidReferenceValue()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 8
c 0
b 0
f 0
nc 5
nop 1
dl 0
loc 14
rs 9.2222
1
<?php
2
3
namespace Kaliop\eZMigrationBundle\Core\Executor;
4
5
use Kaliop\eZMigrationBundle\API\Collection\AbstractCollection;
6
use Kaliop\eZMigrationBundle\API\Exception\InvalidMatchResultsNumberException;
7
use Kaliop\eZMigrationBundle\API\Exception\InvalidStepDefinitionException;
8
use Kaliop\eZMigrationBundle\API\Value\MigrationStep;
9
10
/**
11
 * A trait used by Executors which hsa code useful to set values to references.
12
 * @todo add a method that validates the 'references' key. Atm all we can check is that it is an array (we could as well
13
 * reject empty reference arrays in fact)
14
 */
15
trait ReferenceSetterTrait
16
{
17
    /**
18
     * Allows to have refs defined using two syntax variants:
19
     *   - { identifier: xxx, attribute: yyy, overwrite: bool }
20
     * or
21
     *   identifier: attribute
22
     * @param $key
23
     * @param $value
24
     * @return array
25
     * @throws InvalidStepDefinitionException
26
     *
27
     * @todo should we resolve references in ref identifiers, attributes and overwrite? Inception! :-D
28
     */
29
    protected function parseReferenceDefinition($key, $value)
30
    {
31
        if (is_string($key) && is_string($value)) {
32
            return array('identifier' => $key, 'attribute' => $value);
33
        }
34
        if (!is_array($value) || !isset($value['identifier']) || ! isset($value['attribute'])) {
35
            throw new InvalidStepDefinitionException("Invalid reference definition for reference number $key");
36
        }
37
        return $value;
38
    }
39
40
    /**
41
     * Valid reference values are either scalars or nested arrays. No objects, no resources.
42
     * @param mixed $value
43
     * @return bool
44
     */
45
    protected function isValidReferenceValue($value)
46
    {
47
        if (is_scalar($value)) {
48
            return true;
49
        }
50
        if (is_object($value) || is_resource($value)) {
51
            return false;
52
        }
53
        foreach($value as $subValue) {
54
            if (!$this->isValidReferenceValue($subValue)) {
55
                return false;
56
            }
57
        }
58
        return true;
59
    }
60
}
61