<commit_before><?php
/**
 * This file is part of DomainSpecificQuery
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author <NAME> <<EMAIL>>
 */

namespace DSQ\Comperio\Compiler\Map;


use DSQ\Expression\BinaryExpression;
use DSQ\Lucene\FieldExpression;
use DSQ\Lucene\SpanExpression;

class StandardNumberMap
{
    private $numbers = array(
        'EAN' => 'mrc_d073_sa',
        'ISBN' => 'mrc_d010_sa',
        'ISSN' => 'mrc_d011_sa',
        'FINGERPRINT' => 'mrc_d012_sa',
        'ISMN' => 'mrc_d013_sa',
        'ARTICLE' => 'mrc_d014_sa',
        'ISRN' => 'mrc_d015_sa',
        'ISRC' => 'mrc_d016_sa',
        'OTHER' => 'mrc_d017_sa',
        'NLN' => 'mrc_d020_sa', #National Library Number
        'LDN' => 'mrc_d021_sa', #Legal Deposit Number
        'GOV' => 'mrc_d022_sa', #Governative number
        'CODEN' => 'mrc_d040_sa', #CODEN
        'PN' => 'mrc_d071_sa', #Publisher Number
        'UPC' => 'mrc_d072_sa',
    );

    /**
     * @param null $numbers
     */
    public function __construct($numbers = null)
    {
        if ($numbers)
            $this->numbers = $numbers;
    }

    /**
     * @param BinaryExpression $expr
     * @return FieldExpression|SpanExpression
     */
    public function __invoke(BinaryExpression $expr)
    {
        $rightVal = $expr->getRight()->getValue();

        if ($this->isSingleNumberValue($rightVal))
            return new FieldExpression($this->numbers[$rightVal['subfield']], $rightVal['value']);

        return $this->allNumbersExpression(isset($rightVal['value']) ? $rightVal['value'] : $rightVal);
    }

    /**
     * @param string|array $value
     * @return bool
     */
    private function isSingleNumberValue($value)
    {
        return
            is_array($value)
            && isset($value['subfield'])
            && isset($this->numbers[$value['subfield']])
            && isset($value['value'])
        ;
    }

    /**
     * @param $value
     * @return SpanExpression
     */
    private function allNumbersExpression($value)
    {
        $span = new SpanExpression('OR');

        foreach ($this->numbers as $field)
            $span->addExpression(new FieldExpression($field, $value));

        return $span;
    }
} <commit_msg>Fixed problems with php 5.3 and array indexes.<commit_after><?php
/**
 * This file is part of DomainSpecificQuery
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author <NAME> <<EMAIL>>
 */

namespace DSQ\Comperio\Compiler\Map;


use DSQ\Expression\BinaryExpression;
use DSQ\Lucene\FieldExpression;
use DSQ\Lucene\SpanExpression;

class StandardNumberMap
{
    private $numbers = array(
        'EAN' => 'mrc_d073_sa',
        'ISBN' => 'mrc_d010_sa',
        'ISSN' => 'mrc_d011_sa',
        'FINGERPRINT' => 'mrc_d012_sa',
        'ISMN' => 'mrc_d013_sa',
        'ARTICLE' => 'mrc_d014_sa',
        'ISRN' => 'mrc_d015_sa',
        'ISRC' => 'mrc_d016_sa',
        'OTHER' => 'mrc_d017_sa',
        'NLN' => 'mrc_d020_sa', #National Library Number
        'LDN' => 'mrc_d021_sa', #Legal Deposit Number
        'GOV' => 'mrc_d022_sa', #Governative number
        'CODEN' => 'mrc_d040_sa', #CODEN
        'PN' => 'mrc_d071_sa', #Publisher Number
        'UPC' => 'mrc_d072_sa',
    );

    /**
     * @param null $numbers
     */
    public function __construct($numbers = null)
    {
        if ($numbers)
            $this->numbers = $numbers;
    }

    /**
     * @param BinaryExpression $expr
     * @return FieldExpression|SpanExpression
     */
    public function __invoke(BinaryExpression $expr)
    {
        $rightVal = $expr->getRight()->getValue();

        if ($this->isSingleNumberValue($rightVal))
            return new FieldExpression($this->numbers[$rightVal['subfield']], $rightVal['value']);

        return $this->allNumbersExpression(
            is_array($rightVal) && isset($rightVal['value']) ? $rightVal['value'] : $rightVal
        );
    }

    /**
     * @param string|array $value
     * @return bool
     */
    private function isSingleNumberValue($value)
    {
        return
            is_array($value)
            && isset($value['subfield'])
            && isset($this->numbers[$value['subfield']])
            && isset($value['value'])
        ;
    }

    /**
     * @param $value
     * @return SpanExpression
     */
    private function allNumbersExpression($value)
    {
        $span = new SpanExpression('OR');

        foreach ($this->numbers as $field)
            $span->addExpression(new FieldExpression($field, $value));

        return $span;
    }
} 
{'fraction_non_alphanumeric': 0.14889549702633814, 'fraction_numerical': 0.01954120645709431, 'mean_word_length': 2.488148148148148, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7780', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1497, 'n_words': 303}