Completed
Branch 5.3-dev (e55dab)
by Rémi
10:37
created
src/Plugins/SoftDeletes/SoftDeletesPlugin.php 2 patches
Indentation   +66 added lines, -66 removed lines patch added patch discarded remove patch
@@ -13,80 +13,80 @@
 block discarded – undo
13 13
  */
14 14
 class SoftDeletesPlugin extends AnaloguePlugin
15 15
 {
16
-    /**
17
-     * Register the plugin
18
-     *
19
-     * @throws \Exception
20
-     * @return void
21
-     */
22
-    public function register()
23
-    {
24
-        $host = $this;
25
-
26
-        // Hook any mapper init and check the mapping include soft deletes.
27
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) {
28
-            $entityMap = $mapper->getEntityMap();
29
-
30
-            if ($entityMap->usesSoftDeletes()) {
31
-                $host->registerSoftDelete($mapper);
32
-            }
33
-
34
-        });
35
-    }
36
-
37
-    /**
38
-     * By hooking to the mapper initialization event, we can extend it
39
-     * with the softDelete capacity.
40
-     *
41
-     * @param  \Analogue\ORM\System\Mapper $mapper
42
-     * @throws \Analogue\ORM\Exceptions\MappingException
43
-     * @return bool|void
44
-     */
45
-    protected function registerSoftDelete(Mapper $mapper)
46
-    {
47
-        $entityMap = $mapper->getEntityMap();
48
-
49
-        // Add Scopes
50
-        $mapper->addGlobalScope(new SoftDeletingScope);
51
-
52
-        $host = $this;
53
-
54
-        // Register 'deleting' events
55
-        $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) {
16
+	/**
17
+	 * Register the plugin
18
+	 *
19
+	 * @throws \Exception
20
+	 * @return void
21
+	 */
22
+	public function register()
23
+	{
24
+		$host = $this;
25
+
26
+		// Hook any mapper init and check the mapping include soft deletes.
27
+		$this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) {
28
+			$entityMap = $mapper->getEntityMap();
29
+
30
+			if ($entityMap->usesSoftDeletes()) {
31
+				$host->registerSoftDelete($mapper);
32
+			}
33
+
34
+		});
35
+	}
36
+
37
+	/**
38
+	 * By hooking to the mapper initialization event, we can extend it
39
+	 * with the softDelete capacity.
40
+	 *
41
+	 * @param  \Analogue\ORM\System\Mapper $mapper
42
+	 * @throws \Analogue\ORM\Exceptions\MappingException
43
+	 * @return bool|void
44
+	 */
45
+	protected function registerSoftDelete(Mapper $mapper)
46
+	{
47
+		$entityMap = $mapper->getEntityMap();
48
+
49
+		// Add Scopes
50
+		$mapper->addGlobalScope(new SoftDeletingScope);
51
+
52
+		$host = $this;
53
+
54
+		// Register 'deleting' events
55
+		$mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) {
56 56
             
57
-            // Convert Entity into an EntityWrapper
58
-            $factory = new Factory;
57
+			// Convert Entity into an EntityWrapper
58
+			$factory = new Factory;
59 59
 
60
-            $wrappedEntity = $factory->make($entity);
60
+			$wrappedEntity = $factory->make($entity);
61 61
 
62
-            $deletedAtField = $entityMap->getQualifiedDeletedAtColumn();
62
+			$deletedAtField = $entityMap->getQualifiedDeletedAtColumn();
63 63
 
64
-            if (!is_null($wrappedEntity->getEntityAttribute($deletedAtField))) {
65
-                return true;
66
-            } else {
67
-                $time = new Carbon;
64
+			if (!is_null($wrappedEntity->getEntityAttribute($deletedAtField))) {
65
+				return true;
66
+			} else {
67
+				$time = new Carbon;
68 68
 
69
-                $wrappedEntity->setEntityAttribute($deletedAtField, $time);
69
+				$wrappedEntity->setEntityAttribute($deletedAtField, $time);
70 70
 
71
-                $plainObject = $wrappedEntity->getObject();
72
-                $host->manager->mapper(get_class($plainObject))->store($plainObject);
71
+				$plainObject = $wrappedEntity->getObject();
72
+				$host->manager->mapper(get_class($plainObject))->store($plainObject);
73 73
 
74
-                return false;
75
-            }
74
+				return false;
75
+			}
76 76
 
77
-        });
77
+		});
78 78
 
79
-        // Register RestoreCommand
80
-        $mapper->addCustomCommand('Analogue\ORM\Plugins\SoftDeletes\Restore');
81
-    }
79
+		// Register RestoreCommand
80
+		$mapper->addCustomCommand('Analogue\ORM\Plugins\SoftDeletes\Restore');
81
+	}
82 82
 
83
-    /**
84
-     * Get custom events provided by the plugin
85
-     *
86
-     * @return string[]
87
-     */
88
-    public function getCustomEvents()
89
-    {
90
-        return ['restoring', 'restored'];
91
-    }
83
+	/**
84
+	 * Get custom events provided by the plugin
85
+	 *
86
+	 * @return string[]
87
+	 */
88
+	public function getCustomEvents()
89
+	{
90
+		return ['restoring', 'restored'];
91
+	}
92 92
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -24,7 +24,7 @@  discard block
 block discarded – undo
24 24
         $host = $this;
25 25
 
26 26
         // Hook any mapper init and check the mapping include soft deletes.
27
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) use ($host) {
27
+        $this->manager->registerGlobalEvent('initialized', function(Mapper $mapper) use ($host) {
28 28
             $entityMap = $mapper->getEntityMap();
29 29
 
30 30
             if ($entityMap->usesSoftDeletes()) {
@@ -52,7 +52,7 @@  discard block
 block discarded – undo
52 52
         $host = $this;
53 53
 
54 54
         // Register 'deleting' events
55
-        $mapper->registerEvent('deleting', function ($entity) use ($entityMap, $host) {
55
+        $mapper->registerEvent('deleting', function($entity) use ($entityMap, $host) {
56 56
             
57 57
             // Convert Entity into an EntityWrapper
58 58
             $factory = new Factory;
Please login to merge, or discard this patch.
src/Plugins/Timestamps/TimestampsPlugin.php 2 patches
Indentation   +39 added lines, -39 removed lines patch added patch discarded remove patch
@@ -12,55 +12,55 @@
 block discarded – undo
12 12
  */
13 13
 class TimestampsPlugin extends AnaloguePlugin
14 14
 {
15
-    /**
16
-     * Register the plugin
17
-     *
18
-     * @throws \Exception
19
-     * @return void
20
-     */
21
-    public function register()
22
-    {
23
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) {
24
-            $entityMap = $mapper->getEntityMap();
15
+	/**
16
+	 * Register the plugin
17
+	 *
18
+	 * @throws \Exception
19
+	 * @return void
20
+	 */
21
+	public function register()
22
+	{
23
+		$this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) {
24
+			$entityMap = $mapper->getEntityMap();
25 25
 
26
-            if ($entityMap->usesTimestamps()) {
27
-                $mapper->registerEvent('creating', function ($entity) use ($entityMap) {
26
+			if ($entityMap->usesTimestamps()) {
27
+				$mapper->registerEvent('creating', function ($entity) use ($entityMap) {
28 28
 
29
-                    $factory = new Factory;
30
-                    $wrappedEntity = $factory->make($entity);
29
+					$factory = new Factory;
30
+					$wrappedEntity = $factory->make($entity);
31 31
 
32
-                    $createdAtField = $entityMap->getCreatedAtColumn();
33
-                    $updatedAtField = $entityMap->getUpdatedAtColumn();
32
+					$createdAtField = $entityMap->getCreatedAtColumn();
33
+					$updatedAtField = $entityMap->getUpdatedAtColumn();
34 34
 
35
-                    $time = new Carbon;
35
+					$time = new Carbon;
36 36
 
37
-                    $wrappedEntity->setEntityAttribute($createdAtField, $time);
38
-                    $wrappedEntity->setEntityAttribute($updatedAtField, $time);
37
+					$wrappedEntity->setEntityAttribute($createdAtField, $time);
38
+					$wrappedEntity->setEntityAttribute($updatedAtField, $time);
39 39
 
40
-                });
40
+				});
41 41
 
42
-                $mapper->registerEvent('updating', function ($entity) use ($entityMap) {
42
+				$mapper->registerEvent('updating', function ($entity) use ($entityMap) {
43 43
 
44
-                    $factory = new Factory;
45
-                    $wrappedEntity = $factory->make($entity);
44
+					$factory = new Factory;
45
+					$wrappedEntity = $factory->make($entity);
46 46
 
47
-                    $updatedAtField = $entityMap->getUpdatedAtColumn();
47
+					$updatedAtField = $entityMap->getUpdatedAtColumn();
48 48
 
49
-                    $time = new Carbon;
49
+					$time = new Carbon;
50 50
 
51
-                    $wrappedEntity->setEntityAttribute($updatedAtField, $time);
52
-                });
53
-            }
54
-        });
55
-    }
51
+					$wrappedEntity->setEntityAttribute($updatedAtField, $time);
52
+				});
53
+			}
54
+		});
55
+	}
56 56
 
57
-    /**
58
-     * Get custom events provided by the plugin
59
-     *
60
-     * @return array
61
-     */
62
-    public function getCustomEvents()
63
-    {
64
-        return [];
65
-    }
57
+	/**
58
+	 * Get custom events provided by the plugin
59
+	 *
60
+	 * @return array
61
+	 */
62
+	public function getCustomEvents()
63
+	{
64
+		return [];
65
+	}
66 66
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -20,11 +20,11 @@  discard block
 block discarded – undo
20 20
      */
21 21
     public function register()
22 22
     {
23
-        $this->manager->registerGlobalEvent('initialized', function (Mapper $mapper) {
23
+        $this->manager->registerGlobalEvent('initialized', function(Mapper $mapper) {
24 24
             $entityMap = $mapper->getEntityMap();
25 25
 
26 26
             if ($entityMap->usesTimestamps()) {
27
-                $mapper->registerEvent('creating', function ($entity) use ($entityMap) {
27
+                $mapper->registerEvent('creating', function($entity) use ($entityMap) {
28 28
 
29 29
                     $factory = new Factory;
30 30
                     $wrappedEntity = $factory->make($entity);
@@ -39,7 +39,7 @@  discard block
 block discarded – undo
39 39
 
40 40
                 });
41 41
 
42
-                $mapper->registerEvent('updating', function ($entity) use ($entityMap) {
42
+                $mapper->registerEvent('updating', function($entity) use ($entityMap) {
43 43
 
44 44
                     $factory = new Factory;
45 45
                     $wrappedEntity = $factory->make($entity);
Please login to merge, or discard this patch.
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/MappableTrait.php 1 patch
Indentation   +55 added lines, -55 removed lines patch added patch discarded remove patch
@@ -8,63 +8,63 @@
 block discarded – undo
8 8
  */
9 9
 trait MappableTrait
10 10
 {
11
-    /**
12
-     * The Entity's Attributes
13
-     * @var array
14
-     */
15
-    protected $attributes = [];
11
+	/**
12
+	 * The Entity's Attributes
13
+	 * @var array
14
+	 */
15
+	protected $attributes = [];
16 16
 
17
-    /**
18
-     * Method used by the mapper to set the object
19
-     * attribute raw values (hydration)
20
-     *
21
-     * @param array $attributes
22
-     *
23
-     * @return void
24
-     */
25
-    public function setEntityAttributes(array $attributes)
26
-    {
27
-        $this->attributes = $attributes;
28
-    }
17
+	/**
18
+	 * Method used by the mapper to set the object
19
+	 * attribute raw values (hydration)
20
+	 *
21
+	 * @param array $attributes
22
+	 *
23
+	 * @return void
24
+	 */
25
+	public function setEntityAttributes(array $attributes)
26
+	{
27
+		$this->attributes = $attributes;
28
+	}
29 29
 
30
-    /**
31
-     * Method used by the mapper to get the
32
-     * raw object's values.
33
-     *
34
-     * @return array
35
-     */
36
-    public function getEntityAttributes()
37
-    {
38
-        return $this->attributes;
39
-    }
30
+	/**
31
+	 * Method used by the mapper to get the
32
+	 * raw object's values.
33
+	 *
34
+	 * @return array
35
+	 */
36
+	public function getEntityAttributes()
37
+	{
38
+		return $this->attributes;
39
+	}
40 40
 
41
-    /**
42
-     * Method used by the mapper to set raw
43
-     * key-value pair
44
-     *
45
-     * @param string $key
46
-     * @param string $value
47
-     *
48
-     * @return void
49
-     */
50
-    public function setEntityAttribute($key, $value)
51
-    {
52
-        $this->attributes[$key] = $value;
53
-    }
41
+	/**
42
+	 * Method used by the mapper to set raw
43
+	 * key-value pair
44
+	 *
45
+	 * @param string $key
46
+	 * @param string $value
47
+	 *
48
+	 * @return void
49
+	 */
50
+	public function setEntityAttribute($key, $value)
51
+	{
52
+		$this->attributes[$key] = $value;
53
+	}
54 54
 
55
-    /**
56
-     * Method used by the mapper to get single
57
-     * key-value pair
58
-     *
59
-     * @param  string $key
60
-     * @return mixed|null
61
-     */
62
-    public function getEntityAttribute($key)
63
-    {
64
-        if (array_key_exists($key, $this->attributes)) {
65
-            return $this->attributes[$key];
66
-        } else {
67
-            return null;
68
-        }
69
-    }
55
+	/**
56
+	 * Method used by the mapper to get single
57
+	 * key-value pair
58
+	 *
59
+	 * @param  string $key
60
+	 * @return mixed|null
61
+	 */
62
+	public function getEntityAttribute($key)
63
+	{
64
+		if (array_key_exists($key, $this->attributes)) {
65
+			return $this->attributes[$key];
66
+		} else {
67
+			return null;
68
+		}
69
+	}
70 70
 }
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/EntityMap.php 3 patches
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -393,7 +393,7 @@  discard block
 block discarded – undo
393 393
         if (!is_null($this->sequence)) {
394 394
             return $this->sequence;
395 395
         } else {
396
-            return $this->getTable() . '_id_seq';
396
+            return $this->getTable().'_id_seq';
397 397
         }
398 398
     }
399 399
 
@@ -563,7 +563,7 @@  discard block
 block discarded – undo
563 563
      */
564 564
     public function getQualifiedKeyName()
565 565
     {
566
-        return $this->getTable() . '.' . $this->getKeyName();
566
+        return $this->getTable().'.'.$this->getKeyName();
567 567
     }
568 568
 
569 569
     /**
@@ -644,7 +644,7 @@  discard block
 block discarded – undo
644 644
      */
645 645
     public function getForeignKey()
646 646
     {
647
-        return snake_case(class_basename($this->getClass())) . '_id';
647
+        return snake_case(class_basename($this->getClass())).'_id';
648 648
     }
649 649
 
650 650
     /**
@@ -667,7 +667,7 @@  discard block
 block discarded – undo
667 667
 
668 668
         $localKey = $localKey ?: $this->getKeyName();
669 669
 
670
-        return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey);
670
+        return new HasOne($relatedMapper, $entity, $relatedMap->getTable().'.'.$foreignKey, $localKey);
671 671
     }
672 672
 
673 673
     /**
@@ -692,7 +692,7 @@  discard block
 block discarded – undo
692 692
 
693 693
         $table = $relatedMapper->getEntityMap()->getTable();
694 694
         
695
-        return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
695
+        return new MorphOne($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey);
696 696
     }
697 697
 
698 698
     /**
@@ -721,7 +721,7 @@  discard block
 block discarded – undo
721 721
         // foreign key name by using the name of the relationship function, which
722 722
         // when combined with an "_id" should conventionally match the columns.
723 723
         if (is_null($foreignKey)) {
724
-            $foreignKey = snake_case($relation) . '_id';
724
+            $foreignKey = snake_case($relation).'_id';
725 725
         }
726 726
 
727 727
         $relatedMapper = $this->manager->mapper($related);
@@ -798,7 +798,7 @@  discard block
 block discarded – undo
798 798
 
799 799
         $relatedMapper = $this->manager->mapper($related);
800 800
 
801
-        $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey;
801
+        $table = $relatedMapper->getEntityMap()->getTable().'.'.$foreignKey;
802 802
 
803 803
         $localKey = $localKey ?: $this->getKeyName();
804 804
 
@@ -856,7 +856,7 @@  discard block
 block discarded – undo
856 856
 
857 857
         $localKey = $localKey ?: $this->getKeyName();
858 858
         
859
-        return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
859
+        return new MorphMany($relatedMapper, $entity, $table.'.'.$type, $table.'.'.$id, $localKey);
860 860
     }
861 861
 
862 862
     /**
@@ -921,7 +921,7 @@  discard block
 block discarded – undo
921 921
         // First, we will need to determine the foreign key and "other key" for the
922 922
         // relationship. Once we have determined the keys we will make the query
923 923
         // instances, as well as the relationship instances we need for these.
924
-        $foreignKey = $foreignKey ?: $name . '_id';
924
+        $foreignKey = $foreignKey ?: $name.'_id';
925 925
 
926 926
         $relatedMapper = $this->manager->mapper($related);
927 927
 
@@ -951,7 +951,7 @@  discard block
 block discarded – undo
951 951
         // For the inverse of the polymorphic many-to-many relations, we will change
952 952
         // the way we determine the foreign and other keys, as it is the opposite
953 953
         // of the morph-to-many method since we're figuring out these inverses.
954
-        $otherKey = $otherKey ?: $name . '_id';
954
+        $otherKey = $otherKey ?: $name.'_id';
955 955
 
956 956
         return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true);
957 957
     }
@@ -965,7 +965,7 @@  discard block
 block discarded – undo
965 965
     {
966 966
         $self = __FUNCTION__;
967 967
 
968
-        $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) {
968
+        $caller = array_first(debug_backtrace(false), function($key, $trace) use ($self) {
969 969
             $caller = $trace['function'];
970 970
 
971 971
             return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self);
@@ -1009,9 +1009,9 @@  discard block
 block discarded – undo
1009 1009
      */
1010 1010
     protected function getMorphs($name, $type, $id)
1011 1011
     {
1012
-        $type = $type ?: $name . '_type';
1012
+        $type = $type ?: $name.'_type';
1013 1013
 
1014
-        $id = $id ?: $name . '_id';
1014
+        $id = $id ?: $name.'_id';
1015 1015
 
1016 1016
         return [$type, $id];
1017 1017
     }
@@ -1176,7 +1176,7 @@  discard block
 block discarded – undo
1176 1176
     public function __call($method, $parameters)
1177 1177
     {
1178 1178
         if (!array_key_exists($method, $this->dynamicRelationships)) {
1179
-            throw new Exception(get_class($this) . " has no method $method");
1179
+            throw new Exception(get_class($this)." has no method $method");
1180 1180
         }
1181 1181
 
1182 1182
         // Add $this to parameters so the closure can call relationship method on the map.
Please login to merge, or discard this patch.
Doc Comments   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -777,7 +777,7 @@  discard block
 block discarded – undo
777 777
     /**
778 778
      * Define a polymorphic one-to-one relationship.
779 779
      *
780
-     * @param  mixed       $entity
780
+     * @param  \AnalogueTest\App\Avatar       $entity
781 781
      * @param  string      $related
782 782
      * @param  string      $name
783 783
      * @param  string|null $type
@@ -838,7 +838,7 @@  discard block
 block discarded – undo
838 838
     /**
839 839
      * Define a polymorphic, inverse one-to-one or many relationship.
840 840
      *
841
-     * @param  mixed       $entity
841
+     * @param  \AnalogueTest\App\Image       $entity
842 842
      * @param  string|null $name
843 843
      * @param  string|null $type
844 844
      * @param  string|null $id
@@ -940,7 +940,7 @@  discard block
 block discarded – undo
940 940
     /**
941 941
      * Define a polymorphic one-to-many relationship.
942 942
      *
943
-     * @param  mixed       $entity
943
+     * @param  \AnalogueTest\App\Resource       $entity
944 944
      * @param  string      $related
945 945
      * @param  string      $name
946 946
      * @param  string|null $type
@@ -1040,7 +1040,7 @@  discard block
 block discarded – undo
1040 1040
     /**
1041 1041
      * Define a polymorphic, inverse many-to-many relationship.
1042 1042
      *
1043
-     * @param  mixed       $entity
1043
+     * @param  \AnalogueTest\App\Resource       $entity
1044 1044
      * @param  string      $related
1045 1045
      * @param  string      $name
1046 1046
      * @param  string|null $table
Please login to merge, or discard this patch.
Indentation   +1268 added lines, -1268 removed lines patch added patch discarded remove patch
@@ -23,1272 +23,1272 @@
 block discarded – undo
23 23
  */
24 24
 class EntityMap
25 25
 {
26
-    /**
27
-     * The mapping driver to use with this entity
28
-     *
29
-     * @var  string
30
-     */
31
-    protected $driver = 'illuminate';
32
-
33
-    /**
34
-     * The Database Connection name for the model.
35
-     *
36
-     * @var string
37
-     */
38
-    protected $connection;
39
-
40
-    /**
41
-     * The table associated with the entity.
42
-     *
43
-     * @var string|null
44
-     */
45
-    protected $table = null;
46
-
47
-    /**
48
-     * The primary key for the model.
49
-     *
50
-     * @var string
51
-     */
52
-    protected $primaryKey = 'id';
53
-
54
-    /**
55
-     * Name of the entity's property that should
56
-     * contain the attributes, when $mapToProperties is false
57
-     * 
58
-     * @var string
59
-     */
60
-    protected $arrayName = 'attributes';
61
-
62
-    /**
63
-     * Array containing a list of class attributes. Mandatory if the
64
-     * mapped entity is a Plain PHP Object.
65
-     *
66
-     * @var array
67
-     */
68
-    protected $attributes = [];
69
-
70
-    /**
71
-     * Indicate if the entity's attributes should be mapped to the object's
72
-     * properties. If set to false, attributes will be assigned to an array
73
-     * defined by the $arrayName property of the EntityMap
74
-     * 
75
-     * @var boolean
76
-     */
77
-    protected $mapToProperties = false;
78
-
79
-    /**
80
-     * Attributes that should be casted to a specific type, like a Carbon date
81
-     * JSON, or a custom Value Object
82
-     * 
83
-     * @var array
84
-     */
85
-    protected $casts = [];
86
-
87
-    /**
88
-     * The Custom Domain Class to use with this mapping
89
-     *
90
-     * @var string|null
91
-     */
92
-    protected $class = null;
93
-
94
-    /**
95
-     * Attributes that should be treated as Value Objects
96
-     *
97
-     * @deprecated 5.3 use $casts property instead
98
-     * 
99
-     * @var array
100
-     */
101
-    protected $embeddables = [];
102
-
103
-    /**
104
-     * Determine the relationships method used on the entity.
105
-     * If not set, mapper will autodetect them
106
-     *
107
-     * @var array
108
-     */
109
-    private $relationships = [];
110
-
111
-    /**
112
-     * Relationships that should be treated as collection.
113
-     *
114
-     * @var array
115
-     */
116
-    private $manyRelations = [];
117
-
118
-    /**
119
-     * Relationships that should be treated as single entity.
120
-     *
121
-     * @var array
122
-     */
123
-    private $singleRelations = [];
124
-
125
-    /**
126
-     * Relationships for which the key is stored in the Entity itself
127
-     *
128
-     * @var array
129
-     */
130
-    private $localRelations = [];
131
-
132
-    /**
133
-     * Relationships for which the key is stored in the Related Entity
134
-     *
135
-     * @var array
136
-     */
137
-    private $foreignRelations = [];
138
-
139
-    /**
140
-     * Relationships which use a pivot record.
141
-     *
142
-     * @var array
143
-     */
144
-    private $pivotRelations = [];
145
-
146
-    /**
147
-     * Dynamic relationships
148
-     *
149
-     * @var array
150
-     */
151
-    private $dynamicRelationships = [];
152
-
153
-    /**
154
-     * The number of models to return for pagination.
155
-     *
156
-     * @var int
157
-     */
158
-    protected $perPage = 15;
159
-
160
-    /**
161
-     * The relations to eager load on every query.
162
-     *
163
-     * @var array
164
-     */
165
-    protected $with = [];
166
-
167
-    /**
168
-     * The class name to be used in polymorphic relations.
169
-     *
170
-     * @var string
171
-     */
172
-    protected $morphClass;
173
-
174
-    /**
175
-     * Sequence name, to be used with postgreSql
176
-     * defaults to %table_name%_id_seq
177
-     *
178
-     * @var string|null
179
-     */
180
-    protected $sequence = null;
181
-
182
-    /**
183
-     * Indicates if the entity should be timestamped.
184
-     *
185
-     * @var bool
186
-     */
187
-    public $timestamps = false;
188
-
189
-    /**
190
-     * The name of the "created at" column.
191
-     *
192
-     * @var string
193
-     */
194
-    protected $createdAtColumn = 'created_at';
195
-
196
-    /**
197
-     * The name of the "updated at" column.
198
-     *
199
-     * @var string
200
-     */
201
-    protected $updatedAtColumn = 'updated_at';
202
-
203
-    /**
204
-     * Indicates if the entity uses softdeletes
205
-     *
206
-     * @var boolean
207
-     */
208
-    public $softDeletes = false;
209
-
210
-    /**
211
-     * The name of the "deleted at" column.
212
-     *
213
-     * @var string
214
-     */
215
-    protected $deletedAtColumn = 'deleted_at';
216
-
217
-    /**
218
-     * The many to many relationship methods.
219
-     *
220
-     * @var array
221
-     */
222
-    protected static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany'];
223
-
224
-    /**
225
-     * The 'Many' relationships classes, which related Entity attribute should be
226
-     * an array/entityCollection
227
-     *
228
-     * @var array
229
-     */
230
-    protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphMany', 'MorphToMany'];
231
-
232
-    /**
233
-     * The 'Single' relationships classes, which related Entity attribute should be
234
-     * another Entity.
235
-     *
236
-     * @var array
237
-     */
238
-    protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo'];
239
-
240
-    /**
241
-     * Relationships with a pivot record
242
-     *
243
-     * @var array
244
-     */
245
-    protected static $pivotClasses = ['BelongsToMany', 'MorphToMany'];
246
-
247
-    /**
248
-     * Relationships on which key is stored in the Entity itself
249
-     *
250
-     * @var array
251
-     */
252
-    protected static $localClasses = ['BelongsTo', 'MorphTo'];
253
-
254
-    /**
255
-     * Relationships on which key is stored in the related Entity record or in a pivot record
256
-     *
257
-     * @var array
258
-     */
259
-    protected static $foreignClasses = [
260
-        'BelongsToMany',
261
-        'HasMany',
262
-        'HasManyThrough',
263
-        'MorphMany',
264
-        'MorphToMany',
265
-        'HasOne',
266
-        'MorphOne',
267
-    ];
268
-
269
-    /**
270
-     * The date format to use with the current database connection
271
-     *
272
-     * @var string
273
-     */
274
-    protected $dateFormat;
275
-
276
-    /**
277
-     * The Analogue's manager instance.
278
-     *
279
-     * @var \Analogue\ORM\System\Manager
280
-     */
281
-    private $manager;
282
-
283
-    /**
284
-     * Set this property to true if the entity should be instantiated
285
-     * using the IoC Container
286
-     * 
287
-     * @var boolean
288
-     */
289
-    protected $dependencyInjection = false;
290
-
291
-    /**
292
-     * Set the usage of inheritance, possible values are :
293
-     * "single_table"
294
-     * null
295
-     * 
296
-     * @var string | null
297
-     */
298
-    protected $inheritanceType = null;
299
-
300
-    /**
301
-     * Discriminator column name
302
-     * 
303
-     * @var string
304
-     */
305
-    protected $discriminatorColumn = "type";
306
-
307
-    /**
308
-     * Allow using a string to define which entity type should be instantiated.
309
-     * If not set, analogue will uses entity's FQDN
310
-     * 
311
-     * @var array
312
-     */
313
-    protected $discriminatorColumnMap = [];
314
-
315
-    /**
316
-     * Set the Manager that will be used for relationship's mapper instantiations.
317
-     *
318
-     * @param Manager $manager
319
-     */
320
-    public function setManager(Manager $manager)
321
-    {
322
-        $this->manager = $manager;
323
-    }
324
-
325
-    /**
326
-     * Return Domain class attributes, useful when mapping to a Plain PHP Object
327
-     *
328
-     * @return array
329
-     */
330
-    public function getAttributes()
331
-    {
332
-        return $this->attributes;
333
-    }
334
-
335
-    /**
336
-     * Set the domain class attributes
337
-     *
338
-     * @param array $attributeNames
339
-     */
340
-    public function setAttributes(array $attributeNames)
341
-    {
342
-        $this->attributes = $attributeNames;
343
-    }
344
-
345
-    /**
346
-     * Get all the attribute names for the class, including relationships, embeddables and primary key.
347
-     *
348
-     * @return array
349
-     */
350
-    public function getCompiledAttributes()
351
-    {
352
-        $key = $this->getKeyName();
353
-
354
-        $embeddables = array_keys($this->getEmbeddables());
355
-
356
-        $relationships = $this->getRelationships();
357
-
358
-        $attributes = $this->getAttributes();
359
-
360
-        return array_merge([$key], $embeddables, $relationships, $attributes);
361
-    }
362
-
363
-    /**
364
-     * Set the date format to use with the current database connection
365
-     *
366
-     * @param string $format
367
-     */
368
-    public function setDateFormat($format)
369
-    {
370
-        $this->dateFormat = $format;
371
-    }
372
-
373
-    /**
374
-     * Get the date format to use with the current database connection
375
-     *
376
-     *  @return string
377
-     */
378
-    public function getDateFormat()
379
-    {
380
-        return $this->dateFormat;
381
-    }
382
-
383
-    /**
384
-     * Set the Driver for this mapping
385
-     *
386
-     * @param string $driver
387
-     */
388
-    public function setDriver($driver)
389
-    {
390
-        $this->driver = $driver;
391
-    }
392
-
393
-    /**
394
-     * Get the Driver for this mapping.
395
-     *
396
-     * @return string
397
-     */
398
-    public function getDriver()
399
-    {
400
-        return $this->driver;
401
-    }
402
-
403
-    /**
404
-     * Set the db connection to use on the table
405
-     *
406
-     * @param $connection
407
-     */
408
-    public function setConnection($connection)
409
-    {
410
-        $this->connection = $connection;
411
-    }
412
-
413
-    /**
414
-     * Get the Database connection the Entity is stored on.
415
-     *
416
-     * @return string
417
-     */
418
-    public function getConnection()
419
-    {
420
-        return $this->connection;
421
-    }
422
-
423
-    /**
424
-     * Get the table associated with the entity.
425
-     *
426
-     * @return string
427
-     */
428
-    public function getTable()
429
-    {
430
-        if (!is_null($this->table)) {
431
-            return $this->table;
432
-        }
433
-
434
-        return str_replace('\\', '', snake_case(str_plural(class_basename($this->getClass()))));
435
-    }
436
-
437
-    /**
438
-     * Set the database table name
439
-     *
440
-     * @param  string $table
441
-     */
442
-    public function setTable($table)
443
-    {
444
-        $this->table = $table;
445
-    }
446
-
447
-    /**
448
-     * Get the pgSql sequence name
449
-     *
450
-     * @return string
451
-     */
452
-    public function getSequence()
453
-    {
454
-        if (!is_null($this->sequence)) {
455
-            return $this->sequence;
456
-        } else {
457
-            return $this->getTable() . '_id_seq';
458
-        }
459
-    }
460
-
461
-    /**
462
-     * Get the custom entity class
463
-     *
464
-     * @return string namespaced class name
465
-     */
466
-    public function getClass()
467
-    {
468
-        return isset($this->class) ? $this->class : null;
469
-    }
470
-
471
-    /**
472
-     * Set the custom entity class
473
-     *
474
-     * @param string $class namespaced class name
475
-     */
476
-    public function setClass($class)
477
-    {
478
-        // Throw exception if class not exists
479
-
480
-        $this->class = $class;
481
-    }
482
-
483
-    /**
484
-     * Get the embedded Value Objects
485
-     *
486
-     * @return array
487
-     */
488
-    public function getEmbeddables()
489
-    {
490
-        return $this->embeddables;
491
-    }
492
-
493
-    /**
494
-     * Set the embedded Value Objects
495
-     *
496
-     * @param array $embeddables
497
-     */
498
-    public function setEmbeddables(array $embeddables)
499
-    {
500
-        $this->embeddables = $embeddables;
501
-    }
502
-
503
-    /**
504
-     * Get the relationships to map on a custom domain
505
-     * class.
506
-     *
507
-     * @return array
508
-     */
509
-    public function getRelationships()
510
-    {
511
-        return $this->relationships;
512
-    }
513
-
514
-    /**
515
-     * Relationships of the Entity type
516
-     *
517
-     * @return array
518
-     */
519
-    public function getSingleRelationships()
520
-    {
521
-        return $this->singleRelations;
522
-    }
523
-
524
-    /**
525
-     * Relationships of type Collection
526
-     *
527
-     * @return array
528
-     */
529
-    public function getManyRelationships()
530
-    {
531
-        return $this->manyRelations;
532
-    }
533
-
534
-    /**
535
-     * Relationships with foreign key in the mapped entity record.
536
-     *
537
-     * @return array
538
-     */
539
-    public function getLocalRelationships()
540
-    {
541
-        return $this->localRelations;
542
-    }
543
-
544
-    /**
545
-     * Relationships with foreign key in the related Entity record
546
-     *
547
-     * @return array
548
-     */
549
-    public function getForeignRelationships()
550
-    {
551
-        return $this->foreignRelations;
552
-    }
553
-
554
-    /**
555
-     * Relationships which keys are stored in a pivot record
556
-     *
557
-     * @return array
558
-     */
559
-    public function getPivotRelationships()
560
-    {
561
-        return $this->pivotRelations;
562
-    }
563
-
564
-    /**
565
-     * Add a Dynamic Relationship method at runtime. This has to be done
566
-     * by hooking the 'initializing' event, before entityMap is initialized.
567
-     *
568
-     * @param string  $name         Relation name
569
-     * @param \Closure $relationship
570
-     *
571
-     * @return void
572
-     */
573
-    public function addRelationshipMethod($name, \Closure $relationship)
574
-    {
575
-        $this->dynamicRelationships[$name] = $relationship;
576
-    }
577
-
578
-    /**
579
-     * Get the dynamic relationship method names.
580
-     *
581
-     * @return array
582
-     */
583
-    public function getDynamicRelationships()
584
-    {
585
-        return array_keys($this->dynamicRelationships);
586
-    }
587
-
588
-    /**
589
-     * Get the relationships that have to be eager loaded
590
-     * on each request.
591
-     *
592
-     * @return array
593
-     */
594
-    public function getEagerloadedRelationships()
595
-    {
596
-        return $this->with;
597
-    }
598
-
599
-    /**
600
-     * Get the primary key for the entity.
601
-     *
602
-     * @return string
603
-     */
604
-    public function getKeyName()
605
-    {
606
-        return $this->primaryKey;
607
-    }
608
-
609
-    /**
610
-     * Set the primary key for the entity.
611
-     *
612
-     * @param $key
613
-     * @return void
614
-     */
615
-    public function setKeyName($key)
616
-    {
617
-        $this->primaryKey = $key;
618
-    }
619
-
620
-    /**
621
-     * Get the table qualified key name.
622
-     *
623
-     * @return string
624
-     */
625
-    public function getQualifiedKeyName()
626
-    {
627
-        return $this->getTable() . '.' . $this->getKeyName();
628
-    }
629
-
630
-    /**
631
-     * Get the number of models to return per page.
632
-     *
633
-     * @return int
634
-     */
635
-    public function getPerPage()
636
-    {
637
-        return $this->perPage;
638
-    }
639
-
640
-    /**
641
-     * Set the number of models to return per page.
642
-     *
643
-     * @param  int $perPage
644
-     * @return void
645
-     */
646
-    public function setPerPage($perPage)
647
-    {
648
-        $this->perPage = $perPage;
649
-    }
650
-
651
-    /**
652
-     * Determine if the entity uses get.
653
-     *
654
-     * @return bool
655
-     */
656
-    public function usesTimestamps()
657
-    {
658
-        return $this->timestamps;
659
-    }
660
-
661
-    /**
662
-     * Determine if the entity uses soft deletes
663
-     *
664
-     * @return bool
665
-     */
666
-    public function usesSoftDeletes()
667
-    {
668
-        return $this->softDeletes;
669
-    }
670
-
671
-    /**
672
-     * Get the 'created_at' column name
673
-     *
674
-     * @return string
675
-     */
676
-    public function getCreatedAtColumn()
677
-    {
678
-        return $this->createdAtColumn;
679
-    }
680
-
681
-    /**
682
-     * Get the 'updated_at' column name
683
-     *
684
-     * @return string
685
-     */
686
-    public function getUpdatedAtColumn()
687
-    {
688
-        return $this->updatedAtColumn;
689
-    }
690
-
691
-    /**
692
-     * Get the deleted_at column
693
-     *
694
-     * @return string
695
-     */
696
-    public function getQualifiedDeletedAtColumn()
697
-    {
698
-        return $this->deletedAtColumn;
699
-    }
700
-
701
-    /**
702
-     * Get the default foreign key name for the model.
703
-     *
704
-     * @return string
705
-     */
706
-    public function getForeignKey()
707
-    {
708
-        return snake_case(class_basename($this->getClass())) . '_id';
709
-    }
710
-
711
-    /**
712
-     * Return the inheritance type used by the entity.
713
-     *
714
-     * @return string|null
715
-     */
716
-    public function getInheritanceType()
717
-    {
718
-        return $this->inheritanceType;
719
-    }
720
-
721
-    /**
722
-     * Return the discriminator column name on the entity that's
723
-     * used for table inheritance.
724
-     *
725
-     * @return string
726
-     */
727
-    public function getDiscriminatorColumn()
728
-    {
729
-        return $this->discriminatorColumn;
730
-    }
731
-
732
-    /**
733
-     * Return the mapping of discriminator column values to
734
-     * entity class names that are used for table inheritance.
735
-     *
736
-     * @return array
737
-     */
738
-    public function getDiscriminatorColumnMap()
739
-    {
740
-        return $this->discriminatorColumnMap;
741
-    }
742
-
743
-    /**
744
-     * Return true if the entity should be instanciated using
745
-     * the IoC Container
746
-     * 
747
-     * @return boolean
748
-     */
749
-    public function useDependencyInjection()
750
-    {
751
-        return $this->dependencyInjection;
752
-    }
753
-
754
-    /**
755
-     * Define a one-to-one relationship.
756
-     *
757
-     * @param         $entity
758
-     * @param  string $relatedClass entity class
759
-     * @param  string $foreignKey
760
-     * @param  string $localKey
761
-     * @throws MappingException
762
-     * @return \Analogue\ORM\Relationships\HasOne
763
-     */
764
-    public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = null)
765
-    {
766
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
767
-
768
-        $relatedMapper = $this->manager->mapper($relatedClass);
769
-
770
-        $relatedMap = $relatedMapper->getEntityMap();
771
-
772
-        $localKey = $localKey ?: $this->getKeyName();
773
-
774
-        return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey);
775
-    }
776
-
777
-    /**
778
-     * Define a polymorphic one-to-one relationship.
779
-     *
780
-     * @param  mixed       $entity
781
-     * @param  string      $related
782
-     * @param  string      $name
783
-     * @param  string|null $type
784
-     * @param  string|null $id
785
-     * @param  string|null $localKey
786
-     * @throws MappingException
787
-     * @return \Analogue\ORM\Relationships\MorphOne
788
-     */
789
-    public function morphOne($entity, $related, $name, $type = null, $id = null, $localKey = null)
790
-    {
791
-        list($type, $id) = $this->getMorphs($name, $type, $id);
792
-
793
-        $localKey = $localKey ?: $this->getKeyName();
794
-
795
-        $relatedMapper = $this->manager->mapper($related);
796
-
797
-        $table = $relatedMapper->getEntityMap()->getTable();
798
-
799
-        return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
800
-    }
801
-
802
-    /**
803
-     * Define an inverse one-to-one or many relationship.
804
-     *
805
-     * @param  mixed       $entity
806
-     * @param  string      $related
807
-     * @param  string|null $foreignKey
808
-     * @param  string|null $otherKey
809
-     * @param  string|null $relation
810
-     * @throws MappingException
811
-     * @return \Analogue\ORM\Relationships\BelongsTo
812
-     */
813
-    public function belongsTo($entity, $related, $foreignKey = null, $otherKey = null, $relation = null)
814
-    {
815
-        // If no relation name was given, we will use this debug backtrace to extract
816
-        // the calling method's name and use that as the relationship name as most
817
-        // of the time this will be what we desire to use for the relationships.
818
-        if (is_null($relation)) {
819
-            list(, $caller) = debug_backtrace(false);
820
-
821
-            $relation = $caller['function'];
822
-        }
823
-
824
-        // If no foreign key was supplied, we can use a backtrace to guess the proper
825
-        // foreign key name by using the name of the relationship function, which
826
-        // when combined with an "_id" should conventionally match the columns.
827
-        if (is_null($foreignKey)) {
828
-            $foreignKey = snake_case($relation) . '_id';
829
-        }
830
-
831
-        $relatedMapper = $this->manager->mapper($related);
832
-
833
-        $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName();
834
-
835
-        return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation);
836
-    }
837
-
838
-    /**
839
-     * Define a polymorphic, inverse one-to-one or many relationship.
840
-     *
841
-     * @param  mixed       $entity
842
-     * @param  string|null $name
843
-     * @param  string|null $type
844
-     * @param  string|null $id
845
-     * @throws MappingException
846
-     * @return \Analogue\ORM\Relationships\MorphTo
847
-     */
848
-    public function morphTo($entity, $name = null, $type = null, $id = null)
849
-    {
850
-        // If no name is provided, we will use the backtrace to get the function name
851
-        // since that is most likely the name of the polymorphic interface. We can
852
-        // use that to get both the class and foreign key that will be utilized.
853
-        if (is_null($name)) {
854
-            list(, $caller) = debug_backtrace(false);
855
-
856
-            $name = snake_case($caller['function']);
857
-        }
858
-
859
-        list($type, $id) = $this->getMorphs($name, $type, $id);
860
-
861
-        $mapper = $this->manager->mapper(get_class($entity));
862
-
863
-        // If the type value is null it is probably safe to assume we're eager loading
864
-        // the relationship. When that is the case we will pass in a dummy query as
865
-        // there are multiple types in the morph and we can't use single queries.
866
-        $factory = new Factory;
867
-        $wrapper = $factory->make($entity);
868
-
869
-        if (is_null($class = $wrapper->getEntityAttribute($type))) {
870
-            return new MorphTo(
871
-                $mapper, $entity, $id, null, $type, $name
872
-            );
873
-        }
874
-
875
-        // If we are not eager loading the relationship we will essentially treat this
876
-        // as a belongs-to style relationship since morph-to extends that class and
877
-        // we will pass in the appropriate values so that it behaves as expected.
878
-        else {
879
-            $class = $this->manager->getInverseMorphMap($class);
880
-            $relatedMapper = $this->manager->mapper($class);
881
-
882
-            $foreignKey = $relatedMapper->getEntityMap()->getKeyName();
883
-
884
-            return new MorphTo(
885
-                $relatedMapper, $entity, $id, $foreignKey, $type, $name
886
-            );
887
-        }
888
-    }
889
-
890
-    /**
891
-     * Define a one-to-many relationship.
892
-     *
893
-     * @param  mixed       $entity
894
-     * @param  string      $related
895
-     * @param  string|null $foreignKey
896
-     * @param  string|null $localKey
897
-     * @throws MappingException
898
-     * @return \Analogue\ORM\Relationships\HasMany
899
-     */
900
-    public function hasMany($entity, $related, $foreignKey = null, $localKey = null)
901
-    {
902
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
903
-
904
-        $relatedMapper = $this->manager->mapper($related);
905
-
906
-        $table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey;
907
-
908
-        $localKey = $localKey ?: $this->getKeyName();
909
-
910
-        return new HasMany($relatedMapper, $entity, $table, $localKey);
911
-    }
912
-
913
-    /**
914
-     * Define a has-many-through relationship.
915
-     *
916
-     * @param  mixed       $entity
917
-     * @param  string      $related
918
-     * @param  string      $through
919
-     * @param  string|null $firstKey
920
-     * @param  string|null $secondKey
921
-     * @throws MappingException
922
-     * @return \Analogue\ORM\Relationships\HasManyThrough
923
-     */
924
-    public function hasManyThrough($entity, $related, $through, $firstKey = null, $secondKey = null)
925
-    {
926
-        $relatedMapper = $this->manager->mapper($related);
927
-
928
-        $throughMapper = $this->manager->mapper($through);
929
-
930
-
931
-        $firstKey = $firstKey ?: $this->getForeignKey();
932
-
933
-        $throughMap = $throughMapper->getEntityMap();
934
-
935
-        $secondKey = $secondKey ?: $throughMap->getForeignKey();
936
-
937
-        return new HasManyThrough($relatedMapper, $entity, $throughMap, $firstKey, $secondKey);
938
-    }
939
-
940
-    /**
941
-     * Define a polymorphic one-to-many relationship.
942
-     *
943
-     * @param  mixed       $entity
944
-     * @param  string      $related
945
-     * @param  string      $name
946
-     * @param  string|null $type
947
-     * @param  string|null $id
948
-     * @param  string|null $localKey
949
-     * @return \Analogue\ORM\Relationships\MorphMany
950
-     */
951
-    public function morphMany($entity, $related, $name, $type = null, $id = null, $localKey = null)
952
-    {
953
-        // Here we will gather up the morph type and ID for the relationship so that we
954
-        // can properly query the intermediate table of a relation. Finally, we will
955
-        // get the table and create the relationship instances for the developers.
956
-        list($type, $id) = $this->getMorphs($name, $type, $id);
957
-
958
-        $relatedMapper = $this->manager->mapper($related);
959
-
960
-        $table = $relatedMapper->getEntityMap()->getTable();
961
-
962
-        $localKey = $localKey ?: $this->getKeyName();
963
-
964
-        return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
965
-    }
966
-
967
-    /**
968
-     * Define a many-to-many relationship.
969
-     *
970
-     * @param  mixed       $entity
971
-     * @param  string      $related
972
-     * @param  string|null $table
973
-     * @param  string|null $foreignKey
974
-     * @param  string|null $otherKey
975
-     * @param  string|null $relation
976
-     * @throws MappingException
977
-     * @return \Analogue\ORM\Relationships\BelongsToMany
978
-     */
979
-    public function belongsToMany($entity, $related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
980
-    {
981
-        // If no relationship name was passed, we will pull backtraces to get the
982
-        // name of the calling function. We will use that function name as the
983
-        // title of this relation since that is a great convention to apply.
984
-        if (is_null($relation)) {
985
-            $relation = $this->getBelongsToManyCaller();
986
-        }
987
-
988
-        // First, we'll need to determine the foreign key and "other key" for the
989
-        // relationship. Once we have determined the keys we'll make the query
990
-        // instances as well as the relationship instances we need for this.
991
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
992
-
993
-        $relatedMapper = $this->manager->mapper($related);
994
-
995
-        $relatedMap = $relatedMapper->getEntityMap();
996
-
997
-        $otherKey = $otherKey ?: $relatedMap->getForeignKey();
998
-
999
-        // If no table name was provided, we can guess it by concatenating the two
1000
-        // models using underscores in alphabetical order. The two model names
1001
-        // are transformed to snake case from their default CamelCase also.
1002
-        if (is_null($table)) {
1003
-            $table = $this->joiningTable($relatedMap);
1004
-        }
1005
-
1006
-        return new BelongsToMany($relatedMapper, $entity, $table, $foreignKey, $otherKey, $relation);
1007
-    }
1008
-
1009
-    /**
1010
-     * Define a polymorphic many-to-many relationship.
1011
-     *
1012
-     * @param  mixed       $entity
1013
-     * @param  string      $related
1014
-     * @param  string      $name
1015
-     * @param  string|null $table
1016
-     * @param  string|null $foreignKey
1017
-     * @param  string|null $otherKey
1018
-     * @param  bool        $inverse
1019
-     * @throws MappingException
1020
-     * @return \Analogue\ORM\Relationships\MorphToMany
1021
-     */
1022
-    public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false)
1023
-    {
1024
-        $caller = $this->getBelongsToManyCaller();
1025
-
1026
-        // First, we will need to determine the foreign key and "other key" for the
1027
-        // relationship. Once we have determined the keys we will make the query
1028
-        // instances, as well as the relationship instances we need for these.
1029
-        $foreignKey = $foreignKey ?: $name . '_id';
1030
-
1031
-        $relatedMapper = $this->manager->mapper($related);
1032
-
1033
-        $otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getForeignKey();
1034
-
1035
-        $table = $table ?: str_plural($name);
1036
-
1037
-        return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse);
1038
-    }
1039
-
1040
-    /**
1041
-     * Define a polymorphic, inverse many-to-many relationship.
1042
-     *
1043
-     * @param  mixed       $entity
1044
-     * @param  string      $related
1045
-     * @param  string      $name
1046
-     * @param  string|null $table
1047
-     * @param  string|null $foreignKey
1048
-     * @param  string|null $otherKey
1049
-     * @throws MappingException
1050
-     * @return \Analogue\ORM\Relationships\MorphToMany
1051
-     */
1052
-    public function morphedByMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null)
1053
-    {
1054
-        $foreignKey = $foreignKey ?: $this->getForeignKey();
1055
-
1056
-        // For the inverse of the polymorphic many-to-many relations, we will change
1057
-        // the way we determine the foreign and other keys, as it is the opposite
1058
-        // of the morph-to-many method since we're figuring out these inverses.
1059
-        $otherKey = $otherKey ?: $name . '_id';
1060
-
1061
-        return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true);
1062
-    }
1063
-
1064
-    /**
1065
-     * Get the relationship name of the belongs to many.
1066
-     *
1067
-     * @return string
1068
-     */
1069
-    protected function getBelongsToManyCaller()
1070
-    {
1071
-        $self = __FUNCTION__;
1072
-
1073
-        $caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) {
1074
-            $caller = $trace['function'];
1075
-
1076
-            return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self);
1077
-        });
1078
-
1079
-        return !is_null($caller) ? $caller['function'] : null;
1080
-    }
1081
-
1082
-    /**
1083
-     * Get the joining table name for a many-to-many relation.
1084
-     *
1085
-     * @param  EntityMap $relatedMap
1086
-     * @return string
1087
-     */
1088
-    public function joiningTable($relatedMap)
1089
-    {
1090
-        // The joining table name, by convention, is simply the snake cased models
1091
-        // sorted alphabetically and concatenated with an underscore, so we can
1092
-        // just sort the models and join them together to get the table name.
1093
-        $base = $this->getTable();
1094
-
1095
-        $related = $relatedMap->getTable();
1096
-
1097
-        $tables = [$related, $base];
1098
-
1099
-        // Now that we have the model names in an array we can just sort them and
1100
-        // use the implode function to join them together with an underscores,
1101
-        // which is typically used by convention within the database system.
1102
-        sort($tables);
1103
-
1104
-        return strtolower(implode('_', $tables));
1105
-    }
1106
-
1107
-    /**
1108
-     * Get the polymorphic relationship columns.
1109
-     *
1110
-     * @param  string $name
1111
-     * @param  string $type
1112
-     * @param  string $id
1113
-     * @return string[]
1114
-     */
1115
-    protected function getMorphs($name, $type, $id)
1116
-    {
1117
-        $type = $type ?: $name . '_type';
1118
-
1119
-        $id = $id ?: $name . '_id';
1120
-
1121
-        return [$type, $id];
1122
-    }
1123
-
1124
-    /**
1125
-     * Get the class name for polymorphic relations.
1126
-     *
1127
-     * @return string
1128
-     */
1129
-    public function getMorphClass()
1130
-    {
1131
-        $morphClass = $this->manager->getMorphMap($this->getClass());
1132
-        return $this->morphClass ?: $morphClass;
1133
-    }
1134
-
1135
-    /**
1136
-     * Create a new Entity Collection instance.
1137
-     *
1138
-     * @param  array $entities
1139
-     * @return \Analogue\ORM\EntityCollection
1140
-     */
1141
-    public function newCollection(array $entities = [])
1142
-    {
1143
-        $collection = new EntityCollection($entities, $this);
1144
-        return $collection->keyBy($this->getKeyName());
1145
-    }
1146
-
1147
-    /**
1148
-     * Process EntityMap parsing at initialization time
1149
-     *
1150
-     * @return void
1151
-     */
1152
-    public function initialize()
1153
-    {
1154
-        $userMethods = $this->getCustomMethods();
1155
-
1156
-        // Parse EntityMap for method based relationship
1157
-        if (count($userMethods) > 0) {
1158
-            $this->relationships = $this->parseMethodsForRelationship($userMethods);
1159
-        }
1160
-
1161
-        // Parse EntityMap for dynamic relationships
1162
-        if (count($this->dynamicRelationships) > 0) {
1163
-            $this->relationships = $this->relationships + $this->getDynamicRelationships();
1164
-        }
1165
-    }
1166
-
1167
-    /**
1168
-     * Parse every relationships on the EntityMap and sort
1169
-     * them by type.
1170
-     *
1171
-     * @return void
1172
-     */
1173
-    public function boot()
1174
-    {
1175
-        if (count($this->relationships > 0)) {
1176
-            $this->sortRelationshipsByType();
1177
-        }
1178
-    }
1179
-
1180
-    /**
1181
-     * Get Methods that has been added in the child class.
1182
-     *
1183
-     * @return array
1184
-     */
1185
-    protected function getCustomMethods()
1186
-    {
1187
-        $mapMethods = get_class_methods($this);
1188
-
1189
-        $parentsMethods = get_class_methods('Analogue\ORM\EntityMap');
1190
-
1191
-        return array_diff($mapMethods, $parentsMethods);
1192
-    }
1193
-
1194
-    /**
1195
-     * Parse user's class methods for relationships
1196
-     *
1197
-     * @param  array $customMethods
1198
-     * @return array
1199
-     */
1200
-    protected function parseMethodsForRelationship(array $customMethods)
1201
-    {
1202
-        $relationships = [];
1203
-
1204
-        $class = new ReflectionClass(get_class($this));
1205
-
1206
-        // Get the mapped Entity class, as we will detect relationships
1207
-        // methods by testing that the first argument is type-hinted to
1208
-        // the same class as the mapped Entity.
1209
-        $entityClass = $this->getClass();
1210
-
1211
-        foreach ($customMethods as $methodName) {
1212
-            $method = $class->getMethod($methodName);
1213
-
1214
-            if ($method->getNumberOfParameters() > 0) {
1215
-                $params = $method->getParameters();
1216
-
1217
-                if ($params[0]->getClass() && ($params[0]->getClass()->name == $entityClass || is_subclass_of($entityClass, $params[0]->getClass()->name))) {
1218
-                    $relationships[] = $methodName;
1219
-                }
1220
-            }
1221
-        }
1222
-
1223
-        return $relationships;
1224
-    }
1225
-
1226
-    /**
1227
-     * Sort Relationships methods by type
1228
-     *
1229
-     * @return void
1230
-     */
1231
-    protected function sortRelationshipsByType()
1232
-    {
1233
-        $entityClass = $this->getClass();
1234
-
1235
-        // Instantiate a dummy entity which we will pass to relationship methods.
1236
-        $entity = unserialize(sprintf('O:%d:"%s":0:{}', strlen($entityClass), $entityClass));
1237
-
1238
-        foreach ($this->relationships as $relation) {
1239
-            $relationObject = $this->$relation($entity);
1240
-
1241
-            $class = class_basename(get_class($relationObject));
1242
-
1243
-            if (in_array($class, static::$singleClasses)) {
1244
-                $this->singleRelations[] = $relation;
1245
-            }
1246
-
1247
-            if (in_array($class, static::$manyClasses)) {
1248
-                $this->manyRelations[] = $relation;
1249
-            }
1250
-
1251
-            if (in_array($class, static::$localClasses)) {
1252
-                $this->localRelations[] = $relation;
1253
-            }
1254
-
1255
-            if (in_array($class, static::$foreignClasses)) {
1256
-                $this->foreignRelations[] = $relation;
1257
-            }
1258
-
1259
-            if (in_array($class, static::$pivotClasses)) {
1260
-                $this->pivotRelations[] = $relation;
1261
-            }
1262
-        }
1263
-    }
1264
-
1265
-    /**
1266
-     * Override this method for custom entity instantiation
1267
-     *
1268
-     * @return null
1269
-     */
1270
-    public function activator()
1271
-    {
1272
-        return null;
1273
-    }
1274
-
1275
-    /**
1276
-     * Call dynamic relationship, if it exists
1277
-     *
1278
-     * @param  string $method
1279
-     * @param  array  $parameters
1280
-     * @throws Exception
1281
-     * @return mixed
1282
-     */
1283
-    public function __call($method, $parameters)
1284
-    {
1285
-        if (!array_key_exists($method, $this->dynamicRelationships)) {
1286
-            throw new Exception(get_class($this) . " has no method $method");
1287
-        }
1288
-
1289
-        // Add $this to parameters so the closure can call relationship method on the map.
1290
-        $parameters[] = $this;
1291
-
1292
-        return  call_user_func_array([$this->dynamicRelationships[$method], $parameters]);
1293
-    }
26
+	/**
27
+	 * The mapping driver to use with this entity
28
+	 *
29
+	 * @var  string
30
+	 */
31
+	protected $driver = 'illuminate';
32
+
33
+	/**
34
+	 * The Database Connection name for the model.
35
+	 *
36
+	 * @var string
37
+	 */
38
+	protected $connection;
39
+
40
+	/**
41
+	 * The table associated with the entity.
42
+	 *
43
+	 * @var string|null
44
+	 */
45
+	protected $table = null;
46
+
47
+	/**
48
+	 * The primary key for the model.
49
+	 *
50
+	 * @var string
51
+	 */
52
+	protected $primaryKey = 'id';
53
+
54
+	/**
55
+	 * Name of the entity's property that should
56
+	 * contain the attributes, when $mapToProperties is false
57
+	 * 
58
+	 * @var string
59
+	 */
60
+	protected $arrayName = 'attributes';
61
+
62
+	/**
63
+	 * Array containing a list of class attributes. Mandatory if the
64
+	 * mapped entity is a Plain PHP Object.
65
+	 *
66
+	 * @var array
67
+	 */
68
+	protected $attributes = [];
69
+
70
+	/**
71
+	 * Indicate if the entity's attributes should be mapped to the object's
72
+	 * properties. If set to false, attributes will be assigned to an array
73
+	 * defined by the $arrayName property of the EntityMap
74
+	 * 
75
+	 * @var boolean
76
+	 */
77
+	protected $mapToProperties = false;
78
+
79
+	/**
80
+	 * Attributes that should be casted to a specific type, like a Carbon date
81
+	 * JSON, or a custom Value Object
82
+	 * 
83
+	 * @var array
84
+	 */
85
+	protected $casts = [];
86
+
87
+	/**
88
+	 * The Custom Domain Class to use with this mapping
89
+	 *
90
+	 * @var string|null
91
+	 */
92
+	protected $class = null;
93
+
94
+	/**
95
+	 * Attributes that should be treated as Value Objects
96
+	 *
97
+	 * @deprecated 5.3 use $casts property instead
98
+	 * 
99
+	 * @var array
100
+	 */
101
+	protected $embeddables = [];
102
+
103
+	/**
104
+	 * Determine the relationships method used on the entity.
105
+	 * If not set, mapper will autodetect them
106
+	 *
107
+	 * @var array
108
+	 */
109
+	private $relationships = [];
110
+
111
+	/**
112
+	 * Relationships that should be treated as collection.
113
+	 *
114
+	 * @var array
115
+	 */
116
+	private $manyRelations = [];
117
+
118
+	/**
119
+	 * Relationships that should be treated as single entity.
120
+	 *
121
+	 * @var array
122
+	 */
123
+	private $singleRelations = [];
124
+
125
+	/**
126
+	 * Relationships for which the key is stored in the Entity itself
127
+	 *
128
+	 * @var array
129
+	 */
130
+	private $localRelations = [];
131
+
132
+	/**
133
+	 * Relationships for which the key is stored in the Related Entity
134
+	 *
135
+	 * @var array
136
+	 */
137
+	private $foreignRelations = [];
138
+
139
+	/**
140
+	 * Relationships which use a pivot record.
141
+	 *
142
+	 * @var array
143
+	 */
144
+	private $pivotRelations = [];
145
+
146
+	/**
147
+	 * Dynamic relationships
148
+	 *
149
+	 * @var array
150
+	 */
151
+	private $dynamicRelationships = [];
152
+
153
+	/**
154
+	 * The number of models to return for pagination.
155
+	 *
156
+	 * @var int
157
+	 */
158
+	protected $perPage = 15;
159
+
160
+	/**
161
+	 * The relations to eager load on every query.
162
+	 *
163
+	 * @var array
164
+	 */
165
+	protected $with = [];
166
+
167
+	/**
168
+	 * The class name to be used in polymorphic relations.
169
+	 *
170
+	 * @var string
171
+	 */
172
+	protected $morphClass;
173
+
174
+	/**
175
+	 * Sequence name, to be used with postgreSql
176
+	 * defaults to %table_name%_id_seq
177
+	 *
178
+	 * @var string|null
179
+	 */
180
+	protected $sequence = null;
181
+
182
+	/**
183
+	 * Indicates if the entity should be timestamped.
184
+	 *
185
+	 * @var bool
186
+	 */
187
+	public $timestamps = false;
188
+
189
+	/**
190
+	 * The name of the "created at" column.
191
+	 *
192
+	 * @var string
193
+	 */
194
+	protected $createdAtColumn = 'created_at';
195
+
196
+	/**
197
+	 * The name of the "updated at" column.
198
+	 *
199
+	 * @var string
200
+	 */
201
+	protected $updatedAtColumn = 'updated_at';
202
+
203
+	/**
204
+	 * Indicates if the entity uses softdeletes
205
+	 *
206
+	 * @var boolean
207
+	 */
208
+	public $softDeletes = false;
209
+
210
+	/**
211
+	 * The name of the "deleted at" column.
212
+	 *
213
+	 * @var string
214
+	 */
215
+	protected $deletedAtColumn = 'deleted_at';
216
+
217
+	/**
218
+	 * The many to many relationship methods.
219
+	 *
220
+	 * @var array
221
+	 */
222
+	protected static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany'];
223
+
224
+	/**
225
+	 * The 'Many' relationships classes, which related Entity attribute should be
226
+	 * an array/entityCollection
227
+	 *
228
+	 * @var array
229
+	 */
230
+	protected static $manyClasses = ['BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphMany', 'MorphToMany'];
231
+
232
+	/**
233
+	 * The 'Single' relationships classes, which related Entity attribute should be
234
+	 * another Entity.
235
+	 *
236
+	 * @var array
237
+	 */
238
+	protected static $singleClasses = ['BelongsTo', 'HasOne', 'MorphOne', 'MorphTo'];
239
+
240
+	/**
241
+	 * Relationships with a pivot record
242
+	 *
243
+	 * @var array
244
+	 */
245
+	protected static $pivotClasses = ['BelongsToMany', 'MorphToMany'];
246
+
247
+	/**
248
+	 * Relationships on which key is stored in the Entity itself
249
+	 *
250
+	 * @var array
251
+	 */
252
+	protected static $localClasses = ['BelongsTo', 'MorphTo'];
253
+
254
+	/**
255
+	 * Relationships on which key is stored in the related Entity record or in a pivot record
256
+	 *
257
+	 * @var array
258
+	 */
259
+	protected static $foreignClasses = [
260
+		'BelongsToMany',
261
+		'HasMany',
262
+		'HasManyThrough',
263
+		'MorphMany',
264
+		'MorphToMany',
265
+		'HasOne',
266
+		'MorphOne',
267
+	];
268
+
269
+	/**
270
+	 * The date format to use with the current database connection
271
+	 *
272
+	 * @var string
273
+	 */
274
+	protected $dateFormat;
275
+
276
+	/**
277
+	 * The Analogue's manager instance.
278
+	 *
279
+	 * @var \Analogue\ORM\System\Manager
280
+	 */
281
+	private $manager;
282
+
283
+	/**
284
+	 * Set this property to true if the entity should be instantiated
285
+	 * using the IoC Container
286
+	 * 
287
+	 * @var boolean
288
+	 */
289
+	protected $dependencyInjection = false;
290
+
291
+	/**
292
+	 * Set the usage of inheritance, possible values are :
293
+	 * "single_table"
294
+	 * null
295
+	 * 
296
+	 * @var string | null
297
+	 */
298
+	protected $inheritanceType = null;
299
+
300
+	/**
301
+	 * Discriminator column name
302
+	 * 
303
+	 * @var string
304
+	 */
305
+	protected $discriminatorColumn = "type";
306
+
307
+	/**
308
+	 * Allow using a string to define which entity type should be instantiated.
309
+	 * If not set, analogue will uses entity's FQDN
310
+	 * 
311
+	 * @var array
312
+	 */
313
+	protected $discriminatorColumnMap = [];
314
+
315
+	/**
316
+	 * Set the Manager that will be used for relationship's mapper instantiations.
317
+	 *
318
+	 * @param Manager $manager
319
+	 */
320
+	public function setManager(Manager $manager)
321
+	{
322
+		$this->manager = $manager;
323
+	}
324
+
325
+	/**
326
+	 * Return Domain class attributes, useful when mapping to a Plain PHP Object
327
+	 *
328
+	 * @return array
329
+	 */
330
+	public function getAttributes()
331
+	{
332
+		return $this->attributes;
333
+	}
334
+
335
+	/**
336
+	 * Set the domain class attributes
337
+	 *
338
+	 * @param array $attributeNames
339
+	 */
340
+	public function setAttributes(array $attributeNames)
341
+	{
342
+		$this->attributes = $attributeNames;
343
+	}
344
+
345
+	/**
346
+	 * Get all the attribute names for the class, including relationships, embeddables and primary key.
347
+	 *
348
+	 * @return array
349
+	 */
350
+	public function getCompiledAttributes()
351
+	{
352
+		$key = $this->getKeyName();
353
+
354
+		$embeddables = array_keys($this->getEmbeddables());
355
+
356
+		$relationships = $this->getRelationships();
357
+
358
+		$attributes = $this->getAttributes();
359
+
360
+		return array_merge([$key], $embeddables, $relationships, $attributes);
361
+	}
362
+
363
+	/**
364
+	 * Set the date format to use with the current database connection
365
+	 *
366
+	 * @param string $format
367
+	 */
368
+	public function setDateFormat($format)
369
+	{
370
+		$this->dateFormat = $format;
371
+	}
372
+
373
+	/**
374
+	 * Get the date format to use with the current database connection
375
+	 *
376
+	 *  @return string
377
+	 */
378
+	public function getDateFormat()
379
+	{
380
+		return $this->dateFormat;
381
+	}
382
+
383
+	/**
384
+	 * Set the Driver for this mapping
385
+	 *
386
+	 * @param string $driver
387
+	 */
388
+	public function setDriver($driver)
389
+	{
390
+		$this->driver = $driver;
391
+	}
392
+
393
+	/**
394
+	 * Get the Driver for this mapping.
395
+	 *
396
+	 * @return string
397
+	 */
398
+	public function getDriver()
399
+	{
400
+		return $this->driver;
401
+	}
402
+
403
+	/**
404
+	 * Set the db connection to use on the table
405
+	 *
406
+	 * @param $connection
407
+	 */
408
+	public function setConnection($connection)
409
+	{
410
+		$this->connection = $connection;
411
+	}
412
+
413
+	/**
414
+	 * Get the Database connection the Entity is stored on.
415
+	 *
416
+	 * @return string
417
+	 */
418
+	public function getConnection()
419
+	{
420
+		return $this->connection;
421
+	}
422
+
423
+	/**
424
+	 * Get the table associated with the entity.
425
+	 *
426
+	 * @return string
427
+	 */
428
+	public function getTable()
429
+	{
430
+		if (!is_null($this->table)) {
431
+			return $this->table;
432
+		}
433
+
434
+		return str_replace('\\', '', snake_case(str_plural(class_basename($this->getClass()))));
435
+	}
436
+
437
+	/**
438
+	 * Set the database table name
439
+	 *
440
+	 * @param  string $table
441
+	 */
442
+	public function setTable($table)
443
+	{
444
+		$this->table = $table;
445
+	}
446
+
447
+	/**
448
+	 * Get the pgSql sequence name
449
+	 *
450
+	 * @return string
451
+	 */
452
+	public function getSequence()
453
+	{
454
+		if (!is_null($this->sequence)) {
455
+			return $this->sequence;
456
+		} else {
457
+			return $this->getTable() . '_id_seq';
458
+		}
459
+	}
460
+
461
+	/**
462
+	 * Get the custom entity class
463
+	 *
464
+	 * @return string namespaced class name
465
+	 */
466
+	public function getClass()
467
+	{
468
+		return isset($this->class) ? $this->class : null;
469
+	}
470
+
471
+	/**
472
+	 * Set the custom entity class
473
+	 *
474
+	 * @param string $class namespaced class name
475
+	 */
476
+	public function setClass($class)
477
+	{
478
+		// Throw exception if class not exists
479
+
480
+		$this->class = $class;
481
+	}
482
+
483
+	/**
484
+	 * Get the embedded Value Objects
485
+	 *
486
+	 * @return array
487
+	 */
488
+	public function getEmbeddables()
489
+	{
490
+		return $this->embeddables;
491
+	}
492
+
493
+	/**
494
+	 * Set the embedded Value Objects
495
+	 *
496
+	 * @param array $embeddables
497
+	 */
498
+	public function setEmbeddables(array $embeddables)
499
+	{
500
+		$this->embeddables = $embeddables;
501
+	}
502
+
503
+	/**
504
+	 * Get the relationships to map on a custom domain
505
+	 * class.
506
+	 *
507
+	 * @return array
508
+	 */
509
+	public function getRelationships()
510
+	{
511
+		return $this->relationships;
512
+	}
513
+
514
+	/**
515
+	 * Relationships of the Entity type
516
+	 *
517
+	 * @return array
518
+	 */
519
+	public function getSingleRelationships()
520
+	{
521
+		return $this->singleRelations;
522
+	}
523
+
524
+	/**
525
+	 * Relationships of type Collection
526
+	 *
527
+	 * @return array
528
+	 */
529
+	public function getManyRelationships()
530
+	{
531
+		return $this->manyRelations;
532
+	}
533
+
534
+	/**
535
+	 * Relationships with foreign key in the mapped entity record.
536
+	 *
537
+	 * @return array
538
+	 */
539
+	public function getLocalRelationships()
540
+	{
541
+		return $this->localRelations;
542
+	}
543
+
544
+	/**
545
+	 * Relationships with foreign key in the related Entity record
546
+	 *
547
+	 * @return array
548
+	 */
549
+	public function getForeignRelationships()
550
+	{
551
+		return $this->foreignRelations;
552
+	}
553
+
554
+	/**
555
+	 * Relationships which keys are stored in a pivot record
556
+	 *
557
+	 * @return array
558
+	 */
559
+	public function getPivotRelationships()
560
+	{
561
+		return $this->pivotRelations;
562
+	}
563
+
564
+	/**
565
+	 * Add a Dynamic Relationship method at runtime. This has to be done
566
+	 * by hooking the 'initializing' event, before entityMap is initialized.
567
+	 *
568
+	 * @param string  $name         Relation name
569
+	 * @param \Closure $relationship
570
+	 *
571
+	 * @return void
572
+	 */
573
+	public function addRelationshipMethod($name, \Closure $relationship)
574
+	{
575
+		$this->dynamicRelationships[$name] = $relationship;
576
+	}
577
+
578
+	/**
579
+	 * Get the dynamic relationship method names.
580
+	 *
581
+	 * @return array
582
+	 */
583
+	public function getDynamicRelationships()
584
+	{
585
+		return array_keys($this->dynamicRelationships);
586
+	}
587
+
588
+	/**
589
+	 * Get the relationships that have to be eager loaded
590
+	 * on each request.
591
+	 *
592
+	 * @return array
593
+	 */
594
+	public function getEagerloadedRelationships()
595
+	{
596
+		return $this->with;
597
+	}
598
+
599
+	/**
600
+	 * Get the primary key for the entity.
601
+	 *
602
+	 * @return string
603
+	 */
604
+	public function getKeyName()
605
+	{
606
+		return $this->primaryKey;
607
+	}
608
+
609
+	/**
610
+	 * Set the primary key for the entity.
611
+	 *
612
+	 * @param $key
613
+	 * @return void
614
+	 */
615
+	public function setKeyName($key)
616
+	{
617
+		$this->primaryKey = $key;
618
+	}
619
+
620
+	/**
621
+	 * Get the table qualified key name.
622
+	 *
623
+	 * @return string
624
+	 */
625
+	public function getQualifiedKeyName()
626
+	{
627
+		return $this->getTable() . '.' . $this->getKeyName();
628
+	}
629
+
630
+	/**
631
+	 * Get the number of models to return per page.
632
+	 *
633
+	 * @return int
634
+	 */
635
+	public function getPerPage()
636
+	{
637
+		return $this->perPage;
638
+	}
639
+
640
+	/**
641
+	 * Set the number of models to return per page.
642
+	 *
643
+	 * @param  int $perPage
644
+	 * @return void
645
+	 */
646
+	public function setPerPage($perPage)
647
+	{
648
+		$this->perPage = $perPage;
649
+	}
650
+
651
+	/**
652
+	 * Determine if the entity uses get.
653
+	 *
654
+	 * @return bool
655
+	 */
656
+	public function usesTimestamps()
657
+	{
658
+		return $this->timestamps;
659
+	}
660
+
661
+	/**
662
+	 * Determine if the entity uses soft deletes
663
+	 *
664
+	 * @return bool
665
+	 */
666
+	public function usesSoftDeletes()
667
+	{
668
+		return $this->softDeletes;
669
+	}
670
+
671
+	/**
672
+	 * Get the 'created_at' column name
673
+	 *
674
+	 * @return string
675
+	 */
676
+	public function getCreatedAtColumn()
677
+	{
678
+		return $this->createdAtColumn;
679
+	}
680
+
681
+	/**
682
+	 * Get the 'updated_at' column name
683
+	 *
684
+	 * @return string
685
+	 */
686
+	public function getUpdatedAtColumn()
687
+	{
688
+		return $this->updatedAtColumn;
689
+	}
690
+
691
+	/**
692
+	 * Get the deleted_at column
693
+	 *
694
+	 * @return string
695
+	 */
696
+	public function getQualifiedDeletedAtColumn()
697
+	{
698
+		return $this->deletedAtColumn;
699
+	}
700
+
701
+	/**
702
+	 * Get the default foreign key name for the model.
703
+	 *
704
+	 * @return string
705
+	 */
706
+	public function getForeignKey()
707
+	{
708
+		return snake_case(class_basename($this->getClass())) . '_id';
709
+	}
710
+
711
+	/**
712
+	 * Return the inheritance type used by the entity.
713
+	 *
714
+	 * @return string|null
715
+	 */
716
+	public function getInheritanceType()
717
+	{
718
+		return $this->inheritanceType;
719
+	}
720
+
721
+	/**
722
+	 * Return the discriminator column name on the entity that's
723
+	 * used for table inheritance.
724
+	 *
725
+	 * @return string
726
+	 */
727
+	public function getDiscriminatorColumn()
728
+	{
729
+		return $this->discriminatorColumn;
730
+	}
731
+
732
+	/**
733
+	 * Return the mapping of discriminator column values to
734
+	 * entity class names that are used for table inheritance.
735
+	 *
736
+	 * @return array
737
+	 */
738
+	public function getDiscriminatorColumnMap()
739
+	{
740
+		return $this->discriminatorColumnMap;
741
+	}
742
+
743
+	/**
744
+	 * Return true if the entity should be instanciated using
745
+	 * the IoC Container
746
+	 * 
747
+	 * @return boolean
748
+	 */
749
+	public function useDependencyInjection()
750
+	{
751
+		return $this->dependencyInjection;
752
+	}
753
+
754
+	/**
755
+	 * Define a one-to-one relationship.
756
+	 *
757
+	 * @param         $entity
758
+	 * @param  string $relatedClass entity class
759
+	 * @param  string $foreignKey
760
+	 * @param  string $localKey
761
+	 * @throws MappingException
762
+	 * @return \Analogue\ORM\Relationships\HasOne
763
+	 */
764
+	public function hasOne($entity, $relatedClass, $foreignKey = null, $localKey = null)
765
+	{
766
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
767
+
768
+		$relatedMapper = $this->manager->mapper($relatedClass);
769
+
770
+		$relatedMap = $relatedMapper->getEntityMap();
771
+
772
+		$localKey = $localKey ?: $this->getKeyName();
773
+
774
+		return new HasOne($relatedMapper, $entity, $relatedMap->getTable() . '.' . $foreignKey, $localKey);
775
+	}
776
+
777
+	/**
778
+	 * Define a polymorphic one-to-one relationship.
779
+	 *
780
+	 * @param  mixed       $entity
781
+	 * @param  string      $related
782
+	 * @param  string      $name
783
+	 * @param  string|null $type
784
+	 * @param  string|null $id
785
+	 * @param  string|null $localKey
786
+	 * @throws MappingException
787
+	 * @return \Analogue\ORM\Relationships\MorphOne
788
+	 */
789
+	public function morphOne($entity, $related, $name, $type = null, $id = null, $localKey = null)
790
+	{
791
+		list($type, $id) = $this->getMorphs($name, $type, $id);
792
+
793
+		$localKey = $localKey ?: $this->getKeyName();
794
+
795
+		$relatedMapper = $this->manager->mapper($related);
796
+
797
+		$table = $relatedMapper->getEntityMap()->getTable();
798
+
799
+		return new MorphOne($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
800
+	}
801
+
802
+	/**
803
+	 * Define an inverse one-to-one or many relationship.
804
+	 *
805
+	 * @param  mixed       $entity
806
+	 * @param  string      $related
807
+	 * @param  string|null $foreignKey
808
+	 * @param  string|null $otherKey
809
+	 * @param  string|null $relation
810
+	 * @throws MappingException
811
+	 * @return \Analogue\ORM\Relationships\BelongsTo
812
+	 */
813
+	public function belongsTo($entity, $related, $foreignKey = null, $otherKey = null, $relation = null)
814
+	{
815
+		// If no relation name was given, we will use this debug backtrace to extract
816
+		// the calling method's name and use that as the relationship name as most
817
+		// of the time this will be what we desire to use for the relationships.
818
+		if (is_null($relation)) {
819
+			list(, $caller) = debug_backtrace(false);
820
+
821
+			$relation = $caller['function'];
822
+		}
823
+
824
+		// If no foreign key was supplied, we can use a backtrace to guess the proper
825
+		// foreign key name by using the name of the relationship function, which
826
+		// when combined with an "_id" should conventionally match the columns.
827
+		if (is_null($foreignKey)) {
828
+			$foreignKey = snake_case($relation) . '_id';
829
+		}
830
+
831
+		$relatedMapper = $this->manager->mapper($related);
832
+
833
+		$otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getKeyName();
834
+
835
+		return new BelongsTo($relatedMapper, $entity, $foreignKey, $otherKey, $relation);
836
+	}
837
+
838
+	/**
839
+	 * Define a polymorphic, inverse one-to-one or many relationship.
840
+	 *
841
+	 * @param  mixed       $entity
842
+	 * @param  string|null $name
843
+	 * @param  string|null $type
844
+	 * @param  string|null $id
845
+	 * @throws MappingException
846
+	 * @return \Analogue\ORM\Relationships\MorphTo
847
+	 */
848
+	public function morphTo($entity, $name = null, $type = null, $id = null)
849
+	{
850
+		// If no name is provided, we will use the backtrace to get the function name
851
+		// since that is most likely the name of the polymorphic interface. We can
852
+		// use that to get both the class and foreign key that will be utilized.
853
+		if (is_null($name)) {
854
+			list(, $caller) = debug_backtrace(false);
855
+
856
+			$name = snake_case($caller['function']);
857
+		}
858
+
859
+		list($type, $id) = $this->getMorphs($name, $type, $id);
860
+
861
+		$mapper = $this->manager->mapper(get_class($entity));
862
+
863
+		// If the type value is null it is probably safe to assume we're eager loading
864
+		// the relationship. When that is the case we will pass in a dummy query as
865
+		// there are multiple types in the morph and we can't use single queries.
866
+		$factory = new Factory;
867
+		$wrapper = $factory->make($entity);
868
+
869
+		if (is_null($class = $wrapper->getEntityAttribute($type))) {
870
+			return new MorphTo(
871
+				$mapper, $entity, $id, null, $type, $name
872
+			);
873
+		}
874
+
875
+		// If we are not eager loading the relationship we will essentially treat this
876
+		// as a belongs-to style relationship since morph-to extends that class and
877
+		// we will pass in the appropriate values so that it behaves as expected.
878
+		else {
879
+			$class = $this->manager->getInverseMorphMap($class);
880
+			$relatedMapper = $this->manager->mapper($class);
881
+
882
+			$foreignKey = $relatedMapper->getEntityMap()->getKeyName();
883
+
884
+			return new MorphTo(
885
+				$relatedMapper, $entity, $id, $foreignKey, $type, $name
886
+			);
887
+		}
888
+	}
889
+
890
+	/**
891
+	 * Define a one-to-many relationship.
892
+	 *
893
+	 * @param  mixed       $entity
894
+	 * @param  string      $related
895
+	 * @param  string|null $foreignKey
896
+	 * @param  string|null $localKey
897
+	 * @throws MappingException
898
+	 * @return \Analogue\ORM\Relationships\HasMany
899
+	 */
900
+	public function hasMany($entity, $related, $foreignKey = null, $localKey = null)
901
+	{
902
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
903
+
904
+		$relatedMapper = $this->manager->mapper($related);
905
+
906
+		$table = $relatedMapper->getEntityMap()->getTable() . '.' . $foreignKey;
907
+
908
+		$localKey = $localKey ?: $this->getKeyName();
909
+
910
+		return new HasMany($relatedMapper, $entity, $table, $localKey);
911
+	}
912
+
913
+	/**
914
+	 * Define a has-many-through relationship.
915
+	 *
916
+	 * @param  mixed       $entity
917
+	 * @param  string      $related
918
+	 * @param  string      $through
919
+	 * @param  string|null $firstKey
920
+	 * @param  string|null $secondKey
921
+	 * @throws MappingException
922
+	 * @return \Analogue\ORM\Relationships\HasManyThrough
923
+	 */
924
+	public function hasManyThrough($entity, $related, $through, $firstKey = null, $secondKey = null)
925
+	{
926
+		$relatedMapper = $this->manager->mapper($related);
927
+
928
+		$throughMapper = $this->manager->mapper($through);
929
+
930
+
931
+		$firstKey = $firstKey ?: $this->getForeignKey();
932
+
933
+		$throughMap = $throughMapper->getEntityMap();
934
+
935
+		$secondKey = $secondKey ?: $throughMap->getForeignKey();
936
+
937
+		return new HasManyThrough($relatedMapper, $entity, $throughMap, $firstKey, $secondKey);
938
+	}
939
+
940
+	/**
941
+	 * Define a polymorphic one-to-many relationship.
942
+	 *
943
+	 * @param  mixed       $entity
944
+	 * @param  string      $related
945
+	 * @param  string      $name
946
+	 * @param  string|null $type
947
+	 * @param  string|null $id
948
+	 * @param  string|null $localKey
949
+	 * @return \Analogue\ORM\Relationships\MorphMany
950
+	 */
951
+	public function morphMany($entity, $related, $name, $type = null, $id = null, $localKey = null)
952
+	{
953
+		// Here we will gather up the morph type and ID for the relationship so that we
954
+		// can properly query the intermediate table of a relation. Finally, we will
955
+		// get the table and create the relationship instances for the developers.
956
+		list($type, $id) = $this->getMorphs($name, $type, $id);
957
+
958
+		$relatedMapper = $this->manager->mapper($related);
959
+
960
+		$table = $relatedMapper->getEntityMap()->getTable();
961
+
962
+		$localKey = $localKey ?: $this->getKeyName();
963
+
964
+		return new MorphMany($relatedMapper, $entity, $table . '.' . $type, $table . '.' . $id, $localKey);
965
+	}
966
+
967
+	/**
968
+	 * Define a many-to-many relationship.
969
+	 *
970
+	 * @param  mixed       $entity
971
+	 * @param  string      $related
972
+	 * @param  string|null $table
973
+	 * @param  string|null $foreignKey
974
+	 * @param  string|null $otherKey
975
+	 * @param  string|null $relation
976
+	 * @throws MappingException
977
+	 * @return \Analogue\ORM\Relationships\BelongsToMany
978
+	 */
979
+	public function belongsToMany($entity, $related, $table = null, $foreignKey = null, $otherKey = null, $relation = null)
980
+	{
981
+		// If no relationship name was passed, we will pull backtraces to get the
982
+		// name of the calling function. We will use that function name as the
983
+		// title of this relation since that is a great convention to apply.
984
+		if (is_null($relation)) {
985
+			$relation = $this->getBelongsToManyCaller();
986
+		}
987
+
988
+		// First, we'll need to determine the foreign key and "other key" for the
989
+		// relationship. Once we have determined the keys we'll make the query
990
+		// instances as well as the relationship instances we need for this.
991
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
992
+
993
+		$relatedMapper = $this->manager->mapper($related);
994
+
995
+		$relatedMap = $relatedMapper->getEntityMap();
996
+
997
+		$otherKey = $otherKey ?: $relatedMap->getForeignKey();
998
+
999
+		// If no table name was provided, we can guess it by concatenating the two
1000
+		// models using underscores in alphabetical order. The two model names
1001
+		// are transformed to snake case from their default CamelCase also.
1002
+		if (is_null($table)) {
1003
+			$table = $this->joiningTable($relatedMap);
1004
+		}
1005
+
1006
+		return new BelongsToMany($relatedMapper, $entity, $table, $foreignKey, $otherKey, $relation);
1007
+	}
1008
+
1009
+	/**
1010
+	 * Define a polymorphic many-to-many relationship.
1011
+	 *
1012
+	 * @param  mixed       $entity
1013
+	 * @param  string      $related
1014
+	 * @param  string      $name
1015
+	 * @param  string|null $table
1016
+	 * @param  string|null $foreignKey
1017
+	 * @param  string|null $otherKey
1018
+	 * @param  bool        $inverse
1019
+	 * @throws MappingException
1020
+	 * @return \Analogue\ORM\Relationships\MorphToMany
1021
+	 */
1022
+	public function morphToMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null, $inverse = false)
1023
+	{
1024
+		$caller = $this->getBelongsToManyCaller();
1025
+
1026
+		// First, we will need to determine the foreign key and "other key" for the
1027
+		// relationship. Once we have determined the keys we will make the query
1028
+		// instances, as well as the relationship instances we need for these.
1029
+		$foreignKey = $foreignKey ?: $name . '_id';
1030
+
1031
+		$relatedMapper = $this->manager->mapper($related);
1032
+
1033
+		$otherKey = $otherKey ?: $relatedMapper->getEntityMap()->getForeignKey();
1034
+
1035
+		$table = $table ?: str_plural($name);
1036
+
1037
+		return new MorphToMany($relatedMapper, $entity, $name, $table, $foreignKey, $otherKey, $caller, $inverse);
1038
+	}
1039
+
1040
+	/**
1041
+	 * Define a polymorphic, inverse many-to-many relationship.
1042
+	 *
1043
+	 * @param  mixed       $entity
1044
+	 * @param  string      $related
1045
+	 * @param  string      $name
1046
+	 * @param  string|null $table
1047
+	 * @param  string|null $foreignKey
1048
+	 * @param  string|null $otherKey
1049
+	 * @throws MappingException
1050
+	 * @return \Analogue\ORM\Relationships\MorphToMany
1051
+	 */
1052
+	public function morphedByMany($entity, $related, $name, $table = null, $foreignKey = null, $otherKey = null)
1053
+	{
1054
+		$foreignKey = $foreignKey ?: $this->getForeignKey();
1055
+
1056
+		// For the inverse of the polymorphic many-to-many relations, we will change
1057
+		// the way we determine the foreign and other keys, as it is the opposite
1058
+		// of the morph-to-many method since we're figuring out these inverses.
1059
+		$otherKey = $otherKey ?: $name . '_id';
1060
+
1061
+		return $this->morphToMany($entity, $related, $name, $table, $foreignKey, $otherKey, true);
1062
+	}
1063
+
1064
+	/**
1065
+	 * Get the relationship name of the belongs to many.
1066
+	 *
1067
+	 * @return string
1068
+	 */
1069
+	protected function getBelongsToManyCaller()
1070
+	{
1071
+		$self = __FUNCTION__;
1072
+
1073
+		$caller = array_first(debug_backtrace(false), function ($key, $trace) use ($self) {
1074
+			$caller = $trace['function'];
1075
+
1076
+			return (!in_array($caller, EntityMap::$manyMethods) && $caller != $self);
1077
+		});
1078
+
1079
+		return !is_null($caller) ? $caller['function'] : null;
1080
+	}
1081
+
1082
+	/**
1083
+	 * Get the joining table name for a many-to-many relation.
1084
+	 *
1085
+	 * @param  EntityMap $relatedMap
1086
+	 * @return string
1087
+	 */
1088
+	public function joiningTable($relatedMap)
1089
+	{
1090
+		// The joining table name, by convention, is simply the snake cased models
1091
+		// sorted alphabetically and concatenated with an underscore, so we can
1092
+		// just sort the models and join them together to get the table name.
1093
+		$base = $this->getTable();
1094
+
1095
+		$related = $relatedMap->getTable();
1096
+
1097
+		$tables = [$related, $base];
1098
+
1099
+		// Now that we have the model names in an array we can just sort them and
1100
+		// use the implode function to join them together with an underscores,
1101
+		// which is typically used by convention within the database system.
1102
+		sort($tables);
1103
+
1104
+		return strtolower(implode('_', $tables));
1105
+	}
1106
+
1107
+	/**
1108
+	 * Get the polymorphic relationship columns.
1109
+	 *
1110
+	 * @param  string $name
1111
+	 * @param  string $type
1112
+	 * @param  string $id
1113
+	 * @return string[]
1114
+	 */
1115
+	protected function getMorphs($name, $type, $id)
1116
+	{
1117
+		$type = $type ?: $name . '_type';
1118
+
1119
+		$id = $id ?: $name . '_id';
1120
+
1121
+		return [$type, $id];
1122
+	}
1123
+
1124
+	/**
1125
+	 * Get the class name for polymorphic relations.
1126
+	 *
1127
+	 * @return string
1128
+	 */
1129
+	public function getMorphClass()
1130
+	{
1131
+		$morphClass = $this->manager->getMorphMap($this->getClass());
1132
+		return $this->morphClass ?: $morphClass;
1133
+	}
1134
+
1135
+	/**
1136
+	 * Create a new Entity Collection instance.
1137
+	 *
1138
+	 * @param  array $entities
1139
+	 * @return \Analogue\ORM\EntityCollection
1140
+	 */
1141
+	public function newCollection(array $entities = [])
1142
+	{
1143
+		$collection = new EntityCollection($entities, $this);
1144
+		return $collection->keyBy($this->getKeyName());
1145
+	}
1146
+
1147
+	/**
1148
+	 * Process EntityMap parsing at initialization time
1149
+	 *
1150
+	 * @return void
1151
+	 */
1152
+	public function initialize()
1153
+	{
1154
+		$userMethods = $this->getCustomMethods();
1155
+
1156
+		// Parse EntityMap for method based relationship
1157
+		if (count($userMethods) > 0) {
1158
+			$this->relationships = $this->parseMethodsForRelationship($userMethods);
1159
+		}
1160
+
1161
+		// Parse EntityMap for dynamic relationships
1162
+		if (count($this->dynamicRelationships) > 0) {
1163
+			$this->relationships = $this->relationships + $this->getDynamicRelationships();
1164
+		}
1165
+	}
1166
+
1167
+	/**
1168
+	 * Parse every relationships on the EntityMap and sort
1169
+	 * them by type.
1170
+	 *
1171
+	 * @return void
1172
+	 */
1173
+	public function boot()
1174
+	{
1175
+		if (count($this->relationships > 0)) {
1176
+			$this->sortRelationshipsByType();
1177
+		}
1178
+	}
1179
+
1180
+	/**
1181
+	 * Get Methods that has been added in the child class.
1182
+	 *
1183
+	 * @return array
1184
+	 */
1185
+	protected function getCustomMethods()
1186
+	{
1187
+		$mapMethods = get_class_methods($this);
1188
+
1189
+		$parentsMethods = get_class_methods('Analogue\ORM\EntityMap');
1190
+
1191
+		return array_diff($mapMethods, $parentsMethods);
1192
+	}
1193
+
1194
+	/**
1195
+	 * Parse user's class methods for relationships
1196
+	 *
1197
+	 * @param  array $customMethods
1198
+	 * @return array
1199
+	 */
1200
+	protected function parseMethodsForRelationship(array $customMethods)
1201
+	{
1202
+		$relationships = [];
1203
+
1204
+		$class = new ReflectionClass(get_class($this));
1205
+
1206
+		// Get the mapped Entity class, as we will detect relationships
1207
+		// methods by testing that the first argument is type-hinted to
1208
+		// the same class as the mapped Entity.
1209
+		$entityClass = $this->getClass();
1210
+
1211
+		foreach ($customMethods as $methodName) {
1212
+			$method = $class->getMethod($methodName);
1213
+
1214
+			if ($method->getNumberOfParameters() > 0) {
1215
+				$params = $method->getParameters();
1216
+
1217
+				if ($params[0]->getClass() && ($params[0]->getClass()->name == $entityClass || is_subclass_of($entityClass, $params[0]->getClass()->name))) {
1218
+					$relationships[] = $methodName;
1219
+				}
1220
+			}
1221
+		}
1222
+
1223
+		return $relationships;
1224
+	}
1225
+
1226
+	/**
1227
+	 * Sort Relationships methods by type
1228
+	 *
1229
+	 * @return void
1230
+	 */
1231
+	protected function sortRelationshipsByType()
1232
+	{
1233
+		$entityClass = $this->getClass();
1234
+
1235
+		// Instantiate a dummy entity which we will pass to relationship methods.
1236
+		$entity = unserialize(sprintf('O:%d:"%s":0:{}', strlen($entityClass), $entityClass));
1237
+
1238
+		foreach ($this->relationships as $relation) {
1239
+			$relationObject = $this->$relation($entity);
1240
+
1241
+			$class = class_basename(get_class($relationObject));
1242
+
1243
+			if (in_array($class, static::$singleClasses)) {
1244
+				$this->singleRelations[] = $relation;
1245
+			}
1246
+
1247
+			if (in_array($class, static::$manyClasses)) {
1248
+				$this->manyRelations[] = $relation;
1249
+			}
1250
+
1251
+			if (in_array($class, static::$localClasses)) {
1252
+				$this->localRelations[] = $relation;
1253
+			}
1254
+
1255
+			if (in_array($class, static::$foreignClasses)) {
1256
+				$this->foreignRelations[] = $relation;
1257
+			}
1258
+
1259
+			if (in_array($class, static::$pivotClasses)) {
1260
+				$this->pivotRelations[] = $relation;
1261
+			}
1262
+		}
1263
+	}
1264
+
1265
+	/**
1266
+	 * Override this method for custom entity instantiation
1267
+	 *
1268
+	 * @return null
1269
+	 */
1270
+	public function activator()
1271
+	{
1272
+		return null;
1273
+	}
1274
+
1275
+	/**
1276
+	 * Call dynamic relationship, if it exists
1277
+	 *
1278
+	 * @param  string $method
1279
+	 * @param  array  $parameters
1280
+	 * @throws Exception
1281
+	 * @return mixed
1282
+	 */
1283
+	public function __call($method, $parameters)
1284
+	{
1285
+		if (!array_key_exists($method, $this->dynamicRelationships)) {
1286
+			throw new Exception(get_class($this) . " has no method $method");
1287
+		}
1288
+
1289
+		// Add $this to parameters so the closure can call relationship method on the map.
1290
+		$parameters[] = $this;
1291
+
1292
+		return  call_user_func_array([$this->dynamicRelationships[$method], $parameters]);
1293
+	}
1294 1294
 }
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.