<?php
namespace nulib\php\access;

use nulib\tests\TestCase;
use stdClass;

class KeyAccessTest extends TestCase {
  function testValueAccess() {
    $default = new stdClass();

    #
    $i = null;
    $a = new KeyAccess($i);
    self::assertFalse($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $i = false;
    $a = new KeyAccess($i);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(false, $a->get($default));

    $i = "";
    $a = new KeyAccess($i);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame("", $a->get($default));

    #
    $i = null;
    $a = new KeyAccess($i, null, ["allow_null" => false]);
    self::assertFalse($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $i = null;
    $a = new KeyAccess($i, null, ["allow_null" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(null, $a->get($default));

    #
    $i = false;
    $a = new KeyAccess($i, null, ["allow_false" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $i = false;
    $a = new KeyAccess($i, null, ["allow_false" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(false, $a->get($default));

    #
    $i = "";
    $a = new KeyAccess($i, null, ["allow_empty" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $i = "";
    $a = new KeyAccess($i, null, ["allow_empty" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame("", $a->get($default));
  }
  
  function testArrayAccess() {
    $default = new stdClass();
    $array = ["null" => null, "false" => false, "empty" => ""];

    #
    $a = new KeyAccess($array, "inexistant");
    self::assertFalse($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "null");
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(null, $a->get($default));

    $a = new KeyAccess($array, "false");
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "empty");
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame("", $a->get($default));

    #
    $a = new KeyAccess($array, "null", ["allow_null" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "null", ["allow_null" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(null, $a->get($default));

    #
    $a = new KeyAccess($array, "false", ["allow_false" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "false", ["allow_false" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame(false, $a->get($default));

    #
    $a = new KeyAccess($array, "empty", ["allow_empty" => false]);
    self::assertTrue($a->exists());
    self::assertFalse($a->available());
    self::assertSame($default, $a->get($default));

    $a = new KeyAccess($array, "empty", ["allow_empty" => true]);
    self::assertTrue($a->exists());
    self::assertTrue($a->available());
    self::assertSame("", $a->get($default));
  }

  private function _ensureAssoc(?array $orig, ?array $expected, array $keys, ?array $params=null) {
    $v = $orig; $a = new KeyAccess($v);
    $a->ensureAssoc($keys, $params);
    self::assertSame($expected, $v);
  }
  function testEnsureAssoc() {
    $keys = ["a", "b", "c"];

    $this->_ensureAssoc(null, null, $keys);
    $this->_ensureAssoc([], [], $keys);
    $this->_ensureAssoc([1], ["a" => 1], $keys);
    $this->_ensureAssoc([1, 2, 3], ["a" => 1, "b" => 2, "c" => 3], $keys);
    $this->_ensureAssoc([1, 2, 3, 4], [3 => 4, "a" => 1, "b" => 2, "c" => 3], $keys);
    $this->_ensureAssoc(["c" => 3, 1], ["c" => 3, "a" => 1], $keys);
    $this->_ensureAssoc(["c" => 3, "b" => 2, 1], ["c" => 3, "b" => 2, "a" => 1], $keys);
    $this->_ensureAssoc(["c" => 3, "b" => 2, "a" => 1], ["c" => 3, "b" => 2, "a" => 1], $keys);
    $this->_ensureAssoc(["a" => 1, 2], ["a" => 1, "b" => 2], $keys);
    $this->_ensureAssoc([2, "a" => 1], ["a" => 1, "b" => 2], $keys);

    $keys = [0, "a", "b"];
    $this->_ensureAssoc([1], [1], $keys);
    $this->_ensureAssoc([1, 2], [1, "a" => 2], $keys);
  }

  private function _ensureKeys(?array $orig, ?array $expected, array $defaults, ?array $params=null) {
    $v = $orig; $a = new KeyAccess($v);
    $a->ensureKeys($defaults, $missings, $params);
    self::assertSame($expected, $v);
  }
  function testEnsureKeys() {
    $defaults = ["a" => false, "b" => false, "c" => false];

    $this->_ensureKeys(null, ["a" => false, "b" => false, "c" => false], $defaults);
    $this->_ensureKeys([], ["a" => false, "b" => false, "c" => false], $defaults);
    $this->_ensureKeys(["a" => 1], ["a" => 1, "b" => false, "c" => false], $defaults);
    $this->_ensureKeys(["a" => 1, "b" => 2, "c" => 3], ["a" => 1, "b" => 2, "c" => 3], $defaults);
    $this->_ensureKeys(["x"], ["x", "a" => false, "b" => false, "c" => false], $defaults);
    $this->_ensureKeys(["x", "a" => 1], ["x", "a" => 1, "b" => false, "c" => false], $defaults);
    $this->_ensureKeys(["a" => 1, "x"], ["a" => 1, "x", "b" => false, "c" => false], $defaults);
    $this->_ensureKeys(["a" => 1, "b" => 2, "c" => 3, "x"], ["a" => 1, "b" => 2, "c" => 3, "x"], $defaults);
  }

  private function _ensureOrder(?array $orig, ?array $expected, array $keys, ?array $params=null) {
    $v = $orig; $a = new KeyAccess($v);
    $a->ensureOrder($keys, $params);
    self::assertSame($expected, $v);
  }
  function testEnsureOrder() {
    $keys = ["a", "b", "c"];

    $this->_ensureOrder(null, null, $keys);
    $this->_ensureOrder([], [], $keys);
    $this->_ensureOrder([1], [1], $keys);
    $this->_ensureOrder(["b" => 2, "a" => 1], ["a" => 1, "b" => 2], $keys);
    $this->_ensureOrder(["c" => 3, "a" => 1], ["a" => 1, "c" => 3], $keys);
  }

  private function _ensureAssocKeysOrder(?array $orig, ?array $expected, array $defaults, ?array $params=null) {
    $v = $orig; $a = new KeyAccess($v);
    $keys = array_keys($defaults);
    $a->ensureAssoc($keys, $params);
    $a->ensureKeys($defaults, $missings, $params);
    $a->ensureOrder($keys, $params);
    self::assertSame($expected, $v);
  }
  function testEnsureAssocKeysOrder() {
    $defaults = ["a" => false, "b" => false, "c" => false];

    $this->_ensureAssocKeysOrder(null, ["a" => false, "b" => false, "c" => false], $defaults);
    $this->_ensureAssocKeysOrder([], ["a" => false, "b" => false, "c" => false], $defaults);
    $this->_ensureAssocKeysOrder([1], ["a" => 1, "b" => false, "c" => false], $defaults);
    $this->_ensureAssocKeysOrder([1, 2, 3], ["a" => 1, "b" => 2, "c" => 3], $defaults);
    $this->_ensureAssocKeysOrder([1, 2, 3, 4], ["a" => 1, "b" => 2, "c" => 3, 4], $defaults);
    $this->_ensureAssocKeysOrder([1, 2, 3, 4], ["a" => 1, "b" => 2, "c" => 3, 3 => 4], $defaults, [
      "preserve_keys" => true,
    ]);
    $this->_ensureAssocKeysOrder(["c" => 3, 1], ["a" => 1, "b" => false, "c" => 3], $defaults);
    $this->_ensureAssocKeysOrder(["c" => 3, "b" => 2, 1], ["a" => 1, "b" => 2, "c" => 3], $defaults);
    $this->_ensureAssocKeysOrder(["c" => 3, "b" => 2, "a" => 1], ["a" => 1, "b" => 2, "c" => 3], $defaults);
    $this->_ensureAssocKeysOrder(["a" => 1, 2], ["a" => 1, "b" => 2, "c" => false], $defaults);
    $this->_ensureAssocKeysOrder([2, "a" => 1], ["a" => 1, "b" => 2, "c" => false], $defaults);

    $this->_ensureAssocKeysOrder([1], ["x_a" => 1, "x_b" => false, "x_c" => false], $defaults, [
      "key_prefix" => "x_",
    ]);
  }
}
