Newer
Older
<?php
/**
* Copyright (C) 2019 Leipzig University Library
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* @author Sebastian Kehr <kehr@ub.uni-leipzig.de>
* @license http://opensource.org/licenses/gpl-2.0.php GNU GPLv2
*/
namespace fid\Service\DataTransferObject;
use Symfony\Component\Serializer\Annotation\Groups;
class User
{
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $id;
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:response",
* "user:update-username:request"
* })
*/
protected $username;
/**
* @var null|string
* @Groups({
* "user:details:response",
* })
*/
protected $password;
/**
* @var string
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $salutation;
/**
* @var string
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $academicTitle;
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $firstname;
/**
* @var null|string
* @Groups({
* "order:creation:response",
* "order:details:response",
* "order:list:response",
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $lastname;
/**
* @var null|string
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $homeLibrary;
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $college = '';
/**
* @var string
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $jobTitle = '';
* @var Address[]
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $addresses = [];
/**
* @var Order[]
* @Groups({
* "user:details:response",
* "user:creation:response",
* "user:update:response"
* })
*/
protected $orders = [];
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $permissions = [];
* @Groups({
* "user:details:response",
* "user:creation:request",
* "user:creation:response",
* "user:update:request",
* "user:update:response"
* })
*/
protected $data = [];
/**
* Magic function as shortcut for all getters
* TODO ensure public visibility of all called getters
* @return |null
*/
public function __get($name)
{
$methodName = 'get' . ucfirst($name);
if (method_exists($this, $methodName)) {
} else {
return null;
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* @return string|null
*/
public function getId(): ?string
{
return $this->id;
}
/**
* @param string|null $id
*/
public function setId(?string $id): void
{
$this->id = $id;
}
/**
* @return string|null
*/
public function getUsername(): ?string
{
return $this->username;
}
/**
* @param string|null $username
*/
public function setUsername(?string $username): void
{
$this->username = $username;
}
/**
* @return string|null
*/
public function getPassword(): ?string
{
return $this->password;
}
/**
* @param string|null $password
*/
public function setPassword(?string $password): void
{
$this->password = $password;
}
/**
* @return string|null
*/
public function getSalutation(): ?string
{
return $this->salutation;
}
/**
* @param string|null $salutation
*/
public function setSalutation(?string $salutation): void
{
$this->salutation = $salutation;
}
/**
* @return string|null
*/
public function getAcademicTitle(): ?string
{
return $this->academicTitle;
}
/**
* @param string|null $academicTitle
*/
public function setAcademicTitle(?string $academicTitle): void
{
$this->academicTitle = $academicTitle;
}
/**
* @return string|null
*/
public function getFirstname(): ?string
{
return $this->firstname;
}
/**
* @param string|null $firstname
*/
public function setFirstname(?string $firstname): void
{
$this->firstname = $firstname;
}
/**
* @return string|null
*/
public function getLastname(): ?string
{
return $this->lastname;
}
/**
* @param string|null $lastname
*/
public function setLastname(?string $lastname): void
{
$this->lastname = $lastname;
}
/**
* @return string
*/
public function getHomeLibrary(): string
{
return $this->homeLibrary;
}
/**
* @param string $homeLibrary
*/
public function setHomeLibrary(string $homeLibrary): void
{
$this->homeLibrary = $homeLibrary;
}
/**
* @return string
*/
public function getCollege(): string
{
return $this->college;
}
/**
* @param string $college
*/
public function setCollege(string $college): void
{
$this->college = $college;
}
/**
* @return string|null
*/
public function getJobTitle(): ?string
{
return $this->jobTitle;
}
/**
* @param string|null $jobTitle
*/
public function setJobTitle(?string $jobTitle): void
{
$this->jobTitle = $jobTitle;
}
/**
* @return Address[]
*/
public function getAddresses(): array
{
return $this->addresses;
}
* @return Address | null
public function getDeliveryAddress(): ?Address
if (empty($addresses = $this->getAddresses())) return null;
$addressIndex = !empty($this->getData()["deliveryAddress"]) ? $this->getData()["deliveryAddress"] : 0;
return $addresses[$addressIndex];
}
/**
* @return boolean
*/
public function deliveryAddressIsBusinessAddress(): bool
{
$addressIndex = !empty($this->getData()["deliveryAddress"]) ? $this->getData()["deliveryAddress"] : 0;
if ($addressIndex == 0) {
return true;
}
else {
return false;
}
}
/**
* @return boolean
*/
public function isNewsletter(): bool
{
return !empty($this->getData()["newsletter"]) ? $this->getData()["newsletter"] : 0;
}
/**
* @param Address[] $addresses
*/
public function setAddresses(array $addresses): void
{
$this->addresses = $addresses;
foreach ($addresses as $address) {
$address->setUser($this);
}
}
/**
* @return Order[]
*/
public function getOrders(): array
{
return $this->orders;
}
/**
* @param Order[] $orders
*/
public function setOrders(array $orders): void
{
$this->orders = $orders;
foreach ($orders as $order) {
$order->setUser($this);
}
}

Alexander Purr
committed
/**
* @param Order $order
*/
public function addOrder($order): void {
$this->orders[] = $order;
}
/**
* @return array[]
*/
public function getPermissions(): array
{
return $this->permissions;
}
/**
* @param array[] $permissions
*/
public function setPermissions(array $permissions): void
{
$this->permissions = $permissions;
}
public function hasPermission($permission): bool
{
if (
isset($this->permissions[$permission])
&& $this->permissions[$permission] === 'granted'
) {
return true;
}
return false;
}
/**
* @return array
*/
public function getData(): array
{
return $this->data;
}
/**
* @param array $data
*/
public function setData(array $data): void
{
$this->data = $data;
}
/**
* @return string
*/
public function getLiberoId () : string {
return 'FID'.str_pad($this->getId(),10,"0",STR_PAD_LEFT);
}
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
public function getFieldList()
{
$fields = [];
foreach ($this as $property => $value) {
if ($property === 'password') continue;
$fields[] = $property;
}
return $fields;
}
public function export($fields,$delimiter="\t") {
$output = '';
foreach ($fields as $property) {
if ($property === 'password') continue;
if ($property === 'permissions') {
$value = [];
$permissions = $this->getPermissions();
foreach ($permissions as $key => $perm) {
$value[] = $key.' ('.$perm.')';
}
} else {
$value = $this->{$property} ?? '';
if ($property === 'data' && is_array($value))
{
array_walk(
$value,
function(&$data,$key) {
if($key == 'phone') {
$data = $key.': '.($data ? $data: 'no');
}
else {
$data = $key.': '.($data ? 'yes': 'no');
}
}
);
}
}
if (is_array($value)) {
$value = implode(', ', $value);
}
$output .= $value . $delimiter;
}
return rtrim($output,$delimiter);
}