public function __invoke(...$data) { $sort = _Array::sort(_Comparator::numeric()); $arr = $sort($data[0]); $length = count($arr); if ($length % 2) { return $arr[(int) floor($length / 2)]; } return ($arr[(int) floor($length / 2)] + $arr[(int) floor($length / 2) - 1]) / 2; }
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage invalid property */ public function testComparatorFail5() { $cmp = new ObjectComparator(['score', 'id']); $data = [GenericObject::init(['score' => ['asa'], 'id' => 5]), GenericObject::init(['score' => ['assa'], 'id' => 3])]; $sort = _Array::sort($cmp); $sort($data); }
public function testSortInvertMap_Map_Implode() { $map = function ($obj) { return $obj->val; }; $mapf = function ($obj) { return $obj->f; }; $a = new \stdClass(); $a->val = 1; $a->f = 2; $b = new \stdClass(); $b->val = 2; $b->f = 1; $c = new \stdClass(); $c->val = 3; $c->f = 1; $d = new \stdClass(); $d->val = 4; $d->f = 2; $data = [$c, $b, $a, $d]; $node = _Array::sort(_Comparator::numeric()->invert()->map($mapf)->then(_Comparator::numeric()->map($map)->invert()))->map($map)->implode(''); $this->assertEquals('4132', $node($data)); }
use Hurl\Node\Statics\_Node; use Hurl\Node\Statics\_String; require_once __DIR__ . '/vendor/autoload.php'; $fromHex = _Node::init(function ($data) { return hexdec($data); }); var_dump($fromHex('a')); //int(10) $explode = _String::explode('.'); var_dump($explode('a.b')); $chain = $explode->implode('-'); var_dump($chain('a.b')); $map = $explode->map($fromHex)->implode('.'); var_dump($map('a.b')); $sort = _Array::sort(function ($a, $b) { return $a - $b; }); var_dump($sort([2, 5, 3, 4, 1])); //array(5) { // [0]=> // int(1) // [1]=> // int(2) // [2]=> // int(3) // [3]=> // int(4) // [4]=> // int(5) //} $string = 'a,3,e,22,a2,3e0,cf';
/** * @expectedException InvalidArgumentException * @expectedExceptionMessage invalid property */ public function testComparatorFail5() { $cmp = new ArrayComparator(['score', 'id']); $data = [['score' => ['asa'], 'id' => 5], ['score' => ['assa'], 'id' => 3]]; $sort = _Array::sort($cmp); $sort($data); }
abstract class AbstractArray extends AbstractNode implements CollectionNodeInterface { /** * @param callable $do * @return AbstractNode */ public function then(callable $do) { return new class($this, $do) extends AbstractArray implements ContainerTraitInterface { use ContainerTrait; }; } /** * @param callable $do * @return ArrayEach */ public function each(callable $do) { return new class($this, _Array::each($do)) extends ArrayEach implements ContainerTraitInterface { use ContainerTrait; }; } /** * @param callable $callable * @return ArrayMap */ public function map(callable $callable) { return new class($this, _Array::map($callable)) extends ArrayMap implements ContainerTraitInterface { use ContainerTrait; }; } /** * @param callable $callable * @return ArrayMap */ public function fold(callable $callable) { return new class($this, _Array::map($callable)) extends ArrayMap implements ContainerTraitInterface { use ContainerTrait; }; } /** * @param \callable[] ...$callable * @return ArraySort */ public function sort(callable ...$callable) { return new class($this, _Array::sort(...$callable)) extends ArraySort implements ContainerTraitInterface { use ContainerTrait; }; } /** * @return ArraySort */ public function merge() { return new class($this, _Array::merge()) extends ArrayMerge implements ContainerTraitInterface { use ContainerTrait; }; } /** * @return AbstractNode */ public function sum() { return new class($this, MathNode::sum()) extends AbstractNode implements ContainerTraitInterface { use ContainerTrait; }; } /** * @return ArraySort */ public function values() { return new class($this, _Array::values()) extends AbstractArray implements ContainerTraitInterface { use ContainerTrait; }; } /** * @param string $glue * @return AbstractNode */ public function implode(string $glue) { return $this->then(_Array::implode($glue)); } /** * @return IsEmptyFilter */ public function isEmpty() { return new class($this, _Filter::isEmpty()) extends IsEmptyFilter implements ContainerTraitInterface { use ContainerTrait; }; } /** * @return ContainsFilter */ public function contains($needle, $strict = null) { return new class($this, _Filter::contains($needle, $strict)) extends ContainsFilter implements ContainerTraitInterface { use ContainerTrait; }; } }