TitleRecord.php 5.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php

/**
 * Pica+ title record.
 *
 * This file is part of PicaRecord.
 *
 * PicaRecord is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * PicaRecord is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PicaRecord.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package   PicaRecord
 * @author    David Maus <maus@hab.de>
 * @copyright Copyright (c) 2012, 2013 by Herzog August Bibliothek Wolfenbüttel
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License v3
 */

namespace HAB\Pica\Record;

use InvalidArgumentException;

class TitleRecord extends NestedRecord
{

    /**
     * Append a field to the title record.
     *
     * @see Record::append()
     *
     * You can only directly add fields with a level of 0.
     *
     * @throws InvalidArgumentException Field level invalid
     * @throws InvalidArgumentException Field already in record
     *
     * @param  Field $field Field to append
     * @return void
     */
    public function append (Field $field)
    {
        if ($field->getLevel() !== 0) {
            throw new InvalidArgumentException("Invalid field level: {$field->getLevel()}");
        }
        parent::append($field);
    }

    /**
     * Set the record's fields.
     *
     * @todo   Relocate to \HAB\Pica\Record\Record::factory(), maybe
     *
     * @param  array $fields Field
     * @return void
     */
    public function setFields (array $fields)
    {
        $this->_fields = array();
        $this->_records = array();
        $prevLevel = null;
        foreach ($fields as $field) {
            $level = $field->getLevel();
            if ($level === 0) {
                $this->append($field);
            } else {
                if ($level === 1 && $prevLevel !== 1) {
                    $localRecord = new LocalRecord(array($field));
                    $this->addLocalRecord($localRecord);
                } else {
                    $records = $this->getLocalRecords();
                    $localRecord = end($records);
                    if ($level === 1) {
                        $localRecord->append($field);
                    } else {
                        $copyRecord = $localRecord->getCopyRecordByItemNumber($field->getOccurrence());
                        if ($copyRecord) {
                            $copyRecord->append($field);
                        } else {
                            $localRecord->addCopyRecord(new CopyRecord(array($field)));
                        }
                    }
                }
            }
            $prevLevel = $level;
        }
    }

    /**
     * Add a local record.
     *
     * @throws InvalidArgumentException Record already contains the local record
     *
     * @param  LocalRecord $record Local record
     * @return void
     */
    public function addLocalRecord (LocalRecord $record)
    {
        $this->addRecord($record);
        $record->setTitleRecord($this);
    }

    /**
     * Remove a local record.
     *
     * @param  LocalRecord $record Local record to remove
     * @return void
     */
    public function removeLocalRecord (LocalRecord $record)
    {
        $this->removeRecord($record);
        $record->unsetTitleRecord();
    }

    /**
     * Return array of all local records.
     *
     * @return array Local records
     */
    public function getLocalRecords ()
    {
        return $this->_records;
    }

    /**
     * Return a local record identified by its ILN.
     *
     * @param  integer $iln Intenal library number
     * @return LocalRecord|null
     */
    public function getLocalRecordByILN ($iln)
    {
        foreach ($this->getLocalRecords() as $localRecord) {
            if ($localRecord->getILN() == $iln) {
                return $localRecord;
            }
        }
        return null;
    }

    /**
     * Return the Pica production number (record identifier).
     *
     * @return string|null
     */
    public function getPPN ()
    {
        $ppnField = $this->getFirstMatchingField('003@/00');
        if ($ppnField) {
            $ppnSubfield = $ppnField->getNthSubfield('0', 0);
            if ($ppnSubfield) {
                return $ppnSubfield->getValue();
            }
        }
        return null;
    }

    /**
     * Set the Pica production number.
     *
     * Create a field 003@/00 if necessary.
     *
     * @param  string $ppn Pica production number
     * @return void
     */
    public function setPPN ($ppn)
    {
        $ppnField = $this->getFirstMatchingField('003@/00');
        if ($ppnField) {
            $ppnSubfield = $ppnField->getNthSubfield('0', 0);
            if ($ppnSubfield) {
                $ppnSubfield->setValue($ppn);
            } else {
                $ppnField->append(new Subfield('0', $ppn));
            }
        } else {
            $this->append(new Field('003@', 0, array(new Subfield('0', $ppn))));
        }
    }

    /**
     * Return true if title record contains the local record.
     *
     * @param  LocalRecord $record Local record
     * @return boolean
     */
    public function containsLocalRecord (LocalRecord $record)
    {
        return $this->containsRecord($record);
    }

    /**
     * Compare two local records.
     *
     * @see NestedRecord::compareRecords()
     *
     * Local records are compared by their ILN.
     *
     * @param  Record $a First record
     * @param  Record $b Second record
     * @return Comparism value
     */
    protected function compareRecords (Record $a, Record $b)
    {
        return $a->getILN() - $b->getILN();
    }

}