Gc\Core\Object::getData PHP Method

getData() public method

If $key is empty will return all the data as an array Otherwise it will return value of the attribute specified by $key If $index is specified it will assume that attribute data is an array and retrieve corresponding member.
public getData ( string $key = '', string | integer $index = null ) : mixed
$key string key
$index string | integer Index
return mixed
    public function getData($key = '', $index = null)
    {
        if ('' === $key) {
            return $this->data;
        }
        $default = null;
        // accept a/b/c as ['a']['b']['c']
        // Not  !== false no need '/a/b always return null
        if (strpos($key, '/')) {
            $keyArray = explode('/', $key);
            $data = $this->data;
            foreach ($keyArray as $i => $k) {
                if ($k === '') {
                    return $default;
                }
                if (is_array($data)) {
                    if (!isset($data[$k])) {
                        return $default;
                    }
                    $data = $data[$k];
                }
            }
            return $data;
        }
        // legacy functionality for $index
        if (isset($this->data[$key])) {
            if (is_null($index)) {
                return $this->data[$key];
            }
            $value = $this->data[$key];
            if (is_array($value)) {
                if (isset($value[$index])) {
                    return $value[$index];
                }
                return null;
            } elseif (is_string($value)) {
                $array = explode(PHP_EOL, $value);
                return isset($array[$index]) && (!empty($array[$index]) || strlen($array[$index]) > 0) ? $array[$index] : null;
            } elseif ($value instanceof Object) {
                return $value->getData($index);
            }
            return $default;
        }
        return $default;
    }

Usage Example

Example #1
0
 /**
  * Test
  *
  * @return void
  */
 public function testOffsetUnset()
 {
     $this->object->setData('k', 'v');
     $this->object->offsetUnset('k');
     $this->assertNull($this->object->getData('k'));
 }