Esempio n. 1
0
 /**
  * @expectedException \Exception
  */
 public function testParseExceptionNotCompletedVariableCall()
 {
     LJSON::parse('(v0) => v0( ');
 }
Esempio n. 2
0
$ljsonString = \LJSON\LJSON::stringify($func);
echo PHP_EOL . $ljsonString . PHP_EOL . PHP_EOL;
//(v0,v1,v2) => ([v1,v0('*',v2,2)])
$functionFromClient = \LJSON\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 = \LJSON\LJSON::stringify($func);
echo PHP_EOL . $ljsonString . PHP_EOL . PHP_EOL;
//(v0,v1,v2) => ([v1,v0('*',v2,2)])
$functionFromClient = \LJSON\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 = \LJSON\LJSON::withStdLib($func);
echo json_encode($resultFunction1(2, 3)) . PHP_EOL;
Esempio n. 3
0
 /**
  * code coverage
  */
 public function testParseWithStdLibFunctionError2()
 {
     $resultFunction = LJSON::parseWithStdLib('(v0,v1) => (v0("error",v0,v0))');
     $this->assertEquals(null, $resultFunction(1));
 }
Esempio n. 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 = \LJSON\LJSON::stringify($person);
$personVal = \LJSON\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 \LJSON\LJSON::stringify($mail) . "\n";
//{"author":"John","message":"hello"}
Esempio n. 5
0
 /**
  * @expectedException \LJSON\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;
     });
 }
Esempio n. 6
0
 * stringify()
 * Wrong:
 **************/
echo 'stringify() wrong:' . PHP_EOL . PHP_EOL;
$func = function ($first) {
    return $first + 3;
};
try {
    $ljsonString = \LJSON\LJSON::stringify($func);
} catch (\LJSON\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 = \LJSON\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 = \LJSON\LJSON::parseWithStdLib($ljsonString);
echo json_encode($functionFromClient(7)) . PHP_EOL;
//10
echo json_encode($functionFromClient(9999)) . PHP_EOL;
//10002
Esempio n. 7
0
<?php

require_once "../vendor/autoload.php";
/*
 * Client:
 */
$func = function () {
    echo "Hallo";
    return "World";
};
$ljsonString = \LJSON\LJSON::stringify($func);
//Hello
echo "\nsend to server -> " . $ljsonString . "\n";
//<br>
// send to server -> () => ("World")
//<br>
/*
 * Server:
 */
$functionFromClient = \LJSON\LJSON::parse($ljsonString);
echo $functionFromClient();
//World
/*
 * The full Result:
 */
$fullResult = 'Hallo
send to server -> () => ("World")
World';