1. Jiří Knesl
  2. Googy

Commits

Jiří Knesl  committed 3eef189

more underscorish behaviour

  • Participants
  • Parent commits e6ad4c3
  • Branches default

Comments (0)

Files changed (4)

File lib/Googy/Arr.php

View file
  • Ignore whitespace
 /**
 * smart Array create class 
 */
-function Arr($data = array(), $options = array())
+function Arr()
 {
-	return new ArrayClass($data, $options);
+	$args = func_get_args();
+	if (empty($args)) {
+		$data = array();
+	} else if (is_array($args[0]) && sizeof($args) == 1) {
+		$data = $args[0];
+	} else {
+		$data = $args;
+	} 	
+	return new ArrayClass($data);
 }

File lib/Googy/ArrayClass.php

View file
  • Ignore whitespace
 {
 	
 	const JSON_WRITE_MODE = 'json_write_mode';
-	const PHP_WRITE_MODE = 'php_write_mode';
 	const SLICE_WRITE_MODE = 'slice_write_mode';
 	
 	/**
 	 * @var array
 	 **/
 	protected $options = array(
-		'writeMode'=>self::PHP_WRITE_MODE
+		'writeMode'=>self::JSON_WRITE_MODE
 		);
 	
 	
 	 * @param Array $options 
 	 * @author Jiří Knesl
 	 */
-	public function __construct($defaults = array(), $options = array())
+	public function __construct($defaults = array())
 	{
 		$this->data = $defaults;
-		
-		$this->options = array_merge($this->options, $options);
 	}
 	
 	/**
 		return $this;
 	}
 
+
+	/**
+	 * Apply callback to all items
+	 *
+	 * @param mixed $callback
+	 * @return null
+	 *
+	 */
+	public function each($callback)
+	{
+		array_map($callback, $this->data);
+	}
+
+	/**
+	 * Reduce left
+	 *
+	 * @return mixed
+	 * @author Jiri Knesl
+	 **/
+	public function reduceLeft($callback)
+	{
+		return array_reduce($this->data, $callback);
+	}
+
+	/**
+	 * Reduce right
+	 *
+	 * @return mixed
+	 * @author Jiri Knesl
+	 **/
+	public function reduceRight($callback)
+	{
+		return array_reduce(array_reverse($this->data), $callback);
+	}
+
+
+	/**
+	 * Return first value which passes test
+	 *
+	 * @return mixed
+	 * @author Jiri Knesl
+	 **/
+	public function detect($callback)
+	{
+		foreach ($this->data as $key=>$value) {
+			if (call_user_func($callback, $value, $key)) {
+				return $value;
+			}
+		}
+	}
+
+	/**
+	 * Return if all elements pass given callback
+	 *
+	 * @return bool
+	 * @author Jiri Knesl
+	 **/
+	public function every($callback)
+	{
+		foreach ($this->data as $key=>$value) {
+			if (! call_user_func($callback, $value, $key)) {
+				return false;
+			}
+		}
+		return true;
+	}
+
+	/**
+	 * Return if at least once item pass the callback
+	 * @return bool
+	 */
+	public function any($callback)
+	{
+		foreach ($this->data as $key=>$value) {
+			if (call_user_func($callback, $value, $key)) {
+				return true;
+			}
+		}
+		return false;
+	}
+
+	/**
+	 * Return if $value is in array
+	 *
+	 * @return bool
+	 * @author Jiri Knesl
+	 **/
+	public function includes($value)
+	{
+		return in_array($value, $this->data);
+	}
+
+	/**
+	 * Return all subitems by given key
+	 *
+	 * @return Googy\ArrayClass
+	 * @author Jiri Knesl
+	 **/
+	public function pluck($key)
+	{
+		return $this->map(function($v) use ($key) {return $v[$key];});
+	}
+
+	/**
+	 * Return maximum value
+	 *
+	 * @return mixed
+	 * @author Jiri Knesl
+	 **/
+	public function max()
+	{
+		return max($this->data);
+	}
+
+	/**
+	 * Return minimal value
+	 *
+	 * @return mixed
+	 * @author Jiri Knesl
+	 **/
+	public function min()
+	{
+		return min($this->data);
+	}
+
+	/**
+	 * Sort by given callback
+	 *
+	 * @return Googy\ArrayClass
+	 * @author Jiri Knesl
+	 **/
+	public function sortBy($callback)
+	{
+		uasort($this->data, $callback);
+		return $this;
+	}
+
 	/**
 	 * Filter each by callback
 	 * 
 	}
 
 	/**
+	 * Return all values for which $callback returns false
+	 * @param mixed $callback
+	 * @return Googy\ArrayClass 
+	 */
+	public function reject($callback)
+	{
+		$f = function($v) use ($callback){return ! call_user_func($callback, $v);};
+		return $this->filter($f);
+	}
+
+	/**
 	 * Search value
 	 * 
 	 * @todo Refactor to own class

File tests/ArrFunctionalTest.php

View file
  • Ignore whitespace
+<?php
+
+use Googy as _;
+
+require_once __DIR__ . '/../lib/Googy/Arr.php';
+// require_once "PHPUnit/Framework.php";
+
+class ArrFunctionalTest_Mock
+{
+	function doIt() {}
+}
+
+/**
+ * Array functional methods
+ *
+ * @author Jiri Knesl
+**/
+class ArrFunctionalTest extends PHPUnit_Framework_TestCase
+{
+	/**
+	 * 
+	 */
+	public function testEach()
+	{
+		$mock = $this->getMock("ArrFunctionalTest_Mock");
+		$mock->expects($this->exactly(3))->method("doIt");
+		$callback = array($mock, "doIt");
+		_\Arr(3, 3, 3)->each($callback);
+	}
+	/**
+	 * 
+	 */
+	public function testReduceLeft()
+	{
+		$result = _\Arr(1, 2, 3)->reduceLeft(function(&$accumulator, $val){
+			return $accumulator . $val; 
+		});
+		$this->assertEquals("123", $result);
+	}
+	/**
+	 * 
+	 */
+	public function testReduceRight()
+	{
+		
+		$result = _\Arr(1, 2, 3)->reduceRight(function(&$accumulator, $val){
+			return $accumulator . $val; 
+		});
+		$this->assertEquals("321", $result);
+	}
+	/**
+	 * 
+	 */
+	public function testDetect()
+	{
+		$result = _\Arr(range(1, 100))->detect(function($value) {
+			return $value > 400;
+		});
+		$this->assertEquals(null, $result);
+
+		$result = _\Arr(range(1, 100))->detect(function($value) {
+			return $value > 40;
+		});
+		$this->assertEquals(41, $result);
+	}
+	/**
+	 * 
+	 */
+	public function testEvery()
+	{
+
+		$result = _\Arr(range(1, 100))->every(function($value) {
+			return $value > 0 && $value < 101;
+		});
+		$this->assertTrue($result);
+		
+		$result = _\Arr(range(1, 100))->every(function($value) {
+			return $value > 0 && $value < 100;
+		});
+		$this->assertFalse($result);
+	}
+	/**
+	 * 
+	 */
+	public function testAny()
+	{
+		
+		$result = _\Arr(range(1, 100))->any(function($value) {
+			return $value > 99 && $value < 101;
+		});
+		$this->assertTrue($result);
+		
+		$result = _\Arr(range(1, 100))->any(function($value) {
+			return $value < 0;
+		});
+		$this->assertFalse($result);
+	}
+	/**
+	 * 
+	 */
+	public function testIncludes()
+	{
+		$arr = _\Arr(range(1, 100));
+		$this->assertTrue($arr->includes(100));
+		$this->assertFalse($arr->includes(110));
+	}
+	/**
+	 * 
+	 */
+	public function testPluck()
+	{
+		$array = array(
+			array('x'=>1),
+			array('x'=>2),
+			array('x'=>3),
+		);
+		$this->assertEquals(array(1, 2, 3), _\Arr($array)->pluck("x")->toArray());
+	}
+	/**
+	 * 
+	 */
+	public function testMax()
+	{
+		$arr = _\Arr(array_reverse(range(1, 10)));
+		$this->assertEquals(10, $arr->max());	
+	}
+	/**
+	 * 
+	 */
+	public function testMin()
+	{
+		$arr = _\Arr(array_reverse(range(1, 10)));
+		$this->assertEquals(1, $arr->min());	
+	}
+	/**
+	 * 
+	 */
+	public function testSortBy()
+	{
+		$arr = array(1, 3, 2, 5);	
+		$arr2 = _\Arr($arr)->sortBy(function($a, $b){
+			return $a > $b;
+		})->toArray();
+		asort($arr);
+		$this->assertEquals($arr, $arr2);
+	}
+	/**
+	 * 
+	 */
+	public function testReject()
+	{
+		$result = _\Arr(range(1,100))->reject(function($value) { return $value <= 50; })->toArray();
+		$this->assertEquals(range(51, 100), array_values($result));
+
+	}
+}

File tests/ArrSliceTest.php

View file
  • Ignore whitespace
 	 */
 	public function testToArray()
 	{
-		$this->assertEquals(array(3), Googy\Arr(array(1,2,3))->slice->{'2:'}->toArray(), "Testing 2:");
+		$this->assertEquals(array(3), Googy\Arr(1,2,3)->slice->{'2:'}->toArray(), "Testing 2:");
 		$this->assertEquals(array(1,2,3), Googy\Arr(array(1,2,3))->slice->{':2'}->toArray(), "Testing :2");
 		$this->assertEquals(array(2,3), Googy\Arr(array(1,2,3))->slice->{'1:2'}->toArray(), "Testing 1:2");
-		$this->assertEquals(array(1,2), Googy\Arr(array(1,2,3), array('writeMode'=>Googy\ArrayClass::SLICE_WRITE_MODE))->{'0:1'}->toArray(), "Testing 1:2");
+		$this->assertEquals(array(1,2), Googy\Arr(array(1,2,3))->slice->{'0:1'}->toArray(), "Testing 1:2");
 		$this->assertEquals(array(1), Googy\Arr(array(1,2,3))->slice->{0}->toArray(), "Testing 0");
 		
 		$this->assertEquals("b", Googy\Arr(array(1,2,3))->json->{'"1":"b"'}->slice->{'1:'}[0] );