Merge pull request #111 from Orange-Management/develop

Merge changes
This commit is contained in:
Dennis Eichhorn 2017-07-12 11:44:24 +02:00 committed by GitHub
commit 9f7627f09e
12 changed files with 152 additions and 32 deletions

View File

@ -141,6 +141,9 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
protected static $fields = []; protected static $fields = [];
protected static $initObjects = [];
protected static $parentMapper = null;
/** /**
* Extended value collection. * Extended value collection.
* *
@ -291,6 +294,12 @@ class DataMapperAbstract implements DataMapperInterface
'ownsOne' => [], 'ownsOne' => [],
'table' => [], '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); $value = self::parseValue($column['type'], $id);
$query->insert($column['name'])->value($value, $column['type']); $query->insert($column['name'])->value($value, $column['type']);
$temp = $query->toSql();
$test = 1;
break; break;
} elseif (isset(static::$belongsTo[$propertyName]) && $column['internal'] === $propertyName) { } elseif (isset(static::$belongsTo[$propertyName]) && $column['internal'] === $propertyName) {
$id = self::createBelongsTo($propertyName, $property->getValue($obj)); $id = self::createBelongsTo($propertyName, $property->getValue($obj));
@ -1280,6 +1287,11 @@ class DataMapperAbstract implements DataMapperInterface
$mapper = static::$hasMany[$member]['mapper']; $mapper = static::$hasMany[$member]['mapper'];
$reflectionProperty = $reflectionClass->getProperty($member); $reflectionProperty = $reflectionClass->getProperty($member);
$values = array_diff($values, array_keys(self::$initObjects[$mapper] ?? []));
if(empty($values)) {
continue;
}
if (!($accessible = $reflectionProperty->isPublic())) { if (!($accessible = $reflectionProperty->isPublic())) {
$reflectionProperty->setAccessible(true); $reflectionProperty->setAccessible(true);
} }
@ -1322,7 +1334,12 @@ class DataMapperAbstract implements DataMapperInterface
/** @var DataMapperAbstract $mapper */ /** @var DataMapperAbstract $mapper */
$mapper = static::$hasOne[$member]['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); $reflectionProperty->setValue($obj, $value);
if (!$accessible) { if (!$accessible) {
@ -1360,7 +1377,12 @@ class DataMapperAbstract implements DataMapperInterface
/** @var DataMapperAbstract $mapper */ /** @var DataMapperAbstract $mapper */
$mapper = static::$ownsOne[$member]['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); $reflectionProperty->setValue($obj, $value);
if (!$accessible) { if (!$accessible) {
@ -1398,7 +1420,12 @@ class DataMapperAbstract implements DataMapperInterface
/** @var DataMapperAbstract $mapper */ /** @var DataMapperAbstract $mapper */
$mapper = static::$belongsTo[$member]['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); $reflectionProperty->setValue($obj, $value);
if (!$accessible) { if (!$accessible) {
@ -1470,6 +1497,10 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null) public static function get($primaryKey, int $relations = RelationType::ALL, $fill = null)
{ {
if(!isset(self::$parentMapper)) {
self::setUpParentMapper();
}
self::extend(__CLASS__); self::extend(__CLASS__);
$primaryKey = (array) $primaryKey; $primaryKey = (array) $primaryKey;
@ -1479,6 +1510,10 @@ class DataMapperAbstract implements DataMapperInterface
$toFill = null; $toFill = null;
foreach ($primaryKey as $key => $value) { foreach ($primaryKey as $key => $value) {
if(self::isInitialized(static::class, $value)) {
continue;
}
if ($fCount > 0) { if ($fCount > 0) {
$toFill = current($fill); $toFill = current($fill);
next($fill); next($fill);
@ -1489,6 +1524,8 @@ class DataMapperAbstract implements DataMapperInterface
if(method_exists($obj[$value], 'initialize')) { if(method_exists($obj[$value], 'initialize')) {
$obj[$value]->initialize(); $obj[$value]->initialize();
} }
self::addInitialized(static::class, $value);
} }
self::fillRelations($obj, $relations); self::fillRelations($obj, $relations);
@ -1510,6 +1547,10 @@ class DataMapperAbstract implements DataMapperInterface
*/ */
public static function getAll(int $relations = RelationType::ALL, string $lang = '') public static function getAll(int $relations = RelationType::ALL, string $lang = '')
{ {
if(!isset(self::$parentMapper)) {
self::setUpParentMapper();
}
$obj = self::populateIterable(self::getAllRaw($lang)); $obj = self::populateIterable(self::getAllRaw($lang));
self::fillRelations($obj, $relations); self::fillRelations($obj, $relations);
self::clear(); self::clear();
@ -1650,25 +1691,23 @@ class DataMapperAbstract implements DataMapperInterface
$ownsOne = !empty(static::$ownsOne); $ownsOne = !empty(static::$ownsOne);
$belongsTo = !empty(static::$belongsTo); $belongsTo = !empty(static::$belongsTo);
if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne)) { if ($relations !== RelationType::NONE && ($hasMany || $hasOne || $ownsOne || $belongsTo)) {
foreach ($obj as $key => $value) { foreach ($obj as $key => $value) {
/* loading relations from relations table and populating them and then adding them to the object */ /* loading relations from relations table and populating them and then adding them to the object */
if ($relations !== RelationType::NONE) { if ($hasMany) {
if ($hasMany) { self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]);
self::populateManyToMany(self::getHasManyRaw($key, $relations), $obj[$key]); }
}
if ($hasOne) { if ($hasOne) {
self::populateHasOne($obj[$key]); self::populateHasOne($obj[$key]);
} }
if ($ownsOne) { if ($ownsOne) {
self::populateOwnsOne($obj[$key]); self::populateOwnsOne($obj[$key]);
} }
if ($belongsTo) { if ($belongsTo) {
self::populateBelongsTo($obj[$key]); self::populateBelongsTo($obj[$key]);
}
} }
} }
} }
@ -1843,7 +1882,7 @@ class DataMapperAbstract implements DataMapperInterface
$query = static::getQuery(); $query = static::getQuery();
$query->limit($limit); $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(), '>=', $from);
$query->where(static::getCreatedAt(), '<=', $to); $query->where(static::getCreatedAt(), '<=', $to);
} }
@ -1863,4 +1902,22 @@ class DataMapperAbstract implements DataMapperInterface
return $result; 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;
}
} }

View File

@ -36,6 +36,9 @@ abstract class RelationType extends Enum
{ {
/* public */ const NONE = 0; /* public */ const NONE = 0;
/* public */ const NEWEST = 1; /* public */ const NEWEST = 1;
/* public */ const ALL = 2; /* public */ const BELONGS_TO = 2;
/* public */ const REFERENCE = 4; /* public */ const OWNS_ONE = 4;
/* public */ const HAS_MANY = 8;
/* public */ const ALL = 16;
/* public */ const REFERENCE = 32;
} }

View File

@ -31,6 +31,14 @@ namespace phpOMS\Datatypes;
class Location implements \JsonSerializable, \Serializable class Location implements \JsonSerializable, \Serializable
{ {
/**
* Location id
*
* @var int
* @since 1.0.0
*/
private $id = 0;
/** /**
* Zip or postal. * Zip or postal.
* *
@ -63,6 +71,14 @@ class Location implements \JsonSerializable, \Serializable
*/ */
private $address = ''; private $address = '';
/**
* Address type
*
* @var int
* @since 1.0.0
*/
private $type = AddressType::HOME;
/** /**
* State. * State.
* *
@ -89,6 +105,33 @@ class Location implements \JsonSerializable, \Serializable
{ {
} }
/**
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getId() : int
{
return $this->id;
}
/**
* @return int
*
* @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com>
*/
public function getType() : int
{
return $this->type;
}
public function setType(int $type) /* : void */
{
$this->type = $type;
}
/** /**
* @return string * @return string
* *

View File

@ -151,7 +151,7 @@ class L11nManager
{ {
if (!isset($module) && isset($this->language[$language])) { if (!isset($module) && isset($this->language[$language])) {
return $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]; return $this->language[$language][$module];
} else { } else {
return []; return [];

View File

@ -77,7 +77,7 @@ class Request extends RequestAbstract
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function __construct(Localization $l11n, UriInterface $uri = null) public function __construct(Localization $l11n = null, UriInterface $uri = null)
{ {
$this->l11n = $l11n; $this->l11n = $l11n;
$this->uri = $uri; $this->uri = $uri;
@ -104,12 +104,10 @@ class Request extends RequestAbstract
if (!isset($this->uri)) { if (!isset($this->uri)) {
$this->initCurrentRequest(); $this->initCurrentRequest();
$this->lock(); $this->lock();
$this->cleanupGlobals();
} }
$this->data = array_change_key_case($this->data, CASE_LOWER); $this->data = array_change_key_case($this->data, CASE_LOWER);
$this->cleanupGlobals();
$this->path = explode('/', $this->uri->getPath()); $this->path = explode('/', $this->uri->getPath());
$this->setupUriBuilder(); $this->setupUriBuilder();

View File

@ -217,7 +217,7 @@ class BinaryTree extends Tree
$right2 = isset($node2) ? $this->getRight($node1) : $this->getRight($node2); $right2 = isset($node2) ? $this->getRight($node1) : $this->getRight($node2);
// todo: compare values? true symmetry requires the values to be the same // 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); return $this->isSymmetric($left1, $right2) && $this->isSymmetric($right1, $left2);
} }

View File

@ -370,7 +370,7 @@ class MultiMap implements \Countable
return false; 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->keys[$old] = $this->keys[$new];
$this->garbageCollect(); $this->garbageCollect();

View File

@ -51,7 +51,7 @@ class FtpStorage extends StorageAbstract
ftp_pasv($this->con, $mode); ftp_pasv($this->con, $mode);
if(isset($login) && isset($pass)) { if(isset($login, $pass)) {
ftp_login($this->con, $login, $pass); ftp_login($this->con, $login, $pass);
} }
} }

View File

@ -268,6 +268,14 @@ class Http implements UriInterface
return substr_count($this->rootPath, '/') - 1; return substr_count($this->rootPath, '/') - 1;
} }
/**
* {@inheritdoc}
*/
public function getPathOffset() : int
{
return substr_count($this->rootPath, '/') - 1;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -148,6 +148,17 @@ class ArrayUtils
return $found; 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. * Stringify array.
* *

View File

@ -127,7 +127,7 @@ class C25 extends C128Abstract
} }
for ($posX = 1; $posX <= $length; $posX += 2) { 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]); $temp1 = explode('-', $temp[$posX]);
$temp2 = explode('-', $temp[($posX + 1)]); $temp2 = explode('-', $temp[($posX + 1)]);

View File

@ -245,7 +245,7 @@ abstract class ViewAbstract implements \Serializable
* @since 1.0.0 * @since 1.0.0
* @author Dennis Eichhorn <d.eichhorn@oms.com> * @author Dennis Eichhorn <d.eichhorn@oms.com>
*/ */
public function render() : string public function render(...$data) : string
{ {
$path = __DIR__ . '/../..' . $this->template . '.tpl.php'; $path = __DIR__ . '/../..' . $this->template . '.tpl.php';