Exemplo n.º 1
0
Arquivo: DSTest.php Projeto: ihor/Nspl
 public function testDefaultArray()
 {
     $array = new DefaultArray(0);
     $this->assertEquals(0, $array['apples']);
     $array['apples'] += 2;
     $this->assertEquals(2, $array['apples']);
     $array['bananas'] = 5;
     $this->assertEquals(5, $array['bananas']);
     $array['bananas'] += 5;
     $this->assertEquals(10, $array['bananas']);
     $array = new DefaultArray(function () {
         return time();
     });
     $this->assertEquals(time(), $array['apples'], '', 0.1);
     $array = defaultarray(0);
     $this->assertEquals(0, $array['apples']);
     $array['apples'] += 2;
     $this->assertEquals(2, $array['apples']);
     $array['bananas'] = 5;
     $this->assertEquals(5, $array['bananas']);
     $array['bananas'] += 5;
     $this->assertEquals(10, $array['bananas']);
     $array = defaultarray(function () {
         return time();
     });
     $this->assertEquals(time(), $array['apples'], '', 0.1);
     $array = new DefaultArray(10, array('apples' => 20, 'bananas' => 30));
     $this->assertEquals(10, $array['oranges']);
     $this->assertEquals(20, $array['apples']);
     $this->assertEquals(30, $array['bananas']);
     $array = defaultarray(10, array('apples' => 20, 'bananas' => 30));
     $this->assertEquals(10, $array['oranges']);
     $this->assertEquals(20, $array['apples']);
     $this->assertEquals(30, $array['bananas']);
 }
Exemplo n.º 2
0
require_once __DIR__ . '/../autoload.php';
use function nspl\ds\defaultarray;
use function nspl\ds\dstring;
use function nspl\f\map;
use function nspl\op\methodCaller;
// 1. Cast default array to regular PHP array
$wordCounter = defaultarray(0);
++$wordCounter['hello'];
++$wordCounter['hello'];
++$wordCounter['hello'];
++$wordCounter['world'];
echo "Word counter:\n";
print_r($wordCounter->toArray());
// 2. Multidimensional default array
// Note that we create nested default array with an anonymous function.
// Otherwise, default array object will be shared across all parent array fields.
$matrix = defaultarray(function () {
    return defaultarray(0);
});
for ($i = 0; $i < 3; ++$i) {
    for ($j = 0; $j < 3; ++$j) {
        ++$matrix[$i][$j];
    }
}
echo "Matrix 3x3:\n";
print_r(map(methodCaller('toArray'), $matrix->toArray()));
// casting default array with all nested default arrays to PHP array
// 3. Dynamic email subject
$subject = dstring()->addStr('Something bad happened on ')->addConstant('APPLICATION_ENV')->addStr(' at ')->addFunction('date', ['Y-m-d H:i:s']);
define('APPLICATION_ENV', 'staging');
echo $subject . "\n";