1<?php
2
3/* vim: set expandtab shiftwidth=4 tabstop=4 softtabstop=4 foldmethod=marker: */
4
5/**
6 * Parser for MARC records
7 *
8 * This package is based on the PHP MARC package, originally called "php-marc",
9 * that is part of the Emilda Project (http://www.emilda.org). Christoffer
10 * Landtman generously agreed to make the "php-marc" code available under the
11 * GNU LGPL so it could be used as the basis of this PEAR package.
12 *
13 * PHP version 5
14 *
15 * LICENSE: This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation; either version 2.1 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
28 *
29 * @category  File_Formats
30 * @package   File_MARC
31 * @author    Christoffer Landtman <landtman@realnode.com>
32 * @author    Dan Scott <dscott@laurentian.ca>
33 * @copyright 2003-2008 Oy Realnode Ab, Dan Scott
34 * @license   http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
35 * @version   CVS: $Id$
36 * @link      http://pear.php.net/package/File_MARC
37 */
38
39// {{{ class File_MARC_Subfield
40/**
41 * The File_MARC_Subfield class represents a single subfield in a MARC
42 * record field.
43 *
44 * Represents a subfield within a MARC field and implements all management
45 * functions related to a single subfield. This class also implements
46 * the possibility of duplicate subfields within a single field, for example
47 * 650 _z Test1 _z Test2.
48 *
49 * @category File_Formats
50 * @package  File_MARC
51 * @author   Christoffer Landtman <landtman@realnode.com>
52 * @author   Dan Scott <dscott@laurentian.ca>
53 * @license  http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
54 * @link     http://pear.php.net/package/File_MARC
55 */
56class File_MARC_Subfield
57{
58    // {{{ properties
59    /**
60     * Subfield code, e.g. _a, _b
61     * @var string
62     */
63    protected $code;
64
65    /**
66     * Data contained by the subfield
67     * @var string
68     */
69    protected $data;
70
71    /**
72     * Position of the subfield
73     * @var int
74     */
75    protected $position;
76
77    // }}}
78
79    // {{{ Constructor: function __construct()
80    /**
81     * File_MARC_Subfield constructor
82     *
83     * Create a new subfield to represent the code and data
84     *
85     * @param string $code Subfield code
86     * @param string $data Subfield data
87     */
88    function __construct($code, $data)
89    {
90        $this->code = $code;
91        $this->data = $data;
92    }
93    // }}}
94
95    // {{{ Destructor: function __destruct()
96    /**
97     * Destroys the subfield
98     */
99    function __destruct()
100    {
101        $this->code = null;
102        $this->data = null;
103        $this->position = null;
104    }
105    // }}}
106
107    // {{{ Explicit destructor: function delete()
108    /**
109     * Destroys the subfield
110     *
111     * @return true
112     */
113    function delete()
114    {
115        $this->__destruct();
116    }
117    // }}}
118
119    // {{{ getCode()
120    /**
121     * Return code of the subfield
122     *
123     * @return string Tag name
124     */
125    function getCode()
126    {
127        return (string)$this->code;
128    }
129    // }}}
130
131    // {{{ getData()
132    /**
133     * Return data of the subfield
134     *
135     * @return string data
136     */
137    function getData()
138    {
139        return (string)$this->data;
140    }
141    // }}}
142
143    // {{{ getPosition()
144    /**
145     * Return position of the subfield
146     *
147     * @return int data
148     */
149    function getPosition()
150    {
151        return $this->position;
152    }
153    // }}}
154
155    // {{{ __toString()
156    /**
157     * Return string representation of subfield
158     *
159     * @return string String representation
160     */
161    public function __toString()
162    {
163        $pretty = '[' . $this->getCode() . ']: ' . $this->getData();
164        return $pretty;
165    }
166    // }}}
167
168    // {{{ toRaw()
169    /**
170     * Return the USMARC representation of the subfield
171     *
172     * @return string USMARC representation
173     */
174    function toRaw()
175    {
176        $result = File_MARC::SUBFIELD_INDICATOR.$this->code.$this->data;
177        return (string)$result;
178    }
179    // }}}
180
181    // {{{ setCode()
182    /**
183     * Sets code of the subfield
184     *
185     * @param string $code new code for the subfield
186     *
187     * @return string code
188     */
189    function setCode($code)
190    {
191        if ($code) {
192            // could check more stringently; m/[a-Z]/ or the likes
193            $this->code = $code;
194        } else {
195            // code must be _something_; raise error
196            return false;
197        }
198        return true;
199    }
200    // }}}
201
202    // {{{ setData()
203    /**
204     * Sets data of the subfield
205     *
206     * @param string $data new data for the subfield
207     *
208     * @return string data
209     */
210    function setData($data)
211    {
212        $this->data = $data;
213        return true;
214    }
215    // }}}
216
217    // {{{ setPosition()
218    /**
219     * Sets position of the subfield
220     *
221     * @param string $pos new position of the subfield
222     *
223     * @return void
224     */
225    function setPosition($pos)
226    {
227        $this->position = $pos;
228    }
229    // }}}
230
231    // {{{ isEmpty()
232    /**
233     * Checks whether the subfield is empty or not
234     *
235     * @return bool True or false
236     */
237    function isEmpty()
238    {
239        // There is data
240        if (strlen($this->data)) {
241            return false;
242        }
243
244        // There is no data
245        return true;
246    }
247    // }}}
248}
249// }}}
250
251