Search for first object that match accurate data

- with data[':etag' => 'xxx'] you can search for
specific object by hash tag

@param array $data

@return mixed|null
491690
function findOne(array $data)
    {
        $r = null;
        foreach ( $this->find($data) as $r)
            break;

        return $r;
    }
549179
protected function getResult($data)
    {
        if (is_array($data)) {
            header('content-type: application/json');
            return $this->cypher->encode(json_encode($data));
        }

        return $data;
    }
977828
private static function decode($data) {
		if((is_array($data) && isset($data[0])) || (isset($data[0]) && !is_string($data[0]))) {
			return self::parseArray($data);
		} else if (is_null($data) || is_scalar($data)) {
			return $data;
		} else {
			return self::decodeNewObject($data);
		}
	}
1203002
public function match($data)
    {
        if (is_object($data)) {
            $data = get_class($data);
        }

        return in_array($data, (array)$this->getValue());
    }
243352
private function parseSingleRelationship($data)
    {
        if ($data === null) {
            $result = null;
        } elseif (is_array($data) === true &&
            array_key_exists(DI::KEYWORD_TYPE, $data) === true &&
            array_key_exists(DI::KEYWORD_ID, $data) === true &&
            is_scalar($type = $data[DI::KEYWORD_TYPE]) === true &&
            is_scalar($index = $data[DI::KEYWORD_ID]) === true
        ) {
            $result = [$type => $index];
        } else {
            $result = false;
        }

        return $result;
    }
211424
public function search(array $data)
    {
        $url = "https://api.weixin.qq.com/shakearound/device/search?access_token=ACCESS_TOKEN";
        $this->registerApi($url, __FUNCTION__, func_get_args());
        return $this->httpPostForJson($url, $data);
    }
1205007
protected function createObjectFromData(array $data) {
    // determine if we are loading or creating
    $createFromLoadedData = (sizeof($data) > 0) ? true : false;

    // initialize data and oid
    $oid = null;
    $initialData = $data;
    if ($createFromLoadedData) {
      $oid = $this->constructOID($initialData);
      // cleanup data
      foreach($initialData as $name => $value) {
        if ($this->hasAttribute($name) || strpos($name, self::INTERNAL_VALUE_PREFIX) === 0) {
          $value = $this->convertValueFromStorage($name, $value);
          $initialData[$name] = $value;
        }
        else {
          unset($initialData[$name]);
        }
      }
    }

    // construct object
    $object = $this->createObject($oid, $initialData);
    return $object;
  }
1284735
public static function first(array $data, ?string $key = null)
    {
        if (count($data) === 0) {
            return null;
        }
        reset($data);
        $element = current($data);
        if ($key !== null && is_array($element) && isset($element[$key])) {
            return $element[$key];
        }
        return $element;
    }
906729
public function whereExists(array $data)
    {
        $this->parseConstraints($data);

        $query = $this->connection->getPdo()->prepare($this->getSelectQuery());

        return $this->execute($query)->fetch();
    }
1096097
private static function getObjectFromArray($data): self
    {
        if (!isset($data['last_access'])) {
            $lastAccess = null;
        } else {
            $lastAccess = new \DateTime($data['last_access']);
        }

        return new self(
            $data['user_id'],
            $data['organization_id'],
            $data['roles'],
            $data['active'],
            $lastAccess
        );
    }
1316144
public static function parseSignature($data)
    {
        if (empty($data)) {
            return null;
        }

        $signatureData = array();

        $list = explode(';', rtrim($data, ';'));
        if (empty($list)) {
            return null;
        }

        foreach ($list as $value) {
            $explode = explode('=', $value);
            if (count($explode) != 2) {
                return null;
            }
            $signatureData[$explode[0]] = $explode[1];
        }

        return $signatureData;
    }
54655
protected function parseArrayOfPrimaryData(array $data)
    {
        $isValid = true;
        $result  = null;
        foreach ($data as $primaryData) {
            $parsed = $this->parseSinglePrimaryData($primaryData);
            if ($parsed === null) {
                $isValid = false;
            } else {
                $result[] = $parsed;
            }
        }

        return $isValid === true ? $result : null;
    }
451369
public function getObject($data)
    {
        $obj = (object)json_decode($data);

        if ($obj->success === false) {
            $this->handleErrors($obj);

            return null;
        }

        return $obj->data;
    }
287335
public function search(array $data = [])
    {
        //set the defaults
        $filter = [];
        $range = 50;
        $start = 0;
        $order = ['model_id' => 'asc'];
        $count = 0;

        //merge passed data with default data
        if (isset($data['filter']) && is_array($data['filter'])) {
            $filter = $data['filter'];
        }

        if (isset($data['range']) && is_numeric($data['range'])) {
            $range = $data['range'];
        }

        if (isset($data['start']) && is_numeric($data['start'])) {
            $start = $data['start'];
        }

        if (isset($data['order']) && is_array($data['order'])) {
            $order = $data['order'];
        }

        //prepare the search object
        $search = [];


        //keyword search
        if (isset($data['q'])) {
            if (!is_array($data['q'])) {
                $data['q'] = [$data['q']];
            }

            foreach ($data['q'] as $keyword) {
                $search['query']['bool']['filter'][]['query_string'] = [
                    'query' => $keyword . '*',
                    'fields' => [
                        'model_singular','model_plural','model_detail',
                    ],
                    'default_operator' => 'AND'
                ];
            }
        }


        //generic full match filters

        //model_active
        if (!isset($filter['model_active'])) {
            $filter['model_active'] = 1;
        }


        foreach ($filter as $key => $value) {
            $search['query']['bool']['filter'][]['term'][$key] = $value;
        }

        //add sorting
        foreach ($order as $sort => $direction) {
            $search['sort'] = [$sort => $direction];
        }

        try {
            $results = $this->resource->search([
                'index' => static::INDEX_NAME,
                'type' => static::INDEX_TYPE,
                'body' => $search,
                'size' => $range,
                'from' => $start
            ]);
        } catch (NoNodesAvailableException $e) {
            return false;
        }

        // fix it
        $rows = array();

        foreach ($results['hits']['hits'] as $item) {
            $rows[] = $item['_source'];
        }

        //return response format
        return [
            'rows' => $rows,
            'total' => $results['hits']['total']
        ];
    }
538683
private function getErrorFromRegData($data)
	{
		$data['username'] = e($data['username']);
		$validator = Validator::make($data, Validators::rulesForRegisterAcc(), Validators::messagesForErrors());
		$userMessages = $validator->messages();

		if ($validator->fails()) {
			$result['errors'] = array(
				'username'        => $userMessages->first('username'),
				'callback'        => $userMessages->first('callback'),
				'email'           => $userMessages->first('email'),
				'password'        => $userMessages->first('password'),
				'confirmPassword' => $userMessages->first('confirmPassword'),
			);

			return $result;
		}

		return null;
	}
971326
private function _toCleanArray(array $data)
    {
        switch (count($data)) {
            case 0:
                return null;

            case 1:
                if (isset($data[0])) {
                    // Return array content when only one item
                    // with the key 0.
                    if (is_array($data[0])) {
                        return $this->_toCleanArray($data[0]);
                    } else {
                        return $data[0];
                    }
                }
                break;

            case 2:
                if (isset($data[0], $data[1]) && is_string($data[1])) {
                    if (('info' === $data[1]) || ('warning' === $data[1]) || ('error' === $data[1])) {
                        if (is_array($data[0])) {
                            return $this->_toCleanArray($data[0]);
                        } else {
                            return $data[0];
                        }
                    }
                }
        }
        $output = array();
        foreach ($data as $key => $value) {
            if (is_array($value)) {
                $output[$key] = $this->_toCleanArray($value);
            } else {
                if (is_string($value)) {
                    // Filter passwords from the log
                    if (\MUtil_String::contains($key, 'password', true) || \MUtil_String::contains($key, 'pwd', true)) {
                        $value = '****';
                    }
                } elseif ($value instanceof Zend_Date) {
                    // Output iso representation for date objects
                    $value = $value->getIso();
                }
                $output[$key] = $value;
            }
        }

        return $output;
    }
384067
final public static function getInstance($data=null)
    {
        $class = get_called_class();
        if (empty($class)) return null;

        if (empty($data)){
            $classID = $class;

        } elseif(is_array($data)){
            $classID = $class.md5(implode('', array_keys($data)));

        } else {
            $classID = $class.md5($data);
        }

        if (!isset(self::$_instances[$classID])){
            self::$_instances[$classID] = new $class($data);
        }

        return self::$_instances[$classID];
    }