MongoResult.php 3.23 KB
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 15/6/14
 * Time: 上午12:38
 */

namespace Hood\Dao\MongoDB;


class MongoResult
{
    private $_attrs = array();

    public function lastInsertId()
    {
        return $this->__get('_id')->__toString();
    }

    public function lastInsertMongoID()
    {
        return $this->__get('_id');
    }

    /**
     * @return bool
     */
    public function getStatus()
    {
        $status = $this->__get('status');
        if (!empty($status['ok']) && $status['ok'] == 1) {
            return true;
        }
        return false;
    }

    public function getStatusData()
    {
        return $this->__get('status');
    }

    public function getAttrs()
    {
        return $this->__get('attrs');
    }

    public function __get($name)
    {
        return $this->arrayGet($this->_attrs, $name);
    }

    public function __set($name, $val)
    {
        $this->_attrs = $this->arraySet($this->_attrs, $name, $val);
        return $this;
    }

    private function arraySet(array $array, $keys, $value)
    {
        if (is_array($keys) && empty($keys)) {
            return $array;
        }
        if (!is_array($keys)) {
            if (strstr($keys, "`")) {
                $keys = preg_replace_callback("/`(.+)`/U", create_function('$match', 'return str_replace(".", "\.", $match[1]);'), $keys);
            }
            $keys = preg_split("/(?<!\\\\)\\./", $keys);
        }
        if (count($keys) == 1) {
            $firstKey = array_pop($keys);
            $firstKey = str_replace("\\.", ".", $firstKey);
            $array[$firstKey] = $value;
            return $array;
        }
        $lastKey = array_pop($keys);
        $lastKey = str_replace("\\.", ".", $lastKey);
        $lastConfig = &$array;
        foreach ($keys as $key) {
            $key = str_replace("\\.", ".", $key);
            if (!isset($lastConfig[$key]) || !is_array($lastConfig[$key])) {
                $lastConfig[$key] = array();
            }
            $lastConfig = &$lastConfig[$key];
        }
        $lastConfig[$lastKey] = $value;
        return $array;
    }

    private function arrayGet(array $array, $keys)
    {
        if (is_array($keys) && empty($keys)) {
            return $array;
        }
        if (!is_array($keys)) {
            if (strstr($keys, "`")) {
                $keys = preg_replace_callback("/`(.+)`/U", create_function('$match', 'return str_replace(".", "\.", $match[1]);'), $keys);
            }
            $keys = preg_split("/(?<!\\\\)\\./", $keys);
        }
        if (count($keys) == 1) {
            $firstKey = array_pop($keys);
            $firstKey = str_replace("\\.", ".", $firstKey);
            return array_key_exists($firstKey, $array) ? $array[$firstKey] : null;
        }
        $lastKey = array_pop($keys);
        $lastKey = str_replace("\\.", ".", $lastKey);
        $lastArray = $array;
        foreach ($keys as $key) {
            $key = str_replace("\\.", ".", $key);
            if (is_array($lastArray) && array_key_exists($key, $lastArray)) {
                $lastArray = $lastArray[$key];
            } else {
                return null;
            }
        }

        return (is_array($lastArray) && array_key_exists($lastKey, $lastArray)) ? $lastArray[$lastKey] : null;
    }
}