Beispiel #1
0
 * stringify()
 * Wrong:
 **************/
echo 'stringify() wrong:' . PHP_EOL . PHP_EOL;
$func = function ($first) {
    return $first + 3;
};
try {
    $ljsonString = \Kanti\LJSON::stringify($func);
} catch (\Kanti\StringifyException $e) {
    echo "message: " . $e->getMessage() . PHP_EOL;
    echo "file: " . $e->getFile() . PHP_EOL;
    echo "line: " . $e->getLine() . PHP_EOL . PHP_EOL;
}
/***************
 * stringify()
 * Right:
 **************/
echo 'stringify() right' . PHP_EOL;
$func = function ($stdLib, $first) {
    return $stdLib('+', $first, 3);
};
$ljsonString = \Kanti\LJSON::stringify($func);
//Notice: Object of class Kanti\Parameter could not be converted to int in D:\www\LJSON\example\example4.php on line 11
echo PHP_EOL . $ljsonString . PHP_EOL . PHP_EOL;
//(v0,v1) => (v0("+",v1,1))
$functionFromClient = \Kanti\LJSON::parseWithStdLib($ljsonString);
echo json_encode($functionFromClient(7)) . PHP_EOL;
//10
echo json_encode($functionFromClient(9999)) . PHP_EOL;
//10002
Beispiel #2
0
<?php

require_once "../vendor/autoload.php";
/*
 * Client:
 */
$func = function () {
    echo "Hallo";
    return "World";
};
$ljsonString = \Kanti\LJSON::stringify($func);
//Hello
echo "\nsend to server -> " . $ljsonString . "\n";
//<br>
// send to server -> () => ("World")
//<br>
/*
 * Server:
 */
$functionFromClient = \Kanti\LJSON::parse($ljsonString);
echo $functionFromClient();
//World
/*
 * The full Result:
 */
$fullResult = 'Hallo
send to server -> () => ("World")
World';
Beispiel #3
0
$ljsonString = \Kanti\LJSON::stringify($func);
echo PHP_EOL . $ljsonString . PHP_EOL . PHP_EOL;
//(v0,v1,v2) => ([v1,v0('*',v2,2)])
$functionFromClient = \Kanti\LJSON::parseWithStdLib($ljsonString);
echo json_encode($functionFromClient(2, 3)) . PHP_EOL;
/***************
 * parseWithStdLib()
 * complex example
 **************/
echo 'parseWithStdLib() complex:' . PHP_EOL;
$func = function ($lib, $one, $tow) {
    return function () use($lib, $one, $tow) {
        return [$one, $lib('*', $tow, 2)];
    };
};
$ljsonString = \Kanti\LJSON::stringify($func);
echo PHP_EOL . $ljsonString . PHP_EOL . PHP_EOL;
//(v0,v1,v2) => ([v1,v0('*',v2,2)])
$functionFromClient = \Kanti\LJSON::parseWithStdLib($ljsonString);
$resultFunction1 = $functionFromClient(2, 3);
echo json_encode($resultFunction1()) . PHP_EOL;
/***************
 * withStdLib()
 * simple example
 **************/
echo 'withStdLib() simple:' . PHP_EOL;
$func = function ($lib, $one, $tow) {
    return [$one, $lib('*', $tow, 2)];
};
$resultFunction1 = \Kanti\LJSON::withStdLib($func);
echo json_encode($resultFunction1(2, 3)) . PHP_EOL;
Beispiel #4
0
<?php

require_once "../vendor/autoload.php";
// A random object with a pure function inside.
$person = ["name" => "John", "mail" => function ($msg) {
    return ["author" => "John", "message" => $msg];
}];
$personStr = \Kanti\LJSON::stringify($person);
$personVal = \Kanti\LJSON::parse($personStr);
$mailFunction = $personVal->mail;
$mail = $mailFunction("hello");
// would crash with JSON
echo $personStr . "\n";
//{"name":"John","mail":(v0) => ({"author":"John","message":v0})}
echo \Kanti\LJSON::stringify($mail) . "\n";
//{"author":"John","message":"hello"}
Beispiel #5
0
 /**
  * @expectedException \Exception
  */
 public function testParseExceptionNotCompletedVariableCall()
 {
     LJSON::parse('(v0) => v0( ');
 }
 /**
  * @expectedException \Kanti\StringifyException
  */
 public function testNoErrorHandlerPreset()
 {
     if (PHP_MAJOR_VERSION < 5 || PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 5) {
         $this->markTestSkipped('only works for php >=5.5');
         return;
     }
     set_error_handler(null);
     LJSON::stringify(function ($aaa) {
         return (double) $aaa;
     });
 }
Beispiel #7
0
 /**
  * code coverage
  */
 public function testParseWithStdLibFunctionError2()
 {
     $resultFunction = LJSON::parseWithStdLib('(v0,v1) => (v0("error",v0,v0))');
     $this->assertEquals(null, $resultFunction(1));
 }