From d687f029515b19aeea903551b00963ad50932bb1 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Wed, 10 May 2017 21:16:47 +0200 Subject: [PATCH 1/6] Add location type/docblock --- Datatypes/Location.php | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/Datatypes/Location.php b/Datatypes/Location.php index 45f9788cc..14148d1be 100644 --- a/Datatypes/Location.php +++ b/Datatypes/Location.php @@ -31,6 +31,14 @@ namespace phpOMS\Datatypes; class Location implements \JsonSerializable, \Serializable { + /** + * Location id + * + * @var int + * @since 1.0.0 + */ + private $id = 0; + /** * Zip or postal. * @@ -63,6 +71,14 @@ class Location implements \JsonSerializable, \Serializable */ private $address = ''; + /** + * Address type + * + * @var int + * @since 1.0.0 + */ + private $type = AddressType::HOME; + /** * State. * @@ -89,6 +105,33 @@ class Location implements \JsonSerializable, \Serializable { } + /** + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getId() : int + { + return $this->id; + } + + /** + * @return int + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + public function getType() : int + { + return $this->type; + } + + public function setType(int $type) /* : void */ + { + $this->type = $type; + } + /** * @return string * From 098fe4130a78a0ccb489fd63d3284367b06655a0 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 18 May 2017 19:54:53 +0200 Subject: [PATCH 2/6] Allow to pass data to renderer --- Views/ViewAbstract.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Views/ViewAbstract.php b/Views/ViewAbstract.php index 9c3a8c3e2..ffba85390 100644 --- a/Views/ViewAbstract.php +++ b/Views/ViewAbstract.php @@ -245,7 +245,7 @@ abstract class ViewAbstract implements \Serializable * @since 1.0.0 * @author Dennis Eichhorn */ - public function render() : string + public function render(...$data) : string { $path = __DIR__ . '/../..' . $this->template . '.tpl.php'; From f86d8d862236820a5aa4706cd05359788781cda5 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Mon, 29 May 2017 21:26:14 +0200 Subject: [PATCH 3/6] optimize isset --- DataStorage/Database/DataMapperAbstract.php | 2 +- Localization/L11nManager.php | 2 +- Stdlib/Graph/BinaryTree.php | 2 +- Stdlib/Map/MultiMap.php | 2 +- System/File/Ftp/FtpStorage.php | 2 +- Utils/Barcode/C25.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 67bd6ef66..9cab711d0 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -1843,7 +1843,7 @@ class DataMapperAbstract implements DataMapperInterface $query = static::getQuery(); $query->limit($limit); - if (isset($from) && isset($to) && !empty(static::getCreatedAt())) { + if (isset($from, $to) && !empty(static::getCreatedAt())) { $query->where(static::getCreatedAt(), '>=', $from); $query->where(static::getCreatedAt(), '<=', $to); } diff --git a/Localization/L11nManager.php b/Localization/L11nManager.php index 6ed90d66f..edff93711 100644 --- a/Localization/L11nManager.php +++ b/Localization/L11nManager.php @@ -151,7 +151,7 @@ class L11nManager { if (!isset($module) && isset($this->language[$language])) { return $this->language[$language]; - } elseif (isset($this->language[$language]) && isset($this->language[$language][$module])) { + } elseif (isset($this->language[$language], $this->language[$language][$module])) { return $this->language[$language][$module]; } else { return []; diff --git a/Stdlib/Graph/BinaryTree.php b/Stdlib/Graph/BinaryTree.php index b82df944a..a3dced390 100644 --- a/Stdlib/Graph/BinaryTree.php +++ b/Stdlib/Graph/BinaryTree.php @@ -217,7 +217,7 @@ class BinaryTree extends Tree $right2 = isset($node2) ? $this->getRight($node1) : $this->getRight($node2); // todo: compare values? true symmetry requires the values to be the same - if(isset($node1) && isset($node2)) { + if(isset($node1, $node2)) { return $this->isSymmetric($left1, $right2) && $this->isSymmetric($right1, $left2); } diff --git a/Stdlib/Map/MultiMap.php b/Stdlib/Map/MultiMap.php index 307a422cb..14051a3be 100644 --- a/Stdlib/Map/MultiMap.php +++ b/Stdlib/Map/MultiMap.php @@ -370,7 +370,7 @@ class MultiMap implements \Countable return false; } - if (isset($this->keys[$old]) && isset($this->keys[$new])) { + if (isset($this->keys[$old], $this->keys[$new])) { $this->keys[$old] = $this->keys[$new]; $this->garbageCollect(); diff --git a/System/File/Ftp/FtpStorage.php b/System/File/Ftp/FtpStorage.php index 6b7d11950..6656db956 100644 --- a/System/File/Ftp/FtpStorage.php +++ b/System/File/Ftp/FtpStorage.php @@ -51,7 +51,7 @@ class FtpStorage extends StorageAbstract ftp_pasv($this->con, $mode); - if(isset($login) && isset($pass)) { + if(isset($login, $pass)) { ftp_login($this->con, $login, $pass); } } diff --git a/Utils/Barcode/C25.php b/Utils/Barcode/C25.php index c7218f887..9cb3fac68 100644 --- a/Utils/Barcode/C25.php +++ b/Utils/Barcode/C25.php @@ -127,7 +127,7 @@ class C25 extends C128Abstract } for ($posX = 1; $posX <= $length; $posX += 2) { - if (isset($temp[$posX]) && isset($temp[($posX + 1)])) { + if (isset($temp[$posX], $temp[($posX + 1)])) { $temp1 = explode('-', $temp[$posX]); $temp2 = explode('-', $temp[($posX + 1)]); From 15914a10f0371ceaa7272c1243cf7eb00605ad64 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Thu, 1 Jun 2017 21:22:14 +0200 Subject: [PATCH 4/6] Add root path offset --- Uri/Http.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Uri/Http.php b/Uri/Http.php index b6362eb4e..b8755bb94 100644 --- a/Uri/Http.php +++ b/Uri/Http.php @@ -263,6 +263,14 @@ class Http implements UriInterface return $this->path; } + /** + * {@inheritdoc} + */ + public function getPathOffset() : int + { + return substr_count($this->rootPath, '/') - 1; + } + /** * {@inheritdoc} */ From 9e49ff680220fc2c220f61f3324caaab7dd96bed Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 7 Jul 2017 19:21:11 +0200 Subject: [PATCH 5/6] Fixes #109 #107 --- DataStorage/Database/DataMapperAbstract.php | 95 ++++++++++++++++----- DataStorage/Database/RelationType.php | 7 +- Utils/ArrayUtils.php | 11 +++ 3 files changed, 92 insertions(+), 21 deletions(-) diff --git a/DataStorage/Database/DataMapperAbstract.php b/DataStorage/Database/DataMapperAbstract.php index 9cab711d0..6f7662e84 100644 --- a/DataStorage/Database/DataMapperAbstract.php +++ b/DataStorage/Database/DataMapperAbstract.php @@ -141,6 +141,9 @@ class DataMapperAbstract implements DataMapperInterface */ protected static $fields = []; + protected static $initObjects = []; + protected static $parentMapper = null; + /** * Extended value collection. * @@ -291,6 +294,12 @@ class DataMapperAbstract implements DataMapperInterface 'ownsOne' => [], 'table' => [], ]; + + // clear parent and objects + if(static::class === self::$parentMapper) { + self::$initObjects = []; + self::$parentMapper = null; + } } /** @@ -385,8 +394,6 @@ class DataMapperAbstract implements DataMapperInterface $value = self::parseValue($column['type'], $id); $query->insert($column['name'])->value($value, $column['type']); - $temp = $query->toSql(); - $test = 1; break; } elseif (isset(static::$belongsTo[$propertyName]) && $column['internal'] === $propertyName) { $id = self::createBelongsTo($propertyName, $property->getValue($obj)); @@ -1280,6 +1287,11 @@ class DataMapperAbstract implements DataMapperInterface $mapper = static::$hasMany[$member]['mapper']; $reflectionProperty = $reflectionClass->getProperty($member); + $values = array_diff($values, array_keys(self::$initObjects[$mapper] ?? [])); + if(empty($values)) { + continue; + } + if (!($accessible = $reflectionProperty->isPublic())) { $reflectionProperty->setAccessible(true); } @@ -1322,7 +1334,12 @@ class DataMapperAbstract implements DataMapperInterface /** @var DataMapperAbstract $mapper */ $mapper = static::$hasOne[$member]['mapper']; - $value = $mapper::get($reflectionProperty->getValue($obj)); + if(self::isInitialized($mapper, $reflectionProperty->getValue($obj))) { + $value = self::$initObjects[$mapper][$id]; + } else { + $value = $mapper::get($reflectionProperty->getValue($obj)); + } + $reflectionProperty->setValue($obj, $value); if (!$accessible) { @@ -1360,7 +1377,12 @@ class DataMapperAbstract implements DataMapperInterface /** @var DataMapperAbstract $mapper */ $mapper = static::$ownsOne[$member]['mapper']; - $value = $mapper::get($reflectionProperty->getValue($obj)); + if(self::isInitialized($mapper, $reflectionProperty->getValue($obj))) { + $value = self::$initObjects[$mapper][$id]; + } else { + $value = $mapper::get($reflectionProperty->getValue($obj)); + } + $reflectionProperty->setValue($obj, $value); if (!$accessible) { @@ -1398,7 +1420,12 @@ class DataMapperAbstract implements DataMapperInterface /** @var DataMapperAbstract $mapper */ $mapper = static::$belongsTo[$member]['mapper']; - $value = $mapper::get($reflectionProperty->getValue($obj)); + if(self::isInitialized($mapper, $reflectionProperty->getValue($obj))) { + $value = self::$initObjects[$mapper][$id]; + } else { + $value = $mapper::get($reflectionProperty->getValue($obj)); + } + $reflectionProperty->setValue($obj, $value); if (!$accessible) { @@ -1470,6 +1497,10 @@ class DataMapperAbstract implements DataMapperInterface */ public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null) { + if(!isset(self::$parentMapper)) { + self::setUpParentMapper(); + } + self::extend(__CLASS__); $primaryKey = (array) $primaryKey; @@ -1479,6 +1510,10 @@ class DataMapperAbstract implements DataMapperInterface $toFill = null; foreach ($primaryKey as $key => $value) { + if(self::isInitialized(static::class, $value)) { + continue; + } + if ($fCount > 0) { $toFill = current($fill); next($fill); @@ -1489,6 +1524,8 @@ class DataMapperAbstract implements DataMapperInterface if(method_exists($obj[$value], 'initialize')) { $obj[$value]->initialize(); } + + self::addInitialized(static::class, $value); } self::fillRelations($obj, $relations); @@ -1510,6 +1547,10 @@ class DataMapperAbstract implements DataMapperInterface */ public static function getAll(int $relations = RelationType::ALL, string $lang = '') { + if(!isset(self::$parentMapper)) { + self::setUpParentMapper(); + } + $obj = self::populateIterable(self::getAllRaw($lang)); self::fillRelations($obj, $relations); self::clear(); @@ -1650,25 +1691,23 @@ class DataMapperAbstract implements DataMapperInterface $ownsOne = !empty(static::$ownsOne); $belongsTo = !empty(static::$belongsTo); - if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne)) { + if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne || $belongsTo)) { foreach ($obj as $key => $value) { /* loading relations from relations table and populating them and then adding them to the object */ - if ($relations !== RelationType::NONE) { - if ($hasMany) { - self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]); - } + if ($hasMany) { + self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]); + } - if ($hasOne) { - self::populateHasOne($obj[$key]); - } + if ($hasOne) { + self::populateHasOne($obj[$key]); + } - if ($ownsOne) { - self::populateOwnsOne($obj[$key]); - } + if ($ownsOne) { + self::populateOwnsOne($obj[$key]); + } - if ($belongsTo) { - self::populateBelongsTo($obj[$key]); - } + if ($belongsTo) { + self::populateBelongsTo($obj[$key]); } } } @@ -1863,4 +1902,22 @@ class DataMapperAbstract implements DataMapperInterface return $result; } + private static function addInitialized($mapper, $id, $obj = null) + { + if(!isset(self::$initObjects[$mapper])) { + self::$initObjects[$mapper] = []; + } + + self::$initObjects[$mapper][$id] = $obj; + } + + private static function isInitialized($mapper, $id) + { + return isset(self::$initObjects[$mapper]) && isset(self::$initObjects[$mapper][$id]); + } + + private static function setUpParentMapper() + { + self::$parentMapper = static::class; + } } diff --git a/DataStorage/Database/RelationType.php b/DataStorage/Database/RelationType.php index 2f5ae2007..2a24baa8d 100644 --- a/DataStorage/Database/RelationType.php +++ b/DataStorage/Database/RelationType.php @@ -36,6 +36,9 @@ abstract class RelationType extends Enum { /* public */ const NONE = 0; /* public */ const NEWEST = 1; - /* public */ const ALL = 2; - /* public */ const REFERENCE = 4; + /* public */ const BELONGS_TO = 2; + /* public */ const OWNS_ONE = 4; + /* public */ const HAS_MANY = 8; + /* public */ const ALL = 16; + /* public */ const REFERENCE = 32; } diff --git a/Utils/ArrayUtils.php b/Utils/ArrayUtils.php index 59fb6629a..6ed1c3412 100644 --- a/Utils/ArrayUtils.php +++ b/Utils/ArrayUtils.php @@ -148,6 +148,17 @@ class ArrayUtils return $found; } + public static function anyInArray(array $needles, array $haystack) : bool + { + foreach($needles as $needle) { + if(in_array($needle, $haystack)) { + return true; + } + } + + return false; + } + /** * Stringify array. * From ff4a6864c30922df953a69a571b0c5639567adf4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sun, 9 Jul 2017 10:10:09 +0200 Subject: [PATCH 6/6] Fix init and global cleanup --- Message/Http/Request.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Message/Http/Request.php b/Message/Http/Request.php index a54376f26..52d3d82a9 100644 --- a/Message/Http/Request.php +++ b/Message/Http/Request.php @@ -77,7 +77,7 @@ class Request extends RequestAbstract * @since 1.0.0 * @author Dennis Eichhorn */ - public function __construct(Localization $l11n, UriInterface $uri = null) + public function __construct(Localization $l11n = null, UriInterface $uri = null) { $this->l11n = $l11n; $this->uri = $uri; @@ -104,12 +104,10 @@ class Request extends RequestAbstract if (!isset($this->uri)) { $this->initCurrentRequest(); $this->lock(); + $this->cleanupGlobals(); } $this->data = array_change_key_case($this->data, CASE_LOWER); - - $this->cleanupGlobals(); - $this->path = explode('/', $this->uri->getPath()); $this->setupUriBuilder();