Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
adding method call by lambda-string
Browse files Browse the repository at this point in the history
  • Loading branch information
perryflynn committed Jun 6, 2017
1 parent 9c29c4e commit a7ca61e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
23 changes: 23 additions & 0 deletions src/PerrysLambda/ArrayBase.php
Expand Up @@ -1180,4 +1180,27 @@ public function count()
}


// String ------------------------------------------------------------------


/**
* Convert object content to string (php serialize)
* @return string
*/
public function toString()
{
return serialize($this->getData());
}


/**
* Convert object content to string
* @return string
*/
public function __toString()
{
return $this->toString();
}


}
8 changes: 6 additions & 2 deletions src/PerrysLambda/LambdaUtils.php
Expand Up @@ -7,7 +7,7 @@

class LambdaUtils
{

/**
* Converts strings, empty strings and NULL into a callable
* @param string|callable|null $mixed
Expand All @@ -33,6 +33,10 @@ public static function toCallable($mixed=null)
{
if(is_object($v))
{
if(method_exists($v, $mixed))
{
return $v->$mixed();
}
return $v->$mixed;
}
else
Expand All @@ -41,7 +45,7 @@ public static function toCallable($mixed=null)
}
};
}

throw new InvalidException("Could not convert expression of type ".gettype($mixed)." into a lambda callable");
}

Expand Down
9 changes: 9 additions & 0 deletions test/LambdaTest.php
Expand Up @@ -229,6 +229,11 @@ public function testLambda()
{
$this->assertEquals($basicdata[$key], $row);
}

// string conversion
$expected = 'a:3:{s:3:"foo";s:3:"bar";s:4:"foo2";s:4:"bar2";s:6:"foobar";s:6:"barfoo";}';
$this->assertSame($expected, $named->toString());
$this->assertSame($expected, "".$named);
}


Expand Down Expand Up @@ -263,6 +268,10 @@ public function testLambdaByString()

$ordertestdesc = $list->orderDesc('b')->thenByDesc('a')->toList()->select('a')->toArray();
$this->assertSame(array(5, 4, 2, 3), $ordertestdesc);

// Method call
$expected = 'a:4:{s:1:"a";i:3;s:1:"b";s:3:"bar";s:1:"c";s:7:"foobar2";s:1:"d";s:7:"barfoo2";}';
$this->assertSame($expected, $list->select('toString')->getAt(1));
}


Expand Down

0 comments on commit a7ca61e

Please sign in to comment.