Subfield.php 3.82 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
<?php

/**
 * Pica+ subfield.
 *
 * A subfield is a cons of an alphanumeric character and a possibly empty
 * string value.
 *
 * 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>
David Maus's avatar
David Maus committed
26
 * @copyright Copyright (c) 2012 - 2016 by Herzog August Bibliothek Wolfenbüttel
27
28
29
30
31
32
33
34
35
 * @license   http://www.gnu.org/licenses/gpl.html GNU General Public License v3
 */

namespace HAB\Pica\Record;

use InvalidArgumentException;

class Subfield
{
David Maus's avatar
David Maus committed
36
37
38
39
40
    /**
     * Regular expression matching a valid subfield code.
     *
     * @var string
     */
Sebastian Böttger's avatar
Sebastian Böttger committed
41
    public static $validSubfieldCodePattern = '/^[a-z0-9#]$/Di';
42
43
44
45
46
47
48
49
50

    /**
     * Return true if argument is a valid subfield code.
     *
     * @param  mixed $arg Variable to check
     * @return boolean
     */
    public static function isValidSubfieldCode ($arg)
    {
David Maus's avatar
David Maus committed
51
        return (bool)preg_match(self::$validSubfieldCodePattern, $arg);
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
    }

    /**
     * Return a new subfield based on its array representation.
     *
     * The array representation of a subfield is an associative array with the
     * keys `code' and `value', holding the subfield code and value.
     *
     * @throws InvalidArgumentException Missing code or value index
     *
     * @param  array $subfield Array representation of a subfield
     * @return Subfield New subfield
     */
    public static function factory (array $subfield)
    {
        if (!array_key_exists('code', $subfield)) {
            throw new InvalidArgumentException("Missing 'code' index in subfield array");
        }
        if (!array_key_exists('value', $subfield)) {
            throw new InvalidArgumentException("Missing 'value' index in subfield array");
        }
        return new Subfield($subfield['code'], $subfield['value']);
    }

    ///

    /**
     * The subfield code.
     *
     * @var string
     */
    protected $_code;

    /**
     * The subfield value.
     *
     * @var string Value
     */
    protected $_value;

    /**
     * Constructor.
     *
     * @throws InvalidArgumentException Invalid subfield code
     *
     * @param  string $code Subfield code
     * @param  string $value Subfield value
     * @return void
     */
    public function __construct ($code, $value)
    {
        if (!self::isValidSubfieldCode($code)) {
            throw new InvalidArgumentException("Invalid subfield code: {$code}");
        }
        $this->_code = $code;
        $this->setValue($value);
    }

    /**
     * Set the subfield value.
     *
     * @param  string $value Subfield value
     * @return void
     */
    public function setValue ($value)
    {
        $this->_value = $value;
    }

    /**
     * Return the subfield value.
     *
     * @return string Subfield value
     */
    public function getValue ()
    {
        return $this->_value;
    }

    /**
     * Return the subfield code.
     *
     * @return string Subfield code
     */
    public function getCode ()
    {
        return $this->_code;
    }

    /**
     * Return printable representation of the subfield.
     *
     * The printable representation of a subfield is its value.
     *
     * @return string Subfield value
     */
    public function __toString ()
    {
        return $this->getValue();
    }
Sebastian Böttger's avatar
Sebastian Böttger committed
152
}