]> git.street.me.uk Git - andy/gpx.git/blob - src/libgpx/copyright.php
Initial commit
[andy/gpx.git] / src / libgpx / copyright.php
1 <?php
2 /**
3  * copyright.php
4  *
5  * Copyright 2018 Andy Street <andy@street.me.uk>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20  * MA 02110-1301, USA.
21  *
22  *
23  */
24
25 namespace libgpx;
26
27 use \InvalidArgumentException;
28
29 /**
30  * Information about the copyright holder and any license governing use of
31  * the file.
32  *
33  * @see https://www.topografix.com/GPX/1/1/#type_copyrightType
34  *
35  * @author Andy Street <andy@street.me.uk>
36  */
37 class Copyright
38 {
39
40   /**
41    * The copyright holder.
42    *
43    * @var string
44    */
45   protected $author;
46
47   /**
48    * The year of copyright.
49    *
50    * @var string
51    */
52   protected $year;
53
54   /**
55    * Link to external file containing license text.
56    *
57    * @var string
58    */
59   protected $license;
60
61   /**
62    * Create new copyright information.
63    *
64    * @param string $author The copyright holder.
65    */
66   public function __construct(string $author)
67   {
68     $this->setAuthor($author);
69   }
70
71   /**
72    * Fetch the name of the copyright holder.
73    *
74    * @return string The copyright holder.
75    */
76   public function getAuthor()
77   {
78     return $this->author;
79   }
80
81   /**
82    * Set the name of the copyright holder.
83    *
84    * @param string $author The name of the copyright holder.
85    * @return void
86    */
87   public function setAuthor(string $author)
88   {
89     $this->author = $author;
90   }
91
92   /**
93    * Fetch the year of copyright.
94    *
95    * NOTE: The GPX 1.1 schema defines this field as type "xsd:gYear". It is
96    *       therefore possible that this function will return a timezone as
97    *       part of the string.
98    *
99    * @param boolean $asint Make the return value an integer (any timezones will
100    *        be stripped).
101    * @return string|int|null The copyright year or null if not set.
102    */
103   public function getYear(bool $asint = false)
104   {
105     return ($asint ? intval($this->year) : $this->year);
106   }
107
108   /**
109    * Set the year of copyright.
110    *
111    * @param string|null $year The copyright year in xsd:gYear format or null to
112    *        delete.
113    * @return void
114    * @throws InvalidArgumentException If the year is an invalid format.
115    */
116   public function setYear(string $year = null)
117   {
118     if ($year !== null) {
119       $year = trim($year);
120       $regex = '/^(-?)([0-9]+)(Z|[+-][0-9]{2}:[0-9]{2})?$/';
121       if (preg_match($regex, $year, $matches)) {
122         $year =
123           $matches[1] .
124           str_pad($matches[2], 4, '0', STR_PAD_LEFT);
125         if (isset($matches[3]))
126           $year .= $matches[3];
127       } else {
128         throw new InvalidArgumentException(
129           sprintf('Copyright year is an invalid format "%s".', $year)
130         );
131       }
132     }
133     $this->year = $year;
134   }
135
136   /**
137    * Fetch the URL of the license terms.
138    *
139    * @return string|null The URL or null if not set.
140    */
141   public function getLicense()
142   {
143     return $this->license;
144   }
145
146   /**
147    * Set the URL of the license terms.
148    *
149    * @param string|null $license The URL or null to delete.
150    * @return null
151    */
152   public function setLicense(string $license = null)
153   {
154     $this->license = $license;
155   }
156
157 }