public function testNullFields()
 {
     $doc = array('location' => null, 'info' => array('companyName' => null, 'address' => null, 'extra' => null));
     $notFound = (object) array();
     $this->assertEquals($notFound, gs\get($doc, 'location.lat', $notFound));
     $this->assertNull(gs\get($doc, 'location', $notFound));
 }
 public function test()
 {
     $data = new \ArrayObject($this->getData());
     $this->assertEquals('aa', gs\get($data, 'a.aa'));
     $this->assertEquals(null, gs\get($data, 'c', 'default'));
     $this->assertEquals('default', gs\get($data, 'd', 'default'));
     gs\set($data, 'd', array(0, 1, 2, 3, 4, 5));
     $this->assertEquals(4, gs\get($data, 'd.4'));
 }
 public function testMagicMethods()
 {
     $data = new MagicAccessTestClass();
     $data->first_name = 'Joe';
     $data->last_name = 'Rock';
     gs\setValue($data, 'profession', 'Stone cutter');
     gs\setValue($data, 'address', new MagicAccessTestClass());
     gs\set($data, 'address.city', 'Little Rock');
     $this->assertEquals('Joe', gs\getValue($data, 'first_name'));
     $this->assertEquals('Rock', gs\get($data, 'last_name'));
     $this->assertEquals('Little Rock', gs\get($data, 'address.city'));
 }
Exemple #4
0
 public function testMapToField()
 {
     $fnConcatNames = function ($doc) {
         return gs\get($doc, 'firstName') . ' ' . gs\get($doc, 'lastName');
     };
     $fn = fn\fnMapToField('fullName', $fnConcatNames);
     $person = array('firstName' => 'John', 'lastName' => 'Smith');
     $result = $fn($person);
     $this->assertEquals(array_merge(array('fullName' => 'John Smith'), $person), $result);
     // Write to a sub array.
     $fn = fn\fnMapToField('info.fullName', $fnConcatNames);
     $result = $fn($person);
     $this->assertEquals(array_merge(array('info' => array('fullName' => 'John Smith')), $person), $result);
     // As an object
     $result = $fn((object) $person);
     $this->assertEquals((object) array_merge(array('info' => array('fullName' => 'John Smith')), $person), $result);
 }
Exemple #5
0
 /**
  * @param array|ArrayAccess|object $doc
  * @param string[] $path
  * @param null|mixed $default
  * @return mixed
  */
 public static function getPath($doc, $path, $default = null)
 {
     return gs\get($doc, $path, $default);
 }
 /**
  * @description Gets the field value based upon the path.
  * @param array|object $data
  * @param string|array $pathToField
  * @param null|mixed   $default
  * @param string|null  $pathSeparator Optional path separator.
  * @return mixed
  */
 public function get($data, $pathToField, $default = null, $pathSeparator = null)
 {
     $pathSeparator = $pathSeparator ?: $this->pathSeparator;
     return gs\get($data, $pathToField, $default, $pathSeparator);
 }