Passed
Pull Request — 4.2 (#140)
by David
05:21
created
src/Mouf/Database/TDBM/WeakrefObjectStorage.php 1 patch
Indentation   +105 added lines, -105 removed lines patch added patch discarded remove patch
@@ -31,117 +31,117 @@
 block discarded – undo
31 31
  */
32 32
 class WeakrefObjectStorage
33 33
 {
34
-    /**
35
-     * An array of fetched object, accessible via table name and primary key.
36
-     * If the primary key is split on several columns, access is done by an array of columns, serialized.
37
-     *
38
-     * @var array<string, WeakMap<string, TDBMObject>>
39
-     */
40
-    private $objects = array();
34
+	/**
35
+	 * An array of fetched object, accessible via table name and primary key.
36
+	 * If the primary key is split on several columns, access is done by an array of columns, serialized.
37
+	 *
38
+	 * @var array<string, WeakMap<string, TDBMObject>>
39
+	 */
40
+	private $objects = array();
41 41
 
42
-    /**
43
-     * Every 10000 set in the dataset, we perform a cleanup to ensure the WeakRef instances
44
-     * are removed if they are no more valid.
45
-     * This is to avoid having memory used by dangling WeakRef instances.
46
-     *
47
-     * @var int
48
-     */
49
-    private $garbageCollectorCount = 0;
42
+	/**
43
+	 * Every 10000 set in the dataset, we perform a cleanup to ensure the WeakRef instances
44
+	 * are removed if they are no more valid.
45
+	 * This is to avoid having memory used by dangling WeakRef instances.
46
+	 *
47
+	 * @var int
48
+	 */
49
+	private $garbageCollectorCount = 0;
50 50
 
51
-    /**
52
-     * Sets an object in the storage.
53
-     *
54
-     * @param string $tableName
55
-     * @param string $id
56
-     * @param DbRow  $dbRow
57
-     */
58
-    public function set($tableName, $id, DbRow $dbRow)
59
-    {
60
-        $this->objects[$tableName][$id] = new \WeakRef($dbRow);
61
-        ++$this->garbageCollectorCount;
62
-        if ($this->garbageCollectorCount == 10000) {
63
-            $this->garbageCollectorCount = 0;
64
-            $this->cleanupDanglingWeakRefs();
65
-        }
66
-    }
51
+	/**
52
+	 * Sets an object in the storage.
53
+	 *
54
+	 * @param string $tableName
55
+	 * @param string $id
56
+	 * @param DbRow  $dbRow
57
+	 */
58
+	public function set($tableName, $id, DbRow $dbRow)
59
+	{
60
+		$this->objects[$tableName][$id] = new \WeakRef($dbRow);
61
+		++$this->garbageCollectorCount;
62
+		if ($this->garbageCollectorCount == 10000) {
63
+			$this->garbageCollectorCount = 0;
64
+			$this->cleanupDanglingWeakRefs();
65
+		}
66
+	}
67 67
 
68
-    /**
69
-     * Checks if an object is in the storage.
70
-     *
71
-     * @param string $tableName
72
-     * @param string $id
73
-     *
74
-     * @return bool
75
-     */
76
-    public function has($tableName, $id)
77
-    {
78
-        if (isset($this->objects[$tableName][$id])) {
79
-            if ($this->objects[$tableName][$id]->valid()) {
80
-                return true;
81
-            } else {
82
-                unset($this->objects[$tableName][$id]);
83
-            }
84
-        }
68
+	/**
69
+	 * Checks if an object is in the storage.
70
+	 *
71
+	 * @param string $tableName
72
+	 * @param string $id
73
+	 *
74
+	 * @return bool
75
+	 */
76
+	public function has($tableName, $id)
77
+	{
78
+		if (isset($this->objects[$tableName][$id])) {
79
+			if ($this->objects[$tableName][$id]->valid()) {
80
+				return true;
81
+			} else {
82
+				unset($this->objects[$tableName][$id]);
83
+			}
84
+		}
85 85
 
86
-        return false;
87
-    }
86
+		return false;
87
+	}
88 88
 
89
-    /**
90
-     * Returns an object from the storage (or null if no object is set).
91
-     *
92
-     * @param string $tableName
93
-     * @param string $id
94
-     *
95
-     * @return DbRow
96
-     */
97
-    public function get($tableName, $id)
98
-    {
99
-        if (isset($this->objects[$tableName][$id])) {
100
-            if ($this->objects[$tableName][$id]->valid()) {
101
-                return $this->objects[$tableName][$id]->get();
102
-            }
103
-        } else {
104
-            return;
105
-        }
106
-    }
89
+	/**
90
+	 * Returns an object from the storage (or null if no object is set).
91
+	 *
92
+	 * @param string $tableName
93
+	 * @param string $id
94
+	 *
95
+	 * @return DbRow
96
+	 */
97
+	public function get($tableName, $id)
98
+	{
99
+		if (isset($this->objects[$tableName][$id])) {
100
+			if ($this->objects[$tableName][$id]->valid()) {
101
+				return $this->objects[$tableName][$id]->get();
102
+			}
103
+		} else {
104
+			return;
105
+		}
106
+	}
107 107
 
108
-    /**
109
-     * Removes an object from the storage.
110
-     *
111
-     * @param string $tableName
112
-     * @param string $id
113
-     */
114
-    public function remove($tableName, $id)
115
-    {
116
-        unset($this->objects[$tableName][$id]);
117
-    }
108
+	/**
109
+	 * Removes an object from the storage.
110
+	 *
111
+	 * @param string $tableName
112
+	 * @param string $id
113
+	 */
114
+	public function remove($tableName, $id)
115
+	{
116
+		unset($this->objects[$tableName][$id]);
117
+	}
118 118
 
119
-    /**
120
-     * Applies the callback to all objects.
121
-     *
122
-     * @param callable $callback
123
-     */
124
-    public function apply(callable $callback)
125
-    {
126
-        foreach ($this->objects as $tableName => $table) {
127
-            foreach ($table as $id => $obj) {
128
-                if ($obj->valid()) {
129
-                    $callback($obj->get(), $tableName, $id);
130
-                } else {
131
-                    unset($this->objects[$tableName][$id]);
132
-                }
133
-            }
134
-        }
135
-    }
119
+	/**
120
+	 * Applies the callback to all objects.
121
+	 *
122
+	 * @param callable $callback
123
+	 */
124
+	public function apply(callable $callback)
125
+	{
126
+		foreach ($this->objects as $tableName => $table) {
127
+			foreach ($table as $id => $obj) {
128
+				if ($obj->valid()) {
129
+					$callback($obj->get(), $tableName, $id);
130
+				} else {
131
+					unset($this->objects[$tableName][$id]);
132
+				}
133
+			}
134
+		}
135
+	}
136 136
 
137
-    private function cleanupDanglingWeakRefs()
138
-    {
139
-        foreach ($this->objects as $tableName => $table) {
140
-            foreach ($table as $id => $obj) {
141
-                if (!$obj->valid()) {
142
-                    unset($this->objects[$tableName][$id]);
143
-                }
144
-            }
145
-        }
146
-    }
137
+	private function cleanupDanglingWeakRefs()
138
+	{
139
+		foreach ($this->objects as $tableName => $table) {
140
+			foreach ($table as $id => $obj) {
141
+				if (!$obj->valid()) {
142
+					unset($this->objects[$tableName][$id]);
143
+				}
144
+			}
145
+		}
146
+	}
147 147
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/InnerResultArray.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -83,7 +83,7 @@  discard block
 block discarded – undo
83 83
 
84 84
     private function toIndex($offset)
85 85
     {
86
-        if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) {
86
+        if ($offset<0 || filter_var($offset, FILTER_VALIDATE_INT) === false) {
87 87
             throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.');
88 88
         }
89 89
         if ($this->statement === null) {
@@ -100,7 +100,7 @@  discard block
 block discarded – undo
100 100
     public function next()
101 101
     {
102 102
         // Let's overload the next() method to store the result.
103
-        if (isset($this->results[$this->key + 1])) {
103
+        if (isset($this->results[$this->key+1])) {
104 104
             ++$this->key;
105 105
             $this->current = $this->results[$this->key];
106 106
         } else {
Please login to merge, or discard this patch.
Indentation   +89 added lines, -89 removed lines patch added patch discarded remove patch
@@ -27,100 +27,100 @@
 block discarded – undo
27 27
  */
28 28
 class InnerResultArray extends InnerResultIterator
29 29
 {
30
-    /**
31
-     * The list of results already fetched.
32
-     *
33
-     * @var AbstractTDBMObject[]
34
-     */
35
-    private $results = [];
30
+	/**
31
+	 * The list of results already fetched.
32
+	 *
33
+	 * @var AbstractTDBMObject[]
34
+	 */
35
+	private $results = [];
36 36
 
37
-    /**
38
-     * Whether a offset exists.
39
-     *
40
-     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
41
-     *
42
-     * @param mixed $offset <p>
43
-     *                      An offset to check for.
44
-     *                      </p>
45
-     *
46
-     * @return bool true on success or false on failure.
47
-     *              </p>
48
-     *              <p>
49
-     *              The return value will be casted to boolean if non-boolean was returned
50
-     *
51
-     * @since 5.0.0
52
-     */
53
-    public function offsetExists($offset)
54
-    {
55
-        try {
56
-            $this->toIndex($offset);
57
-        } catch (TDBMInvalidOffsetException $e) {
58
-            return false;
59
-        }
37
+	/**
38
+	 * Whether a offset exists.
39
+	 *
40
+	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
41
+	 *
42
+	 * @param mixed $offset <p>
43
+	 *                      An offset to check for.
44
+	 *                      </p>
45
+	 *
46
+	 * @return bool true on success or false on failure.
47
+	 *              </p>
48
+	 *              <p>
49
+	 *              The return value will be casted to boolean if non-boolean was returned
50
+	 *
51
+	 * @since 5.0.0
52
+	 */
53
+	public function offsetExists($offset)
54
+	{
55
+		try {
56
+			$this->toIndex($offset);
57
+		} catch (TDBMInvalidOffsetException $e) {
58
+			return false;
59
+		}
60 60
 
61
-        return true;
62
-    }
61
+		return true;
62
+	}
63 63
 
64
-    /**
65
-     * Offset to retrieve.
66
-     *
67
-     * @link http://php.net/manual/en/arrayaccess.offsetget.php
68
-     *
69
-     * @param mixed $offset <p>
70
-     *                      The offset to retrieve.
71
-     *                      </p>
72
-     *
73
-     * @return mixed Can return all value types
74
-     *
75
-     * @since 5.0.0
76
-     */
77
-    public function offsetGet($offset)
78
-    {
79
-        $this->toIndex($offset);
64
+	/**
65
+	 * Offset to retrieve.
66
+	 *
67
+	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
68
+	 *
69
+	 * @param mixed $offset <p>
70
+	 *                      The offset to retrieve.
71
+	 *                      </p>
72
+	 *
73
+	 * @return mixed Can return all value types
74
+	 *
75
+	 * @since 5.0.0
76
+	 */
77
+	public function offsetGet($offset)
78
+	{
79
+		$this->toIndex($offset);
80 80
 
81
-        return $this->results[$offset];
82
-    }
81
+		return $this->results[$offset];
82
+	}
83 83
 
84
-    private function toIndex($offset)
85
-    {
86
-        if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) {
87
-            throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.');
88
-        }
89
-        if ($this->statement === null) {
90
-            $this->executeQuery();
91
-        }
92
-        while (!isset($this->results[$offset])) {
93
-            $this->next();
94
-            if ($this->current === null) {
95
-                throw new TDBMInvalidOffsetException('Offset "'.$offset.'" does not exist in result set.');
96
-            }
97
-        }
98
-    }
84
+	private function toIndex($offset)
85
+	{
86
+		if ($offset < 0 || filter_var($offset, FILTER_VALIDATE_INT) === false) {
87
+			throw new TDBMInvalidOffsetException('Trying to access result set using offset "'.$offset.'". An offset must be a positive integer.');
88
+		}
89
+		if ($this->statement === null) {
90
+			$this->executeQuery();
91
+		}
92
+		while (!isset($this->results[$offset])) {
93
+			$this->next();
94
+			if ($this->current === null) {
95
+				throw new TDBMInvalidOffsetException('Offset "'.$offset.'" does not exist in result set.');
96
+			}
97
+		}
98
+	}
99 99
 
100
-    public function next()
101
-    {
102
-        // Let's overload the next() method to store the result.
103
-        if (isset($this->results[$this->key + 1])) {
104
-            ++$this->key;
105
-            $this->current = $this->results[$this->key];
106
-        } else {
107
-            parent::next();
108
-            if ($this->current !== null) {
109
-                $this->results[$this->key] = $this->current;
110
-            }
111
-        }
112
-    }
100
+	public function next()
101
+	{
102
+		// Let's overload the next() method to store the result.
103
+		if (isset($this->results[$this->key + 1])) {
104
+			++$this->key;
105
+			$this->current = $this->results[$this->key];
106
+		} else {
107
+			parent::next();
108
+			if ($this->current !== null) {
109
+				$this->results[$this->key] = $this->current;
110
+			}
111
+		}
112
+	}
113 113
 
114
-    /**
115
-     * Overloads the rewind implementation.
116
-     * Do not reexecute the query.
117
-     */
118
-    public function rewind()
119
-    {
120
-        if (!$this->fetchStarted) {
121
-            $this->executeQuery();
122
-        }
123
-        $this->key = -1;
124
-        $this->next();
125
-    }
114
+	/**
115
+	 * Overloads the rewind implementation.
116
+	 * Do not reexecute the query.
117
+	 */
118
+	public function rewind()
119
+	{
120
+		if (!$this->fetchStarted) {
121
+			$this->executeQuery();
122
+		}
123
+		$this->key = -1;
124
+		$this->next();
125
+	}
126 126
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/StandardObjectStorage.php 1 patch
Indentation   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -30,78 +30,78 @@
 block discarded – undo
30 30
  */
31 31
 class StandardObjectStorage
32 32
 {
33
-    /**
34
-     * An array of fetched object, accessible via table name and primary key.
35
-     * If the primary key is split on several columns, access is done by an array of columns, serialized.
36
-     *
37
-     * @var array<string, array<string, TDBMObject>>
38
-     */
39
-    private $objects = array();
33
+	/**
34
+	 * An array of fetched object, accessible via table name and primary key.
35
+	 * If the primary key is split on several columns, access is done by an array of columns, serialized.
36
+	 *
37
+	 * @var array<string, array<string, TDBMObject>>
38
+	 */
39
+	private $objects = array();
40 40
 
41
-    /**
42
-     * Sets an object in the storage.
43
-     *
44
-     * @param string     $tableName
45
-     * @param string     $id
46
-     * @param TDBMObject $dbRow
47
-     */
48
-    public function set($tableName, $id, DbRow $dbRow)
49
-    {
50
-        $this->objects[$tableName][$id] = $dbRow;
51
-    }
41
+	/**
42
+	 * Sets an object in the storage.
43
+	 *
44
+	 * @param string     $tableName
45
+	 * @param string     $id
46
+	 * @param TDBMObject $dbRow
47
+	 */
48
+	public function set($tableName, $id, DbRow $dbRow)
49
+	{
50
+		$this->objects[$tableName][$id] = $dbRow;
51
+	}
52 52
 
53
-    /**
54
-     * Checks if an object is in the storage.
55
-     *
56
-     * @param string $tableName
57
-     * @param string $id
58
-     *
59
-     * @return bool
60
-     */
61
-    public function has($tableName, $id)
62
-    {
63
-        return isset($this->objects[$tableName][$id]);
64
-    }
53
+	/**
54
+	 * Checks if an object is in the storage.
55
+	 *
56
+	 * @param string $tableName
57
+	 * @param string $id
58
+	 *
59
+	 * @return bool
60
+	 */
61
+	public function has($tableName, $id)
62
+	{
63
+		return isset($this->objects[$tableName][$id]);
64
+	}
65 65
 
66
-    /**
67
-     * Returns an object from the storage (or null if no object is set).
68
-     *
69
-     * @param string $tableName
70
-     * @param string $id
71
-     *
72
-     * @return DbRow
73
-     */
74
-    public function get($tableName, $id)
75
-    {
76
-        if (isset($this->objects[$tableName][$id])) {
77
-            return $this->objects[$tableName][$id];
78
-        } else {
79
-            return;
80
-        }
81
-    }
66
+	/**
67
+	 * Returns an object from the storage (or null if no object is set).
68
+	 *
69
+	 * @param string $tableName
70
+	 * @param string $id
71
+	 *
72
+	 * @return DbRow
73
+	 */
74
+	public function get($tableName, $id)
75
+	{
76
+		if (isset($this->objects[$tableName][$id])) {
77
+			return $this->objects[$tableName][$id];
78
+		} else {
79
+			return;
80
+		}
81
+	}
82 82
 
83
-    /**
84
-     * Removes an object from the storage.
85
-     *
86
-     * @param string $tableName
87
-     * @param string $id
88
-     */
89
-    public function remove($tableName, $id)
90
-    {
91
-        unset($this->objects[$tableName][$id]);
92
-    }
83
+	/**
84
+	 * Removes an object from the storage.
85
+	 *
86
+	 * @param string $tableName
87
+	 * @param string $id
88
+	 */
89
+	public function remove($tableName, $id)
90
+	{
91
+		unset($this->objects[$tableName][$id]);
92
+	}
93 93
 
94
-    /**
95
-     * Applies the callback to all objects.
96
-     *
97
-     * @param callable $callback
98
-     */
99
-    public function apply(callable $callback)
100
-    {
101
-        foreach ($this->objects as $tableName => $table) {
102
-            foreach ($table as $id => $obj) {
103
-                $callback($obj, $tableName, $id);
104
-            }
105
-        }
106
-    }
94
+	/**
95
+	 * Applies the callback to all objects.
96
+	 *
97
+	 * @param callable $callback
98
+	 */
99
+	public function apply(callable $callback)
100
+	{
101
+		foreach ($this->objects as $tableName => $table) {
102
+			foreach ($table as $id => $obj) {
103
+				$callback($obj, $tableName, $id);
104
+			}
105
+		}
106
+	}
107 107
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/PageIterator.php 2 patches
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -112,7 +112,7 @@  discard block
 block discarded – undo
112 112
      */
113 113
     public function getCurrentPage()
114 114
     {
115
-        return floor($this->offset / $this->limit) + 1;
115
+        return floor($this->offset/$this->limit)+1;
116 116
     }
117 117
 
118 118
     /**
@@ -251,7 +251,7 @@  discard block
 block discarded – undo
251 251
      */
252 252
     public function jsonSerialize()
253 253
     {
254
-        return array_map(function (AbstractTDBMObject $item) {
254
+        return array_map(function(AbstractTDBMObject $item) {
255 255
             return $item->jsonSerialize();
256 256
         }, $this->toArray());
257 257
     }
Please login to merge, or discard this patch.
Indentation   +211 added lines, -211 removed lines patch added patch discarded remove patch
@@ -30,238 +30,238 @@
 block discarded – undo
30 30
  */
31 31
 class PageIterator implements Page, \ArrayAccess, \JsonSerializable
32 32
 {
33
-    /**
34
-     * @var Statement
35
-     */
36
-    protected $statement;
33
+	/**
34
+	 * @var Statement
35
+	 */
36
+	protected $statement;
37 37
 
38
-    protected $fetchStarted = false;
39
-    private $objectStorage;
40
-    private $className;
38
+	protected $fetchStarted = false;
39
+	private $objectStorage;
40
+	private $className;
41 41
 
42
-    private $parentResult;
43
-    private $tdbmService;
44
-    private $magicSql;
45
-    private $parameters;
46
-    private $limit;
47
-    private $offset;
48
-    private $columnDescriptors;
49
-    private $magicQuery;
42
+	private $parentResult;
43
+	private $tdbmService;
44
+	private $magicSql;
45
+	private $parameters;
46
+	private $limit;
47
+	private $offset;
48
+	private $columnDescriptors;
49
+	private $magicQuery;
50 50
 
51
-    /**
52
-     * The key of the current retrieved object.
53
-     *
54
-     * @var int
55
-     */
56
-    protected $key = -1;
51
+	/**
52
+	 * The key of the current retrieved object.
53
+	 *
54
+	 * @var int
55
+	 */
56
+	protected $key = -1;
57 57
 
58
-    protected $current = null;
58
+	protected $current = null;
59 59
 
60
-    private $databasePlatform;
60
+	private $databasePlatform;
61 61
 
62
-    private $innerResultIterator;
62
+	private $innerResultIterator;
63 63
 
64
-    private $mode;
64
+	private $mode;
65 65
 
66
-    /**
67
-     * @var LoggerInterface
68
-     */
69
-    private $logger;
66
+	/**
67
+	 * @var LoggerInterface
68
+	 */
69
+	private $logger;
70 70
 
71
-    public function __construct(ResultIterator $parentResult, $magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger)
72
-    {
73
-        $this->parentResult = $parentResult;
74
-        $this->magicSql = $magicSql;
75
-        $this->objectStorage = $objectStorage;
76
-        $this->className = $className;
77
-        $this->tdbmService = $tdbmService;
78
-        $this->parameters = $parameters;
79
-        $this->limit = $limit;
80
-        $this->offset = $offset;
81
-        $this->columnDescriptors = $columnDescriptors;
82
-        $this->magicQuery = $magicQuery;
83
-        $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform();
84
-        $this->mode = $mode;
85
-        $this->logger = $logger;
86
-    }
71
+	public function __construct(ResultIterator $parentResult, $magicSql, array $parameters, $limit, $offset, array $columnDescriptors, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger)
72
+	{
73
+		$this->parentResult = $parentResult;
74
+		$this->magicSql = $magicSql;
75
+		$this->objectStorage = $objectStorage;
76
+		$this->className = $className;
77
+		$this->tdbmService = $tdbmService;
78
+		$this->parameters = $parameters;
79
+		$this->limit = $limit;
80
+		$this->offset = $offset;
81
+		$this->columnDescriptors = $columnDescriptors;
82
+		$this->magicQuery = $magicQuery;
83
+		$this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform();
84
+		$this->mode = $mode;
85
+		$this->logger = $logger;
86
+	}
87 87
 
88
-    /**
89
-     * Retrieve an external iterator.
90
-     *
91
-     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
92
-     *
93
-     * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
94
-     *                             <b>Traversable</b>
95
-     *
96
-     * @since 5.0.0
97
-     */
98
-    public function getIterator()
99
-    {
100
-        if ($this->innerResultIterator === null) {
101
-            if ($this->mode === TDBMService::MODE_CURSOR) {
102
-                $this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
103
-            } else {
104
-                $this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
105
-            }
106
-        }
88
+	/**
89
+	 * Retrieve an external iterator.
90
+	 *
91
+	 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
92
+	 *
93
+	 * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
94
+	 *                             <b>Traversable</b>
95
+	 *
96
+	 * @since 5.0.0
97
+	 */
98
+	public function getIterator()
99
+	{
100
+		if ($this->innerResultIterator === null) {
101
+			if ($this->mode === TDBMService::MODE_CURSOR) {
102
+				$this->innerResultIterator = new InnerResultIterator($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
103
+			} else {
104
+				$this->innerResultIterator = new InnerResultArray($this->magicSql, $this->parameters, $this->limit, $this->offset, $this->columnDescriptors, $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
105
+			}
106
+		}
107 107
 
108
-        return $this->innerResultIterator;
109
-    }
108
+		return $this->innerResultIterator;
109
+	}
110 110
 
111
-    /**
112
-     * @return int
113
-     */
114
-    public function getCurrentOffset()
115
-    {
116
-        return $this->offset;
117
-    }
111
+	/**
112
+	 * @return int
113
+	 */
114
+	public function getCurrentOffset()
115
+	{
116
+		return $this->offset;
117
+	}
118 118
 
119
-    /**
120
-     * @return int
121
-     */
122
-    public function getCurrentPage()
123
-    {
124
-        return floor($this->offset / $this->limit) + 1;
125
-    }
119
+	/**
120
+	 * @return int
121
+	 */
122
+	public function getCurrentPage()
123
+	{
124
+		return floor($this->offset / $this->limit) + 1;
125
+	}
126 126
 
127
-    /**
128
-     * @return int
129
-     */
130
-    public function getCurrentLimit()
131
-    {
132
-        return $this->limit;
133
-    }
127
+	/**
128
+	 * @return int
129
+	 */
130
+	public function getCurrentLimit()
131
+	{
132
+		return $this->limit;
133
+	}
134 134
 
135
-    /**
136
-     * Return the number of results on the current page of the {@link Result}.
137
-     *
138
-     * @return int
139
-     */
140
-    public function count()
141
-    {
142
-        return $this->getIterator()->count();
143
-    }
135
+	/**
136
+	 * Return the number of results on the current page of the {@link Result}.
137
+	 *
138
+	 * @return int
139
+	 */
140
+	public function count()
141
+	{
142
+		return $this->getIterator()->count();
143
+	}
144 144
 
145
-    /**
146
-     * Return the number of ALL results in the paginatable of {@link Result}.
147
-     *
148
-     * @return int
149
-     */
150
-    public function totalCount()
151
-    {
152
-        return $this->parentResult->count();
153
-    }
145
+	/**
146
+	 * Return the number of ALL results in the paginatable of {@link Result}.
147
+	 *
148
+	 * @return int
149
+	 */
150
+	public function totalCount()
151
+	{
152
+		return $this->parentResult->count();
153
+	}
154 154
 
155
-    /**
156
-     * Casts the result set to a PHP array.
157
-     *
158
-     * @return array
159
-     */
160
-    public function toArray()
161
-    {
162
-        return iterator_to_array($this->getIterator());
163
-    }
155
+	/**
156
+	 * Casts the result set to a PHP array.
157
+	 *
158
+	 * @return array
159
+	 */
160
+	public function toArray()
161
+	{
162
+		return iterator_to_array($this->getIterator());
163
+	}
164 164
 
165
-    /**
166
-     * Returns a new iterator mapping any call using the $callable function.
167
-     *
168
-     * @param callable $callable
169
-     *
170
-     * @return MapIterator
171
-     */
172
-    public function map(callable $callable)
173
-    {
174
-        return new MapIterator($this->getIterator(), $callable);
175
-    }
165
+	/**
166
+	 * Returns a new iterator mapping any call using the $callable function.
167
+	 *
168
+	 * @param callable $callable
169
+	 *
170
+	 * @return MapIterator
171
+	 */
172
+	public function map(callable $callable)
173
+	{
174
+		return new MapIterator($this->getIterator(), $callable);
175
+	}
176 176
 
177
-    /**
178
-     * Whether a offset exists.
179
-     *
180
-     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
181
-     *
182
-     * @param mixed $offset <p>
183
-     *                      An offset to check for.
184
-     *                      </p>
185
-     *
186
-     * @return bool true on success or false on failure.
187
-     *              </p>
188
-     *              <p>
189
-     *              The return value will be casted to boolean if non-boolean was returned
190
-     *
191
-     * @since 5.0.0
192
-     */
193
-    public function offsetExists($offset)
194
-    {
195
-        return $this->getIterator()->offsetExists($offset);
196
-    }
177
+	/**
178
+	 * Whether a offset exists.
179
+	 *
180
+	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
181
+	 *
182
+	 * @param mixed $offset <p>
183
+	 *                      An offset to check for.
184
+	 *                      </p>
185
+	 *
186
+	 * @return bool true on success or false on failure.
187
+	 *              </p>
188
+	 *              <p>
189
+	 *              The return value will be casted to boolean if non-boolean was returned
190
+	 *
191
+	 * @since 5.0.0
192
+	 */
193
+	public function offsetExists($offset)
194
+	{
195
+		return $this->getIterator()->offsetExists($offset);
196
+	}
197 197
 
198
-    /**
199
-     * Offset to retrieve.
200
-     *
201
-     * @link http://php.net/manual/en/arrayaccess.offsetget.php
202
-     *
203
-     * @param mixed $offset <p>
204
-     *                      The offset to retrieve.
205
-     *                      </p>
206
-     *
207
-     * @return mixed Can return all value types
208
-     *
209
-     * @since 5.0.0
210
-     */
211
-    public function offsetGet($offset)
212
-    {
213
-        return $this->getIterator()->offsetGet($offset);
214
-    }
198
+	/**
199
+	 * Offset to retrieve.
200
+	 *
201
+	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
202
+	 *
203
+	 * @param mixed $offset <p>
204
+	 *                      The offset to retrieve.
205
+	 *                      </p>
206
+	 *
207
+	 * @return mixed Can return all value types
208
+	 *
209
+	 * @since 5.0.0
210
+	 */
211
+	public function offsetGet($offset)
212
+	{
213
+		return $this->getIterator()->offsetGet($offset);
214
+	}
215 215
 
216
-    /**
217
-     * Offset to set.
218
-     *
219
-     * @link http://php.net/manual/en/arrayaccess.offsetset.php
220
-     *
221
-     * @param mixed $offset <p>
222
-     *                      The offset to assign the value to.
223
-     *                      </p>
224
-     * @param mixed $value  <p>
225
-     *                      The value to set.
226
-     *                      </p>
227
-     *
228
-     * @since 5.0.0
229
-     */
230
-    public function offsetSet($offset, $value)
231
-    {
232
-        return $this->getIterator()->offsetSet($offset, $value);
233
-    }
216
+	/**
217
+	 * Offset to set.
218
+	 *
219
+	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
220
+	 *
221
+	 * @param mixed $offset <p>
222
+	 *                      The offset to assign the value to.
223
+	 *                      </p>
224
+	 * @param mixed $value  <p>
225
+	 *                      The value to set.
226
+	 *                      </p>
227
+	 *
228
+	 * @since 5.0.0
229
+	 */
230
+	public function offsetSet($offset, $value)
231
+	{
232
+		return $this->getIterator()->offsetSet($offset, $value);
233
+	}
234 234
 
235
-    /**
236
-     * Offset to unset.
237
-     *
238
-     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
239
-     *
240
-     * @param mixed $offset <p>
241
-     *                      The offset to unset.
242
-     *                      </p>
243
-     *
244
-     * @since 5.0.0
245
-     */
246
-    public function offsetUnset($offset)
247
-    {
248
-        return $this->getIterator()->offsetUnset($offset);
249
-    }
235
+	/**
236
+	 * Offset to unset.
237
+	 *
238
+	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
239
+	 *
240
+	 * @param mixed $offset <p>
241
+	 *                      The offset to unset.
242
+	 *                      </p>
243
+	 *
244
+	 * @since 5.0.0
245
+	 */
246
+	public function offsetUnset($offset)
247
+	{
248
+		return $this->getIterator()->offsetUnset($offset);
249
+	}
250 250
 
251
-    /**
252
-     * Specify data which should be serialized to JSON.
253
-     *
254
-     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
255
-     *
256
-     * @return mixed data which can be serialized by <b>json_encode</b>,
257
-     *               which is a value of any type other than a resource
258
-     *
259
-     * @since 5.4.0
260
-     */
261
-    public function jsonSerialize()
262
-    {
263
-        return array_map(function (AbstractTDBMObject $item) {
264
-            return $item->jsonSerialize();
265
-        }, $this->toArray());
266
-    }
251
+	/**
252
+	 * Specify data which should be serialized to JSON.
253
+	 *
254
+	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
255
+	 *
256
+	 * @return mixed data which can be serialized by <b>json_encode</b>,
257
+	 *               which is a value of any type other than a resource
258
+	 *
259
+	 * @since 5.4.0
260
+	 */
261
+	public function jsonSerialize()
262
+	{
263
+		return array_map(function (AbstractTDBMObject $item) {
264
+			return $item->jsonSerialize();
265
+		}, $this->toArray());
266
+	}
267 267
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Controllers/TdbmInstallController.php 3 patches
Doc Comments   +3 added lines patch added patch discarded remove patch
@@ -181,6 +181,9 @@
 block discarded – undo
181 181
 
182 182
     protected $errorMsg;
183 183
 
184
+    /**
185
+     * @param string $msg
186
+     */
184 187
     private function displayErrorMsg($msg)
185 188
     {
186 189
         $this->errorMsg = $msg;
Please login to merge, or discard this patch.
Unused Use Statements   -1 removed lines patch added patch discarded remove patch
@@ -6,7 +6,6 @@
 block discarded – undo
6 6
 use Mouf\Actions\InstallUtils;
7 7
 use Mouf\Console\ConsoleUtils;
8 8
 use Mouf\Database\TDBM\Commands\GenerateCommand;
9
-use Mouf\Database\TDBM\Configuration;
10 9
 use Mouf\Database\TDBM\MoufConfiguration;
11 10
 use Mouf\Database\TDBM\Utils\DefaultNamingStrategy;
12 11
 use Mouf\Database\TDBM\Utils\MoufDiListener;
Please login to merge, or discard this patch.
Indentation   +215 added lines, -215 removed lines patch added patch discarded remove patch
@@ -21,219 +21,219 @@
 block discarded – undo
21 21
  */
22 22
 class TdbmInstallController extends Controller
23 23
 {
24
-    /**
25
-     * @var HtmlBlock
26
-     */
27
-    public $content;
28
-
29
-    public $selfedit;
30
-
31
-    /**
32
-     * The active MoufManager to be edited/viewed.
33
-     *
34
-     * @var MoufManager
35
-     */
36
-    public $moufManager;
37
-
38
-    /**
39
-     * The template used by the main page for mouf.
40
-     *
41
-     * @Property
42
-     * @Compulsory
43
-     *
44
-     * @var TemplateInterface
45
-     */
46
-    public $template;
47
-
48
-    /**
49
-     * Displays the first install screen.
50
-     *
51
-     * @Action
52
-     * @Logged
53
-     *
54
-     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
55
-     */
56
-    public function defaultAction($selfedit = 'false')
57
-    {
58
-        $this->selfedit = $selfedit;
59
-
60
-        if ($selfedit == 'true') {
61
-            $this->moufManager = MoufManager::getMoufManager();
62
-        } else {
63
-            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
64
-        }
65
-
66
-        $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep1.php', $this);
67
-        $this->template->toHtml();
68
-    }
69
-
70
-    /**
71
-     * Skips the install process.
72
-     *
73
-     * @Action
74
-     * @Logged
75
-     *
76
-     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
77
-     */
78
-    public function skip($selfedit = 'false')
79
-    {
80
-        InstallUtils::continueInstall($selfedit == 'true');
81
-    }
82
-
83
-    protected $daoNamespace;
84
-    protected $beanNamespace;
85
-    protected $autoloadDetected;
86
-    //protected $storeInUtc;
87
-    protected $useCustomComposer = false;
88
-    protected $composerFile;
89
-
90
-    /**
91
-     * Displays the second install screen.
92
-     *
93
-     * @Action
94
-     * @Logged
95
-     *
96
-     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
97
-     */
98
-    public function configure($selfedit = 'false')
99
-    {
100
-        $this->selfedit = $selfedit;
101
-
102
-        if ($selfedit == 'true') {
103
-            $this->moufManager = MoufManager::getMoufManager();
104
-        } else {
105
-            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
106
-        }
107
-
108
-        // Let's start by performing basic checks about the instances we assume to exist.
109
-        if (!$this->moufManager->instanceExists('dbalConnection')) {
110
-            $this->displayErrorMsg("The TDBM install process assumes your database connection instance is already created, and that the name of this instance is 'dbalConnection'. Could not find the 'dbalConnection' instance.");
111
-
112
-            return;
113
-        }
114
-
115
-        if ($this->moufManager->has('tdbmConfiguration')) {
116
-            $tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
117
-
118
-            $this->beanNamespace = $tdbmConfiguration->getConstructorArgumentProperty('beanNamespace')->getValue();
119
-            $this->daoNamespace = $tdbmConfiguration->getConstructorArgumentProperty('daoNamespace')->getValue();
120
-        } else {
121
-            // Old TDBM 4.2 fallback
122
-            $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService');
123
-            $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService');
124
-        }
125
-
126
-        if ($this->daoNamespace == null && $this->beanNamespace == null) {
127
-            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
128
-
129
-            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
130
-            if ($autoloadNamespaces) {
131
-                $this->autoloadDetected = true;
132
-                $rootNamespace = $autoloadNamespaces[0];
133
-                $this->daoNamespace = $rootNamespace.'Dao';
134
-                $this->beanNamespace = $rootNamespace.'Model';
135
-            } else {
136
-                $this->autoloadDetected = false;
137
-                $this->daoNamespace = 'YourApplication\\Dao';
138
-                $this->beanNamespace = 'YourApplication\\Model';
139
-            }
140
-        } else {
141
-            $this->autoloadDetected = true;
142
-        }
143
-        $this->defaultPath = true;
144
-        $this->storePath = '';
145
-
146
-        $this->castDatesToDateTime = true;
147
-
148
-        $this->content->addFile(__DIR__.'/../../../../views/installStep2.php', $this);
149
-        $this->template->toHtml();
150
-    }
151
-
152
-    /**
153
-     * This action generates the TDBM instance, then the DAOs and Beans.
154
-     *
155
-     * @Action
156
-     *
157
-     * @param string $daonamespace
158
-     * @param string $beannamespace
159
-     * @param string $selfedit
160
-     *
161
-     * @throws \Mouf\MoufException
162
-     */
163
-    public function generate($daonamespace, $beannamespace, /*$storeInUtc = 0,*/ $selfedit = 'false', $defaultPath = false, $storePath = '')
164
-    {
165
-        $this->selfedit = $selfedit;
166
-
167
-        if ($selfedit == 'true') {
168
-            $this->moufManager = MoufManager::getMoufManager();
169
-        } else {
170
-            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
171
-        }
172
-
173
-        $doctrineCache = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');
174
-
175
-        $migratingFrom42 = false;
176
-        if ($this->moufManager->has('tdbmService') && !$this->moufManager->has('tdbmConfiguration')) {
177
-            $migratingFrom42 = true;
178
-        }
179
-
180
-        $namingStrategy = InstallUtils::getOrCreateInstance('namingStrategy', DefaultNamingStrategy::class, $this->moufManager);
181
-        if ($migratingFrom42) {
182
-            // Let's setup the naming strategy for compatibility
183
-            $namingStrategy->getSetterProperty('setBeanPrefix')->setValue('');
184
-            $namingStrategy->getSetterProperty('setBeanSuffix')->setValue('Bean');
185
-            $namingStrategy->getSetterProperty('setBaseBeanPrefix')->setValue('');
186
-            $namingStrategy->getSetterProperty('setBaseBeanSuffix')->setValue('BaseBean');
187
-            $namingStrategy->getSetterProperty('setDaoPrefix')->setValue('');
188
-            $namingStrategy->getSetterProperty('setDaoSuffix')->setValue('Dao');
189
-            $namingStrategy->getSetterProperty('setBaseDaoPrefix')->setValue('');
190
-            $namingStrategy->getSetterProperty('setBaseDaoSuffix')->setValue('BaseDao');
191
-        }
192
-
193
-        if (!$this->moufManager->instanceExists('tdbmConfiguration')) {
194
-            $moufListener = InstallUtils::getOrCreateInstance(MoufDiListener::class, MoufDiListener::class, $this->moufManager);
195
-
196
-            $tdbmConfiguration = $this->moufManager->createInstance(MoufConfiguration::class)->setName('tdbmConfiguration');
197
-            $tdbmConfiguration->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection'));
198
-            $tdbmConfiguration->getConstructorArgumentProperty('cache')->setValue($doctrineCache);
199
-            $tdbmConfiguration->getConstructorArgumentProperty('namingStrategy')->setValue($namingStrategy);
200
-            $tdbmConfiguration->getProperty('daoFactoryInstanceName')->setValue('daoFactory');
201
-            $tdbmConfiguration->getConstructorArgumentProperty('generatorListeners')->setValue([$moufListener]);
202
-
203
-            // Let's also delete the tdbmService if migrating versions <= 4.2
204
-            if ($migratingFrom42) {
205
-                $this->moufManager->removeComponent('tdbmService');
206
-            }
207
-        } else {
208
-            $tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
209
-        }
210
-
211
-        if (!$this->moufManager->instanceExists('tdbmService')) {
212
-            $tdbmService = $this->moufManager->createInstance('Mouf\\Database\\TDBM\\TDBMService')->setName('tdbmService');
213
-            $tdbmService->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
214
-        }
215
-
216
-        // We declare our instance of the Symfony command as a Mouf instance
217
-        $generateCommand = InstallUtils::getOrCreateInstance('generateCommand', GenerateCommand::class, $this->moufManager);
218
-        $generateCommand->getConstructorArgumentProperty('tdbmConfiguration')->setValue($tdbmConfiguration);
219
-
220
-        // We register that instance descriptor using "ConsoleUtils"
221
-        $consoleUtils = new ConsoleUtils($this->moufManager);
222
-        $consoleUtils->registerCommand($generateCommand);
223
-
224
-        $this->moufManager->rewriteMouf();
225
-
226
-        TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'daoFactory', $selfedit, /*$storeInUtc,*/ $defaultPath, $storePath);
227
-
228
-        InstallUtils::continueInstall($selfedit == 'true');
229
-    }
230
-
231
-    protected $errorMsg;
232
-
233
-    private function displayErrorMsg($msg)
234
-    {
235
-        $this->errorMsg = $msg;
236
-        $this->content->addFile(__DIR__.'/../../../../views/installError.php', $this);
237
-        $this->template->toHtml();
238
-    }
24
+	/**
25
+	 * @var HtmlBlock
26
+	 */
27
+	public $content;
28
+
29
+	public $selfedit;
30
+
31
+	/**
32
+	 * The active MoufManager to be edited/viewed.
33
+	 *
34
+	 * @var MoufManager
35
+	 */
36
+	public $moufManager;
37
+
38
+	/**
39
+	 * The template used by the main page for mouf.
40
+	 *
41
+	 * @Property
42
+	 * @Compulsory
43
+	 *
44
+	 * @var TemplateInterface
45
+	 */
46
+	public $template;
47
+
48
+	/**
49
+	 * Displays the first install screen.
50
+	 *
51
+	 * @Action
52
+	 * @Logged
53
+	 *
54
+	 * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
55
+	 */
56
+	public function defaultAction($selfedit = 'false')
57
+	{
58
+		$this->selfedit = $selfedit;
59
+
60
+		if ($selfedit == 'true') {
61
+			$this->moufManager = MoufManager::getMoufManager();
62
+		} else {
63
+			$this->moufManager = MoufManager::getMoufManagerHiddenInstance();
64
+		}
65
+
66
+		$this->content->addFile(dirname(__FILE__).'/../../../../views/installStep1.php', $this);
67
+		$this->template->toHtml();
68
+	}
69
+
70
+	/**
71
+	 * Skips the install process.
72
+	 *
73
+	 * @Action
74
+	 * @Logged
75
+	 *
76
+	 * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
77
+	 */
78
+	public function skip($selfedit = 'false')
79
+	{
80
+		InstallUtils::continueInstall($selfedit == 'true');
81
+	}
82
+
83
+	protected $daoNamespace;
84
+	protected $beanNamespace;
85
+	protected $autoloadDetected;
86
+	//protected $storeInUtc;
87
+	protected $useCustomComposer = false;
88
+	protected $composerFile;
89
+
90
+	/**
91
+	 * Displays the second install screen.
92
+	 *
93
+	 * @Action
94
+	 * @Logged
95
+	 *
96
+	 * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
97
+	 */
98
+	public function configure($selfedit = 'false')
99
+	{
100
+		$this->selfedit = $selfedit;
101
+
102
+		if ($selfedit == 'true') {
103
+			$this->moufManager = MoufManager::getMoufManager();
104
+		} else {
105
+			$this->moufManager = MoufManager::getMoufManagerHiddenInstance();
106
+		}
107
+
108
+		// Let's start by performing basic checks about the instances we assume to exist.
109
+		if (!$this->moufManager->instanceExists('dbalConnection')) {
110
+			$this->displayErrorMsg("The TDBM install process assumes your database connection instance is already created, and that the name of this instance is 'dbalConnection'. Could not find the 'dbalConnection' instance.");
111
+
112
+			return;
113
+		}
114
+
115
+		if ($this->moufManager->has('tdbmConfiguration')) {
116
+			$tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
117
+
118
+			$this->beanNamespace = $tdbmConfiguration->getConstructorArgumentProperty('beanNamespace')->getValue();
119
+			$this->daoNamespace = $tdbmConfiguration->getConstructorArgumentProperty('daoNamespace')->getValue();
120
+		} else {
121
+			// Old TDBM 4.2 fallback
122
+			$this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService');
123
+			$this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService');
124
+		}
125
+
126
+		if ($this->daoNamespace == null && $this->beanNamespace == null) {
127
+			$classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
128
+
129
+			$autoloadNamespaces = $classNameMapper->getManagedNamespaces();
130
+			if ($autoloadNamespaces) {
131
+				$this->autoloadDetected = true;
132
+				$rootNamespace = $autoloadNamespaces[0];
133
+				$this->daoNamespace = $rootNamespace.'Dao';
134
+				$this->beanNamespace = $rootNamespace.'Model';
135
+			} else {
136
+				$this->autoloadDetected = false;
137
+				$this->daoNamespace = 'YourApplication\\Dao';
138
+				$this->beanNamespace = 'YourApplication\\Model';
139
+			}
140
+		} else {
141
+			$this->autoloadDetected = true;
142
+		}
143
+		$this->defaultPath = true;
144
+		$this->storePath = '';
145
+
146
+		$this->castDatesToDateTime = true;
147
+
148
+		$this->content->addFile(__DIR__.'/../../../../views/installStep2.php', $this);
149
+		$this->template->toHtml();
150
+	}
151
+
152
+	/**
153
+	 * This action generates the TDBM instance, then the DAOs and Beans.
154
+	 *
155
+	 * @Action
156
+	 *
157
+	 * @param string $daonamespace
158
+	 * @param string $beannamespace
159
+	 * @param string $selfedit
160
+	 *
161
+	 * @throws \Mouf\MoufException
162
+	 */
163
+	public function generate($daonamespace, $beannamespace, /*$storeInUtc = 0,*/ $selfedit = 'false', $defaultPath = false, $storePath = '')
164
+	{
165
+		$this->selfedit = $selfedit;
166
+
167
+		if ($selfedit == 'true') {
168
+			$this->moufManager = MoufManager::getMoufManager();
169
+		} else {
170
+			$this->moufManager = MoufManager::getMoufManagerHiddenInstance();
171
+		}
172
+
173
+		$doctrineCache = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');
174
+
175
+		$migratingFrom42 = false;
176
+		if ($this->moufManager->has('tdbmService') && !$this->moufManager->has('tdbmConfiguration')) {
177
+			$migratingFrom42 = true;
178
+		}
179
+
180
+		$namingStrategy = InstallUtils::getOrCreateInstance('namingStrategy', DefaultNamingStrategy::class, $this->moufManager);
181
+		if ($migratingFrom42) {
182
+			// Let's setup the naming strategy for compatibility
183
+			$namingStrategy->getSetterProperty('setBeanPrefix')->setValue('');
184
+			$namingStrategy->getSetterProperty('setBeanSuffix')->setValue('Bean');
185
+			$namingStrategy->getSetterProperty('setBaseBeanPrefix')->setValue('');
186
+			$namingStrategy->getSetterProperty('setBaseBeanSuffix')->setValue('BaseBean');
187
+			$namingStrategy->getSetterProperty('setDaoPrefix')->setValue('');
188
+			$namingStrategy->getSetterProperty('setDaoSuffix')->setValue('Dao');
189
+			$namingStrategy->getSetterProperty('setBaseDaoPrefix')->setValue('');
190
+			$namingStrategy->getSetterProperty('setBaseDaoSuffix')->setValue('BaseDao');
191
+		}
192
+
193
+		if (!$this->moufManager->instanceExists('tdbmConfiguration')) {
194
+			$moufListener = InstallUtils::getOrCreateInstance(MoufDiListener::class, MoufDiListener::class, $this->moufManager);
195
+
196
+			$tdbmConfiguration = $this->moufManager->createInstance(MoufConfiguration::class)->setName('tdbmConfiguration');
197
+			$tdbmConfiguration->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection'));
198
+			$tdbmConfiguration->getConstructorArgumentProperty('cache')->setValue($doctrineCache);
199
+			$tdbmConfiguration->getConstructorArgumentProperty('namingStrategy')->setValue($namingStrategy);
200
+			$tdbmConfiguration->getProperty('daoFactoryInstanceName')->setValue('daoFactory');
201
+			$tdbmConfiguration->getConstructorArgumentProperty('generatorListeners')->setValue([$moufListener]);
202
+
203
+			// Let's also delete the tdbmService if migrating versions <= 4.2
204
+			if ($migratingFrom42) {
205
+				$this->moufManager->removeComponent('tdbmService');
206
+			}
207
+		} else {
208
+			$tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
209
+		}
210
+
211
+		if (!$this->moufManager->instanceExists('tdbmService')) {
212
+			$tdbmService = $this->moufManager->createInstance('Mouf\\Database\\TDBM\\TDBMService')->setName('tdbmService');
213
+			$tdbmService->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
214
+		}
215
+
216
+		// We declare our instance of the Symfony command as a Mouf instance
217
+		$generateCommand = InstallUtils::getOrCreateInstance('generateCommand', GenerateCommand::class, $this->moufManager);
218
+		$generateCommand->getConstructorArgumentProperty('tdbmConfiguration')->setValue($tdbmConfiguration);
219
+
220
+		// We register that instance descriptor using "ConsoleUtils"
221
+		$consoleUtils = new ConsoleUtils($this->moufManager);
222
+		$consoleUtils->registerCommand($generateCommand);
223
+
224
+		$this->moufManager->rewriteMouf();
225
+
226
+		TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'daoFactory', $selfedit, /*$storeInUtc,*/ $defaultPath, $storePath);
227
+
228
+		InstallUtils::continueInstall($selfedit == 'true');
229
+	}
230
+
231
+	protected $errorMsg;
232
+
233
+	private function displayErrorMsg($msg)
234
+	{
235
+		$this->errorMsg = $msg;
236
+		$this->content->addFile(__DIR__.'/../../../../views/installError.php', $this);
237
+		$this->template->toHtml();
238
+	}
239 239
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Utils/AbstractBeanPropertyDescriptor.php 1 patch
Indentation   +127 added lines, -127 removed lines patch added patch discarded remove patch
@@ -9,131 +9,131 @@
 block discarded – undo
9 9
  */
10 10
 abstract class AbstractBeanPropertyDescriptor
11 11
 {
12
-    /**
13
-     * @var Table
14
-     */
15
-    protected $table;
16
-
17
-    /**
18
-     * Whether to use the more complex name in case of conflict.
19
-     *
20
-     * @var bool
21
-     */
22
-    protected $alternativeName = false;
23
-
24
-    /**
25
-     * @param Table $table
26
-     */
27
-    public function __construct(Table $table)
28
-    {
29
-        $this->table = $table;
30
-    }
31
-
32
-    /**
33
-     * Use the more complex name in case of conflict.
34
-     */
35
-    public function useAlternativeName()
36
-    {
37
-        $this->alternativeName = true;
38
-    }
39
-
40
-    /**
41
-     * Returns the name of the class linked to this property or null if this is not a foreign key.
42
-     *
43
-     * @return null|string
44
-     */
45
-    abstract public function getClassName();
46
-
47
-    /**
48
-     * Returns the param annotation for this property (useful for constructor).
49
-     *
50
-     * @return string
51
-     */
52
-    abstract public function getParamAnnotation();
53
-
54
-    public function getVariableName()
55
-    {
56
-        return '$'.$this->getLowerCamelCaseName();
57
-    }
58
-
59
-    public function getLowerCamelCaseName()
60
-    {
61
-        return TDBMDaoGenerator::toVariableName($this->getUpperCamelCaseName());
62
-    }
63
-
64
-    abstract public function getUpperCamelCaseName();
65
-
66
-    public function getSetterName()
67
-    {
68
-        return 'set'.$this->getUpperCamelCaseName();
69
-    }
70
-
71
-    public function getGetterName()
72
-    {
73
-        return 'get'.$this->getUpperCamelCaseName();
74
-    }
75
-
76
-    /**
77
-     * Returns the PHP code used in the ben constructor for this property.
78
-     *
79
-     * @return string
80
-     */
81
-    public function getConstructorAssignCode()
82
-    {
83
-        $str = '        $this->%s(%s);';
84
-
85
-        return sprintf($str, $this->getSetterName(), $this->getVariableName());
86
-    }
87
-
88
-    /**
89
-     * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
90
-     *
91
-     * @return bool
92
-     */
93
-    abstract public function isCompulsory();
94
-
95
-    /**
96
-     * Returns true if the property has a default value.
97
-     *
98
-     * @return bool
99
-     */
100
-    abstract public function hasDefault();
101
-
102
-    /**
103
-     * Returns the code that assigns a value to its default value.
104
-     *
105
-     * @return string
106
-     *
107
-     * @throws \TDBMException
108
-     */
109
-    abstract public function assignToDefaultCode();
110
-
111
-    /**
112
-     * Returns true if the property is the primary key.
113
-     *
114
-     * @return bool
115
-     */
116
-    abstract public function isPrimaryKey();
117
-
118
-    /**
119
-     * @return Table
120
-     */
121
-    public function getTable()
122
-    {
123
-        return $this->table;
124
-    }
125
-
126
-    /**
127
-     * Returns the PHP code for getters and setters.
128
-     *
129
-     * @return string
130
-     */
131
-    abstract public function getGetterSetterCode();
132
-
133
-    /**
134
-     * Returns the part of code useful when doing json serialization.
135
-     *
136
-     * @return string
137
-     */
138
-    abstract public function getJsonSerializeCode();
12
+	/**
13
+	 * @var Table
14
+	 */
15
+	protected $table;
16
+
17
+	/**
18
+	 * Whether to use the more complex name in case of conflict.
19
+	 *
20
+	 * @var bool
21
+	 */
22
+	protected $alternativeName = false;
23
+
24
+	/**
25
+	 * @param Table $table
26
+	 */
27
+	public function __construct(Table $table)
28
+	{
29
+		$this->table = $table;
30
+	}
31
+
32
+	/**
33
+	 * Use the more complex name in case of conflict.
34
+	 */
35
+	public function useAlternativeName()
36
+	{
37
+		$this->alternativeName = true;
38
+	}
39
+
40
+	/**
41
+	 * Returns the name of the class linked to this property or null if this is not a foreign key.
42
+	 *
43
+	 * @return null|string
44
+	 */
45
+	abstract public function getClassName();
46
+
47
+	/**
48
+	 * Returns the param annotation for this property (useful for constructor).
49
+	 *
50
+	 * @return string
51
+	 */
52
+	abstract public function getParamAnnotation();
53
+
54
+	public function getVariableName()
55
+	{
56
+		return '$'.$this->getLowerCamelCaseName();
57
+	}
58
+
59
+	public function getLowerCamelCaseName()
60
+	{
61
+		return TDBMDaoGenerator::toVariableName($this->getUpperCamelCaseName());
62
+	}
63
+
64
+	abstract public function getUpperCamelCaseName();
65
+
66
+	public function getSetterName()
67
+	{
68
+		return 'set'.$this->getUpperCamelCaseName();
69
+	}
70
+
71
+	public function getGetterName()
72
+	{
73
+		return 'get'.$this->getUpperCamelCaseName();
74
+	}
75
+
76
+	/**
77
+	 * Returns the PHP code used in the ben constructor for this property.
78
+	 *
79
+	 * @return string
80
+	 */
81
+	public function getConstructorAssignCode()
82
+	{
83
+		$str = '        $this->%s(%s);';
84
+
85
+		return sprintf($str, $this->getSetterName(), $this->getVariableName());
86
+	}
87
+
88
+	/**
89
+	 * Returns true if the property is compulsory (and therefore should be fetched in the constructor).
90
+	 *
91
+	 * @return bool
92
+	 */
93
+	abstract public function isCompulsory();
94
+
95
+	/**
96
+	 * Returns true if the property has a default value.
97
+	 *
98
+	 * @return bool
99
+	 */
100
+	abstract public function hasDefault();
101
+
102
+	/**
103
+	 * Returns the code that assigns a value to its default value.
104
+	 *
105
+	 * @return string
106
+	 *
107
+	 * @throws \TDBMException
108
+	 */
109
+	abstract public function assignToDefaultCode();
110
+
111
+	/**
112
+	 * Returns true if the property is the primary key.
113
+	 *
114
+	 * @return bool
115
+	 */
116
+	abstract public function isPrimaryKey();
117
+
118
+	/**
119
+	 * @return Table
120
+	 */
121
+	public function getTable()
122
+	{
123
+		return $this->table;
124
+	}
125
+
126
+	/**
127
+	 * Returns the PHP code for getters and setters.
128
+	 *
129
+	 * @return string
130
+	 */
131
+	abstract public function getGetterSetterCode();
132
+
133
+	/**
134
+	 * Returns the part of code useful when doing json serialization.
135
+	 *
136
+	 * @return string
137
+	 */
138
+	abstract public function getJsonSerializeCode();
139 139
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/ResultIterator.php 3 patches
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -245,7 +245,7 @@
 block discarded – undo
245 245
      */
246 246
     public function jsonSerialize($stopRecursion = false)
247 247
     {
248
-        return array_map(function (AbstractTDBMObject $item) use ($stopRecursion) {
248
+        return array_map(function(AbstractTDBMObject $item) use ($stopRecursion) {
249 249
             return $item->jsonSerialize($stopRecursion);
250 250
         }, $this->toArray());
251 251
     }
Please login to merge, or discard this patch.
Indentation   +281 added lines, -281 removed lines patch added patch discarded remove patch
@@ -32,285 +32,285 @@
 block discarded – undo
32 32
  */
33 33
 class ResultIterator implements Result, \ArrayAccess, \JsonSerializable
34 34
 {
35
-    /**
36
-     * @var Statement
37
-     */
38
-    protected $statement;
39
-
40
-    private $objectStorage;
41
-    private $className;
42
-
43
-    private $tdbmService;
44
-    private $parameters;
45
-    private $magicQuery;
46
-
47
-    /**
48
-     * @var QueryFactory
49
-     */
50
-    private $queryFactory;
51
-
52
-    /**
53
-     * @var InnerResultIterator
54
-     */
55
-    private $innerResultIterator;
56
-
57
-    private $databasePlatform;
58
-
59
-    private $totalCount;
60
-
61
-    private $mode;
62
-
63
-    private $logger;
64
-
65
-    public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger)
66
-    {
67
-        if ($mode !== null && $mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) {
68
-            throw new TDBMException("Unknown fetch mode: '".$mode."'");
69
-        }
70
-
71
-        $this->queryFactory = $queryFactory;
72
-        $this->objectStorage = $objectStorage;
73
-        $this->className = $className;
74
-        $this->tdbmService = $tdbmService;
75
-        $this->parameters = $parameters;
76
-        $this->magicQuery = $magicQuery;
77
-        $this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform();
78
-        $this->mode = $mode;
79
-        $this->logger = $logger;
80
-    }
81
-
82
-    protected function executeCountQuery()
83
-    {
84
-        $sql = $this->magicQuery->build($this->queryFactory->getMagicSqlCount(), $this->parameters);
85
-        $this->logger->debug('Running count query: '.$sql);
86
-        $this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters);
87
-    }
88
-
89
-    /**
90
-     * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings).
91
-     *
92
-     * @return int
93
-     */
94
-    public function count()
95
-    {
96
-        if ($this->totalCount === null) {
97
-            $this->executeCountQuery();
98
-        }
99
-
100
-        return $this->totalCount;
101
-    }
102
-
103
-    /**
104
-     * Casts the result set to a PHP array.
105
-     *
106
-     * @return array
107
-     */
108
-    public function toArray()
109
-    {
110
-        return iterator_to_array($this->getIterator());
111
-    }
112
-
113
-    /**
114
-     * Returns a new iterator mapping any call using the $callable function.
115
-     *
116
-     * @param callable $callable
117
-     *
118
-     * @return MapIterator
119
-     */
120
-    public function map(callable $callable)
121
-    {
122
-        return new MapIterator($this->getIterator(), $callable);
123
-    }
124
-
125
-    /**
126
-     * Retrieve an external iterator.
127
-     *
128
-     * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
129
-     *
130
-     * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
131
-     *                             <b>Traversable</b>
132
-     *
133
-     * @since 5.0.0
134
-     */
135
-    public function getIterator()
136
-    {
137
-        if ($this->innerResultIterator === null) {
138
-            if ($this->mode === TDBMService::MODE_CURSOR) {
139
-                $this->innerResultIterator = new InnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
140
-            } else {
141
-                $this->innerResultIterator = new InnerResultArray($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
142
-            }
143
-        }
144
-
145
-        return $this->innerResultIterator;
146
-    }
147
-
148
-    /**
149
-     * @param int $offset
150
-     * @param int $limit
151
-     *
152
-     * @return PageIterator
153
-     */
154
-    public function take($offset, $limit)
155
-    {
156
-        return new PageIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger);
157
-    }
158
-
159
-    /**
160
-     * Whether a offset exists.
161
-     *
162
-     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
163
-     *
164
-     * @param mixed $offset <p>
165
-     *                      An offset to check for.
166
-     *                      </p>
167
-     *
168
-     * @return bool true on success or false on failure.
169
-     *              </p>
170
-     *              <p>
171
-     *              The return value will be casted to boolean if non-boolean was returned
172
-     *
173
-     * @since 5.0.0
174
-     */
175
-    public function offsetExists($offset)
176
-    {
177
-        return $this->getIterator()->offsetExists($offset);
178
-    }
179
-
180
-    /**
181
-     * Offset to retrieve.
182
-     *
183
-     * @link http://php.net/manual/en/arrayaccess.offsetget.php
184
-     *
185
-     * @param mixed $offset <p>
186
-     *                      The offset to retrieve.
187
-     *                      </p>
188
-     *
189
-     * @return mixed Can return all value types
190
-     *
191
-     * @since 5.0.0
192
-     */
193
-    public function offsetGet($offset)
194
-    {
195
-        return $this->getIterator()->offsetGet($offset);
196
-    }
197
-
198
-    /**
199
-     * Offset to set.
200
-     *
201
-     * @link http://php.net/manual/en/arrayaccess.offsetset.php
202
-     *
203
-     * @param mixed $offset <p>
204
-     *                      The offset to assign the value to.
205
-     *                      </p>
206
-     * @param mixed $value  <p>
207
-     *                      The value to set.
208
-     *                      </p>
209
-     *
210
-     * @since 5.0.0
211
-     */
212
-    public function offsetSet($offset, $value)
213
-    {
214
-        return $this->getIterator()->offsetSet($offset, $value);
215
-    }
216
-
217
-    /**
218
-     * Offset to unset.
219
-     *
220
-     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
221
-     *
222
-     * @param mixed $offset <p>
223
-     *                      The offset to unset.
224
-     *                      </p>
225
-     *
226
-     * @since 5.0.0
227
-     */
228
-    public function offsetUnset($offset)
229
-    {
230
-        return $this->getIterator()->offsetUnset($offset);
231
-    }
232
-
233
-    /**
234
-     * Specify data which should be serialized to JSON.
235
-     *
236
-     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
237
-     *
238
-     * @param bool $stopRecursion Parameter used internally by TDBM to
239
-     *                            stop embedded objects from embedding
240
-     *                            other objects
241
-     *
242
-     * @return mixed data which can be serialized by <b>json_encode</b>,
243
-     *               which is a value of any type other than a resource
244
-     *
245
-     * @since 5.4.0
246
-     */
247
-    public function jsonSerialize($stopRecursion = false)
248
-    {
249
-        return array_map(function (AbstractTDBMObject $item) use ($stopRecursion) {
250
-            return $item->jsonSerialize($stopRecursion);
251
-        }, $this->toArray());
252
-    }
253
-
254
-    /**
255
-     * Returns only one value (the first) of the result set.
256
-     * Returns null if no value exists.
257
-     *
258
-     * @return mixed|null
259
-     */
260
-    public function first()
261
-    {
262
-        $page = $this->take(0, 1);
263
-        foreach ($page as $bean) {
264
-            return $bean;
265
-        }
266
-
267
-        return;
268
-    }
269
-
270
-    /**
271
-     * Sets the ORDER BY directive executed in SQL and returns a NEW ResultIterator.
272
-     *
273
-     * For instance:
274
-     *
275
-     *  $resultSet = $resultSet->withOrder('label ASC, status DESC');
276
-     *
277
-     * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter.
278
-     * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter:
279
-     *
280
-     *  $resultSet->withOrder(new UncheckedOrderBy('RAND()'))
281
-     *
282
-     * @param string|UncheckedOrderBy|null $orderBy
283
-     *
284
-     * @return ResultIterator
285
-     */
286
-    public function withOrder($orderBy) : ResultIterator
287
-    {
288
-        $clone = clone $this;
289
-        $clone->queryFactory = clone $this->queryFactory;
290
-        $clone->queryFactory->sort($orderBy);
291
-        $clone->innerResultIterator = null;
292
-
293
-        return $clone;
294
-    }
295
-
296
-    /**
297
-     * Sets new parameters for the SQL query and returns a NEW ResultIterator.
298
-     *
299
-     * For instance:
300
-     *
301
-     *  $resultSet = $resultSet->withParameters('label ASC, status DESC');
302
-     *
303
-     * @param string|UncheckedOrderBy|null $orderBy
304
-     *
305
-     * @return ResultIterator
306
-     */
307
-    public function withParameters(array $parameters) : ResultIterator
308
-    {
309
-        $clone = clone $this;
310
-        $clone->parameters = $parameters;
311
-        $clone->innerResultIterator = null;
312
-        $clone->totalCount = null;
313
-
314
-        return $clone;
315
-    }
35
+	/**
36
+	 * @var Statement
37
+	 */
38
+	protected $statement;
39
+
40
+	private $objectStorage;
41
+	private $className;
42
+
43
+	private $tdbmService;
44
+	private $parameters;
45
+	private $magicQuery;
46
+
47
+	/**
48
+	 * @var QueryFactory
49
+	 */
50
+	private $queryFactory;
51
+
52
+	/**
53
+	 * @var InnerResultIterator
54
+	 */
55
+	private $innerResultIterator;
56
+
57
+	private $databasePlatform;
58
+
59
+	private $totalCount;
60
+
61
+	private $mode;
62
+
63
+	private $logger;
64
+
65
+	public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger)
66
+	{
67
+		if ($mode !== null && $mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) {
68
+			throw new TDBMException("Unknown fetch mode: '".$mode."'");
69
+		}
70
+
71
+		$this->queryFactory = $queryFactory;
72
+		$this->objectStorage = $objectStorage;
73
+		$this->className = $className;
74
+		$this->tdbmService = $tdbmService;
75
+		$this->parameters = $parameters;
76
+		$this->magicQuery = $magicQuery;
77
+		$this->databasePlatform = $this->tdbmService->getConnection()->getDatabasePlatform();
78
+		$this->mode = $mode;
79
+		$this->logger = $logger;
80
+	}
81
+
82
+	protected function executeCountQuery()
83
+	{
84
+		$sql = $this->magicQuery->build($this->queryFactory->getMagicSqlCount(), $this->parameters);
85
+		$this->logger->debug('Running count query: '.$sql);
86
+		$this->totalCount = $this->tdbmService->getConnection()->fetchColumn($sql, $this->parameters);
87
+	}
88
+
89
+	/**
90
+	 * Counts found records (this is the number of records fetched, taking into account the LIMIT and OFFSET settings).
91
+	 *
92
+	 * @return int
93
+	 */
94
+	public function count()
95
+	{
96
+		if ($this->totalCount === null) {
97
+			$this->executeCountQuery();
98
+		}
99
+
100
+		return $this->totalCount;
101
+	}
102
+
103
+	/**
104
+	 * Casts the result set to a PHP array.
105
+	 *
106
+	 * @return array
107
+	 */
108
+	public function toArray()
109
+	{
110
+		return iterator_to_array($this->getIterator());
111
+	}
112
+
113
+	/**
114
+	 * Returns a new iterator mapping any call using the $callable function.
115
+	 *
116
+	 * @param callable $callable
117
+	 *
118
+	 * @return MapIterator
119
+	 */
120
+	public function map(callable $callable)
121
+	{
122
+		return new MapIterator($this->getIterator(), $callable);
123
+	}
124
+
125
+	/**
126
+	 * Retrieve an external iterator.
127
+	 *
128
+	 * @link http://php.net/manual/en/iteratoraggregate.getiterator.php
129
+	 *
130
+	 * @return InnerResultIterator An instance of an object implementing <b>Iterator</b> or
131
+	 *                             <b>Traversable</b>
132
+	 *
133
+	 * @since 5.0.0
134
+	 */
135
+	public function getIterator()
136
+	{
137
+		if ($this->innerResultIterator === null) {
138
+			if ($this->mode === TDBMService::MODE_CURSOR) {
139
+				$this->innerResultIterator = new InnerResultIterator($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
140
+			} else {
141
+				$this->innerResultIterator = new InnerResultArray($this->queryFactory->getMagicSql(), $this->parameters, null, null, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->logger);
142
+			}
143
+		}
144
+
145
+		return $this->innerResultIterator;
146
+	}
147
+
148
+	/**
149
+	 * @param int $offset
150
+	 * @param int $limit
151
+	 *
152
+	 * @return PageIterator
153
+	 */
154
+	public function take($offset, $limit)
155
+	{
156
+		return new PageIterator($this, $this->queryFactory->getMagicSql(), $this->parameters, $limit, $offset, $this->queryFactory->getColumnDescriptors(), $this->objectStorage, $this->className, $this->tdbmService, $this->magicQuery, $this->mode, $this->logger);
157
+	}
158
+
159
+	/**
160
+	 * Whether a offset exists.
161
+	 *
162
+	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
163
+	 *
164
+	 * @param mixed $offset <p>
165
+	 *                      An offset to check for.
166
+	 *                      </p>
167
+	 *
168
+	 * @return bool true on success or false on failure.
169
+	 *              </p>
170
+	 *              <p>
171
+	 *              The return value will be casted to boolean if non-boolean was returned
172
+	 *
173
+	 * @since 5.0.0
174
+	 */
175
+	public function offsetExists($offset)
176
+	{
177
+		return $this->getIterator()->offsetExists($offset);
178
+	}
179
+
180
+	/**
181
+	 * Offset to retrieve.
182
+	 *
183
+	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
184
+	 *
185
+	 * @param mixed $offset <p>
186
+	 *                      The offset to retrieve.
187
+	 *                      </p>
188
+	 *
189
+	 * @return mixed Can return all value types
190
+	 *
191
+	 * @since 5.0.0
192
+	 */
193
+	public function offsetGet($offset)
194
+	{
195
+		return $this->getIterator()->offsetGet($offset);
196
+	}
197
+
198
+	/**
199
+	 * Offset to set.
200
+	 *
201
+	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
202
+	 *
203
+	 * @param mixed $offset <p>
204
+	 *                      The offset to assign the value to.
205
+	 *                      </p>
206
+	 * @param mixed $value  <p>
207
+	 *                      The value to set.
208
+	 *                      </p>
209
+	 *
210
+	 * @since 5.0.0
211
+	 */
212
+	public function offsetSet($offset, $value)
213
+	{
214
+		return $this->getIterator()->offsetSet($offset, $value);
215
+	}
216
+
217
+	/**
218
+	 * Offset to unset.
219
+	 *
220
+	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
221
+	 *
222
+	 * @param mixed $offset <p>
223
+	 *                      The offset to unset.
224
+	 *                      </p>
225
+	 *
226
+	 * @since 5.0.0
227
+	 */
228
+	public function offsetUnset($offset)
229
+	{
230
+		return $this->getIterator()->offsetUnset($offset);
231
+	}
232
+
233
+	/**
234
+	 * Specify data which should be serialized to JSON.
235
+	 *
236
+	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
237
+	 *
238
+	 * @param bool $stopRecursion Parameter used internally by TDBM to
239
+	 *                            stop embedded objects from embedding
240
+	 *                            other objects
241
+	 *
242
+	 * @return mixed data which can be serialized by <b>json_encode</b>,
243
+	 *               which is a value of any type other than a resource
244
+	 *
245
+	 * @since 5.4.0
246
+	 */
247
+	public function jsonSerialize($stopRecursion = false)
248
+	{
249
+		return array_map(function (AbstractTDBMObject $item) use ($stopRecursion) {
250
+			return $item->jsonSerialize($stopRecursion);
251
+		}, $this->toArray());
252
+	}
253
+
254
+	/**
255
+	 * Returns only one value (the first) of the result set.
256
+	 * Returns null if no value exists.
257
+	 *
258
+	 * @return mixed|null
259
+	 */
260
+	public function first()
261
+	{
262
+		$page = $this->take(0, 1);
263
+		foreach ($page as $bean) {
264
+			return $bean;
265
+		}
266
+
267
+		return;
268
+	}
269
+
270
+	/**
271
+	 * Sets the ORDER BY directive executed in SQL and returns a NEW ResultIterator.
272
+	 *
273
+	 * For instance:
274
+	 *
275
+	 *  $resultSet = $resultSet->withOrder('label ASC, status DESC');
276
+	 *
277
+	 * **Important:** TDBM does its best to protect you from SQL injection. In particular, it will only allow column names in the "ORDER BY" clause. This means you are safe to pass input from the user directly in the ORDER BY parameter.
278
+	 * If you want to pass an expression to the ORDER BY clause, you will need to tell TDBM to stop checking for SQL injections. You do this by passing a `UncheckedOrderBy` object as a parameter:
279
+	 *
280
+	 *  $resultSet->withOrder(new UncheckedOrderBy('RAND()'))
281
+	 *
282
+	 * @param string|UncheckedOrderBy|null $orderBy
283
+	 *
284
+	 * @return ResultIterator
285
+	 */
286
+	public function withOrder($orderBy) : ResultIterator
287
+	{
288
+		$clone = clone $this;
289
+		$clone->queryFactory = clone $this->queryFactory;
290
+		$clone->queryFactory->sort($orderBy);
291
+		$clone->innerResultIterator = null;
292
+
293
+		return $clone;
294
+	}
295
+
296
+	/**
297
+	 * Sets new parameters for the SQL query and returns a NEW ResultIterator.
298
+	 *
299
+	 * For instance:
300
+	 *
301
+	 *  $resultSet = $resultSet->withParameters('label ASC, status DESC');
302
+	 *
303
+	 * @param string|UncheckedOrderBy|null $orderBy
304
+	 *
305
+	 * @return ResultIterator
306
+	 */
307
+	public function withParameters(array $parameters) : ResultIterator
308
+	{
309
+		$clone = clone $this;
310
+		$clone->parameters = $parameters;
311
+		$clone->innerResultIterator = null;
312
+		$clone->totalCount = null;
313
+
314
+		return $clone;
315
+	}
316 316
 }
Please login to merge, or discard this patch.
Doc Comments   +4 added lines, -1 removed lines patch added patch discarded remove patch
@@ -62,6 +62,10 @@  discard block
 block discarded – undo
62 62
 
63 63
     private $logger;
64 64
 
65
+    /**
66
+     * @param null|string $className
67
+     * @param integer $mode
68
+     */
65 69
     public function __construct(QueryFactory $queryFactory, array $parameters, $objectStorage, $className, TDBMService $tdbmService, MagicQuery $magicQuery, $mode, LoggerInterface $logger)
66 70
     {
67 71
         if ($mode !== null && $mode !== TDBMService::MODE_CURSOR && $mode !== TDBMService::MODE_ARRAY) {
@@ -300,7 +304,6 @@  discard block
 block discarded – undo
300 304
      *
301 305
      *  $resultSet = $resultSet->withParameters('label ASC, status DESC');
302 306
      *
303
-     * @param string|UncheckedOrderBy|null $orderBy
304 307
      *
305 308
      * @return ResultIterator
306 309
      */
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/AlterableResultIterator.php 2 patches
Doc Comments   +4 added lines, -3 removed lines patch added patch discarded remove patch
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
     /**
69 69
      * Adds an additional object to the result set (if not already available).
70 70
      *
71
-     * @param $object
71
+     * @param AbstractTDBMObject $object
72 72
      */
73 73
     public function add($object)
74 74
     {
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
     /**
86 86
      * Removes an object from the result set.
87 87
      *
88
-     * @param $object
88
+     * @param AbstractTDBMObject $object
89 89
      */
90 90
     public function remove($object)
91 91
     {
@@ -205,6 +205,7 @@  discard block
 block discarded – undo
205 205
 
206 206
     /**
207 207
      * @param int $offset
208
+     * @param integer $limit
208 209
      *
209 210
      * @return \Porpaginas\Page
210 211
      */
@@ -227,7 +228,7 @@  discard block
 block discarded – undo
227 228
     /**
228 229
      * Return an iterator over all results of the paginatable.
229 230
      *
230
-     * @return Iterator
231
+     * @return \Iterator
231 232
      */
232 233
     public function getIterator()
233 234
     {
Please login to merge, or discard this patch.
Indentation   +245 added lines, -245 removed lines patch added patch discarded remove patch
@@ -14,274 +14,274 @@
 block discarded – undo
14 14
  */
15 15
 class AlterableResultIterator implements Result, \ArrayAccess, \JsonSerializable
16 16
 {
17
-    /**
18
-     * @var \Iterator|null
19
-     */
20
-    private $resultIterator;
17
+	/**
18
+	 * @var \Iterator|null
19
+	 */
20
+	private $resultIterator;
21 21
 
22
-    /**
23
-     * Key: the object to alter in the result set.
24
-     * Value: "add" => the object will be added to the resultset (if it is not found in it)
25
-     *        "delete" => the object will be removed from the resultset (if found).
26
-     *
27
-     * @var \SplObjectStorage
28
-     */
29
-    private $alterations;
22
+	/**
23
+	 * Key: the object to alter in the result set.
24
+	 * Value: "add" => the object will be added to the resultset (if it is not found in it)
25
+	 *        "delete" => the object will be removed from the resultset (if found).
26
+	 *
27
+	 * @var \SplObjectStorage
28
+	 */
29
+	private $alterations;
30 30
 
31
-    /**
32
-     * The result array from the result set.
33
-     *
34
-     * @var array|null
35
-     */
36
-    private $resultArray;
31
+	/**
32
+	 * The result array from the result set.
33
+	 *
34
+	 * @var array|null
35
+	 */
36
+	private $resultArray;
37 37
 
38
-    /**
39
-     * @param \Iterator|null $resultIterator
40
-     */
41
-    public function __construct(\Iterator $resultIterator = null)
42
-    {
43
-        $this->resultIterator = $resultIterator;
44
-        $this->alterations = new \SplObjectStorage();
45
-    }
38
+	/**
39
+	 * @param \Iterator|null $resultIterator
40
+	 */
41
+	public function __construct(\Iterator $resultIterator = null)
42
+	{
43
+		$this->resultIterator = $resultIterator;
44
+		$this->alterations = new \SplObjectStorage();
45
+	}
46 46
 
47
-    /**
48
-     * Sets a new iterator as the base iterator to be altered.
49
-     *
50
-     * @param \Iterator $resultIterator
51
-     */
52
-    public function setResultIterator(\Iterator $resultIterator)
53
-    {
54
-        $this->resultIterator = $resultIterator;
55
-        $this->resultArray = null;
56
-    }
47
+	/**
48
+	 * Sets a new iterator as the base iterator to be altered.
49
+	 *
50
+	 * @param \Iterator $resultIterator
51
+	 */
52
+	public function setResultIterator(\Iterator $resultIterator)
53
+	{
54
+		$this->resultIterator = $resultIterator;
55
+		$this->resultArray = null;
56
+	}
57 57
 
58
-    /**
59
-     * Returns the non altered result iterator (or null if none exist).
60
-     *
61
-     * @return \Iterator|null
62
-     */
63
-    public function getUnderlyingResultIterator()
64
-    {
65
-        return $this->resultIterator;
66
-    }
58
+	/**
59
+	 * Returns the non altered result iterator (or null if none exist).
60
+	 *
61
+	 * @return \Iterator|null
62
+	 */
63
+	public function getUnderlyingResultIterator()
64
+	{
65
+		return $this->resultIterator;
66
+	}
67 67
 
68
-    /**
69
-     * Adds an additional object to the result set (if not already available).
70
-     *
71
-     * @param $object
72
-     */
73
-    public function add($object)
74
-    {
75
-        $this->alterations->attach($object, 'add');
68
+	/**
69
+	 * Adds an additional object to the result set (if not already available).
70
+	 *
71
+	 * @param $object
72
+	 */
73
+	public function add($object)
74
+	{
75
+		$this->alterations->attach($object, 'add');
76 76
 
77
-        if ($this->resultArray !== null) {
78
-            $foundKey = array_search($object, $this->resultArray, true);
79
-            if ($foundKey === false) {
80
-                $this->resultArray[] = $object;
81
-            }
82
-        }
83
-    }
77
+		if ($this->resultArray !== null) {
78
+			$foundKey = array_search($object, $this->resultArray, true);
79
+			if ($foundKey === false) {
80
+				$this->resultArray[] = $object;
81
+			}
82
+		}
83
+	}
84 84
 
85
-    /**
86
-     * Removes an object from the result set.
87
-     *
88
-     * @param $object
89
-     */
90
-    public function remove($object)
91
-    {
92
-        $this->alterations->attach($object, 'delete');
85
+	/**
86
+	 * Removes an object from the result set.
87
+	 *
88
+	 * @param $object
89
+	 */
90
+	public function remove($object)
91
+	{
92
+		$this->alterations->attach($object, 'delete');
93 93
 
94
-        if ($this->resultArray !== null) {
95
-            $foundKey = array_search($object, $this->resultArray, true);
96
-            if ($foundKey !== false) {
97
-                unset($this->resultArray[$foundKey]);
98
-            }
99
-        }
100
-    }
94
+		if ($this->resultArray !== null) {
95
+			$foundKey = array_search($object, $this->resultArray, true);
96
+			if ($foundKey !== false) {
97
+				unset($this->resultArray[$foundKey]);
98
+			}
99
+		}
100
+	}
101 101
 
102
-    /**
103
-     * Casts the result set to a PHP array.
104
-     *
105
-     * @return array
106
-     */
107
-    public function toArray()
108
-    {
109
-        if ($this->resultArray === null) {
110
-            if ($this->resultIterator !== null) {
111
-                $this->resultArray = iterator_to_array($this->resultIterator);
112
-            } else {
113
-                $this->resultArray = [];
114
-            }
102
+	/**
103
+	 * Casts the result set to a PHP array.
104
+	 *
105
+	 * @return array
106
+	 */
107
+	public function toArray()
108
+	{
109
+		if ($this->resultArray === null) {
110
+			if ($this->resultIterator !== null) {
111
+				$this->resultArray = iterator_to_array($this->resultIterator);
112
+			} else {
113
+				$this->resultArray = [];
114
+			}
115 115
 
116
-            foreach ($this->alterations as $obj) {
117
-                $action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL
116
+			foreach ($this->alterations as $obj) {
117
+				$action = $this->alterations->getInfo(); // return, if exists, associated with cur. obj. data; else NULL
118 118
 
119
-                $foundKey = array_search($obj, $this->resultArray, true);
119
+				$foundKey = array_search($obj, $this->resultArray, true);
120 120
 
121
-                if ($action === 'add' && $foundKey === false) {
122
-                    $this->resultArray[] = $obj;
123
-                } elseif ($action === 'delete' && $foundKey !== false) {
124
-                    unset($this->resultArray[$foundKey]);
125
-                }
126
-            }
127
-        }
121
+				if ($action === 'add' && $foundKey === false) {
122
+					$this->resultArray[] = $obj;
123
+				} elseif ($action === 'delete' && $foundKey !== false) {
124
+					unset($this->resultArray[$foundKey]);
125
+				}
126
+			}
127
+		}
128 128
 
129
-        return array_values($this->resultArray);
130
-    }
129
+		return array_values($this->resultArray);
130
+	}
131 131
 
132
-    /**
133
-     * Whether a offset exists.
134
-     *
135
-     * @link http://php.net/manual/en/arrayaccess.offsetexists.php
136
-     *
137
-     * @param mixed $offset <p>
138
-     *                      An offset to check for.
139
-     *                      </p>
140
-     *
141
-     * @return bool true on success or false on failure.
142
-     *              </p>
143
-     *              <p>
144
-     *              The return value will be casted to boolean if non-boolean was returned
145
-     *
146
-     * @since 5.0.0
147
-     */
148
-    public function offsetExists($offset)
149
-    {
150
-        return isset($this->toArray()[$offset]);
151
-    }
132
+	/**
133
+	 * Whether a offset exists.
134
+	 *
135
+	 * @link http://php.net/manual/en/arrayaccess.offsetexists.php
136
+	 *
137
+	 * @param mixed $offset <p>
138
+	 *                      An offset to check for.
139
+	 *                      </p>
140
+	 *
141
+	 * @return bool true on success or false on failure.
142
+	 *              </p>
143
+	 *              <p>
144
+	 *              The return value will be casted to boolean if non-boolean was returned
145
+	 *
146
+	 * @since 5.0.0
147
+	 */
148
+	public function offsetExists($offset)
149
+	{
150
+		return isset($this->toArray()[$offset]);
151
+	}
152 152
 
153
-    /**
154
-     * Offset to retrieve.
155
-     *
156
-     * @link http://php.net/manual/en/arrayaccess.offsetget.php
157
-     *
158
-     * @param mixed $offset <p>
159
-     *                      The offset to retrieve.
160
-     *                      </p>
161
-     *
162
-     * @return mixed Can return all value types
163
-     *
164
-     * @since 5.0.0
165
-     */
166
-    public function offsetGet($offset)
167
-    {
168
-        return $this->toArray()[$offset];
169
-    }
153
+	/**
154
+	 * Offset to retrieve.
155
+	 *
156
+	 * @link http://php.net/manual/en/arrayaccess.offsetget.php
157
+	 *
158
+	 * @param mixed $offset <p>
159
+	 *                      The offset to retrieve.
160
+	 *                      </p>
161
+	 *
162
+	 * @return mixed Can return all value types
163
+	 *
164
+	 * @since 5.0.0
165
+	 */
166
+	public function offsetGet($offset)
167
+	{
168
+		return $this->toArray()[$offset];
169
+	}
170 170
 
171
-    /**
172
-     * Offset to set.
173
-     *
174
-     * @link http://php.net/manual/en/arrayaccess.offsetset.php
175
-     *
176
-     * @param mixed $offset <p>
177
-     *                      The offset to assign the value to.
178
-     *                      </p>
179
-     * @param mixed $value  <p>
180
-     *                      The value to set.
181
-     *                      </p>
182
-     *
183
-     * @since 5.0.0
184
-     */
185
-    public function offsetSet($offset, $value)
186
-    {
187
-        throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.');
188
-    }
171
+	/**
172
+	 * Offset to set.
173
+	 *
174
+	 * @link http://php.net/manual/en/arrayaccess.offsetset.php
175
+	 *
176
+	 * @param mixed $offset <p>
177
+	 *                      The offset to assign the value to.
178
+	 *                      </p>
179
+	 * @param mixed $value  <p>
180
+	 *                      The value to set.
181
+	 *                      </p>
182
+	 *
183
+	 * @since 5.0.0
184
+	 */
185
+	public function offsetSet($offset, $value)
186
+	{
187
+		throw new TDBMInvalidOperationException('You can set values in a TDBM result set, even in an alterable one. Use the add method instead.');
188
+	}
189 189
 
190
-    /**
191
-     * Offset to unset.
192
-     *
193
-     * @link http://php.net/manual/en/arrayaccess.offsetunset.php
194
-     *
195
-     * @param mixed $offset <p>
196
-     *                      The offset to unset.
197
-     *                      </p>
198
-     *
199
-     * @since 5.0.0
200
-     */
201
-    public function offsetUnset($offset)
202
-    {
203
-        throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.');
204
-    }
190
+	/**
191
+	 * Offset to unset.
192
+	 *
193
+	 * @link http://php.net/manual/en/arrayaccess.offsetunset.php
194
+	 *
195
+	 * @param mixed $offset <p>
196
+	 *                      The offset to unset.
197
+	 *                      </p>
198
+	 *
199
+	 * @since 5.0.0
200
+	 */
201
+	public function offsetUnset($offset)
202
+	{
203
+		throw new TDBMInvalidOperationException('You can unset values in a TDBM result set, even in an alterable one. Use the delete method instead.');
204
+	}
205 205
 
206
-    /**
207
-     * @param int $offset
208
-     *
209
-     * @return \Porpaginas\Page
210
-     */
211
-    public function take($offset, $limit)
212
-    {
213
-        // TODO: replace this with a class implementing the map method.
214
-        return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray()));
215
-    }
206
+	/**
207
+	 * @param int $offset
208
+	 *
209
+	 * @return \Porpaginas\Page
210
+	 */
211
+	public function take($offset, $limit)
212
+	{
213
+		// TODO: replace this with a class implementing the map method.
214
+		return new ArrayPage(array_slice($this->toArray(), $offset, $limit), $offset, $limit, count($this->toArray()));
215
+	}
216 216
 
217
-    /**
218
-     * Return the number of all results in the paginatable.
219
-     *
220
-     * @return int
221
-     */
222
-    public function count()
223
-    {
224
-        return count($this->toArray());
225
-    }
217
+	/**
218
+	 * Return the number of all results in the paginatable.
219
+	 *
220
+	 * @return int
221
+	 */
222
+	public function count()
223
+	{
224
+		return count($this->toArray());
225
+	}
226 226
 
227
-    /**
228
-     * Return an iterator over all results of the paginatable.
229
-     *
230
-     * @return Iterator
231
-     */
232
-    public function getIterator()
233
-    {
234
-        if ($this->alterations->count() === 0) {
235
-            if ($this->resultIterator !== null) {
236
-                return $this->resultIterator;
237
-            } else {
238
-                return new \ArrayIterator([]);
239
-            }
240
-        } else {
241
-            return new \ArrayIterator($this->toArray());
242
-        }
243
-    }
227
+	/**
228
+	 * Return an iterator over all results of the paginatable.
229
+	 *
230
+	 * @return Iterator
231
+	 */
232
+	public function getIterator()
233
+	{
234
+		if ($this->alterations->count() === 0) {
235
+			if ($this->resultIterator !== null) {
236
+				return $this->resultIterator;
237
+			} else {
238
+				return new \ArrayIterator([]);
239
+			}
240
+		} else {
241
+			return new \ArrayIterator($this->toArray());
242
+		}
243
+	}
244 244
 
245
-    /**
246
-     * Specify data which should be serialized to JSON.
247
-     *
248
-     * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
249
-     *
250
-     * @return mixed data which can be serialized by <b>json_encode</b>,
251
-     *               which is a value of any type other than a resource
252
-     *
253
-     * @since 5.4.0
254
-     */
255
-    public function jsonSerialize()
256
-    {
257
-        return $this->toArray();
258
-    }
245
+	/**
246
+	 * Specify data which should be serialized to JSON.
247
+	 *
248
+	 * @link http://php.net/manual/en/jsonserializable.jsonserialize.php
249
+	 *
250
+	 * @return mixed data which can be serialized by <b>json_encode</b>,
251
+	 *               which is a value of any type other than a resource
252
+	 *
253
+	 * @since 5.4.0
254
+	 */
255
+	public function jsonSerialize()
256
+	{
257
+		return $this->toArray();
258
+	}
259 259
 
260
-    /**
261
-     * Returns only one value (the first) of the result set.
262
-     * Returns null if no value exists.
263
-     *
264
-     * @return mixed|null
265
-     */
266
-    public function first()
267
-    {
268
-        $page = $this->take(0, 1);
269
-        foreach ($page as $bean) {
270
-            return $bean;
271
-        }
260
+	/**
261
+	 * Returns only one value (the first) of the result set.
262
+	 * Returns null if no value exists.
263
+	 *
264
+	 * @return mixed|null
265
+	 */
266
+	public function first()
267
+	{
268
+		$page = $this->take(0, 1);
269
+		foreach ($page as $bean) {
270
+			return $bean;
271
+		}
272 272
 
273
-        return;
274
-    }
273
+		return;
274
+	}
275 275
 
276
-    /**
277
-     * Returns a new iterator mapping any call using the $callable function.
278
-     *
279
-     * @param callable $callable
280
-     *
281
-     * @return MapIterator
282
-     */
283
-    public function map(callable $callable)
284
-    {
285
-        return new MapIterator($this->getIterator(), $callable);
286
-    }
276
+	/**
277
+	 * Returns a new iterator mapping any call using the $callable function.
278
+	 *
279
+	 * @param callable $callable
280
+	 *
281
+	 * @return MapIterator
282
+	 */
283
+	public function map(callable $callable)
284
+	{
285
+		return new MapIterator($this->getIterator(), $callable);
286
+	}
287 287
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/MapIterator.php 2 patches
Indentation   +87 added lines, -87 removed lines patch added patch discarded remove patch
@@ -11,91 +11,91 @@
 block discarded – undo
11 11
  */
12 12
 class MapIterator implements Iterator, \JsonSerializable
13 13
 {
14
-    /**
15
-     * @var Iterator
16
-     */
17
-    protected $iterator;
18
-
19
-    /**
20
-     * @var callable Modifies the current item in iterator
21
-     */
22
-    protected $callable;
23
-
24
-    /**
25
-     * @param $iterator Iterator|array
26
-     * @param $callable callable This can have two parameters
27
-     *
28
-     * @throws TDBMException
29
-     */
30
-    public function __construct($iterator, callable $callable)
31
-    {
32
-        if (is_array($iterator)) {
33
-            $this->iterator = new \ArrayIterator($iterator);
34
-        } elseif (!($iterator instanceof Iterator)) {
35
-            throw new TDBMException('$iterator parameter must be an instance of Iterator');
36
-        } else {
37
-            $this->iterator = $iterator;
38
-        }
39
-
40
-        if ($callable instanceof \Closure) {
41
-            // make sure there's one argument
42
-            $reflection = new \ReflectionObject($callable);
43
-            if ($reflection->hasMethod('__invoke')) {
44
-                $method = $reflection->getMethod('__invoke');
45
-                if ($method->getNumberOfParameters() !== 1) {
46
-                    throw new TDBMException('$callable must accept one and only one parameter.');
47
-                }
48
-            }
49
-        }
50
-
51
-        $this->callable = $callable;
52
-    }
53
-
54
-    /**
55
-     * Alters the current item with $this->callable and returns a new item.
56
-     * Be careful with your types as we can't do static type checking here!
57
-     *
58
-     * @return mixed
59
-     */
60
-    public function current()
61
-    {
62
-        $callable = $this->callable;
63
-
64
-        return $callable($this->iterator->current());
65
-    }
66
-
67
-    public function next()
68
-    {
69
-        $this->iterator->next();
70
-    }
71
-
72
-    public function key()
73
-    {
74
-        return $this->iterator->key();
75
-    }
76
-
77
-    public function valid()
78
-    {
79
-        return $this->iterator->valid();
80
-    }
81
-
82
-    public function rewind()
83
-    {
84
-        $this->iterator->rewind();
85
-    }
86
-
87
-    /**
88
-     * Casts the iterator to a PHP array.
89
-     *
90
-     * @return array
91
-     */
92
-    public function toArray()
93
-    {
94
-        return iterator_to_array($this);
95
-    }
96
-
97
-    public function jsonSerialize()
98
-    {
99
-        return $this->toArray();
100
-    }
14
+	/**
15
+	 * @var Iterator
16
+	 */
17
+	protected $iterator;
18
+
19
+	/**
20
+	 * @var callable Modifies the current item in iterator
21
+	 */
22
+	protected $callable;
23
+
24
+	/**
25
+	 * @param $iterator Iterator|array
26
+	 * @param $callable callable This can have two parameters
27
+	 *
28
+	 * @throws TDBMException
29
+	 */
30
+	public function __construct($iterator, callable $callable)
31
+	{
32
+		if (is_array($iterator)) {
33
+			$this->iterator = new \ArrayIterator($iterator);
34
+		} elseif (!($iterator instanceof Iterator)) {
35
+			throw new TDBMException('$iterator parameter must be an instance of Iterator');
36
+		} else {
37
+			$this->iterator = $iterator;
38
+		}
39
+
40
+		if ($callable instanceof \Closure) {
41
+			// make sure there's one argument
42
+			$reflection = new \ReflectionObject($callable);
43
+			if ($reflection->hasMethod('__invoke')) {
44
+				$method = $reflection->getMethod('__invoke');
45
+				if ($method->getNumberOfParameters() !== 1) {
46
+					throw new TDBMException('$callable must accept one and only one parameter.');
47
+				}
48
+			}
49
+		}
50
+
51
+		$this->callable = $callable;
52
+	}
53
+
54
+	/**
55
+	 * Alters the current item with $this->callable and returns a new item.
56
+	 * Be careful with your types as we can't do static type checking here!
57
+	 *
58
+	 * @return mixed
59
+	 */
60
+	public function current()
61
+	{
62
+		$callable = $this->callable;
63
+
64
+		return $callable($this->iterator->current());
65
+	}
66
+
67
+	public function next()
68
+	{
69
+		$this->iterator->next();
70
+	}
71
+
72
+	public function key()
73
+	{
74
+		return $this->iterator->key();
75
+	}
76
+
77
+	public function valid()
78
+	{
79
+		return $this->iterator->valid();
80
+	}
81
+
82
+	public function rewind()
83
+	{
84
+		$this->iterator->rewind();
85
+	}
86
+
87
+	/**
88
+	 * Casts the iterator to a PHP array.
89
+	 *
90
+	 * @return array
91
+	 */
92
+	public function toArray()
93
+	{
94
+		return iterator_to_array($this);
95
+	}
96
+
97
+	public function jsonSerialize()
98
+	{
99
+		return $this->toArray();
100
+	}
101 101
 }
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -1,6 +1,6 @@
 block discarded – undo
1 1
 <?php
2 2
 
3
-declare(strict_types=1);
3
+declare(strict_types = 1);
4 4
 
5 5
 namespace Mouf\Database\TDBM;
6 6
 
Please login to merge, or discard this patch.