Completed
Branch master (5737c6)
by Rémi
02:51
created
src/Drivers/DBAdapter.php 1 patch
Indentation   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -4,38 +4,38 @@
 block discarded – undo
4 4
 
5 5
 interface DBAdapter
6 6
 {
7
-    /**
8
-     * Return's Driver specific Query Implementation.
9
-     *
10
-     * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
11
-     */
12
-    public function getQuery();
7
+	/**
8
+	 * Return's Driver specific Query Implementation.
9
+	 *
10
+	 * @return \Analogue\ORM\Drivers\QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter
11
+	 */
12
+	public function getQuery();
13 13
 
14
-    /**
15
-     * Return the Date format used on this adapter.
16
-     *
17
-     * @return string
18
-     */
19
-    public function getDateFormat();
14
+	/**
15
+	 * Return the Date format used on this adapter.
16
+	 *
17
+	 * @return string
18
+	 */
19
+	public function getDateFormat();
20 20
 
21
-    /**
22
-     * Start a DB transaction on driver that supports it.
23
-     *
24
-     * @return void
25
-     */
26
-    public function beginTransaction();
21
+	/**
22
+	 * Start a DB transaction on driver that supports it.
23
+	 *
24
+	 * @return void
25
+	 */
26
+	public function beginTransaction();
27 27
 
28
-    /**
29
-     * Commit a DB transaction on driver that supports it.
30
-     *
31
-     * @return void
32
-     */
33
-    public function commit();
28
+	/**
29
+	 * Commit a DB transaction on driver that supports it.
30
+	 *
31
+	 * @return void
32
+	 */
33
+	public function commit();
34 34
 
35
-    /**
36
-     * Rollback a DB transaction on driver that supports it.
37
-     *
38
-     * @return void
39
-     */
40
-    public function rollback();
35
+	/**
36
+	 * Rollback a DB transaction on driver that supports it.
37
+	 *
38
+	 * @return void
39
+	 */
40
+	public function rollback();
41 41
 }
Please login to merge, or discard this patch.
src/Drivers/IlluminateDriver.php 1 patch
Indentation   +36 added lines, -36 removed lines patch added patch discarded remove patch
@@ -4,44 +4,44 @@
 block discarded – undo
4 4
 
5 5
 class IlluminateDriver implements DriverInterface
6 6
 {
7
-    /**
8
-     * The Illuminate Connection Provider.
9
-     *
10
-     * @var CapsuleConnectionProvider|IlluminateConnectionProvider
11
-     */
12
-    protected $connectionProvider;
7
+	/**
8
+	 * The Illuminate Connection Provider.
9
+	 *
10
+	 * @var CapsuleConnectionProvider|IlluminateConnectionProvider
11
+	 */
12
+	protected $connectionProvider;
13 13
 
14
-    /**
15
-     * IlluminateDriver constructor.
16
-     *
17
-     * @param $connectionProvider
18
-     */
19
-    public function __construct($connectionProvider)
20
-    {
21
-        $this->connectionProvider = $connectionProvider;
22
-    }
14
+	/**
15
+	 * IlluminateDriver constructor.
16
+	 *
17
+	 * @param $connectionProvider
18
+	 */
19
+	public function __construct($connectionProvider)
20
+	{
21
+		$this->connectionProvider = $connectionProvider;
22
+	}
23 23
 
24
-    /**
25
-     * Return the name of the driver.
26
-     *
27
-     * @return string
28
-     */
29
-    public function getName()
30
-    {
31
-        return 'illuminate';
32
-    }
24
+	/**
25
+	 * Return the name of the driver.
26
+	 *
27
+	 * @return string
28
+	 */
29
+	public function getName()
30
+	{
31
+		return 'illuminate';
32
+	}
33 33
 
34
-    /**
35
-     * Get Analogue DBAdapter.
36
-     *
37
-     * @param string|null $connection
38
-     *
39
-     * @return IlluminateDBAdapter
40
-     */
41
-    public function getAdapter($connection = null)
42
-    {
43
-        $connection = $this->connectionProvider->connection($connection);
34
+	/**
35
+	 * Get Analogue DBAdapter.
36
+	 *
37
+	 * @param string|null $connection
38
+	 *
39
+	 * @return IlluminateDBAdapter
40
+	 */
41
+	public function getAdapter($connection = null)
42
+	{
43
+		$connection = $this->connectionProvider->connection($connection);
44 44
 
45
-        return new IlluminateDBAdapter($connection);
46
-    }
45
+		return new IlluminateDBAdapter($connection);
46
+	}
47 47
 }
Please login to merge, or discard this patch.
src/Drivers/CapsuleConnectionProvider.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -6,30 +6,30 @@
 block discarded – undo
6 6
 
7 7
 class CapsuleConnectionProvider
8 8
 {
9
-    /**
10
-     * @var Capsule
11
-     */
12
-    protected $capsule;
9
+	/**
10
+	 * @var Capsule
11
+	 */
12
+	protected $capsule;
13 13
 
14
-    /**
15
-     * CapsuleConnectionProvider constructor.
16
-     *
17
-     * @param Capsule $capsule
18
-     */
19
-    public function __construct(Capsule $capsule)
20
-    {
21
-        $this->capsule = $capsule;
22
-    }
14
+	/**
15
+	 * CapsuleConnectionProvider constructor.
16
+	 *
17
+	 * @param Capsule $capsule
18
+	 */
19
+	public function __construct(Capsule $capsule)
20
+	{
21
+		$this->capsule = $capsule;
22
+	}
23 23
 
24
-    /**
25
-     * Get a Database connection object.
26
-     *
27
-     * @param  $name
28
-     *
29
-     * @return \Illuminate\Database\Connection
30
-     */
31
-    public function connection($name = null)
32
-    {
33
-        return $this->capsule->getConnection($name);
34
-    }
24
+	/**
25
+	 * Get a Database connection object.
26
+	 *
27
+	 * @param  $name
28
+	 *
29
+	 * @return \Illuminate\Database\Connection
30
+	 */
31
+	public function connection($name = null)
32
+	{
33
+		return $this->capsule->getConnection($name);
34
+	}
35 35
 }
Please login to merge, or discard this patch.
src/Drivers/Manager.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -4,33 +4,33 @@
 block discarded – undo
4 4
 
5 5
 class Manager
6 6
 {
7
-    /**
8
-     * @var DriverInterface[]
9
-     */
10
-    protected $drivers = [];
7
+	/**
8
+	 * @var DriverInterface[]
9
+	 */
10
+	protected $drivers = [];
11 11
 
12
-    /**
13
-     * Add a Mapping Driver.
14
-     *
15
-     * @param DriverInterface $driver
16
-     */
17
-    public function addDriver(DriverInterface $driver)
18
-    {
19
-        $this->drivers[$driver->getName()] = $driver;
20
-    }
12
+	/**
13
+	 * Add a Mapping Driver.
14
+	 *
15
+	 * @param DriverInterface $driver
16
+	 */
17
+	public function addDriver(DriverInterface $driver)
18
+	{
19
+		$this->drivers[$driver->getName()] = $driver;
20
+	}
21 21
 
22
-    /**
23
-     * Get the DBAdapter.
24
-     *
25
-     * @param string $driver
26
-     * @param string $connection connection name for drivers supporting multiple connection.
27
-     *
28
-     * @return DriverInterface|void
29
-     */
30
-    public function getAdapter($driver, $connection = null)
31
-    {
32
-        if (array_key_exists($driver, $this->drivers)) {
33
-            return $this->drivers[$driver]->getAdapter($connection);
34
-        }
35
-    }
22
+	/**
23
+	 * Get the DBAdapter.
24
+	 *
25
+	 * @param string $driver
26
+	 * @param string $connection connection name for drivers supporting multiple connection.
27
+	 *
28
+	 * @return DriverInterface|void
29
+	 */
30
+	public function getAdapter($driver, $connection = null)
31
+	{
32
+		if (array_key_exists($driver, $this->drivers)) {
33
+			return $this->drivers[$driver]->getAdapter($connection);
34
+		}
35
+	}
36 36
 }
Please login to merge, or discard this patch.
src/MagicCasting.php 1 patch
Indentation   +102 added lines, -102 removed lines patch added patch discarded remove patch
@@ -8,117 +8,117 @@
 block discarded – undo
8 8
 
9 9
 trait MagicCasting
10 10
 {
11
-    /**
12
-     * Determine if the given attribute exists.
13
-     *
14
-     * @param mixed $offset
15
-     *
16
-     * @return bool
17
-     */
18
-    public function offsetExists($offset)
19
-    {
20
-        return isset($this->$offset);
21
-    }
11
+	/**
12
+	 * Determine if the given attribute exists.
13
+	 *
14
+	 * @param mixed $offset
15
+	 *
16
+	 * @return bool
17
+	 */
18
+	public function offsetExists($offset)
19
+	{
20
+		return isset($this->$offset);
21
+	}
22 22
 
23
-    /**
24
-     * Get the value for a given offset.
25
-     *
26
-     * @param mixed $offset
27
-     *
28
-     * @return mixed
29
-     */
30
-    public function offsetGet($offset)
31
-    {
32
-        return $this->$offset;
33
-    }
23
+	/**
24
+	 * Get the value for a given offset.
25
+	 *
26
+	 * @param mixed $offset
27
+	 *
28
+	 * @return mixed
29
+	 */
30
+	public function offsetGet($offset)
31
+	{
32
+		return $this->$offset;
33
+	}
34 34
 
35
-    /**
36
-     * Set the value for a given offset.
37
-     *
38
-     * @param mixed $offset
39
-     * @param mixed $value
40
-     *
41
-     * @return void
42
-     */
43
-    public function offsetSet($offset, $value)
44
-    {
45
-        $this->$offset = $value;
46
-    }
35
+	/**
36
+	 * Set the value for a given offset.
37
+	 *
38
+	 * @param mixed $offset
39
+	 * @param mixed $value
40
+	 *
41
+	 * @return void
42
+	 */
43
+	public function offsetSet($offset, $value)
44
+	{
45
+		$this->$offset = $value;
46
+	}
47 47
 
48
-    /**
49
-     * Unset the value for a given offset.
50
-     *
51
-     * @param mixed $offset
52
-     *
53
-     * @return void
54
-     */
55
-    public function offsetUnset($offset)
56
-    {
57
-        unset($this->$offset);
58
-    }
48
+	/**
49
+	 * Unset the value for a given offset.
50
+	 *
51
+	 * @param mixed $offset
52
+	 *
53
+	 * @return void
54
+	 */
55
+	public function offsetUnset($offset)
56
+	{
57
+		unset($this->$offset);
58
+	}
59 59
 
60
-    /**
61
-     * Convert the object into something JSON serializable.
62
-     *
63
-     * @return array
64
-     */
65
-    public function jsonSerialize()
66
-    {
67
-        return $this->toArray();
68
-    }
60
+	/**
61
+	 * Convert the object into something JSON serializable.
62
+	 *
63
+	 * @return array
64
+	 */
65
+	public function jsonSerialize()
66
+	{
67
+		return $this->toArray();
68
+	}
69 69
 
70
-    /**
71
-     * Convert the entity instance to JSON.
72
-     *
73
-     * @param int $options
74
-     *
75
-     * @return string
76
-     */
77
-    public function toJson($options = 0)
78
-    {
79
-        return json_encode($this->toArray(), $options);
80
-    }
70
+	/**
71
+	 * Convert the entity instance to JSON.
72
+	 *
73
+	 * @param int $options
74
+	 *
75
+	 * @return string
76
+	 */
77
+	public function toJson($options = 0)
78
+	{
79
+		return json_encode($this->toArray(), $options);
80
+	}
81 81
 
82
-    /**
83
-     * Convert Mappable object to array;.
84
-     *
85
-     * @return array
86
-     */
87
-    public function toArray()
88
-    {
89
-        return $this->attributesToArray($this->attributes);
90
-    }
82
+	/**
83
+	 * Convert Mappable object to array;.
84
+	 *
85
+	 * @return array
86
+	 */
87
+	public function toArray()
88
+	{
89
+		return $this->attributesToArray($this->attributes);
90
+	}
91 91
 
92
-    /**
93
-     * Transform the Object to array/json,.
94
-     *
95
-     * @param array $sourceAttributes
96
-     *
97
-     * @return array
98
-     */
99
-    protected function attributesToArray(array $sourceAttributes)
100
-    {
101
-        $attributes = [];
92
+	/**
93
+	 * Transform the Object to array/json,.
94
+	 *
95
+	 * @param array $sourceAttributes
96
+	 *
97
+	 * @return array
98
+	 */
99
+	protected function attributesToArray(array $sourceAttributes)
100
+	{
101
+		$attributes = [];
102 102
 
103
-        foreach ($sourceAttributes as $key => $attribute) {
104
-            // If the attribute is a proxy, and hasn't be loaded, we discard
105
-            // it from the returned set.
106
-            if ($attribute instanceof ProxyInterface && !$attribute->isProxyInitialized()) {
107
-                continue;
108
-            }
103
+		foreach ($sourceAttributes as $key => $attribute) {
104
+			// If the attribute is a proxy, and hasn't be loaded, we discard
105
+			// it from the returned set.
106
+			if ($attribute instanceof ProxyInterface && !$attribute->isProxyInitialized()) {
107
+				continue;
108
+			}
109 109
 
110
-            if ($attribute instanceof Carbon) {
111
-                $attributes[$key] = $attribute->__toString();
112
-                continue;
113
-            }
110
+			if ($attribute instanceof Carbon) {
111
+				$attributes[$key] = $attribute->__toString();
112
+				continue;
113
+			}
114 114
 
115
-            if ($attribute instanceof Arrayable) {
116
-                $attributes[$key] = $attribute->toArray();
117
-            } else {
118
-                $attributes[$key] = $attribute;
119
-            }
120
-        }
115
+			if ($attribute instanceof Arrayable) {
116
+				$attributes[$key] = $attribute->toArray();
117
+			} else {
118
+				$attributes[$key] = $attribute;
119
+			}
120
+		}
121 121
 
122
-        return $attributes;
123
-    }
122
+		return $attributes;
123
+	}
124 124
 }
Please login to merge, or discard this patch.
src/System/CachedRelationship.php 1 patch
Indentation   +59 added lines, -59 removed lines patch added patch discarded remove patch
@@ -8,69 +8,69 @@
 block discarded – undo
8 8
  */
9 9
 class CachedRelationship
10 10
 {
11
-    /**
12
-     * The Hash of the related entity.
13
-     *
14
-     * @var string
15
-     */
16
-    protected $hash;
11
+	/**
12
+	 * The Hash of the related entity.
13
+	 *
14
+	 * @var string
15
+	 */
16
+	protected $hash;
17 17
 
18
-    /**
19
-     * Pivot attributes, if any.
20
-     *
21
-     * @var array
22
-     */
23
-    protected $pivotAttributes;
18
+	/**
19
+	 * Pivot attributes, if any.
20
+	 *
21
+	 * @var array
22
+	 */
23
+	protected $pivotAttributes;
24 24
 
25
-    /**
26
-     * CachedRelationship constructor.
27
-     *
28
-     * @param $hash
29
-     * @param array $pivotAttributes
30
-     */
31
-    public function __construct($hash, $pivotAttributes = [])
32
-    {
33
-        $this->hash = $hash;
34
-        $this->pivotAttributes = $pivotAttributes;
35
-    }
25
+	/**
26
+	 * CachedRelationship constructor.
27
+	 *
28
+	 * @param $hash
29
+	 * @param array $pivotAttributes
30
+	 */
31
+	public function __construct($hash, $pivotAttributes = [])
32
+	{
33
+		$this->hash = $hash;
34
+		$this->pivotAttributes = $pivotAttributes;
35
+	}
36 36
 
37
-    /**
38
-     * Return true if any pivot attributes are present.
39
-     *
40
-     * @return bool
41
-     */
42
-    public function hasPivotAttributes()
43
-    {
44
-        return count($this->pivotAttributes) > 0;
45
-    }
37
+	/**
38
+	 * Return true if any pivot attributes are present.
39
+	 *
40
+	 * @return bool
41
+	 */
42
+	public function hasPivotAttributes()
43
+	{
44
+		return count($this->pivotAttributes) > 0;
45
+	}
46 46
 
47
-    /**
48
-     * Returns the hash of the related entity.
49
-     *
50
-     * @return string
51
-     */
52
-    public function getHash()
53
-    {
54
-        return $this->hash;
55
-    }
47
+	/**
48
+	 * Returns the hash of the related entity.
49
+	 *
50
+	 * @return string
51
+	 */
52
+	public function getHash()
53
+	{
54
+		return $this->hash;
55
+	}
56 56
 
57
-    /**
58
-     * Get the cached values for the pivot attributes.
59
-     *
60
-     * @return array
61
-     */
62
-    public function getPivotAttributes()
63
-    {
64
-        return $this->pivotAttributes;
65
-    }
57
+	/**
58
+	 * Get the cached values for the pivot attributes.
59
+	 *
60
+	 * @return array
61
+	 */
62
+	public function getPivotAttributes()
63
+	{
64
+		return $this->pivotAttributes;
65
+	}
66 66
 
67
-    /**
68
-     * Access to the hash for fast cache comparison.
69
-     *
70
-     * @return string
71
-     */
72
-    public function __toString()
73
-    {
74
-        return $this->hash;
75
-    }
67
+	/**
68
+	 * Access to the hash for fast cache comparison.
69
+	 *
70
+	 * @return string
71
+	 */
72
+	public function __toString()
73
+	{
74
+		return $this->hash;
75
+	}
76 76
 }
Please login to merge, or discard this patch.
src/System/EntityCache.php 1 patch
Indentation   +337 added lines, -337 removed lines patch added patch discarded remove patch
@@ -15,341 +15,341 @@
 block discarded – undo
15 15
  */
16 16
 class EntityCache
17 17
 {
18
-    /**
19
-     * Entity's raw attributes/relationships.
20
-     *
21
-     * @var array
22
-     */
23
-    protected $cache = [];
24
-
25
-    /**
26
-     * Entity Map for the current Entity Type.
27
-     *
28
-     * @var \Analogue\ORM\EntityMap
29
-     */
30
-    protected $entityMap;
31
-
32
-    /**
33
-     * Wrapper factory.
34
-     *
35
-     * @var \Analogue\ORM\System\Wrappers\Factory
36
-     */
37
-    protected $factory;
38
-
39
-    /**
40
-     * Associative array containing list of pivot attributes per relationship
41
-     * so we don't have to call relationship method on refresh.
42
-     *
43
-     * @var array
44
-     */
45
-    protected $pivotAttributes = [];
46
-
47
-    /**
48
-     * EntityCache constructor.
49
-     *
50
-     * @param EntityMap $entityMap
51
-     */
52
-    public function __construct(EntityMap $entityMap)
53
-    {
54
-        $this->entityMap = $entityMap;
55
-
56
-        $this->factory = new Factory();
57
-    }
58
-
59
-    /**
60
-     * Add an array of key=>attributes representing
61
-     * the initial state of loaded entities.
62
-     *
63
-     * @param array $results
64
-     */
65
-    public function add(array $results)
66
-    {
67
-        $cachedResults = [];
68
-
69
-        $keyColumn = $this->entityMap->getKeyName();
70
-
71
-        foreach ($results as $result) {
72
-            $id = $result[$keyColumn];
73
-
74
-            // Forget the ID field from the cache attributes
75
-            // to prevent any side effect.
76
-            unset($result[$keyColumn]);
77
-            $cachedResults[$id] = $result;
78
-        }
79
-
80
-        if (count($this->cache) == 0) {
81
-            $this->cache = $cachedResults;
82
-        } else {
83
-            $this->mergeCacheResults($cachedResults);
84
-        }
85
-    }
86
-
87
-    /**
88
-     * Retrieve initial attributes for a single entity.
89
-     *
90
-     * @param string $id
91
-     *
92
-     * @return array
93
-     */
94
-    public function get($id)
95
-    {
96
-        if ($this->has($id)) {
97
-            return $this->cache[$id];
98
-        } else {
99
-            return [];
100
-        }
101
-    }
102
-
103
-    /**
104
-     * Check if a record for this id exists.
105
-     *
106
-     * @param string $id
107
-     *
108
-     * @return bool
109
-     */
110
-    public function has($id)
111
-    {
112
-        return array_key_exists($id, $this->cache);
113
-    }
114
-
115
-    /**
116
-     * Combine new result set with existing attributes in
117
-     * cache.
118
-     *
119
-     * @param array $entities
120
-     *
121
-     * @return void
122
-     */
123
-    protected function mergeCacheResults($entities)
124
-    {
125
-        foreach ($entities as $key => $entity) {
126
-            $this->cache[$key] = $entity;
127
-        }
128
-    }
129
-
130
-    /**
131
-     * Cache Relation's query result for an entity.
132
-     *
133
-     * @param string       $key          primary key of the cached entity
134
-     * @param string       $relation     name of the relation
135
-     * @param mixed        $results      results of the relationship's query
136
-     * @param Relationship $relationship
137
-     *
138
-     * @throws MappingException
139
-     *
140
-     * @return void
141
-     */
142
-    public function cacheLoadedRelationResult($key, $relation, $results, Relationship $relationship)
143
-    {
144
-        if ($results instanceof EntityCollection) {
145
-            $this->cacheManyRelationResults($key, $relation, $results, $relationship);
146
-        }
147
-
148
-        // TODO : As we support popo Entities, Maybe this check isn't needed anymore,
149
-        // or we have to check that $result is an object instead
150
-        if ($results instanceof Mappable) {
151
-            $this->cacheSingleRelationResult($key, $relation, $results, $relationship);
152
-        }
153
-    }
154
-
155
-    /**
156
-     * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
157
-     *
158
-     * @param string       $parentKey
159
-     * @param string       $relation
160
-     * @param array        $result
161
-     * @param Relationship $relationship
162
-     *
163
-     * @throws MappingException
164
-     *
165
-     * @return CachedRelationship
166
-     */
167
-    protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
168
-    {
169
-        $pivotColumns = $relationship->getPivotAttributes();
170
-
171
-        if (!array_key_exists($relation, $this->pivotAttributes)) {
172
-            $this->pivotAttributes[$relation] = $pivotColumns;
173
-        }
174
-
175
-        $wrapper = $this->factory->make($result);
176
-
177
-        $hash = $wrapper->getEntityHash();
178
-
179
-        if (count($pivotColumns) > 0) {
180
-            $pivotAttributes = [];
181
-            foreach ($pivotColumns as $column) {
182
-                $pivot = $wrapper->getEntityAttribute('pivot');
183
-
184
-                $pivotWrapper = $this->factory->make($pivot);
185
-
186
-                $pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
187
-            }
188
-
189
-            $cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
190
-        } else {
191
-            $cachedRelationship = new CachedRelationship($hash);
192
-        }
193
-
194
-        return $cachedRelationship;
195
-    }
196
-
197
-    /**
198
-     * Cache a many relationship.
199
-     *
200
-     * @param                  $parentKey
201
-     * @param string           $relation
202
-     * @param EntityCollection $results
203
-     * @param Relationship     $relationship
204
-     *
205
-     * @throws MappingException
206
-     */
207
-    protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
208
-    {
209
-        $this->cache[$parentKey][$relation] = [];
210
-
211
-        foreach ($results as $result) {
212
-            $cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
213
-
214
-            $relatedHash = $cachedRelationship->getHash();
215
-
216
-            $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
217
-        }
218
-    }
219
-
220
-    /**
221
-     * Cache a single relationship.
222
-     *
223
-     * @param              $parentKey
224
-     * @param string       $relation
225
-     * @param Mappable     $result
226
-     * @param Relationship $relationship
227
-     *
228
-     * @throws MappingException
229
-     */
230
-    protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
231
-    {
232
-        $this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
233
-    }
234
-
235
-    /**
236
-     * Get Entity's Hash.
237
-     *
238
-     * @param  $entity
239
-     *
240
-     * @throws MappingException
241
-     *
242
-     * @return string
243
-     */
244
-    protected function getEntityHash(InternallyMappable $entity)
245
-    {
246
-        $class = $entity->getEntityClass();
247
-
248
-        $mapper = Manager::getMapper($class);
249
-
250
-        $keyName = $mapper->getEntityMap()->getKeyName();
251
-
252
-        return $class.'.'.$entity->getEntityAttribute($keyName);
253
-    }
254
-
255
-    /**
256
-     * Refresh the cache record for an aggregated entity after a write operation.
257
-     *
258
-     * @param Aggregate $entity
259
-     */
260
-    public function refresh(Aggregate $entity)
261
-    {
262
-        $this->cache[$entity->getEntityId()] = $this->transform($entity);
263
-    }
264
-
265
-    /**
266
-     * Transform an Aggregated Entity into a cache record.
267
-     *
268
-     * @param Aggregate $aggregatedEntity
269
-     *
270
-     * @throws MappingException
271
-     *
272
-     * @return array
273
-     */
274
-    protected function transform(Aggregate $aggregatedEntity)
275
-    {
276
-        $baseAttributes = $aggregatedEntity->getRawAttributes();
277
-
278
-        $relationAttributes = [];
279
-
280
-        // First we'll handle each relationships that are a one to one
281
-        // relation, and which will be saved as a CachedRelationship
282
-        // object inside the cache.
283
-
284
-        // NOTE : storing localRelationships maybe useless has we store
285
-        // the foreign key in the attributes already.
286
-
287
-        foreach ($this->entityMap->getSingleRelationships() as $relation) {
288
-            $aggregates = $aggregatedEntity->getRelationship($relation);
289
-
290
-            if (count($aggregates) == 1) {
291
-                $related = $aggregates[0];
292
-                $relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
293
-            }
294
-            if (count($aggregates) > 1) {
295
-                throw new MappingException("Single Relationship '$relation' contains several related entities");
296
-            }
297
-        }
298
-
299
-        // Then we'll handle the 'many' relationships and store them as
300
-        // an array of CachedRelationship objects.
301
-
302
-        foreach ($this->entityMap->getManyRelationships() as $relation) {
303
-            $aggregates = $aggregatedEntity->getRelationship($relation);
304
-
305
-            $relationAttributes[$relation] = [];
306
-
307
-            foreach ($aggregates as $aggregate) {
308
-                $relationAttributes[$relation][] = new CachedRelationship(
309
-                    $aggregate->getEntityHash(),
310
-                    $aggregate->getPivotAttributes()
311
-                );
312
-            }
313
-        }
314
-
315
-        return $baseAttributes + $relationAttributes;
316
-    }
317
-
318
-    /**
319
-     * Get pivot attributes for a relation.
320
-     *
321
-     * @param string             $relation
322
-     * @param InternallyMappable $entity
323
-     *
324
-     * @return array
325
-     */
326
-    protected function getPivotValues($relation, InternallyMappable $entity)
327
-    {
328
-        $values = [];
329
-
330
-        $entityAttributes = $entity->getEntityAttributes();
331
-
332
-        if (array_key_exists($relation, $this->pivotAttributes)) {
333
-            foreach ($this->pivotAttributes[$relation] as $attribute) {
334
-                if (array_key_exists($attribute, $entityAttributes)) {
335
-                    $values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
336
-                }
337
-            }
338
-        }
339
-
340
-        return $values;
341
-    }
342
-
343
-    /**
344
-     * Clear the entity Cache. Use with caution as it could result
345
-     * in impredictable behaviour if the cached entities are stored
346
-     * after the cache clear operation.
347
-     *
348
-     * @return void
349
-     */
350
-    public function clear()
351
-    {
352
-        $this->cache = [];
353
-        $this->pivotAttributes = [];
354
-    }
18
+	/**
19
+	 * Entity's raw attributes/relationships.
20
+	 *
21
+	 * @var array
22
+	 */
23
+	protected $cache = [];
24
+
25
+	/**
26
+	 * Entity Map for the current Entity Type.
27
+	 *
28
+	 * @var \Analogue\ORM\EntityMap
29
+	 */
30
+	protected $entityMap;
31
+
32
+	/**
33
+	 * Wrapper factory.
34
+	 *
35
+	 * @var \Analogue\ORM\System\Wrappers\Factory
36
+	 */
37
+	protected $factory;
38
+
39
+	/**
40
+	 * Associative array containing list of pivot attributes per relationship
41
+	 * so we don't have to call relationship method on refresh.
42
+	 *
43
+	 * @var array
44
+	 */
45
+	protected $pivotAttributes = [];
46
+
47
+	/**
48
+	 * EntityCache constructor.
49
+	 *
50
+	 * @param EntityMap $entityMap
51
+	 */
52
+	public function __construct(EntityMap $entityMap)
53
+	{
54
+		$this->entityMap = $entityMap;
55
+
56
+		$this->factory = new Factory();
57
+	}
58
+
59
+	/**
60
+	 * Add an array of key=>attributes representing
61
+	 * the initial state of loaded entities.
62
+	 *
63
+	 * @param array $results
64
+	 */
65
+	public function add(array $results)
66
+	{
67
+		$cachedResults = [];
68
+
69
+		$keyColumn = $this->entityMap->getKeyName();
70
+
71
+		foreach ($results as $result) {
72
+			$id = $result[$keyColumn];
73
+
74
+			// Forget the ID field from the cache attributes
75
+			// to prevent any side effect.
76
+			unset($result[$keyColumn]);
77
+			$cachedResults[$id] = $result;
78
+		}
79
+
80
+		if (count($this->cache) == 0) {
81
+			$this->cache = $cachedResults;
82
+		} else {
83
+			$this->mergeCacheResults($cachedResults);
84
+		}
85
+	}
86
+
87
+	/**
88
+	 * Retrieve initial attributes for a single entity.
89
+	 *
90
+	 * @param string $id
91
+	 *
92
+	 * @return array
93
+	 */
94
+	public function get($id)
95
+	{
96
+		if ($this->has($id)) {
97
+			return $this->cache[$id];
98
+		} else {
99
+			return [];
100
+		}
101
+	}
102
+
103
+	/**
104
+	 * Check if a record for this id exists.
105
+	 *
106
+	 * @param string $id
107
+	 *
108
+	 * @return bool
109
+	 */
110
+	public function has($id)
111
+	{
112
+		return array_key_exists($id, $this->cache);
113
+	}
114
+
115
+	/**
116
+	 * Combine new result set with existing attributes in
117
+	 * cache.
118
+	 *
119
+	 * @param array $entities
120
+	 *
121
+	 * @return void
122
+	 */
123
+	protected function mergeCacheResults($entities)
124
+	{
125
+		foreach ($entities as $key => $entity) {
126
+			$this->cache[$key] = $entity;
127
+		}
128
+	}
129
+
130
+	/**
131
+	 * Cache Relation's query result for an entity.
132
+	 *
133
+	 * @param string       $key          primary key of the cached entity
134
+	 * @param string       $relation     name of the relation
135
+	 * @param mixed        $results      results of the relationship's query
136
+	 * @param Relationship $relationship
137
+	 *
138
+	 * @throws MappingException
139
+	 *
140
+	 * @return void
141
+	 */
142
+	public function cacheLoadedRelationResult($key, $relation, $results, Relationship $relationship)
143
+	{
144
+		if ($results instanceof EntityCollection) {
145
+			$this->cacheManyRelationResults($key, $relation, $results, $relationship);
146
+		}
147
+
148
+		// TODO : As we support popo Entities, Maybe this check isn't needed anymore,
149
+		// or we have to check that $result is an object instead
150
+		if ($results instanceof Mappable) {
151
+			$this->cacheSingleRelationResult($key, $relation, $results, $relationship);
152
+		}
153
+	}
154
+
155
+	/**
156
+	 * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
157
+	 *
158
+	 * @param string       $parentKey
159
+	 * @param string       $relation
160
+	 * @param array        $result
161
+	 * @param Relationship $relationship
162
+	 *
163
+	 * @throws MappingException
164
+	 *
165
+	 * @return CachedRelationship
166
+	 */
167
+	protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
168
+	{
169
+		$pivotColumns = $relationship->getPivotAttributes();
170
+
171
+		if (!array_key_exists($relation, $this->pivotAttributes)) {
172
+			$this->pivotAttributes[$relation] = $pivotColumns;
173
+		}
174
+
175
+		$wrapper = $this->factory->make($result);
176
+
177
+		$hash = $wrapper->getEntityHash();
178
+
179
+		if (count($pivotColumns) > 0) {
180
+			$pivotAttributes = [];
181
+			foreach ($pivotColumns as $column) {
182
+				$pivot = $wrapper->getEntityAttribute('pivot');
183
+
184
+				$pivotWrapper = $this->factory->make($pivot);
185
+
186
+				$pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
187
+			}
188
+
189
+			$cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
190
+		} else {
191
+			$cachedRelationship = new CachedRelationship($hash);
192
+		}
193
+
194
+		return $cachedRelationship;
195
+	}
196
+
197
+	/**
198
+	 * Cache a many relationship.
199
+	 *
200
+	 * @param                  $parentKey
201
+	 * @param string           $relation
202
+	 * @param EntityCollection $results
203
+	 * @param Relationship     $relationship
204
+	 *
205
+	 * @throws MappingException
206
+	 */
207
+	protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
208
+	{
209
+		$this->cache[$parentKey][$relation] = [];
210
+
211
+		foreach ($results as $result) {
212
+			$cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
213
+
214
+			$relatedHash = $cachedRelationship->getHash();
215
+
216
+			$this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
217
+		}
218
+	}
219
+
220
+	/**
221
+	 * Cache a single relationship.
222
+	 *
223
+	 * @param              $parentKey
224
+	 * @param string       $relation
225
+	 * @param Mappable     $result
226
+	 * @param Relationship $relationship
227
+	 *
228
+	 * @throws MappingException
229
+	 */
230
+	protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
231
+	{
232
+		$this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
233
+	}
234
+
235
+	/**
236
+	 * Get Entity's Hash.
237
+	 *
238
+	 * @param  $entity
239
+	 *
240
+	 * @throws MappingException
241
+	 *
242
+	 * @return string
243
+	 */
244
+	protected function getEntityHash(InternallyMappable $entity)
245
+	{
246
+		$class = $entity->getEntityClass();
247
+
248
+		$mapper = Manager::getMapper($class);
249
+
250
+		$keyName = $mapper->getEntityMap()->getKeyName();
251
+
252
+		return $class.'.'.$entity->getEntityAttribute($keyName);
253
+	}
254
+
255
+	/**
256
+	 * Refresh the cache record for an aggregated entity after a write operation.
257
+	 *
258
+	 * @param Aggregate $entity
259
+	 */
260
+	public function refresh(Aggregate $entity)
261
+	{
262
+		$this->cache[$entity->getEntityId()] = $this->transform($entity);
263
+	}
264
+
265
+	/**
266
+	 * Transform an Aggregated Entity into a cache record.
267
+	 *
268
+	 * @param Aggregate $aggregatedEntity
269
+	 *
270
+	 * @throws MappingException
271
+	 *
272
+	 * @return array
273
+	 */
274
+	protected function transform(Aggregate $aggregatedEntity)
275
+	{
276
+		$baseAttributes = $aggregatedEntity->getRawAttributes();
277
+
278
+		$relationAttributes = [];
279
+
280
+		// First we'll handle each relationships that are a one to one
281
+		// relation, and which will be saved as a CachedRelationship
282
+		// object inside the cache.
283
+
284
+		// NOTE : storing localRelationships maybe useless has we store
285
+		// the foreign key in the attributes already.
286
+
287
+		foreach ($this->entityMap->getSingleRelationships() as $relation) {
288
+			$aggregates = $aggregatedEntity->getRelationship($relation);
289
+
290
+			if (count($aggregates) == 1) {
291
+				$related = $aggregates[0];
292
+				$relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
293
+			}
294
+			if (count($aggregates) > 1) {
295
+				throw new MappingException("Single Relationship '$relation' contains several related entities");
296
+			}
297
+		}
298
+
299
+		// Then we'll handle the 'many' relationships and store them as
300
+		// an array of CachedRelationship objects.
301
+
302
+		foreach ($this->entityMap->getManyRelationships() as $relation) {
303
+			$aggregates = $aggregatedEntity->getRelationship($relation);
304
+
305
+			$relationAttributes[$relation] = [];
306
+
307
+			foreach ($aggregates as $aggregate) {
308
+				$relationAttributes[$relation][] = new CachedRelationship(
309
+					$aggregate->getEntityHash(),
310
+					$aggregate->getPivotAttributes()
311
+				);
312
+			}
313
+		}
314
+
315
+		return $baseAttributes + $relationAttributes;
316
+	}
317
+
318
+	/**
319
+	 * Get pivot attributes for a relation.
320
+	 *
321
+	 * @param string             $relation
322
+	 * @param InternallyMappable $entity
323
+	 *
324
+	 * @return array
325
+	 */
326
+	protected function getPivotValues($relation, InternallyMappable $entity)
327
+	{
328
+		$values = [];
329
+
330
+		$entityAttributes = $entity->getEntityAttributes();
331
+
332
+		if (array_key_exists($relation, $this->pivotAttributes)) {
333
+			foreach ($this->pivotAttributes[$relation] as $attribute) {
334
+				if (array_key_exists($attribute, $entityAttributes)) {
335
+					$values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
336
+				}
337
+			}
338
+		}
339
+
340
+		return $values;
341
+	}
342
+
343
+	/**
344
+	 * Clear the entity Cache. Use with caution as it could result
345
+	 * in impredictable behaviour if the cached entities are stored
346
+	 * after the cache clear operation.
347
+	 *
348
+	 * @return void
349
+	 */
350
+	public function clear()
351
+	{
352
+		$this->cache = [];
353
+		$this->pivotAttributes = [];
354
+	}
355 355
 }
Please login to merge, or discard this patch.
src/System/EntityBuilder.php 1 patch
Indentation   +161 added lines, -161 removed lines patch added patch discarded remove patch
@@ -9,165 +9,165 @@
 block discarded – undo
9 9
  */
10 10
 class EntityBuilder
11 11
 {
12
-    /**
13
-     * The mapper for the entity to build.
14
-     *
15
-     * @var \Analogue\ORM\System\Mapper
16
-     */
17
-    protected $mapper;
18
-
19
-    /**
20
-     * The Entity Map for the entity to build.
21
-     *
22
-     * @var \Analogue\ORM\EntityMap
23
-     */
24
-    protected $entityMap;
25
-
26
-    /**
27
-     * Relations that are eager loaded on this query.
28
-     *
29
-     * @var array
30
-     */
31
-    protected $eagerLoads;
32
-
33
-    /**
34
-     * Relations that will be lazy loaded on this query.
35
-     *
36
-     * @var array
37
-     */
38
-    protected $lazyLoads;
39
-
40
-    /**
41
-     * @var array
42
-     */
43
-    protected $casts;
44
-
45
-    /**
46
-     * Entity Wrapper Factory.
47
-     *
48
-     * @var \Analogue\ORM\System\Wrappers\Factory
49
-     */
50
-    protected $factory;
51
-
52
-    /**
53
-     * EntityBuilder constructor.
54
-     *
55
-     * @param Mapper $mapper
56
-     * @param array  $eagerLoads
57
-     */
58
-    public function __construct(Mapper $mapper, array $eagerLoads)
59
-    {
60
-        $this->mapper = $mapper;
61
-
62
-        $this->entityMap = $mapper->getEntityMap();
63
-
64
-        $this->eagerLoads = $eagerLoads;
65
-
66
-        $this->lazyLoads = $this->getRelationshipsToProxy();
67
-
68
-        $this->factory = new Factory();
69
-    }
70
-
71
-    /**
72
-     * Convert an array of values into an entity.
73
-     *
74
-     * @param array $result
75
-     *
76
-     * @return array
77
-     */
78
-    public function build(array $result)
79
-    {
80
-        $instance = $this->getWrapperInstance();
81
-
82
-        // Hydrate any embedded Value Object
83
-        //
84
-        // TODO Move this to the result builder instead,
85
-        // as we'll handle this the same way as they were
86
-        // eager loaded relationships.
87
-        $this->hydrateValueObjects($result);
88
-
89
-        $instance->setEntityAttributes($result);
90
-
91
-        // Hydrate relationship attributes with lazyloading proxies
92
-        if (count($this->lazyLoads) > 0) {
93
-            $instance->setProxies($this->lazyLoads);
94
-        }
95
-
96
-        // Hydrate and return the instance
97
-        $instance->hydrate();
98
-        $entity = $instance->getObject();
99
-
100
-        return $entity;
101
-    }
102
-
103
-    /**
104
-     * Get the correct wrapper prototype corresponding to the object type.
105
-     *
106
-     * @throws \Analogue\ORM\Exceptions\MappingException
107
-     *
108
-     * @return InternallyMappable
109
-     */
110
-    protected function getWrapperInstance()
111
-    {
112
-        return $this->factory->make($this->mapper->newInstance());
113
-    }
114
-
115
-    /**
116
-     * Hydrate value object embedded in this entity.
117
-     *
118
-     * @param array $attributes
119
-     *
120
-     * @throws \Analogue\ORM\Exceptions\MappingException
121
-     *
122
-     * @return void
123
-     */
124
-    protected function hydrateValueObjects(&$attributes)
125
-    {
126
-        foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
127
-            $this->hydrateValueObject($attributes, $localKey, $valueClass);
128
-        }
129
-    }
130
-
131
-    /**
132
-     * Hydrate a single value object.
133
-     *
134
-     * @param array  $attributes
135
-     * @param string $localKey
136
-     * @param string $valueClass
137
-     *
138
-     * @throws \Analogue\ORM\Exceptions\MappingException
139
-     *
140
-     * @return void
141
-     */
142
-    protected function hydrateValueObject(&$attributes, $localKey, $valueClass)
143
-    {
144
-        $map = $this->mapper->getManager()->getValueMap($valueClass);
145
-
146
-        $embeddedAttributes = $map->getAttributes();
147
-
148
-        $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
149
-        $voWrapper = $this->factory->make($valueObject);
150
-
151
-        foreach ($embeddedAttributes as $key) {
152
-            $prefix = snake_case(class_basename($valueClass)).'_';
153
-
154
-            $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]);
155
-
156
-            unset($attributes[$prefix.$key]);
157
-        }
158
-
159
-        $attributes[$localKey] = $voWrapper->getObject();
160
-    }
161
-
162
-    /**
163
-     * Deduce the relationships for which we will build lazy loading proxies.
164
-     *
165
-     * @return array
166
-     */
167
-    protected function getRelationshipsToProxy()
168
-    {
169
-        $relations = $this->entityMap->getRelationships();
170
-
171
-        return array_diff($relations, $this->eagerLoads);
172
-    }
12
+	/**
13
+	 * The mapper for the entity to build.
14
+	 *
15
+	 * @var \Analogue\ORM\System\Mapper
16
+	 */
17
+	protected $mapper;
18
+
19
+	/**
20
+	 * The Entity Map for the entity to build.
21
+	 *
22
+	 * @var \Analogue\ORM\EntityMap
23
+	 */
24
+	protected $entityMap;
25
+
26
+	/**
27
+	 * Relations that are eager loaded on this query.
28
+	 *
29
+	 * @var array
30
+	 */
31
+	protected $eagerLoads;
32
+
33
+	/**
34
+	 * Relations that will be lazy loaded on this query.
35
+	 *
36
+	 * @var array
37
+	 */
38
+	protected $lazyLoads;
39
+
40
+	/**
41
+	 * @var array
42
+	 */
43
+	protected $casts;
44
+
45
+	/**
46
+	 * Entity Wrapper Factory.
47
+	 *
48
+	 * @var \Analogue\ORM\System\Wrappers\Factory
49
+	 */
50
+	protected $factory;
51
+
52
+	/**
53
+	 * EntityBuilder constructor.
54
+	 *
55
+	 * @param Mapper $mapper
56
+	 * @param array  $eagerLoads
57
+	 */
58
+	public function __construct(Mapper $mapper, array $eagerLoads)
59
+	{
60
+		$this->mapper = $mapper;
61
+
62
+		$this->entityMap = $mapper->getEntityMap();
63
+
64
+		$this->eagerLoads = $eagerLoads;
65
+
66
+		$this->lazyLoads = $this->getRelationshipsToProxy();
67
+
68
+		$this->factory = new Factory();
69
+	}
70
+
71
+	/**
72
+	 * Convert an array of values into an entity.
73
+	 *
74
+	 * @param array $result
75
+	 *
76
+	 * @return array
77
+	 */
78
+	public function build(array $result)
79
+	{
80
+		$instance = $this->getWrapperInstance();
81
+
82
+		// Hydrate any embedded Value Object
83
+		//
84
+		// TODO Move this to the result builder instead,
85
+		// as we'll handle this the same way as they were
86
+		// eager loaded relationships.
87
+		$this->hydrateValueObjects($result);
88
+
89
+		$instance->setEntityAttributes($result);
90
+
91
+		// Hydrate relationship attributes with lazyloading proxies
92
+		if (count($this->lazyLoads) > 0) {
93
+			$instance->setProxies($this->lazyLoads);
94
+		}
95
+
96
+		// Hydrate and return the instance
97
+		$instance->hydrate();
98
+		$entity = $instance->getObject();
99
+
100
+		return $entity;
101
+	}
102
+
103
+	/**
104
+	 * Get the correct wrapper prototype corresponding to the object type.
105
+	 *
106
+	 * @throws \Analogue\ORM\Exceptions\MappingException
107
+	 *
108
+	 * @return InternallyMappable
109
+	 */
110
+	protected function getWrapperInstance()
111
+	{
112
+		return $this->factory->make($this->mapper->newInstance());
113
+	}
114
+
115
+	/**
116
+	 * Hydrate value object embedded in this entity.
117
+	 *
118
+	 * @param array $attributes
119
+	 *
120
+	 * @throws \Analogue\ORM\Exceptions\MappingException
121
+	 *
122
+	 * @return void
123
+	 */
124
+	protected function hydrateValueObjects(&$attributes)
125
+	{
126
+		foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
127
+			$this->hydrateValueObject($attributes, $localKey, $valueClass);
128
+		}
129
+	}
130
+
131
+	/**
132
+	 * Hydrate a single value object.
133
+	 *
134
+	 * @param array  $attributes
135
+	 * @param string $localKey
136
+	 * @param string $valueClass
137
+	 *
138
+	 * @throws \Analogue\ORM\Exceptions\MappingException
139
+	 *
140
+	 * @return void
141
+	 */
142
+	protected function hydrateValueObject(&$attributes, $localKey, $valueClass)
143
+	{
144
+		$map = $this->mapper->getManager()->getValueMap($valueClass);
145
+
146
+		$embeddedAttributes = $map->getAttributes();
147
+
148
+		$valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
149
+		$voWrapper = $this->factory->make($valueObject);
150
+
151
+		foreach ($embeddedAttributes as $key) {
152
+			$prefix = snake_case(class_basename($valueClass)).'_';
153
+
154
+			$voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]);
155
+
156
+			unset($attributes[$prefix.$key]);
157
+		}
158
+
159
+		$attributes[$localKey] = $voWrapper->getObject();
160
+	}
161
+
162
+	/**
163
+	 * Deduce the relationships for which we will build lazy loading proxies.
164
+	 *
165
+	 * @return array
166
+	 */
167
+	protected function getRelationshipsToProxy()
168
+	{
169
+		$relations = $this->entityMap->getRelationships();
170
+
171
+		return array_diff($relations, $this->eagerLoads);
172
+	}
173 173
 }
Please login to merge, or discard this patch.
src/System/Wrappers/Wrapper.php 1 patch
Indentation   +204 added lines, -204 removed lines patch added patch discarded remove patch
@@ -10,208 +10,208 @@
 block discarded – undo
10 10
  */
11 11
 abstract class Wrapper implements InternallyMappable
12 12
 {
13
-    /**
14
-     * Original Entity Object.
15
-     *
16
-     * @var mixed
17
-     */
18
-    protected $entity;
19
-
20
-    /**
21
-     * Corresponding EntityMap.
22
-     *
23
-     * @var \Analogue\ORM\EntityMap
24
-     */
25
-    protected $entityMap;
26
-
27
-    /**
28
-     * @var \Analogue\ORM\System\Proxirs\ProxyFactory
29
-     */
30
-    protected $proxyFactory;
31
-
32
-    /**
33
-     * Wrapper constructor.
34
-     *
35
-     * @param $entity
36
-     * @param $entityMap
37
-     */
38
-    public function __construct($entity, $entityMap)
39
-    {
40
-        $this->entity = $entity;
41
-        $this->entityMap = $entityMap;
42
-        $this->proxyFactory = new ProxyFactory();
43
-    }
44
-
45
-    /**
46
-     * Return the wrapped entity class.
47
-     *
48
-     * @return mixed
49
-     */
50
-    public function getEntityClass()
51
-    {
52
-        return get_class($this->entity);
53
-    }
54
-
55
-    /**
56
-     * Return the entity's primary key valuye.
57
-     *
58
-     * @return string
59
-     */
60
-    public function getEntityKey()
61
-    {
62
-        return $this->getEntityAttribute($this->entityMap->getKeyName());
63
-    }
64
-
65
-    /**
66
-     * Return the Entity class/primary key couple,
67
-     * which is used for internall operations.
68
-     *
69
-     * @return string
70
-     */
71
-    public function getEntityHash()
72
-    {
73
-        return $this->getEntityClass().'.'.$this->getEntityKey();
74
-    }
75
-
76
-    /**
77
-     * Returns the wrapped entity.
78
-     *
79
-     * @return mixed
80
-     */
81
-    public function getObject()
82
-    {
83
-        return $this->entity;
84
-    }
85
-
86
-    /**
87
-     * Returns the wrapped entity's map.
88
-     *
89
-     * @return mixed
90
-     */
91
-    public function getMap()
92
-    {
93
-        return $this->entityMap;
94
-    }
95
-
96
-    /**
97
-     * Set the lazyloading proxies on the wrapped entity objet.
98
-     *
99
-     * @param array $relations list of relations to be lazy loaded
100
-     *
101
-     * @return void
102
-     */
103
-    public function setProxies(array $relations = null)
104
-    {
105
-        $attributes = $this->getEntityAttributes();
106
-        $proxies = [];
107
-
108
-        if (is_null($relations)) {
109
-            $relations = $this->getRelationsToProxy();
110
-        }
111
-
112
-        foreach ($relations as $relation) {
113
-            // We first look if we need to build the proxy on the relationship.
114
-            // If the key is handled locally and we know it not to be set,
115
-            // we'll set the relationship to null
116
-            if (!$this->relationNeedsProxy($relation, $attributes)) {
117
-                $proxies[$relation] = null;
118
-            } else {
119
-                $targetClass = $this->entityMap->getTargettedClass($relation);
120
-                $proxies[$relation] = $this->proxyFactory->make($this->getObject(), $relation, $targetClass);
121
-            }
122
-        }
123
-
124
-        foreach ($proxies as $key => $value) {
125
-            $this->setEntityAttribute($key, $value);
126
-        }
127
-    }
128
-
129
-    /**
130
-     * Determine which relations we have to build proxy for, by parsing
131
-     * attributes and finding methods that aren't set.
132
-     *
133
-     * @return array
134
-     */
135
-    protected function getRelationsToProxy()
136
-    {
137
-        $proxies = [];
138
-        $attributes = $this->getEntityAttributes();
139
-
140
-        foreach ($this->entityMap->getRelationships() as $relation) {
141
-            if (!array_key_exists($relation, $attributes)) {
142
-                $proxies[] = $relation;
143
-            }
144
-        }
145
-
146
-        return $proxies;
147
-    }
148
-
149
-    /**
150
-     * Determine if the relation needs a proxy or not.
151
-     *
152
-     * @param string $relation
153
-     * @param array  $attributes
154
-     *
155
-     * @return bool
156
-     */
157
-    protected function relationNeedsProxy($relation, $attributes)
158
-    {
159
-        if (in_array($relation, $this->entityMap->getRelationshipsWithoutProxy())) {
160
-            return false;
161
-        }
162
-
163
-        $localKey = $this->entityMap->getLocalKeys($relation);
164
-
165
-        if (is_null($localKey)) {
166
-            return true;
167
-        }
168
-
169
-        if (is_array($localKey)) {
170
-            $localKey = $localKey['id'];
171
-        }
172
-
173
-        if (!isset($attributes[$localKey])) {
174
-            return false;
175
-        }
176
-
177
-        if (is_null($attributes[$localKey])) {
178
-            return false;
179
-        }
180
-
181
-        return true;
182
-    }
183
-
184
-    /**
185
-     * @param string $key
186
-     * @param string $value
187
-     *
188
-     * @return mixed
189
-     */
190
-    abstract public function setEntityAttribute($key, $value);
191
-
192
-    /**
193
-     * @param string $key
194
-     *
195
-     * @return mixed
196
-     */
197
-    abstract public function getEntityAttribute($key);
198
-
199
-    /**
200
-     * @param array $attributes
201
-     *
202
-     * @return mixed
203
-     */
204
-    abstract public function setEntityAttributes(array $attributes);
205
-
206
-    /**
207
-     * @return mixed
208
-     */
209
-    abstract public function getEntityAttributes();
210
-
211
-    /**
212
-     * @param string $key
213
-     *
214
-     * @return mixed
215
-     */
216
-    abstract public function hasAttribute($key);
13
+	/**
14
+	 * Original Entity Object.
15
+	 *
16
+	 * @var mixed
17
+	 */
18
+	protected $entity;
19
+
20
+	/**
21
+	 * Corresponding EntityMap.
22
+	 *
23
+	 * @var \Analogue\ORM\EntityMap
24
+	 */
25
+	protected $entityMap;
26
+
27
+	/**
28
+	 * @var \Analogue\ORM\System\Proxirs\ProxyFactory
29
+	 */
30
+	protected $proxyFactory;
31
+
32
+	/**
33
+	 * Wrapper constructor.
34
+	 *
35
+	 * @param $entity
36
+	 * @param $entityMap
37
+	 */
38
+	public function __construct($entity, $entityMap)
39
+	{
40
+		$this->entity = $entity;
41
+		$this->entityMap = $entityMap;
42
+		$this->proxyFactory = new ProxyFactory();
43
+	}
44
+
45
+	/**
46
+	 * Return the wrapped entity class.
47
+	 *
48
+	 * @return mixed
49
+	 */
50
+	public function getEntityClass()
51
+	{
52
+		return get_class($this->entity);
53
+	}
54
+
55
+	/**
56
+	 * Return the entity's primary key valuye.
57
+	 *
58
+	 * @return string
59
+	 */
60
+	public function getEntityKey()
61
+	{
62
+		return $this->getEntityAttribute($this->entityMap->getKeyName());
63
+	}
64
+
65
+	/**
66
+	 * Return the Entity class/primary key couple,
67
+	 * which is used for internall operations.
68
+	 *
69
+	 * @return string
70
+	 */
71
+	public function getEntityHash()
72
+	{
73
+		return $this->getEntityClass().'.'.$this->getEntityKey();
74
+	}
75
+
76
+	/**
77
+	 * Returns the wrapped entity.
78
+	 *
79
+	 * @return mixed
80
+	 */
81
+	public function getObject()
82
+	{
83
+		return $this->entity;
84
+	}
85
+
86
+	/**
87
+	 * Returns the wrapped entity's map.
88
+	 *
89
+	 * @return mixed
90
+	 */
91
+	public function getMap()
92
+	{
93
+		return $this->entityMap;
94
+	}
95
+
96
+	/**
97
+	 * Set the lazyloading proxies on the wrapped entity objet.
98
+	 *
99
+	 * @param array $relations list of relations to be lazy loaded
100
+	 *
101
+	 * @return void
102
+	 */
103
+	public function setProxies(array $relations = null)
104
+	{
105
+		$attributes = $this->getEntityAttributes();
106
+		$proxies = [];
107
+
108
+		if (is_null($relations)) {
109
+			$relations = $this->getRelationsToProxy();
110
+		}
111
+
112
+		foreach ($relations as $relation) {
113
+			// We first look if we need to build the proxy on the relationship.
114
+			// If the key is handled locally and we know it not to be set,
115
+			// we'll set the relationship to null
116
+			if (!$this->relationNeedsProxy($relation, $attributes)) {
117
+				$proxies[$relation] = null;
118
+			} else {
119
+				$targetClass = $this->entityMap->getTargettedClass($relation);
120
+				$proxies[$relation] = $this->proxyFactory->make($this->getObject(), $relation, $targetClass);
121
+			}
122
+		}
123
+
124
+		foreach ($proxies as $key => $value) {
125
+			$this->setEntityAttribute($key, $value);
126
+		}
127
+	}
128
+
129
+	/**
130
+	 * Determine which relations we have to build proxy for, by parsing
131
+	 * attributes and finding methods that aren't set.
132
+	 *
133
+	 * @return array
134
+	 */
135
+	protected function getRelationsToProxy()
136
+	{
137
+		$proxies = [];
138
+		$attributes = $this->getEntityAttributes();
139
+
140
+		foreach ($this->entityMap->getRelationships() as $relation) {
141
+			if (!array_key_exists($relation, $attributes)) {
142
+				$proxies[] = $relation;
143
+			}
144
+		}
145
+
146
+		return $proxies;
147
+	}
148
+
149
+	/**
150
+	 * Determine if the relation needs a proxy or not.
151
+	 *
152
+	 * @param string $relation
153
+	 * @param array  $attributes
154
+	 *
155
+	 * @return bool
156
+	 */
157
+	protected function relationNeedsProxy($relation, $attributes)
158
+	{
159
+		if (in_array($relation, $this->entityMap->getRelationshipsWithoutProxy())) {
160
+			return false;
161
+		}
162
+
163
+		$localKey = $this->entityMap->getLocalKeys($relation);
164
+
165
+		if (is_null($localKey)) {
166
+			return true;
167
+		}
168
+
169
+		if (is_array($localKey)) {
170
+			$localKey = $localKey['id'];
171
+		}
172
+
173
+		if (!isset($attributes[$localKey])) {
174
+			return false;
175
+		}
176
+
177
+		if (is_null($attributes[$localKey])) {
178
+			return false;
179
+		}
180
+
181
+		return true;
182
+	}
183
+
184
+	/**
185
+	 * @param string $key
186
+	 * @param string $value
187
+	 *
188
+	 * @return mixed
189
+	 */
190
+	abstract public function setEntityAttribute($key, $value);
191
+
192
+	/**
193
+	 * @param string $key
194
+	 *
195
+	 * @return mixed
196
+	 */
197
+	abstract public function getEntityAttribute($key);
198
+
199
+	/**
200
+	 * @param array $attributes
201
+	 *
202
+	 * @return mixed
203
+	 */
204
+	abstract public function setEntityAttributes(array $attributes);
205
+
206
+	/**
207
+	 * @return mixed
208
+	 */
209
+	abstract public function getEntityAttributes();
210
+
211
+	/**
212
+	 * @param string $key
213
+	 *
214
+	 * @return mixed
215
+	 */
216
+	abstract public function hasAttribute($key);
217 217
 }
Please login to merge, or discard this patch.