<commit_before><?php
/*
Copyright 2002-2012 MarkLogic Corporation.  All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace MarkLogic\MLPHP;
//use MarkLogic\MLPHP\SearchResult;

/**
 * Represents a search.
 *
 * @package MLPHP
 * @author <NAME> <<EMAIL>>
 */
class Search
{
    private $restClient; // @var RESTClient
    private $query; // @var string
    private $key; // @var string
    private $element; // @var string
    private $attribute; // @var string
    private $value; // @var string
    private $start; // @var int
    private $pageLength; // @var int
    private $options; // @var string
    private $view; // @var string
    private $format; // @var string
    private $collection; // @var string
    private $directory; // @var string

    /**
     * Create a Search object.
     *
     * @param RESTClient $restClient A REST client object.
     */
    public function __construct($restClient = null, $start = 1, $pageLength = 10, $view = 'all', $format = 'xml')
    {
        $this->restClient = $restClient;
        $this->start = (int)$start;
        $this->pageLength = (int)$pageLength;
        $this->view = (string)$view;
        $this->format = (string)$format;
    }

    /**
     * Set the REST client connection.
     *
     * @param RESTClient $restClient The RestClient object.
     */
    public function setConnection($restClient)
    {
        $this->restClient = $restClient;
    }

    /**
     * Get the common search parameters as an associated array.
     *
     * @return array An associated array of name/value pairs.
     */
    public function getParams()
    {
            $params = array('start' => $this->start, 'pageLength' => $this->pageLength, 'view' => $this->view, 'format' => $this->format);
            if(!empty($this->options)) {
                $params['options'] = $this->options;
            }
            if(!empty($this->collection)) {
                $params['collection'] = $this->collection;
            }
            if(!empty($this->directory)) {
                $params['directory'] = $this->directory;
            }
            return $params;
    }

    /**
     * Retrieve the search results using the REST client.
     *
     * @param string $query The search query.
     * @return SearchResults A search results object.
     */
    public function retrieve($query, $params = array())
    {

        $this->query = (string)$query;

        try {
            $params = array_merge(array('q' => $this->query), $this->getParams(), $params);
            $request = new RESTRequest('GET', 'search', $params);
            $response = $this->restClient->send($request);
            //print_r($response);
            $results = new SearchResults($response->getBody());
            return $results;
        } catch(Exception $e) {
            echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . PHP_EOL;
        }
    }

    /**
     * Retrieve the key-value search results for JSON content using the REST client.
     *
     * @param string $key The key (property name) in JSON content.
     * @param string $value The value (property value) in JSON content.
     * @return SearchResults A search results object.
     */
    public function retrieveKeyValue($key, $value, $params = array())
    {

        $this->key = (string)$key;
        $this->value = (string)$value;

        try {
            $params = array_merge(array('key' => $this->key, 'value' => $this->value), $this->getParams(), $params);
            $request = new RESTRequest('GET', 'keyvalue', $params);
            $response = $this->restClient->send($request);
            $results = new SearchResults($response->getBody());<commit_msg>Add support for (XML) structured search<commit_after><?php
/*
Copyright 2002-2012 MarkLogic Corporation.  All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
namespace MarkLogic\MLPHP;
//use MarkLogic\MLPHP\SearchResult;

/**
 * Represents a search.
 *
 * @package MLPHP
 * @author <NAME> <<EMAIL>>
 */
class Search
{
    private $restClient; // @var RESTClient
    private $query; // @var string
    private $key; // @var string
    private $element; // @var string
    private $attribute; // @var string
    private $value; // @var string
    private $start; // @var int
    private $pageLength; // @var int
    private $options; // @var string
    private $view; // @var string
    private $format; // @var string
    private $collection; // @var string
    private $directory; // @var string

    /**
     * Create a Search object.
     *
     * @param RESTClient $restClient A REST client object.
     */
    public function __construct($restClient = null, $start = 1, $pageLength = 10, $view = 'all', $format = 'xml')
    {
        $this->restClient = $restClient;
        $this->start = (int)$start;
        $this->pageLength = (int)$pageLength;
        $this->view = (string)$view;
        $this->format = (string)$format;
    }

    /**
     * Set the REST client connection.
     *
     * @param RESTClient $restClient The RestClient object.
     */
    public function setConnection($restClient)
    {
        $this->restClient = $restClient;
    }

    /**
     * Get the common search parameters as an associated array.
     *
     * @return array An associated array of name/value pairs.
     */
    public function getParams()
    {
            $params = array('start' => $this->start, 'pageLength' => $this->pageLength, 'view' => $this->view, 'format' => $this->format);
            if(!empty($this->options)) {
                $params['options'] = $this->options;
            }
            if(!empty($this->collection)) {
                $params['collection'] = $this->collection;
            }
            if(!empty($this->directory)) {
                $params['directory'] = $this->directory;
            }
            return $params;
    }

    /**
     * Retrieve the search results using the REST client.
     *
     * @param string $query The search query.  
     * @param array $params
     * @param bool $structured defaults to false
     * @return SearchResults A search results object.
     */
    public function retrieve($query, $params = array(), $structured = false)
    {
        $this->query = (string)$query;
        $params = array_merge(array(($structured ? 'structuredQuery' : 'q') => $this->query), $this->getParams(), $params);
        $request = new RESTRequest('GET', 'search', $params);

        try {
            $response = $this->restClient->send($request);
            //print_r($response);
            $results = new SearchResults($response->getBody());
            return $results;
        } catch(Exception $e) {
            echo $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . PHP_EOL;
        }
    }

    /**
     * Retrieve the key-value search results for JSON content using the REST client.
     *
     * @param string $key The key (property name) in JSON content.
     * @param string $value The value (property value) in JSON content.
     * @return SearchResults A search results object.
     */
    public function retrieveKeyValue($key, $value, $params = array())
    {

        $this->key = (string)$key;
        $this->value = (string)$value;

        try {
            $params = array_merge(array('key' => $this->key, 'value' => $this->value), $this->getParams(), $params);
            $request = new RESTRequest('GET', 'keyvalue', $params);
            $response = $this->restClient->send($request);
            $results = new SearchResults($response->getBody());
{'fraction_non_alphanumeric': 0.13755994853199205, 'fraction_numerical': 0.003509182360510001, 'mean_word_length': 2.413173652694611, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 89, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4986240', 'n_tokens_mistral': 2411, 'n_tokens_neox': 2337, 'n_words': 743}