@@ -11,16 +11,21 @@ discard block |
||
| 11 | 11 | foreach ($columns as $column) { |
| 12 | 12 | $parts = explode('.', $column->Field_name); |
| 13 | 13 | $name = $this->quote(end($parts)); |
| 14 | - if ($column->Min_value === null && $column->Max_value === null) $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
| 15 | - else { |
|
| 14 | + if ($column->Min_value === null && $column->Max_value === null) { |
|
| 15 | + $this->pdo->query('ALTER TABLE ' . $this->quote($table) . ' DROP COLUMN ' . $name); |
|
| 16 | + } else { |
|
| 16 | 17 | $type = $column->Optimal_fieldtype; |
| 17 | 18 | if ($column->Max_length < 11) { |
| 18 | 19 | //Check for dates |
| 19 | 20 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d %H:%i:s\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
| 20 | - if ($count == 0) $type = 'DATETIME'; |
|
| 21 | + if ($count == 0) { |
|
| 22 | + $type = 'DATETIME'; |
|
| 23 | + } |
|
| 21 | 24 | |
| 22 | 25 | $count = $this->pdo->query('SELECT count(*) as `count` FROM ' . $this->quote($table) . ' WHERE STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') IS NULL OR STR_TO_DATE(' . $name . ',\'%Y-%m-%d\') != ' . $name . ' LIMIT 1')->fetch(\PDO::FETCH_OBJ)->count; |
| 23 | - if ($count == 0) $type = 'DATE'; |
|
| 26 | + if ($count == 0) { |
|
| 27 | + $type = 'DATE'; |
|
| 28 | + } |
|
| 24 | 29 | } |
| 25 | 30 | |
| 26 | 31 | //If it's text, work out if it would be better to be something else |
@@ -30,8 +35,7 @@ discard block |
||
| 30 | 35 | if ($count == 0) { |
| 31 | 36 | $type = 'INT(11)'; |
| 32 | 37 | $runAgain = true; |
| 33 | - } |
|
| 34 | - else { |
|
| 38 | + } else { |
|
| 35 | 39 | //See if it's decimal |
| 36 | 40 | $count = $this->pdo->query('SELECT count(*) FROM ' . $table . ' WHERE concat(\'\', ' . $name . ' * 1) != ' . $name . ')')->fetch(\PDO::FETCH_OBJ)->count; |
| 37 | 41 | if ($count == 0) { |
@@ -45,6 +49,8 @@ discard block |
||
| 45 | 49 | } |
| 46 | 50 | } |
| 47 | 51 | //Sometimes a second pass is needed, if a column has gone from varchar -> int(11) a better int type may be needed |
| 48 | - if ($runAgain) $this->optimiseColumns($table); |
|
| 52 | + if ($runAgain) { |
|
| 53 | + $this->optimiseColumns($table); |
|
| 54 | + } |
|
| 49 | 55 | } |
| 50 | 56 | } |
| 51 | 57 | \ No newline at end of file |
@@ -12,7 +12,9 @@ |
||
| 12 | 12 | } |
| 13 | 13 | |
| 14 | 14 | public function current() { |
| 15 | - if ($this->intermediateName) return $this->iterator->current()->{$this->intermediateName}; |
|
| 15 | + if ($this->intermediateName) { |
|
| 16 | + return $this->iterator->current()->{$this->intermediateName}; |
|
| 17 | + } |
|
| 16 | 18 | return $this->iterator->current(); |
| 17 | 19 | } |
| 18 | 20 | |
@@ -9,7 +9,9 @@ discard block |
||
| 9 | 9 | private $siblings = []; |
| 10 | 10 | |
| 11 | 11 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $criteria = []) { |
| 12 | - if ($criteria) $mapper = $mapper->filter($this->criteira); |
|
| 12 | + if ($criteria) { |
|
| 13 | + $mapper = $mapper->filter($this->criteira); |
|
| 14 | + } |
|
| 13 | 15 | $this->mapper = $mapper; |
| 14 | 16 | $this->parentField = $parentField; |
| 15 | 17 | $this->localField = $localField; |
@@ -29,7 +31,9 @@ discard block |
||
| 29 | 31 | private function lazyLoad() { |
| 30 | 32 | if (!isset($this->data)) { |
| 31 | 33 | |
| 32 | - if ($this->parentObject == null) throw new \Exception('Error, no object set'); |
|
| 34 | + if ($this->parentObject == null) { |
|
| 35 | + throw new \Exception('Error, no object set'); |
|
| 36 | + } |
|
| 33 | 37 | |
| 34 | 38 | $this->eagerLoad(); |
| 35 | 39 | |
@@ -41,8 +45,10 @@ discard block |
||
| 41 | 45 | $recordsToLoad = []; |
| 42 | 46 | //Get a list of records by FK to eager load |
| 43 | 47 | foreach ($this->siblings as $sibling) { |
| 44 | - if ($sibling->parentField === $this->parentField) // Ensure that it is only loading records from the same type of relation |
|
| 48 | + if ($sibling->parentField === $this->parentField) { |
|
| 49 | + // Ensure that it is only loading records from the same type of relation |
|
| 45 | 50 | $recordsToLoad[] = $sibling->parentObject->{$sibling->parentField}; |
| 51 | + } |
|
| 46 | 52 | } |
| 47 | 53 | |
| 48 | 54 | $recordsToLoad = array_unique($recordsToLoad); |
@@ -59,7 +65,9 @@ discard block |
||
| 59 | 65 | } |
| 60 | 66 | |
| 61 | 67 | foreach ($this->siblings as $sibling) { |
| 62 | - if (isset($cache[$sibling->parentObject->{$this->parentField}])) $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
| 68 | + if (isset($cache[$sibling->parentObject->{$this->parentField}])) { |
|
| 69 | + $sibling->data = $cache[$sibling->parentObject->{$this->parentField}]; |
|
| 70 | + } |
|
| 63 | 71 | } |
| 64 | 72 | /* |
| 65 | 73 | foreach ($this->siblings as $sibling) { |
@@ -70,13 +78,18 @@ discard block |
||
| 70 | 78 | } |
| 71 | 79 | |
| 72 | 80 | public function __call($func, array $args = []) { |
| 73 | - if ($this->lazyLoad() == null) return ''; |
|
| 81 | + if ($this->lazyLoad() == null) { |
|
| 82 | + return ''; |
|
| 83 | + } |
|
| 74 | 84 | return call_user_func_array([$this->lazyLoad(), $func], $args); |
| 75 | 85 | } |
| 76 | 86 | |
| 77 | 87 | public function __get($name) { |
| 78 | - if ($this->lazyLoad()) return $this->lazyLoad()->$name; |
|
| 79 | - else return null; |
|
| 88 | + if ($this->lazyLoad()) { |
|
| 89 | + return $this->lazyLoad()->$name; |
|
| 90 | + } else { |
|
| 91 | + return null; |
|
| 92 | + } |
|
| 80 | 93 | } |
| 81 | 94 | |
| 82 | 95 | public function __isset($name) { |
@@ -32,8 +32,12 @@ discard block |
||
| 32 | 32 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
| 33 | 33 | $this->results = $data; |
| 34 | 34 | $this->object = $parentObject; |
| 35 | - if (empty($parentObject->{$relatedField})) return; |
|
| 36 | - foreach ($data as $dt) $this[] = $dt; |
|
| 35 | + if (empty($parentObject->{$relatedField})) { |
|
| 36 | + return; |
|
| 37 | + } |
|
| 38 | + foreach ($data as $dt) { |
|
| 39 | + $this[] = $dt; |
|
| 40 | + } |
|
| 37 | 41 | } |
| 38 | 42 | |
| 39 | 43 | //bit hacky, breaking encapsulation, but simplest way to work out the info for the other side of the many:many relationship. |
@@ -88,8 +92,9 @@ discard block |
||
| 88 | 92 | |
| 89 | 93 | public function offsetSet($name, $value) { |
| 90 | 94 | list($relatedField, $valueField, $mapper) = $this->getOtherFieldNameInfo(); |
| 91 | - if ($this->autoTraverse) $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
| 92 | - else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
| 95 | + if ($this->autoTraverse) { |
|
| 96 | + $this->offsetSetAutotraverse($value, $relatedField, $valueField); |
|
| 97 | + } else if ($this->doUpdateInterMapper($value, $relatedField, $valueField)) { |
|
| 93 | 98 | $record = $value; |
| 94 | 99 | $record->{$this->parentField} = $value->{$this->intermediateName}->{$this->localField}; |
| 95 | 100 | $record->$valueField = $this->object->{$relatedField}; |
@@ -6,7 +6,9 @@ discard block |
||
| 6 | 6 | private $localField; |
| 7 | 7 | |
| 8 | 8 | public function __construct(\Maphper\Maphper $mapper, $parentField, $localField, array $critiera = []) { |
| 9 | - if ($critiera) $mapper->filter($critiera); |
|
| 9 | + if ($critiera) { |
|
| 10 | + $mapper->filter($critiera); |
|
| 11 | + } |
|
| 10 | 12 | $this->mapper = $mapper; |
| 11 | 13 | $this->parentField = $parentField; |
| 12 | 14 | $this->localField = $localField; |
@@ -14,17 +16,24 @@ discard block |
||
| 14 | 16 | |
| 15 | 17 | |
| 16 | 18 | public function getData($parentObject) { |
| 17 | - if (!isset($parentObject->{$this->parentField})) $mapper = $this->mapper; |
|
| 18 | - else $mapper = $this->mapper->filter([$this->localField => $parentObject->{$this->parentField}]); |
|
| 19 | + if (!isset($parentObject->{$this->parentField})) { |
|
| 20 | + $mapper = $this->mapper; |
|
| 21 | + } else { |
|
| 22 | + $mapper = $this->mapper->filter([$this->localField => $parentObject->{$this->parentField}]); |
|
| 23 | + } |
|
| 19 | 24 | |
| 20 | 25 | return $mapper; |
| 21 | 26 | } |
| 22 | 27 | |
| 23 | 28 | |
| 24 | 29 | public function overwrite($key, &$mapper) { |
| 25 | - if (!isset($key->{$this->parentField})) return false; |
|
| 30 | + if (!isset($key->{$this->parentField})) { |
|
| 31 | + return false; |
|
| 32 | + } |
|
| 26 | 33 | foreach ($mapper as $k => $val) { |
| 27 | - if (!empty($val->{$this->localField}) && $val->{$this->localField} != $key->{$this->parentField}) continue; |
|
| 34 | + if (!empty($val->{$this->localField}) && $val->{$this->localField} != $key->{$this->parentField}) { |
|
| 35 | + continue; |
|
| 36 | + } |
|
| 28 | 37 | $val->{$this->localField} = $key->{$this->parentField}; |
| 29 | 38 | $this->mapper[] = $val; |
| 30 | 39 | } |
@@ -21,8 +21,11 @@ |
||
| 21 | 21 | $notText = ' NOT'; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - if (count($inSql) == 0) return ['args' => [], 'sql' => '']; |
|
| 25 | - else $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
| 24 | + if (count($inSql) == 0) { |
|
| 25 | + return ['args' => [], 'sql' => '']; |
|
| 26 | + } else { |
|
| 27 | + $sql = [$key . $notText . ' IN ( ' . implode(', ', $inSql) . ')']; |
|
| 28 | + } |
|
| 26 | 29 | |
| 27 | 30 | return ['args' => $args, 'sql' => $sql]; |
| 28 | 31 | } |
@@ -60,7 +60,9 @@ discard block |
||
| 60 | 60 | |
| 61 | 61 | $siblings = new \ArrayObject(); |
| 62 | 62 | |
| 63 | - foreach ($results as &$result) $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 63 | + foreach ($results as &$result) { |
|
| 64 | + $result = $this->entity->create($result, $this->relations, $siblings); |
|
| 65 | + } |
|
| 64 | 66 | |
| 65 | 67 | return $results; |
| 66 | 68 | } |
@@ -77,13 +79,17 @@ discard block |
||
| 77 | 79 | private function processFilters($value) { |
| 78 | 80 | //When saving to a mapper with filters, write the filters back into the object being stored |
| 79 | 81 | foreach ($this->settings['filter'] as $key => $filterValue) { |
| 80 | - if (empty($value->$key) && !is_array($filterValue)) $value->$key = $filterValue; |
|
| 82 | + if (empty($value->$key) && !is_array($filterValue)) { |
|
| 83 | + $value->$key = $filterValue; |
|
| 84 | + } |
|
| 81 | 85 | } |
| 82 | 86 | return $value; |
| 83 | 87 | } |
| 84 | 88 | |
| 85 | 89 | public function offsetSet($offset, $valueObj) { |
| 86 | - if ($valueObj instanceof \Maphper\Relation) throw new \Exception(); |
|
| 90 | + if ($valueObj instanceof \Maphper\Relation) { |
|
| 91 | + throw new \Exception(); |
|
| 92 | + } |
|
| 87 | 93 | |
| 88 | 94 | //Extract private properties from the object |
| 89 | 95 | $visibilityOverride = new \Maphper\Lib\VisibilityOverride($valueObj); |
@@ -91,7 +97,9 @@ discard block |
||
| 91 | 97 | |
| 92 | 98 | $value = $this->processFilters($value); |
| 93 | 99 | $pk = $this->dataSource->getPrimaryKey(); |
| 94 | - if ($offset !== null) $value->{$pk[0]} = $offset; |
|
| 100 | + if ($offset !== null) { |
|
| 101 | + $value->{$pk[0]} = $offset; |
|
| 102 | + } |
|
| 95 | 103 | $valueCopy = $this->removeRelations(clone $value, $pk); |
| 96 | 104 | $value = $this->entity->wrap($this->relations, $value); |
| 97 | 105 | $this->dataSource->save($value); |
@@ -101,15 +109,20 @@ discard block |
||
| 101 | 109 | } |
| 102 | 110 | |
| 103 | 111 | private function removeRelations($obj, $pk) { // Prevent saving ManyMany twice except when pk isn't initially set |
| 104 | - foreach ($this->relations as $name => $relation) |
|
| 105 | - if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
| 112 | + foreach ($this->relations as $name => $relation) { |
|
| 113 | + if ($relation instanceOf \Maphper\Relation\ManyMany && isset($obj->$name) && !empty($obj->{$pk[0]})) unset($obj->$name); |
|
| 114 | + } |
|
| 106 | 115 | |
| 107 | - if (empty($obj->{$pk[0]})) unset($obj->{$pk[0]}); |
|
| 116 | + if (empty($obj->{$pk[0]})) { |
|
| 117 | + unset($obj->{$pk[0]}); |
|
| 118 | + } |
|
| 108 | 119 | return $obj; |
| 109 | 120 | } |
| 110 | 121 | |
| 111 | 122 | public function offsetExists($offset) { |
| 112 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 123 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 124 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 125 | + } |
|
| 113 | 126 | if (!empty($this->settings['filter'])) { |
| 114 | 127 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 115 | 128 | return isset($data[0]); |
@@ -122,7 +135,9 @@ discard block |
||
| 122 | 135 | } |
| 123 | 136 | |
| 124 | 137 | public function offsetGet($offset) { |
| 125 | - if (count($this->dataSource->getPrimaryKey()) > 1) return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 138 | + if (count($this->dataSource->getPrimaryKey()) > 1) { |
|
| 139 | + return new MultiPk($this, $offset, $this->dataSource->getPrimaryKey()); |
|
| 140 | + } |
|
| 126 | 141 | if (!empty($this->settings['filter'])) { |
| 127 | 142 | $data = $this->dataSource->findByField(array_merge($this->settings['filter'], [$this->dataSource->getPrimaryKey()[0] => $offset])); |
| 128 | 143 | return $this->entity->create(isset($data[0]) ? $data[0] : null, $this->relations); |
@@ -133,11 +148,15 @@ discard block |
||
| 133 | 148 | public function __call($method, $args) { |
| 134 | 149 | if (array_key_exists($method, $this->settings)) { |
| 135 | 150 | $maphper = new Maphper($this->dataSource, $this->settings, $this->relations); |
| 136 | - if (is_array($maphper->settings[$method])) $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 137 | - else $maphper->settings[$method] = $args[0]; |
|
| 151 | + if (is_array($maphper->settings[$method])) { |
|
| 152 | + $maphper->settings[$method] = $args[0] + $maphper->settings[$method]; |
|
| 153 | + } else { |
|
| 154 | + $maphper->settings[$method] = $args[0]; |
|
| 155 | + } |
|
| 138 | 156 | return $maphper; |
| 157 | + } else { |
|
| 158 | + throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 139 | 159 | } |
| 140 | - else throw new \Exception('Method Maphper::' . $method . ' does not exist'); |
|
| 141 | 160 | } |
| 142 | 161 | |
| 143 | 162 | public function findAggregate($function, $field, $group = null) { |
@@ -25,8 +25,12 @@ discard block |
||
| 25 | 25 | list($columns, $order) = explode(' ', $options['order']); |
| 26 | 26 | usort($filteredArray, $this->getOrderFunction($order, $columns)); |
| 27 | 27 | } |
| 28 | - if (isset($options['offset'])) $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 29 | - if (isset($options['limit'])) $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 28 | + if (isset($options['offset'])) { |
|
| 29 | + $filteredArray = array_slice($filteredArray, $options['offset']); |
|
| 30 | + } |
|
| 31 | + if (isset($options['limit'])) { |
|
| 32 | + $filteredArray = array_slice($filteredArray, 0, $options['limit']); |
|
| 33 | + } |
|
| 30 | 34 | return $filteredArray; |
| 31 | 35 | } |
| 32 | 36 | |
@@ -40,23 +44,28 @@ discard block |
||
| 40 | 44 | |
| 41 | 45 | public function deleteByField(array $fields, array $options) { |
| 42 | 46 | foreach ($this->findByField($fields, $options) as $val) { |
| 43 | - if (count($this->id) > 1) $id = $this->getMultiPkSaveId($val); |
|
| 44 | - else $id = $val->{$this->id[0]}; |
|
| 47 | + if (count($this->id) > 1) { |
|
| 48 | + $id = $this->getMultiPkSaveId($val); |
|
| 49 | + } else { |
|
| 50 | + $id = $val->{$this->id[0]}; |
|
| 51 | + } |
|
| 45 | 52 | |
| 46 | 53 | unset($this->data[$id]); |
| 47 | 54 | } |
| 48 | 55 | } |
| 49 | 56 | |
| 50 | 57 | public function save($data) { |
| 51 | - if (count($this->id) > 1) return $this->saveMultiPk($data); |
|
| 52 | - else return $this->saveSinglePk($data); |
|
| 58 | + if (count($this->id) > 1) { |
|
| 59 | + return $this->saveMultiPk($data); |
|
| 60 | + } else { |
|
| 61 | + return $this->saveSinglePk($data); |
|
| 62 | + } |
|
| 53 | 63 | } |
| 54 | 64 | |
| 55 | 65 | private function saveSinglePk($data) { |
| 56 | 66 | if (isset($data->{$this->id[0]})) { |
| 57 | 67 | $id = $data->{$this->id[0]}; |
| 58 | - } |
|
| 59 | - else { |
|
| 68 | + } else { |
|
| 60 | 69 | $id = count($this->data); |
| 61 | 70 | $data->{$this->id[0]} = $id; |
| 62 | 71 | } |
@@ -90,12 +99,16 @@ discard block |
||
| 90 | 99 | if ($aColumn === $bColumn) { |
| 91 | 100 | $sortVal = 0; |
| 92 | 101 | continue; |
| 102 | + } else { |
|
| 103 | + $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 93 | 104 | } |
| 94 | - else $sortVal = ($aColumn < $bColumn) ? -1 : 1; |
|
| 95 | 105 | break; |
| 96 | 106 | } |
| 97 | - if ($order === 'desc') return -$sortVal; |
|
| 98 | - else return $sortVal; |
|
| 107 | + if ($order === 'desc') { |
|
| 108 | + return -$sortVal; |
|
| 109 | + } else { |
|
| 110 | + return $sortVal; |
|
| 111 | + } |
|
| 99 | 112 | }; |
| 100 | 113 | } |
| 101 | 114 | } |