Completed
Push — master ( 64cadb...9a4f3f )
by Jelle
04:20
created

PropertyDefinition::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 5
ccs 5
cts 5
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 3
crap 1
1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
namespace TheSportsDb\PropertyMapper;
10
11
use TheSportsDb\Entity\EntityInterface;
12
use TheSportsDb\Entity\Factory\FactoryContainerInterface;
13
14
/**
15
 * Description of Property
16
 *
17
 * @author drupalpro
18
 */
19
class PropertyDefinition {
20
21
  /**
22
   * The property name.
23
   *
24
   * @var string
25
   */
26
  protected $name;
27
28
  /**
29
   * The entity type if this property is an entity.
30
   *
31
   * @var string
32
   */
33
  protected $entityType;
34
35
  /**
36
   * Whether this property is an array or not.
37
   *
38
   * @var bool
39
   */
40
  protected $isArray;
41
42
  /**
43
   * Creates a new property definition.
44
   *
45
   * @param string $name
46
   *   The name of the property.
47
   * @param string|null $entityType
48
   *   The entity type of the property if it's an entity.
49
   * @param bool $isArray
50
   *   Whether or not this property is an array.
51
   */
52 3
  public function __construct($name, $entityType = NULL, $isArray = FALSE) {
53 3
    $this->name = $name;
54 3
    $this->entityType = $entityType;
55 3
    $this->isArray = $isArray;
56 3
  }
57
58
  /**
59
   * Gets the property name.
60
   *
61
   * @return string
62
   *   The property name.
63
   */
64 1
  public function getName() {
65 1
    return $this->name;
66
  }
67
68
  /**
69
   * Gets the entity type.
70
   *
71
   * @return string|null
72
   *   The entity type of the property if it's an entity.
73
   */
74 1
  public function getEntityType() {
75 1
    return $this->entityType;
76
  }
77
78
  /**
79
   * Whether or not this property is an array.
80
   *
81
   * @return bool
82
   *   TRUE if it's an array, FALSE, otherwise.
83
   */
84 1
  public function isArray() {
85 1
    return $this->isArray;
86
  }
87
88
  /**
89
   * Sanitizes an object property based on this definition.
90
   *
91
   * @param \stdClass $object
92
   *   The object of which to sanitize the property.
93
   * @param \TheSportsDb\Entity\Factory\FactoryContainerInterface $factoryContainer
94
   *   The factory container.
95
   *
96
   * @return void
97
   */
98 1
  public function sanitizeProperty(\stdClass &$object, FactoryContainerInterface $factoryContainer) {
99 1
    if (($entityType = $this->getEntityType()) && isset($object->{$this->getName()})) {
100 1
      $value = &$object->{$this->getName()};
101 1
      if ($this->isArray()) {
102 1
        foreach ($value as &$val) {
103 1
          if ($val instanceof EntityInterface) {
104 1
            continue;
105
          }
106 1
          $val = $factoryContainer->getFactory($entityType)->create($val, $entityType);
107
        }
108
      }
109 1
      elseif (!($value instanceof EntityInterface)) {
110 1
        $value = $factoryContainer->getFactory($entityType)->create($value, $entityType);
111
      }
112
    }
113 1
  }
114
}
115