/** * Build comparison function for this leaf node. * * @param array(string) $ancestors Array of parent properties e.g. * array('Orders', 'Customer', * 'Customer_Demographics') * * @return AnonymousFunction */ public function buildComparisonFunction($ancestors) { if (count($ancestors) == 0) { throw new \InvalidArgumentException(Messages::orderByLeafNodeArgumentShouldBeNonEmptyArray()); } $parameterNames = null; $accessor1 = null; $accessor2 = null; $a = $this->_isAscending ? 1 : -1; foreach ($ancestors as $i => $anscestor) { if ($i == 0) { $parameterNames = array('$' . $anscestor . 'A', '$' . $anscestor . 'B'); $accessor1 = $parameterNames[0]; $accessor2 = $parameterNames[1]; $flag1 = '$flag1 = ' . 'is_null(' . $accessor1 . ') || '; $flag2 = '$flag2 = ' . 'is_null(' . $accessor2 . ') || '; } else { $accessor1 .= '->' . $anscestor; $accessor2 .= '->' . $anscestor; $flag1 .= 'is_null(' . $accessor1 . ')' . ' || '; $flag2 .= 'is_null(' . $accessor2 . ')' . ' || '; } } $accessor1 .= '->' . $this->propertyName; $accessor2 .= '->' . $this->propertyName; $flag1 .= 'is_null(' . $accessor1 . ')'; $flag2 .= 'is_null(' . $accessor2 . ')'; $code = "{$flag1}; \n {$flag2}; \n if(\$flag1 && \$flag2) { \n return 0;\n } else if (\$flag1) { \n return {$a}*-1;\n } else if (\$flag2) { \n return {$a}*1;\n }\n \n "; $type = $this->resourceProperty->getInstanceType(); if ($type instanceof DateTime) { $code .= " \$result = strtotime({$accessor1}) - strtotime({$accessor2});"; } else { if ($type instanceof String) { $code .= " \$result = strcmp({$accessor1}, {$accessor2});"; } else { if ($type instanceof Guid) { $code .= " \$result = strcmp({$accessor1}, {$accessor2});"; } else { $code .= " \$result = (({$accessor1} == {$accessor2}) ? 0 : (({$accessor1} > {$accessor2}) ? 1 : -1));"; } } } $code .= "\n return {$a}*\$result;"; $this->_anonymousFunction = new AnonymousFunction($parameterNames, $code); return $this->_anonymousFunction; }