<?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", * "user:creation:request", * "user:update-password:request" * }) */ 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; /** * @var string * @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 = []; /** * @var array[] * @Groups({ * "user:details:response", * "user:creation:request", * "user:creation:response", * "user:update:request", * "user:update:response" * }) */ protected $permissions = []; /** * @var array * @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 * * @param $name * * @return |null */ public function __get($name) { $methodName = 'get' . ucfirst($name); if (method_exists($this, $methodName)) { return $this->$methodName(); } else { return null; } } /** * @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); } } /** * @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); } 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); } }