Completed
Branch master (a17b64)
by Rémi
15:50
created
src/Plugins/AnaloguePlugin.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -6,33 +6,33 @@
 block discarded – undo
6 6
 
7 7
 abstract class AnaloguePlugin implements AnaloguePluginInterface
8 8
 {
9
-    /**
10
-     * Manager instance
11
-     *
12
-     * @var Manager
13
-     */
14
-    protected $manager;
9
+	/**
10
+	 * Manager instance
11
+	 *
12
+	 * @var Manager
13
+	 */
14
+	protected $manager;
15 15
 
16
-    /**
17
-     * AnaloguePlugin constructor.
18
-     * @param Manager $manager
19
-     */
20
-    public function __construct(Manager $manager)
21
-    {
22
-        $this->manager = $manager;
23
-    }
16
+	/**
17
+	 * AnaloguePlugin constructor.
18
+	 * @param Manager $manager
19
+	 */
20
+	public function __construct(Manager $manager)
21
+	{
22
+		$this->manager = $manager;
23
+	}
24 24
 
25
-    /**
26
-     * Boot the plugin
27
-     *
28
-     * @return void
29
-     */
30
-    abstract public function register();
25
+	/**
26
+	 * Boot the plugin
27
+	 *
28
+	 * @return void
29
+	 */
30
+	abstract public function register();
31 31
 
32
-    /**
33
-     * Get custom events provided by the plugin
34
-     *
35
-     * @return array
36
-     */
37
-    abstract public function getCustomEvents();
32
+	/**
33
+	 * Get custom events provided by the plugin
34
+	 *
35
+	 * @return array
36
+	 */
37
+	abstract public function getCustomEvents();
38 38
 }
Please login to merge, or discard this patch.
src/Repository.php 2 patches
Indentation   +116 added lines, -116 removed lines patch added patch discarded remove patch
@@ -15,129 +15,129 @@
 block discarded – undo
15 15
  */
16 16
 class Repository
17 17
 {
18
-    /**
19
-     * The mapper object for the corresponding entity
20
-     *
21
-     * @var \Analogue\ORM\System\Mapper
22
-     */
23
-    protected $mapper;
18
+	/**
19
+	 * The mapper object for the corresponding entity
20
+	 *
21
+	 * @var \Analogue\ORM\System\Mapper
22
+	 */
23
+	protected $mapper;
24 24
 
25
-    /**
26
-     * To build a repository, either provide :
27
-     *
28
-     * - Mappable object's class name as a string
29
-     * - Mappable object instance
30
-     * - Instance of mapper
31
-     *
32
-     * @param  Mapper         $mapper
33
-     * @param  EntityMap|null $entityMap (optional)
34
-     * @throws \InvalidArgumentException
35
-     * @throws MappingException
36
-     */
37
-    public function __construct($mapper, EntityMap $entityMap = null)
38
-    {
39
-        if ($mapper instanceof Mappable || is_string($mapper)) {
40
-            $this->mapper = Manager::getMapper($mapper, $entityMap);
41
-        } elseif ($mapper instanceof Mapper) {
42
-            $this->mapper = $mapper;
43
-        } else {
44
-            new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.');
45
-        }
46
-    }
25
+	/**
26
+	 * To build a repository, either provide :
27
+	 *
28
+	 * - Mappable object's class name as a string
29
+	 * - Mappable object instance
30
+	 * - Instance of mapper
31
+	 *
32
+	 * @param  Mapper         $mapper
33
+	 * @param  EntityMap|null $entityMap (optional)
34
+	 * @throws \InvalidArgumentException
35
+	 * @throws MappingException
36
+	 */
37
+	public function __construct($mapper, EntityMap $entityMap = null)
38
+	{
39
+		if ($mapper instanceof Mappable || is_string($mapper)) {
40
+			$this->mapper = Manager::getMapper($mapper, $entityMap);
41
+		} elseif ($mapper instanceof Mapper) {
42
+			$this->mapper = $mapper;
43
+		} else {
44
+			new InvalidArgumentException('Repository class constructor need a valid Mapper or Mappable object.');
45
+		}
46
+	}
47 47
 
48
-    /**
49
-     * Return all Entities from database
50
-     *
51
-     * @return \Analogue\ORM\EntityCollection
52
-     */
53
-    public function all()
54
-    {
55
-        return $this->mapper->get();
56
-    }
48
+	/**
49
+	 * Return all Entities from database
50
+	 *
51
+	 * @return \Analogue\ORM\EntityCollection
52
+	 */
53
+	public function all()
54
+	{
55
+		return $this->mapper->get();
56
+	}
57 57
     
58
-    /**
59
-     * Fetch a record from the database
60
-     * @param  integer $id
61
-     * @return \Analogue\ORM\Mappable
62
-     */
63
-    public function find($id)
64
-    {
65
-        return $this->mapper->find($id);
66
-    }
58
+	/**
59
+	 * Fetch a record from the database
60
+	 * @param  integer $id
61
+	 * @return \Analogue\ORM\Mappable
62
+	 */
63
+	public function find($id)
64
+	{
65
+		return $this->mapper->find($id);
66
+	}
67 67
 
68
-    /**
69
-     * Get the first entity matching the given attributes.
70
-     *
71
-     * @param  array  $attributes
72
-     * @return \Analogue\ORM\Mappable|null
73
-     */
74
-    public function firstMatching(array $attributes)
75
-    {
76
-        return $this->mapper->where($attributes)->first();
77
-    }
68
+	/**
69
+	 * Get the first entity matching the given attributes.
70
+	 *
71
+	 * @param  array  $attributes
72
+	 * @return \Analogue\ORM\Mappable|null
73
+	 */
74
+	public function firstMatching(array $attributes)
75
+	{
76
+		return $this->mapper->where($attributes)->first();
77
+	}
78 78
 
79
-    /**
80
-     * Return all the entities matching the given attributes
81
-     *
82
-     * @param array $attributes
83
-     * @return \Analogue\ORM\EntityCollection
84
-     */
85
-    public function allMatching(array $attributes)
86
-    {
87
-        return $this->mapper->where($attributes)->get();
88
-    }
79
+	/**
80
+	 * Return all the entities matching the given attributes
81
+	 *
82
+	 * @param array $attributes
83
+	 * @return \Analogue\ORM\EntityCollection
84
+	 */
85
+	public function allMatching(array $attributes)
86
+	{
87
+		return $this->mapper->where($attributes)->get();
88
+	}
89 89
 
90
-    /**
91
-     * Return a paginator instance on the EntityCollection
92
-     *
93
-     * @param int|null $perPage number of item per page (fallback on default setup in entity map)
94
-     * @return \Illuminate\Pagination\LengthAwarePaginator
95
-     */
96
-    public function paginate($perPage = null)
97
-    {
98
-        return $this->mapper->paginate($perPage);
99
-    }
90
+	/**
91
+	 * Return a paginator instance on the EntityCollection
92
+	 *
93
+	 * @param int|null $perPage number of item per page (fallback on default setup in entity map)
94
+	 * @return \Illuminate\Pagination\LengthAwarePaginator
95
+	 */
96
+	public function paginate($perPage = null)
97
+	{
98
+		return $this->mapper->paginate($perPage);
99
+	}
100 100
 
101
-    /**
102
-     * Delete an entity or an entity collection from the database
103
-     *
104
-     * @param  Mappable|EntityCollection $entity
105
-     * @throws MappingException
106
-     * @throws \InvalidArgumentException
107
-     * @return \Illuminate\Support\Collection|null
108
-     */
109
-    public function delete($entity)
110
-    {
111
-        return $this->mapper->delete($entity);
112
-    }
101
+	/**
102
+	 * Delete an entity or an entity collection from the database
103
+	 *
104
+	 * @param  Mappable|EntityCollection $entity
105
+	 * @throws MappingException
106
+	 * @throws \InvalidArgumentException
107
+	 * @return \Illuminate\Support\Collection|null
108
+	 */
109
+	public function delete($entity)
110
+	{
111
+		return $this->mapper->delete($entity);
112
+	}
113 113
 
114
-    /**
115
-     * Persist an entity or an entity collection in the database.
116
-     *
117
-     * @param  Mappable|EntityCollection|array $entity
118
-     * @throws MappingException
119
-     * @throws \InvalidArgumentException
120
-     * @return Mappable|EntityCollection|array
121
-     */
122
-    public function store($entity)
123
-    {
124
-        return $this->mapper->store($entity);
125
-    }
114
+	/**
115
+	 * Persist an entity or an entity collection in the database.
116
+	 *
117
+	 * @param  Mappable|EntityCollection|array $entity
118
+	 * @throws MappingException
119
+	 * @throws \InvalidArgumentException
120
+	 * @return Mappable|EntityCollection|array
121
+	 */
122
+	public function store($entity)
123
+	{
124
+		return $this->mapper->store($entity);
125
+	}
126 126
 
127
-    /**
128
-     * Make custom mapper custom commands available in repository
129
-     *
130
-     * @param  string $method
131
-     * @param  array  $parameters
132
-     * @throws Exception
133
-     * @return mixed
134
-     */
135
-    public function __call($method, $parameters)
136
-    {
137
-        if ($this->mapper->hasCustomCommand($method)) {
138
-            call_user_func_array([$this->mapper, $method], $parameters);
139
-        } else {
140
-            throw new Exception("No method $method on " . get_class($this));
141
-        }
142
-    }
127
+	/**
128
+	 * Make custom mapper custom commands available in repository
129
+	 *
130
+	 * @param  string $method
131
+	 * @param  array  $parameters
132
+	 * @throws Exception
133
+	 * @return mixed
134
+	 */
135
+	public function __call($method, $parameters)
136
+	{
137
+		if ($this->mapper->hasCustomCommand($method)) {
138
+			call_user_func_array([$this->mapper, $method], $parameters);
139
+		} else {
140
+			throw new Exception("No method $method on " . get_class($this));
141
+		}
142
+	}
143 143
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -137,7 +137,7 @@
 block discarded – undo
137 137
         if ($this->mapper->hasCustomCommand($method)) {
138 138
             call_user_func_array([$this->mapper, $method], $parameters);
139 139
         } else {
140
-            throw new Exception("No method $method on " . get_class($this));
140
+            throw new Exception("No method $method on ".get_class($this));
141 141
         }
142 142
     }
143 143
 }
Please login to merge, or discard this patch.
src/Exceptions/EntityNotFoundException.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -6,35 +6,35 @@
 block discarded – undo
6 6
 
7 7
 class EntityNotFoundException extends RuntimeException
8 8
 {
9
-    /**
10
-     * Name of the affected Entity Map.
11
-     *
12
-     * @var string
13
-     */
14
-    protected $entity;
9
+	/**
10
+	 * Name of the affected Entity Map.
11
+	 *
12
+	 * @var string
13
+	 */
14
+	protected $entity;
15 15
 
16
-    /**
17
-     * Set the affected Entity Map.
18
-     *
19
-     * @param  string   $entity
20
-     * @return $this
21
-     */
22
-    public function setEntity($entity)
23
-    {
24
-        $this->entity = $entity;
16
+	/**
17
+	 * Set the affected Entity Map.
18
+	 *
19
+	 * @param  string   $entity
20
+	 * @return $this
21
+	 */
22
+	public function setEntity($entity)
23
+	{
24
+		$this->entity = $entity;
25 25
 
26
-        $this->message = "No query results for entity [{$entity}].";
26
+		$this->message = "No query results for entity [{$entity}].";
27 27
 
28
-        return $this;
29
-    }
28
+		return $this;
29
+	}
30 30
 
31
-    /**
32
-     * Get the affected Entity.
33
-     *
34
-     * @return string
35
-     */
36
-    public function getEntity()
37
-    {
38
-        return $this->entity;
39
-    }
31
+	/**
32
+	 * Get the affected Entity.
33
+	 *
34
+	 * @return string
35
+	 */
36
+	public function getEntity()
37
+	{
38
+		return $this->entity;
39
+	}
40 40
 }
Please login to merge, or discard this patch.
src/System/ScopeInterface.php 1 patch
Indentation   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -4,19 +4,19 @@
 block discarded – undo
4 4
 
5 5
 interface ScopeInterface
6 6
 {
7
-    /**
8
-     * Apply the scope to a given Query builder.
9
-     *
10
-     * @param  \Analogue\ORM\System\Query $builder
11
-     * @return void
12
-     */
13
-    public function apply(Query $builder);
7
+	/**
8
+	 * Apply the scope to a given Query builder.
9
+	 *
10
+	 * @param  \Analogue\ORM\System\Query $builder
11
+	 * @return void
12
+	 */
13
+	public function apply(Query $builder);
14 14
 
15
-    /**
16
-     * Remove the scope from the Query builder.
17
-     *
18
-     * @param  \Analogue\ORM\System\Query $builder
19
-     * @return void
20
-     */
21
-    public function remove(Query $builder);
15
+	/**
16
+	 * Remove the scope from the Query builder.
17
+	 *
18
+	 * @param  \Analogue\ORM\System\Query $builder
19
+	 * @return void
20
+	 */
21
+	public function remove(Query $builder);
22 22
 }
Please login to merge, or discard this patch.
src/System/CachedRelationship.php 1 patch
Indentation   +58 added lines, -58 removed lines patch added patch discarded remove patch
@@ -8,68 +8,68 @@
 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
-     * @param $hash
28
-     * @param array $pivotAttributes
29
-     */
30
-    public function __construct($hash, $pivotAttributes = [])
31
-    {
32
-        $this->hash = $hash;
33
-        $this->pivotAttributes = $pivotAttributes;
34
-    }
25
+	/**
26
+	 * CachedRelationship constructor.
27
+	 * @param $hash
28
+	 * @param array $pivotAttributes
29
+	 */
30
+	public function __construct($hash, $pivotAttributes = [])
31
+	{
32
+		$this->hash = $hash;
33
+		$this->pivotAttributes = $pivotAttributes;
34
+	}
35 35
 
36
-    /**
37
-     * Return true if any pivot attributes are present
38
-     *
39
-     * @return boolean
40
-     */
41
-    public function hasPivotAttributes()
42
-    {
43
-        return count($this->pivotAttributes) > 0;
44
-    }
36
+	/**
37
+	 * Return true if any pivot attributes are present
38
+	 *
39
+	 * @return boolean
40
+	 */
41
+	public function hasPivotAttributes()
42
+	{
43
+		return count($this->pivotAttributes) > 0;
44
+	}
45 45
 
46
-    /**
47
-     * Returns the hash of the related entity
48
-     *
49
-     * @return string
50
-     */
51
-    public function getHash()
52
-    {
53
-        return $this->hash;
54
-    }
46
+	/**
47
+	 * Returns the hash of the related entity
48
+	 *
49
+	 * @return string
50
+	 */
51
+	public function getHash()
52
+	{
53
+		return $this->hash;
54
+	}
55 55
 
56
-    /**
57
-     * Get the cached values for the pivot attributes
58
-     *
59
-     * @return array
60
-     */
61
-    public function getPivotAttributes()
62
-    {
63
-        return $this->pivotAttributes;
64
-    }
56
+	/**
57
+	 * Get the cached values for the pivot attributes
58
+	 *
59
+	 * @return array
60
+	 */
61
+	public function getPivotAttributes()
62
+	{
63
+		return $this->pivotAttributes;
64
+	}
65 65
 
66
-    /**
67
-     * Access to the hash for fast cache comparison
68
-     *
69
-     * @return string
70
-     */
71
-    public function __toString()
72
-    {
73
-        return $this->hash;
74
-    }
66
+	/**
67
+	 * Access to the hash for fast cache comparison
68
+	 *
69
+	 * @return string
70
+	 */
71
+	public function __toString()
72
+	{
73
+		return $this->hash;
74
+	}
75 75
 }
Please login to merge, or discard this patch.
src/System/Wrappers/EntityWrapper.php 1 patch
Indentation   +68 added lines, -68 removed lines patch added patch discarded remove patch
@@ -7,79 +7,79 @@
 block discarded – undo
7 7
  */
8 8
 class EntityWrapper extends Wrapper
9 9
 {
10
-    /**
11
-     * Method used by the mapper to set the object
12
-     * attribute raw values (hydration)
13
-     *
14
-     * @param array $attributes
15
-     *
16
-     * @return void
17
-     */
18
-    public function setEntityAttributes(array $attributes)
19
-    {
20
-        $this->entity->setEntityAttributes($attributes);
21
-    }
10
+	/**
11
+	 * Method used by the mapper to set the object
12
+	 * attribute raw values (hydration)
13
+	 *
14
+	 * @param array $attributes
15
+	 *
16
+	 * @return void
17
+	 */
18
+	public function setEntityAttributes(array $attributes)
19
+	{
20
+		$this->entity->setEntityAttributes($attributes);
21
+	}
22 22
 
23
-    /**
24
-     * Method used by the mapper to get the
25
-     * raw object's values.
26
-     *
27
-     * @return array
28
-     */
29
-    public function getEntityAttributes()
30
-    {
31
-        return $this->entity->getEntityAttributes();
32
-    }
23
+	/**
24
+	 * Method used by the mapper to get the
25
+	 * raw object's values.
26
+	 *
27
+	 * @return array
28
+	 */
29
+	public function getEntityAttributes()
30
+	{
31
+		return $this->entity->getEntityAttributes();
32
+	}
33 33
 
34
-    /**
35
-     * Method used by the mapper to set raw
36
-     * key-value pair
37
-     *
38
-     * @param string $key
39
-     * @param string $value
40
-     *
41
-     * @return void
42
-     */
43
-    public function setEntityAttribute($key, $value)
44
-    {
45
-        $attributes = $this->entity->getEntityAttributes();
34
+	/**
35
+	 * Method used by the mapper to set raw
36
+	 * key-value pair
37
+	 *
38
+	 * @param string $key
39
+	 * @param string $value
40
+	 *
41
+	 * @return void
42
+	 */
43
+	public function setEntityAttribute($key, $value)
44
+	{
45
+		$attributes = $this->entity->getEntityAttributes();
46 46
 
47
-        $attributes[$key] = $value;
47
+		$attributes[$key] = $value;
48 48
 
49
-        $this->entity->setEntityAttributes($attributes);
50
-    }
49
+		$this->entity->setEntityAttributes($attributes);
50
+	}
51 51
 
52
-    /**
53
-     * Method used by the mapper to get single
54
-     * key-value pair
55
-     *
56
-     * @param  string $key
57
-     * @return mixed|null
58
-     */
59
-    public function getEntityAttribute($key)
60
-    {
61
-        if ($this->hasAttribute($key)) {
62
-            $attributes = $this->entity->getEntityAttributes();
63
-            return $attributes[$key];
64
-        } else {
65
-            return null;
66
-        }
67
-    }
52
+	/**
53
+	 * Method used by the mapper to get single
54
+	 * key-value pair
55
+	 *
56
+	 * @param  string $key
57
+	 * @return mixed|null
58
+	 */
59
+	public function getEntityAttribute($key)
60
+	{
61
+		if ($this->hasAttribute($key)) {
62
+			$attributes = $this->entity->getEntityAttributes();
63
+			return $attributes[$key];
64
+		} else {
65
+			return null;
66
+		}
67
+	}
68 68
 
69
-    /**
70
-     * Test if a given attribute exists
71
-     *
72
-     * @param  string $key
73
-     * @return boolean
74
-     */
75
-    public function hasAttribute($key)
76
-    {
77
-        $attributes = $this->entity->getEntityAttributes();
69
+	/**
70
+	 * Test if a given attribute exists
71
+	 *
72
+	 * @param  string $key
73
+	 * @return boolean
74
+	 */
75
+	public function hasAttribute($key)
76
+	{
77
+		$attributes = $this->entity->getEntityAttributes();
78 78
 
79
-        if (array_key_exists($key, $attributes)) {
80
-            return true;
81
-        } else {
82
-            return false;
83
-        }
84
-    }
79
+		if (array_key_exists($key, $attributes)) {
80
+			return true;
81
+		} else {
82
+			return false;
83
+		}
84
+	}
85 85
 }
Please login to merge, or discard this patch.
src/System/Wrappers/Factory.php 1 patch
Indentation   +21 added lines, -21 removed lines patch added patch discarded remove patch
@@ -7,27 +7,27 @@
 block discarded – undo
7 7
 
8 8
 class Factory
9 9
 {
10
-    /**
11
-     * Build the wrapper corresponding to the object's type
12
-     *
13
-     * @param  mixed $object
14
-     * @throws \Analogue\ORM\Exceptions\MappingException
15
-     * @return Wrapper
16
-     */
17
-    public function make($object)
18
-    {
19
-        $manager = Manager::getInstance();
10
+	/**
11
+	 * Build the wrapper corresponding to the object's type
12
+	 *
13
+	 * @param  mixed $object
14
+	 * @throws \Analogue\ORM\Exceptions\MappingException
15
+	 * @return Wrapper
16
+	 */
17
+	public function make($object)
18
+	{
19
+		$manager = Manager::getInstance();
20 20
 
21
-        if ($manager->isValueObject($object)) {
22
-            $entityMap = $manager->getValueMap($object);
23
-        } else {
24
-            $entityMap = $manager->mapper($object)->getEntityMap();
25
-        }
21
+		if ($manager->isValueObject($object)) {
22
+			$entityMap = $manager->getValueMap($object);
23
+		} else {
24
+			$entityMap = $manager->mapper($object)->getEntityMap();
25
+		}
26 26
 
27
-        if ($object instanceof Mappable) {
28
-            return new EntityWrapper($object, $entityMap);
29
-        } else {
30
-            return new PlainObjectWrapper($object, $entityMap);
31
-        }
32
-    }
27
+		if ($object instanceof Mappable) {
28
+			return new EntityWrapper($object, $entityMap);
29
+		} else {
30
+			return new PlainObjectWrapper($object, $entityMap);
31
+		}
32
+	}
33 33
 }
Please login to merge, or discard this patch.
src/System/EntityBuilder.php 2 patches
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -152,13 +152,13 @@
 block discarded – undo
152 152
         $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
153 153
 
154 154
         foreach ($embeddedAttributes as $key) {
155
-            $prefix = snake_case(class_basename($valueClass)) . '_';
155
+            $prefix = snake_case(class_basename($valueClass)).'_';
156 156
 
157 157
             $voWrapper = $this->factory->make($valueObject);
158 158
 
159
-            $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
159
+            $voWrapper->setEntityAttribute($key, $attributes[$prefix.$key]);
160 160
             
161
-            unset($attributes[$prefix . $key]);
161
+            unset($attributes[$prefix.$key]);
162 162
         }
163 163
         
164 164
         $attributes[$localKey] = $valueObject;
Please login to merge, or discard this patch.
Indentation   +185 added lines, -185 removed lines patch added patch discarded remove patch
@@ -11,190 +11,190 @@
 block discarded – undo
11 11
  */
12 12
 class EntityBuilder
13 13
 {
14
-    /**
15
-     * The mapper for the entity to build
16
-     * @var \Analogue\ORM\System\Mapper
17
-     */
18
-    protected $mapper;
19
-
20
-    /**
21
-     * The Entity Map for the entity to build.
22
-     *
23
-     * @var \Analogue\ORM\EntityMap
24
-     */
25
-    protected $entityMap;
26
-
27
-    /**
28
-     * Relations that will be eager loaded on this query
29
-     *
30
-     * @var array
31
-     */
32
-    protected $eagerLoads;
33
-
34
-    /**
35
-     * Relations that will be lazy loaded on this query
36
-     *
37
-     * @var array
38
-     */
39
-    protected $lazyLoads;
40
-
41
-    /**
42
-     * @var array
43
-     */
44
-    protected $casts;
45
-
46
-    /**
47
-     * Entity Wrapper Factory
48
-     * @var \Analogue\ORM\System\Wrappers\Factory
49
-     */
50
-    protected $factory;
51
-
52
-    /**
53
-     * EntityBuilder constructor.
54
-     * @param Mapper $mapper
55
-     * @param array  $eagerLoads
56
-     */
57
-    public function __construct(Mapper $mapper, array $eagerLoads)
58
-    {
59
-        $this->mapper = $mapper;
60
-
61
-        $this->entityMap = $mapper->getEntityMap();
62
-
63
-        $this->eagerLoads = $eagerLoads;
64
-
65
-        $this->lazyLoads = $this->getRelationshipsToProxy();
66
-
67
-        $this->factory = new Factory;
68
-    }
69
-
70
-    /**
71
-     * Convert an array of values into an entity.
72
-     *
73
-     * @param  array $result
74
-     * @return array
75
-     */
76
-    public function build(array $result)
77
-    {
78
-        $keyName = $this->entityMap->getKeyName();
79
-
80
-        $tmpCache = [];
81
-
82
-        $instance = $this->getWrapperInstance();
14
+	/**
15
+	 * The mapper for the entity to build
16
+	 * @var \Analogue\ORM\System\Mapper
17
+	 */
18
+	protected $mapper;
19
+
20
+	/**
21
+	 * The Entity Map for the entity to build.
22
+	 *
23
+	 * @var \Analogue\ORM\EntityMap
24
+	 */
25
+	protected $entityMap;
26
+
27
+	/**
28
+	 * Relations that will be eager loaded on this query
29
+	 *
30
+	 * @var array
31
+	 */
32
+	protected $eagerLoads;
33
+
34
+	/**
35
+	 * Relations that will be lazy loaded on this query
36
+	 *
37
+	 * @var array
38
+	 */
39
+	protected $lazyLoads;
40
+
41
+	/**
42
+	 * @var array
43
+	 */
44
+	protected $casts;
45
+
46
+	/**
47
+	 * Entity Wrapper Factory
48
+	 * @var \Analogue\ORM\System\Wrappers\Factory
49
+	 */
50
+	protected $factory;
51
+
52
+	/**
53
+	 * EntityBuilder constructor.
54
+	 * @param Mapper $mapper
55
+	 * @param array  $eagerLoads
56
+	 */
57
+	public function __construct(Mapper $mapper, array $eagerLoads)
58
+	{
59
+		$this->mapper = $mapper;
60
+
61
+		$this->entityMap = $mapper->getEntityMap();
62
+
63
+		$this->eagerLoads = $eagerLoads;
64
+
65
+		$this->lazyLoads = $this->getRelationshipsToProxy();
66
+
67
+		$this->factory = new Factory;
68
+	}
69
+
70
+	/**
71
+	 * Convert an array of values into an entity.
72
+	 *
73
+	 * @param  array $result
74
+	 * @return array
75
+	 */
76
+	public function build(array $result)
77
+	{
78
+		$keyName = $this->entityMap->getKeyName();
79
+
80
+		$tmpCache = [];
81
+
82
+		$instance = $this->getWrapperInstance();
83 83
         
84
-        $tmpCache[$result[$keyName]] = $result;
85
-
86
-        // Hydrate any embedded Value Object
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
-        // Directly Unwrap the entity now that it has been hydrated
97
-        $entity = $instance->getObject();
98
-
99
-        $this->mapper->getEntityCache()->add($tmpCache);
100
-
101
-        return $entity;
102
-    }
103
-
104
-    /**
105
-     * Get the correct wrapper prototype corresponding to the object type
106
-     *
107
-     * @throws \Analogue\ORM\Exceptions\MappingException
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
-     * @throws \Analogue\ORM\Exceptions\MappingException
120
-     * @return void
121
-     */
122
-    protected function hydrateValueObjects(& $attributes)
123
-    {
124
-        foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
125
-            $this->hydrateValueObject($attributes, $localKey, $valueClass);
126
-        }
127
-    }
128
-
129
-    /**
130
-     * Hydrate a single value object
131
-     *
132
-     * @param  array  $attributes
133
-     * @param  string $localKey
134
-     * @param  string $valueClass
135
-     * @throws \Analogue\ORM\Exceptions\MappingException
136
-     * @return void
137
-     */
138
-    protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
139
-    {
140
-        $map = $this->mapper->getManager()->getValueMap($valueClass);
141
-
142
-        $embeddedAttributes = $map->getAttributes();
143
-
144
-        $valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
145
-
146
-        foreach ($embeddedAttributes as $key) {
147
-            $prefix = snake_case(class_basename($valueClass)) . '_';
148
-
149
-            $voWrapper = $this->factory->make($valueObject);
150
-
151
-            $voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
152
-
153
-            unset($attributes[$prefix . $key]);
154
-        }
155
-
156
-        $attributes[$localKey] = $valueObject;
157
-    }
158
-
159
-    /**
160
-     * Deduce the relationships for which we will build lazy loading proxies
161
-     *
162
-     * @return array
163
-     */
164
-    protected function getRelationshipsToProxy()
165
-    {
166
-        $relations = $this->entityMap->getRelationships();
167
-
168
-        return array_diff($relations, $this->eagerLoads);
169
-    }
170
-
171
-    /**
172
-     * Build lazy loading proxies for the current entity
173
-     *
174
-     * @param InternallyMappable $entity
175
-     *
176
-     * @return array
177
-     */
178
-    protected function getLazyLoadingProxies(InternallyMappable $entity)
179
-    {
180
-        $proxies = [];
181
-
182
-        $singleRelations = $this->entityMap->getSingleRelationships();
183
-        $manyRelations = $this->entityMap->getManyRelationships();
184
-
185
-        $proxyFactory = new ProxyFactory;
186
-
187
-        foreach ($this->lazyLoads as $relation) {
188
-            if (in_array($relation, $singleRelations)) {
189
-                //$proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
190
-                $targetClass = $this->entityMap->getTargetClass($relation);
191
-                $proxies[$relation] = $proxyFactory->make($entity->getObject(), $relation, $targetClass);
192
-            }
193
-            if (in_array($relation, $manyRelations)) {
194
-                $proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
195
-            }
196
-        }
197
-
198
-        return $proxies;
199
-    }
84
+		$tmpCache[$result[$keyName]] = $result;
85
+
86
+		// Hydrate any embedded Value Object
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
+		// Directly Unwrap the entity now that it has been hydrated
97
+		$entity = $instance->getObject();
98
+
99
+		$this->mapper->getEntityCache()->add($tmpCache);
100
+
101
+		return $entity;
102
+	}
103
+
104
+	/**
105
+	 * Get the correct wrapper prototype corresponding to the object type
106
+	 *
107
+	 * @throws \Analogue\ORM\Exceptions\MappingException
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
+	 * @throws \Analogue\ORM\Exceptions\MappingException
120
+	 * @return void
121
+	 */
122
+	protected function hydrateValueObjects(& $attributes)
123
+	{
124
+		foreach ($this->entityMap->getEmbeddables() as $localKey => $valueClass) {
125
+			$this->hydrateValueObject($attributes, $localKey, $valueClass);
126
+		}
127
+	}
128
+
129
+	/**
130
+	 * Hydrate a single value object
131
+	 *
132
+	 * @param  array  $attributes
133
+	 * @param  string $localKey
134
+	 * @param  string $valueClass
135
+	 * @throws \Analogue\ORM\Exceptions\MappingException
136
+	 * @return void
137
+	 */
138
+	protected function hydrateValueObject(& $attributes, $localKey, $valueClass)
139
+	{
140
+		$map = $this->mapper->getManager()->getValueMap($valueClass);
141
+
142
+		$embeddedAttributes = $map->getAttributes();
143
+
144
+		$valueObject = $this->mapper->getManager()->getValueObjectInstance($valueClass);
145
+
146
+		foreach ($embeddedAttributes as $key) {
147
+			$prefix = snake_case(class_basename($valueClass)) . '_';
148
+
149
+			$voWrapper = $this->factory->make($valueObject);
150
+
151
+			$voWrapper->setEntityAttribute($key, $attributes[$prefix . $key]);
152
+
153
+			unset($attributes[$prefix . $key]);
154
+		}
155
+
156
+		$attributes[$localKey] = $valueObject;
157
+	}
158
+
159
+	/**
160
+	 * Deduce the relationships for which we will build lazy loading proxies
161
+	 *
162
+	 * @return array
163
+	 */
164
+	protected function getRelationshipsToProxy()
165
+	{
166
+		$relations = $this->entityMap->getRelationships();
167
+
168
+		return array_diff($relations, $this->eagerLoads);
169
+	}
170
+
171
+	/**
172
+	 * Build lazy loading proxies for the current entity
173
+	 *
174
+	 * @param InternallyMappable $entity
175
+	 *
176
+	 * @return array
177
+	 */
178
+	protected function getLazyLoadingProxies(InternallyMappable $entity)
179
+	{
180
+		$proxies = [];
181
+
182
+		$singleRelations = $this->entityMap->getSingleRelationships();
183
+		$manyRelations = $this->entityMap->getManyRelationships();
184
+
185
+		$proxyFactory = new ProxyFactory;
186
+
187
+		foreach ($this->lazyLoads as $relation) {
188
+			if (in_array($relation, $singleRelations)) {
189
+				//$proxies[$relation] = new EntityProxy($entity->getObject(), $relation);
190
+				$targetClass = $this->entityMap->getTargetClass($relation);
191
+				$proxies[$relation] = $proxyFactory->make($entity->getObject(), $relation, $targetClass);
192
+			}
193
+			if (in_array($relation, $manyRelations)) {
194
+				$proxies[$relation] = new CollectionProxy($entity->getObject(), $relation);
195
+			}
196
+		}
197
+
198
+		return $proxies;
199
+	}
200 200
 }
201 201
\ No newline at end of file
Please login to merge, or discard this patch.
src/System/EntityCache.php 2 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -231,7 +231,7 @@
 block discarded – undo
231 231
 
232 232
         $keyName = $mapper->getEntityMap()->getKeyName();
233 233
 
234
-        return $class . '.' . $entity->getEntityAttribute($keyName);
234
+        return $class.'.'.$entity->getEntityAttribute($keyName);
235 235
     }
236 236
 
237 237
     /**
Please login to merge, or discard this patch.
Indentation   +313 added lines, -313 removed lines patch added patch discarded remove patch
@@ -15,319 +15,319 @@
 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
-     * @var \Analogue\ORM\EntityMap
28
-     */
29
-    protected $entityMap;
30
-
31
-    /**
32
-     * Wrapper factory
33
-     *
34
-     * @var \Analogue\ORM\System\Wrappers\Factory
35
-     */
36
-    protected $factory;
37
-
38
-    /**
39
-     * Associative array containing list of pivot attributes per relationship
40
-     * so we don't have to call relationship method on refresh.
41
-     *
42
-     * @var array
43
-     */
44
-    protected $pivotAttributes = [];
45
-
46
-    /**
47
-     * EntityCache constructor.
48
-     * @param EntityMap $entityMap
49
-     */
50
-    public function __construct(EntityMap $entityMap)
51
-    {
52
-        $this->entityMap = $entityMap;
53
-
54
-        $this->factory = new Factory;
55
-    }
56
-
57
-    /**
58
-     * Add an array of key=>attributes representing
59
-     * the initial state of loaded entities.
60
-     *
61
-     * @param array $entities
62
-     */
63
-    public function add(array $entities)
64
-    {
65
-        if (count($this->cache) == 0) {
66
-            $this->cache = $entities;
67
-        } else {
68
-            $this->mergeCacheResults($entities);
69
-        }
70
-    }
71
-
72
-    /**
73
-     * Retrieve initial attributes for a single entity
74
-     *
75
-     * @param  string $id
76
-     * @return array
77
-     */
78
-    public function get($id)
79
-    {
80
-        if ($this->has($id)) {
81
-            return $this->cache[$id];
82
-        } else {
83
-            return [];
84
-        }
85
-    }
86
-
87
-    /**
88
-     * Check if a record for this id exists.
89
-     *
90
-     * @param  string  $id
91
-     * @return boolean
92
-     */
93
-    public function has($id)
94
-    {
95
-        return array_key_exists($id, $this->cache);
96
-    }
97
-
98
-    /**
99
-     * Combine new result set with existing attributes in
100
-     * cache.
101
-     *
102
-     * @param  array $entities
103
-     * @return void
104
-     */
105
-    protected function mergeCacheResults($entities)
106
-    {
107
-        foreach ($entities as $key => $entity) {
108
-            $this->cache[$key] = $entity;
109
-        }
110
-    }
111
-
112
-    /**
113
-     * Cache Relation's query result for an entity
114
-     *
115
-     * @param  mixed        $parent
116
-     * @param  string       $relation name of the relation
117
-     * @param  mixed        $results  results of the relationship's query
118
-     * @param  Relationship $relationship
119
-     * @throws MappingException
120
-     * @return void
121
-     */
122
-    public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship)
123
-    {
124
-        $keyName = $this->entityMap->getKeyName();
125
-
126
-        if (!$parent instanceof InternallyMappable) {
127
-            $parent = $this->factory->make($parent);
128
-        }
129
-
130
-        $key = $parent->getEntityAttribute($keyName);
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
+	 * @var \Analogue\ORM\EntityMap
28
+	 */
29
+	protected $entityMap;
30
+
31
+	/**
32
+	 * Wrapper factory
33
+	 *
34
+	 * @var \Analogue\ORM\System\Wrappers\Factory
35
+	 */
36
+	protected $factory;
37
+
38
+	/**
39
+	 * Associative array containing list of pivot attributes per relationship
40
+	 * so we don't have to call relationship method on refresh.
41
+	 *
42
+	 * @var array
43
+	 */
44
+	protected $pivotAttributes = [];
45
+
46
+	/**
47
+	 * EntityCache constructor.
48
+	 * @param EntityMap $entityMap
49
+	 */
50
+	public function __construct(EntityMap $entityMap)
51
+	{
52
+		$this->entityMap = $entityMap;
53
+
54
+		$this->factory = new Factory;
55
+	}
56
+
57
+	/**
58
+	 * Add an array of key=>attributes representing
59
+	 * the initial state of loaded entities.
60
+	 *
61
+	 * @param array $entities
62
+	 */
63
+	public function add(array $entities)
64
+	{
65
+		if (count($this->cache) == 0) {
66
+			$this->cache = $entities;
67
+		} else {
68
+			$this->mergeCacheResults($entities);
69
+		}
70
+	}
71
+
72
+	/**
73
+	 * Retrieve initial attributes for a single entity
74
+	 *
75
+	 * @param  string $id
76
+	 * @return array
77
+	 */
78
+	public function get($id)
79
+	{
80
+		if ($this->has($id)) {
81
+			return $this->cache[$id];
82
+		} else {
83
+			return [];
84
+		}
85
+	}
86
+
87
+	/**
88
+	 * Check if a record for this id exists.
89
+	 *
90
+	 * @param  string  $id
91
+	 * @return boolean
92
+	 */
93
+	public function has($id)
94
+	{
95
+		return array_key_exists($id, $this->cache);
96
+	}
97
+
98
+	/**
99
+	 * Combine new result set with existing attributes in
100
+	 * cache.
101
+	 *
102
+	 * @param  array $entities
103
+	 * @return void
104
+	 */
105
+	protected function mergeCacheResults($entities)
106
+	{
107
+		foreach ($entities as $key => $entity) {
108
+			$this->cache[$key] = $entity;
109
+		}
110
+	}
111
+
112
+	/**
113
+	 * Cache Relation's query result for an entity
114
+	 *
115
+	 * @param  mixed        $parent
116
+	 * @param  string       $relation name of the relation
117
+	 * @param  mixed        $results  results of the relationship's query
118
+	 * @param  Relationship $relationship
119
+	 * @throws MappingException
120
+	 * @return void
121
+	 */
122
+	public function cacheLoadedRelationResult($parent, $relation, $results, Relationship $relationship)
123
+	{
124
+		$keyName = $this->entityMap->getKeyName();
125
+
126
+		if (!$parent instanceof InternallyMappable) {
127
+			$parent = $this->factory->make($parent);
128
+		}
129
+
130
+		$key = $parent->getEntityAttribute($keyName);
131 131
         
132
-        if ($results instanceof EntityCollection) {
133
-            $this->cacheManyRelationResults($key, $relation, $results, $relationship);
134
-        }
135
-
136
-        // POPO : Maybe this check isn't needed, or we have to check for stdClass
137
-        // instead
138
-        if ($results instanceof Mappable) {
139
-            $this->cacheSingleRelationResult($key, $relation, $results, $relationship);
140
-        }
141
-    }
142
-
143
-    /**
144
-     * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
145
-     *
146
-     * @param  string       $parentKey
147
-     * @param  string       $relation
148
-     * @param  array        $result
149
-     * @param  Relationship $relationship
150
-     * @throws MappingException
151
-     * @return CachedRelationship
152
-     */
153
-    protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
154
-    {
155
-        $pivotColumns = $relationship->getPivotAttributes();
156
-
157
-        if (!array_key_exists($relation, $this->pivotAttributes)) {
158
-            $this->pivotAttributes[$relation] = $pivotColumns;
159
-        }
160
-
161
-        $wrapper = $this->factory->make($result);
162
-
163
-        $hash = $this->getEntityHash($wrapper);
164
-
165
-        if (count($pivotColumns) > 0) {
166
-            $pivotAttributes = [];
167
-            foreach ($pivotColumns as $column) {
168
-                $pivot = $wrapper->getEntityAttribute('pivot');
169
-
170
-                $pivotWrapper = $this->factory->make($pivot);
171
-
172
-                $pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
173
-            }
174
-
175
-            $cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
176
-        } else {
177
-            $cachedRelationship = new CachedRelationship($hash);
178
-        }
179
-
180
-        return $cachedRelationship;
181
-    }
182
-
183
-    /**
184
-     * Cache a many relationship
185
-     *
186
-     * @param                  $parentKey
187
-     * @param string           $relation
188
-     * @param EntityCollection $results
189
-     * @param Relationship     $relationship
190
-     * @throws MappingException
191
-     */
192
-    protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
193
-    {
194
-        $this->cache[$parentKey][$relation] = [];
195
-
196
-        foreach ($results as $result) {
197
-            $cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
198
-
199
-            $relatedHash = $cachedRelationship->getHash();
200
-
201
-            $this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
202
-        }
203
-    }
204
-
205
-    /**
206
-     * Cache a single relationship
207
-     *
208
-     * @param              $parentKey
209
-     * @param string       $relation
210
-     * @param Mappable     $result
211
-     * @param Relationship $relationship
212
-     * @throws MappingException
213
-     */
214
-    protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
215
-    {
216
-        $this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
217
-    }
218
-
219
-    /**
220
-     * Get Entity's Hash
221
-     *
222
-     * @param  $entity
223
-     * @throws MappingException
224
-     * @return string
225
-     */
226
-    protected function getEntityHash(InternallyMappable $entity)
227
-    {
228
-        $class = get_class($entity->getObject());
229
-
230
-        $mapper = Manager::getMapper($class);
231
-
232
-        $keyName = $mapper->getEntityMap()->getKeyName();
233
-
234
-        return $class . '.' . $entity->getEntityAttribute($keyName);
235
-    }
236
-
237
-    /**
238
-     * Refresh the cache record for an aggregated entity after a write operation
239
-     * @param Aggregate $entity
240
-     */
241
-    public function refresh(Aggregate $entity)
242
-    {
243
-        $this->cache[$entity->getEntityId()] = $this->transform($entity);
244
-    }
245
-
246
-    /**
247
-     * Transform an Aggregated Entity into a cache record
248
-     *
249
-     * @param  Aggregate $aggregatedEntity
250
-     * @throws MappingException
251
-     * @return array
252
-     */
253
-    protected function transform(Aggregate $aggregatedEntity)
254
-    {
255
-        $baseAttributes = $aggregatedEntity->getRawAttributes();
256
-
257
-        $relationAttributes = [];
258
-
259
-        // First we'll handle each relationships that are a one to one
260
-        // relation, and which will be saved as a CachedRelationship
261
-        // object inside the cache.
262
-
263
-        // NOTE : storing localRelationships maybe useless has we store
264
-        // the foreign key in the attributes already.
265
-
266
-        foreach ($this->entityMap->getSingleRelationships() as $relation) {
267
-            $aggregates = $aggregatedEntity->getRelationship($relation);
132
+		if ($results instanceof EntityCollection) {
133
+			$this->cacheManyRelationResults($key, $relation, $results, $relationship);
134
+		}
135
+
136
+		// POPO : Maybe this check isn't needed, or we have to check for stdClass
137
+		// instead
138
+		if ($results instanceof Mappable) {
139
+			$this->cacheSingleRelationResult($key, $relation, $results, $relationship);
140
+		}
141
+	}
142
+
143
+	/**
144
+	 * Create a cachedRelationship instance which will hold related entity's hash and pivot attributes, if any.
145
+	 *
146
+	 * @param  string       $parentKey
147
+	 * @param  string       $relation
148
+	 * @param  array        $result
149
+	 * @param  Relationship $relationship
150
+	 * @throws MappingException
151
+	 * @return CachedRelationship
152
+	 */
153
+	protected function getCachedRelationship($parentKey, $relation, $result, Relationship $relationship)
154
+	{
155
+		$pivotColumns = $relationship->getPivotAttributes();
156
+
157
+		if (!array_key_exists($relation, $this->pivotAttributes)) {
158
+			$this->pivotAttributes[$relation] = $pivotColumns;
159
+		}
160
+
161
+		$wrapper = $this->factory->make($result);
162
+
163
+		$hash = $this->getEntityHash($wrapper);
164
+
165
+		if (count($pivotColumns) > 0) {
166
+			$pivotAttributes = [];
167
+			foreach ($pivotColumns as $column) {
168
+				$pivot = $wrapper->getEntityAttribute('pivot');
169
+
170
+				$pivotWrapper = $this->factory->make($pivot);
171
+
172
+				$pivotAttributes[$column] = $pivotWrapper->getEntityAttribute($column);
173
+			}
174
+
175
+			$cachedRelationship = new CachedRelationship($hash, $pivotAttributes);
176
+		} else {
177
+			$cachedRelationship = new CachedRelationship($hash);
178
+		}
179
+
180
+		return $cachedRelationship;
181
+	}
182
+
183
+	/**
184
+	 * Cache a many relationship
185
+	 *
186
+	 * @param                  $parentKey
187
+	 * @param string           $relation
188
+	 * @param EntityCollection $results
189
+	 * @param Relationship     $relationship
190
+	 * @throws MappingException
191
+	 */
192
+	protected function cacheManyRelationResults($parentKey, $relation, $results, Relationship $relationship)
193
+	{
194
+		$this->cache[$parentKey][$relation] = [];
195
+
196
+		foreach ($results as $result) {
197
+			$cachedRelationship = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
198
+
199
+			$relatedHash = $cachedRelationship->getHash();
200
+
201
+			$this->cache[$parentKey][$relation][$relatedHash] = $cachedRelationship;
202
+		}
203
+	}
204
+
205
+	/**
206
+	 * Cache a single relationship
207
+	 *
208
+	 * @param              $parentKey
209
+	 * @param string       $relation
210
+	 * @param Mappable     $result
211
+	 * @param Relationship $relationship
212
+	 * @throws MappingException
213
+	 */
214
+	protected function cacheSingleRelationResult($parentKey, $relation, $result, Relationship $relationship)
215
+	{
216
+		$this->cache[$parentKey][$relation] = $this->getCachedRelationship($parentKey, $relation, $result, $relationship);
217
+	}
218
+
219
+	/**
220
+	 * Get Entity's Hash
221
+	 *
222
+	 * @param  $entity
223
+	 * @throws MappingException
224
+	 * @return string
225
+	 */
226
+	protected function getEntityHash(InternallyMappable $entity)
227
+	{
228
+		$class = get_class($entity->getObject());
229
+
230
+		$mapper = Manager::getMapper($class);
231
+
232
+		$keyName = $mapper->getEntityMap()->getKeyName();
233
+
234
+		return $class . '.' . $entity->getEntityAttribute($keyName);
235
+	}
236
+
237
+	/**
238
+	 * Refresh the cache record for an aggregated entity after a write operation
239
+	 * @param Aggregate $entity
240
+	 */
241
+	public function refresh(Aggregate $entity)
242
+	{
243
+		$this->cache[$entity->getEntityId()] = $this->transform($entity);
244
+	}
245
+
246
+	/**
247
+	 * Transform an Aggregated Entity into a cache record
248
+	 *
249
+	 * @param  Aggregate $aggregatedEntity
250
+	 * @throws MappingException
251
+	 * @return array
252
+	 */
253
+	protected function transform(Aggregate $aggregatedEntity)
254
+	{
255
+		$baseAttributes = $aggregatedEntity->getRawAttributes();
256
+
257
+		$relationAttributes = [];
258
+
259
+		// First we'll handle each relationships that are a one to one
260
+		// relation, and which will be saved as a CachedRelationship
261
+		// object inside the cache.
262
+
263
+		// NOTE : storing localRelationships maybe useless has we store
264
+		// the foreign key in the attributes already.
265
+
266
+		foreach ($this->entityMap->getSingleRelationships() as $relation) {
267
+			$aggregates = $aggregatedEntity->getRelationship($relation);
268 268
             
269
-            if (count($aggregates) == 1) {
270
-                $related = $aggregates[0];
271
-                $relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
272
-            }
273
-            if (count($aggregates) > 1) {
274
-                throw new MappingException("Single Relationship '$relation' contains several related entities");
275
-            }
276
-        }
277
-
278
-        // Then we'll handle the 'many' relationships and store them as
279
-        // an array of CachedRelationship objects.
280
-
281
-        foreach ($this->entityMap->getManyRelationships() as $relation) {
282
-            $aggregates = $aggregatedEntity->getRelationship($relation);
283
-
284
-            $relationAttributes[$relation] = [];
285
-
286
-            foreach ($aggregates as $aggregate) {
287
-                $relationAttributes[$relation][] = new CachedRelationship(
288
-                    $aggregate->getEntityHash(),
289
-                    $aggregate->getPivotAttributes()
290
-                );
291
-            }
292
-        }
293
-
294
-        return $baseAttributes + $relationAttributes;
295
-    }
296
-
297
-    /**
298
-     * Get pivot attributes for a relation
299
-     * 
300
-     * @param  string             $relation
301
-     * @param  InternallyMappable $entity
302
-     * @return array
303
-     */
304
-    protected function getPivotValues($relation, InternallyMappable $entity)
305
-    {
306
-        $values = [];
307
-
308
-        $entityAttributes = $entity->getEntityAttributes();
309
-
310
-        if (array_key_exists($relation, $this->pivotAttributes)) {
311
-            foreach ($this->pivotAttributes[$relation] as $attribute) {
312
-                if (array_key_exists($attribute, $entityAttributes)) {
313
-                    $values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
314
-                }
315
-            }
316
-        }
317
-
318
-        return $values;
319
-    }
320
-
321
-    /**
322
-     * Clear the entity Cache. Use with caution as it could result
323
-     * in impredictable behaviour if the cached entities are stored
324
-     * after the cache clear operation. 
325
-     * 
326
-     * @return void
327
-     */
328
-    public function clear()
329
-    {   
330
-        $this->cache = [];
331
-        $this->pivotAttributes = [];
332
-    }
269
+			if (count($aggregates) == 1) {
270
+				$related = $aggregates[0];
271
+				$relationAttributes[$relation] = new CachedRelationship($related->getEntityHash());
272
+			}
273
+			if (count($aggregates) > 1) {
274
+				throw new MappingException("Single Relationship '$relation' contains several related entities");
275
+			}
276
+		}
277
+
278
+		// Then we'll handle the 'many' relationships and store them as
279
+		// an array of CachedRelationship objects.
280
+
281
+		foreach ($this->entityMap->getManyRelationships() as $relation) {
282
+			$aggregates = $aggregatedEntity->getRelationship($relation);
283
+
284
+			$relationAttributes[$relation] = [];
285
+
286
+			foreach ($aggregates as $aggregate) {
287
+				$relationAttributes[$relation][] = new CachedRelationship(
288
+					$aggregate->getEntityHash(),
289
+					$aggregate->getPivotAttributes()
290
+				);
291
+			}
292
+		}
293
+
294
+		return $baseAttributes + $relationAttributes;
295
+	}
296
+
297
+	/**
298
+	 * Get pivot attributes for a relation
299
+	 * 
300
+	 * @param  string             $relation
301
+	 * @param  InternallyMappable $entity
302
+	 * @return array
303
+	 */
304
+	protected function getPivotValues($relation, InternallyMappable $entity)
305
+	{
306
+		$values = [];
307
+
308
+		$entityAttributes = $entity->getEntityAttributes();
309
+
310
+		if (array_key_exists($relation, $this->pivotAttributes)) {
311
+			foreach ($this->pivotAttributes[$relation] as $attribute) {
312
+				if (array_key_exists($attribute, $entityAttributes)) {
313
+					$values[$attribute] = $entity->getEntityAttribute('pivot')->$attribute;
314
+				}
315
+			}
316
+		}
317
+
318
+		return $values;
319
+	}
320
+
321
+	/**
322
+	 * Clear the entity Cache. Use with caution as it could result
323
+	 * in impredictable behaviour if the cached entities are stored
324
+	 * after the cache clear operation. 
325
+	 * 
326
+	 * @return void
327
+	 */
328
+	public function clear()
329
+	{   
330
+		$this->cache = [];
331
+		$this->pivotAttributes = [];
332
+	}
333 333
 }
Please login to merge, or discard this patch.