/** * @param string $property * @param string|int|float $value1 * @param string|int|float $value2 * * @return \Closure */ public static function notRanges(string $property, $value1, $value2) : \Closure { return function ($v) use($property, $value1, $value2) { $v = PropertyValue::get($v, $property); self::sameTypeGuard($v, $value1, $value2); return !($v >= $value1 && $v <= $value2); }; }
/** * @param array $results * @param Sort $sort * * @return array * * @throws \Exception */ public static function sort(array $results, Sort $sort) : array { $sortOrder = array_reverse($sort->orders(), true); /** @var Order $sortDirection */ foreach ($sortOrder as $propertyName => $sortDirection) { if ($sortDirection->isAscending()) { self::stableUasort($results, function ($a, $b) use($propertyName) { $value1 = (string) PropertyValue::get($a, $propertyName); $value2 = (string) PropertyValue::get($b, $propertyName); return (int) (strcmp($value1, $value2) >= 0); }); } else { self::stableUasort($results, function ($a, $b) use($propertyName) { $value1 = (string) PropertyValue::get($a, $propertyName); $value2 = (string) PropertyValue::get($b, $propertyName); return (int) (strcmp($value1, $value2) < 0); }); } } return array_values($results); }
/** * @param $property * @param $value * * @return \Closure */ public static function notEndsWith($property, $value) : \Closure { return function ($v) use($property, $value) { $v = PropertyValue::get($v, $property); self::propertyGuard($v, $property); self::valueGuard($value, 'Ending'); return 0 == preg_match(sprintf('/%s$/i', $value), $v); }; }
/** * @param string $property * @param array $value * * @return \Closure */ public static function notIn(string $property, array $value) : \Closure { return function ($v) use($property, $value) { $hasGroup = true; $v = PropertyValue::get($v, $property); foreach ($value as $groupItem) { if (is_scalar($v)) { $hasGroup = $hasGroup && 0 == preg_match(sprintf('/^%s/i', $groupItem), $v); } if (is_array($v)) { $hasGroup = $hasGroup && false === array_search($groupItem, $v, false); } if (is_object($v)) { $hasGroup = $hasGroup && $groupItem != $v; } } return $hasGroup; }; }
/** * @param Fields $distinctFields * @param $results * * @return array * * @throws \Exception */ protected function resultsWithDistinctFieldsOnly(Fields $distinctFields, $results) : array { $newResults = []; $valueHash = []; foreach ($results as $result) { $distinctValues = []; foreach ($distinctFields->get() as $field) { $distinctValues[$field] = PropertyValue::get($result, $field); } $hash = md5(serialize($distinctValues)); if (false === in_array($hash, $valueHash)) { $valueHash[] = $hash; $newResults[] = $result; } } return $newResults; }
/** * @param string $property * @param string|int|float $value * * @return \Closure */ public static function lessThan(string $property, $value) : \Closure { return function ($v) use($property, $value) { return PropertyValue::get($v, $property) < $value; }; }