<commit_before><?php

namespace BCLib\PrimoServices;

use Doctrine\Common\Cache\Cache;

class PNXTranslator
{

    private $_bib_record_factory;
    private $_holding_factory;
    private $_person_factory;
    private $_bib_record_component_factory;
    private $_record;

    /** @var \Doctrine\Common\Cache\Cache * */
    private $_cache;

    public function __construct($bib_record_factory, $holding_factory,
                                $person_factory, $bib_record_component_factory,
                                Cache $cache)
    {
        $this->_bib_record_factory = $bib_record_factory;
        $this->_holding_factory = $holding_factory;
        $this->_person_factory = $person_factory;
        $this->_bib_record_component_factory = $bib_record_component_factory;
        $this->_cache = $cache;
    }

    /**
     * @param \SimpleXMLElement $doc_xml
     *
     * @return BibRecord[]
     */
    public function translate(\SimpleXMLElement $doc_xml)
    {
        $xpath_to_primo_record = '//sear:DOC/PrimoNMBib/record';
        $xpath_to_pci_record = '//sear:DOC/prim:PrimoNMBib/prim:record';

        $docs_xml = $doc_xml->xpath($xpath_to_primo_record . '|' . $xpath_to_pci_record);
        return \array_map(array($this, '_extractDoc'), $docs_xml);
    }

    private function _extractDoc(\SimpleXMLElement $record_xml)
    {
        /** @var $record BibRecord */
        $this->_record = $record = $this->_bib_record_factory->__invoke();

        $this->_record = $record;
        $this->_record->id = (string) $record_xml->control->recordid;
        $this->_record->title = (string) $record_xml->display->title;
        $this->_record->date = (string) $record_xml->display->creationdate;
        $this->_record->publisher = (string) $record_xml->display->publisher;
        $this->_record->abstract = (string) $record_xml->addata->abstract;
        $this->_record->type = (string) $record_xml->display->type;
        $this->_record->availability = (string) $record_xml->display->availpnx;
        $this->_record->isbn = (string) $record_xml->search->isbn;
        $this->_record->issn = (string) $record_xml->search->issn;
        $this->_record->oclcid = (string) $record_xml->addata->oclcid;
        $this->_record->reserves_info = (string) $record_xml->addata->lad05;
        $this->_record->display_subject = (string) $record_xml->display->subject;
        $this->_record->format = (string) $record_xml->display->format;
        $this->_record->oclcid = (string) $record_xml->addata->oclcid;
        $this->_record->link_to_worldcat = $this->_linkToWorldCat($this->_record->oclcid);

        $this->_record->subjects = $this->_extractFieldArray($record_xml, 'facets', 'topic');
        $this->_record->genres = $this->_extractFieldArray($record_xml, 'facets', 'genre');
        $this->_record->languages = $this->_extractFieldArray($record_xml, 'facets', 'language');
        $this->_record->components = $this->_extractComponents($record_xml);

        $this->_cache->save($this->_record->id, $this->_record, 1200);

        return $this->_record;
    }

    private function _extractFieldArray(\SimpleXMLElement $xml, $section, $field)
    {
        $result = [];
        foreach ($xml->$section->$field as $item)
        {
            $result[] = (string) $item;
        }
        return $result;
    }

    private function _extractComponents($record_xml)
    {
        /** @var $components BibRecordComponent[] */
        $components = [];

        $helper = [];

        $helper['sourceid'] = $this->_extractMultiPartField($record_xml->control->sourceid);
        $helper['originalsourceid'] = $this->_extractMultiPartField($record_xml->control->originalsourceid);
        $helper['sourcerecordid'] = $this->_extractMultiPartField($record_xml->control->sourcerecordid);
        $helper['institution'] = $this->_extractMultiPartField($record_xml->delivery->institution);
        $helper['delcategory'] = $this->_extractMultiPartField($record_xml->delivery->delcategory);

        /** @var $component BibRecordComponent */
        foreach ($helper['delcategory'] as $id => $delcategory)
        {
            $component = $this->_bib_record_component_factory->__invoke();

            $component->delivery_category = $delcategory;
            $component->source_record_id = $helper['sourcerecordid'][$id];
            $components[] = $component;
        }

        return $components;<commit_msg>Added Alma IDs<commit_after><?php

namespace BCLib\PrimoServices;

use Doctrine\Common\Cache\Cache;

class PNXTranslator
{

    private $_bib_record_factory;
    private $_holding_factory;
    private $_person_factory;
    private $_bib_record_component_factory;
    private $_record;

    /** @var \Doctrine\Common\Cache\Cache * */
    private $_cache;

    public function __construct($bib_record_factory, $holding_factory,
                                $person_factory, $bib_record_component_factory,
                                Cache $cache)
    {
        $this->_bib_record_factory = $bib_record_factory;
        $this->_holding_factory = $holding_factory;
        $this->_person_factory = $person_factory;
        $this->_bib_record_component_factory = $bib_record_component_factory;
        $this->_cache = $cache;
    }

    /**
     * @param \SimpleXMLElement $doc_xml
     *
     * @return BibRecord[]
     */
    public function translate(\SimpleXMLElement $doc_xml)
    {
        $xpath_to_primo_record = '//sear:DOC/PrimoNMBib/record';
        $xpath_to_pci_record = '//sear:DOC/prim:PrimoNMBib/prim:record';

        $docs_xml = $doc_xml->xpath($xpath_to_primo_record . '|' . $xpath_to_pci_record);
        return \array_map(array($this, '_extractDoc'), $docs_xml);
    }

    private function _extractDoc(\SimpleXMLElement $record_xml)
    {
        /** @var $record BibRecord */
        $this->_record = $record = $this->_bib_record_factory->__invoke();

        $this->_record = $record;
        $this->_record->id = (string) $record_xml->control->recordid;
        $this->_record->title = (string) $record_xml->display->title;
        $this->_record->date = (string) $record_xml->display->creationdate;
        $this->_record->publisher = (string) $record_xml->display->publisher;
        $this->_record->abstract = (string) $record_xml->addata->abstract;
        $this->_record->type = (string) $record_xml->display->type;
        $this->_record->availability = (string) $record_xml->display->availpnx;
        $this->_record->isbn = (string) $record_xml->search->isbn;
        $this->_record->issn = (string) $record_xml->search->issn;
        $this->_record->oclcid = (string) $record_xml->addata->oclcid;
        $this->_record->reserves_info = (string) $record_xml->addata->lad05;
        $this->_record->display_subject = (string) $record_xml->display->subject;
        $this->_record->format = (string) $record_xml->display->format;
        $this->_record->oclcid = (string) $record_xml->addata->oclcid;
        $this->_record->link_to_worldcat = $this->_linkToWorldCat($this->_record->oclcid);

        $this->_record->subjects = $this->_extractFieldArray($record_xml, 'facets', 'topic');
        $this->_record->genres = $this->_extractFieldArray($record_xml, 'facets', 'genre');
        $this->_record->languages = $this->_extractFieldArray($record_xml, 'facets', 'language');
        $this->_record->components = $this->_extractComponents($record_xml);

        $this->_cache->save($this->_record->id, $this->_record, 1200);

        return $this->_record;
    }

    private function _extractFieldArray(\SimpleXMLElement $xml, $section, $field)
    {
        $result = [];
        foreach ($xml->$section->$field as $item)
        {
            $result[] = (string) $item;
        }
        return $result;
    }

    private function _extractComponents($record_xml)
    {
        /** @var $components BibRecordComponent[] */
        $components = [];

        $helper = [];

        $helper['sourceid'] = $this->_extractMultiPartField($record_xml->control->sourceid);
        $helper['originalsourceid'] = $this->_extractMultiPartField($record_xml->control->originalsourceid);
        $helper['sourcerecordid'] = $this->_extractMultiPartField($record_xml->control->sourcerecordid);
        $helper['institution'] = $this->_extractMultiPartField($record_xml->delivery->institution);
        $helper['delcategory'] = $this->_extractMultiPartField($record_xml->delivery->delcategory);
        $helper['alma_id'] = $this->_extractMultiPartField($record_xml->control->almaid);

        /** @var $component BibRecordComponent */
        foreach ($helper['delcategory'] as $id => $delcategory)
        {
            $component = $this->_bib_record_component_factory->__invoke();

            $component->delivery_category = $delcategory;
            $component->source_record_id = $helper['sourcerecordid'][$id];

            $alma_id_key = str_replace('ALMA-BC','01BC_INST:', $id);
            $component->alma_id = $helper['alma_id'][$alma_id_key];
            $components[] = $component;
        }

        return $components;
{'fraction_non_alphanumeric': 0.15601276268016284, 'fraction_numerical': 0.0015403234679282648, 'mean_word_length': 3.163994502977554, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 229, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 65}, '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': '6189883', 'n_tokens_mistral': 2822, 'n_tokens_neox': 2706, 'n_words': 380}