/** * usort comparison function * @see self::getResult() * @see self::$_sort * @param IEntity * @param IEntity * @return int -1 or 1 */ private final function _sort(IEntity $aRow, IEntity $bRow) { foreach ($this->_sort as $tmp) { $key = $tmp[0]; $direction = $tmp[1]; if (strpos($key, '->') !== false) { $a = $aRow; $b = $bRow; foreach (explode('->', $key) as $k) { if (!$a instanceof IEntity) { $a = NULL; } else { if (!$a->hasParam($k)) { throw new InvalidArgumentException("'{$k}' is not key in '{$key}'"); } else { $a = $a->{$k}; if ($a instanceof IRelationship) { $a = $a->get()->fetch(); } } } if (!$b instanceof IEntity) { $b = NULL; } else { if (!$b->hasParam($k)) { throw new InvalidArgumentException("'{$k}' is not key in '{$key}'"); } else { $b = $b->{$k}; if ($b instanceof IRelationship) { $b = $b->get()->fetch(); } } } } } else { if (!$aRow->hasParam($key) or !$bRow->hasParam($key)) { if (!isset($aRow->{$key}) or !isset($bRow->{$key})) { throw new InvalidArgumentException("'{$key}' is not key"); } } $a = $aRow->{$key}; $b = $bRow->{$key}; } if (is_scalar($a) and is_scalar($b)) { $r = strnatcasecmp($a, $b); } else { if ($a instanceof DateTime and $b instanceof DateTime) { $r = $a < $b ? -1 : 1; } else { if ($b === NULL) { $r = 1; } else { if ($a === NULL) { $r = -1; } else { $tmp = 'unknown'; foreach (array($a, $b) as $ab) { if (!is_scalar($ab) and !$ab instanceof DateTime and $ab !== NULL) { $tmp = is_object($ab) ? get_class($ab) : gettype($ab); break; } } throw new InvalidArgumentException(get_class($aRow) . "::\${$key} contains non-sortable value, {$tmp}"); } } } } if ($r !== 0) { break; } } if ($direction === self::DESC) { return -$r; } return $r; }