firefly-iii/app/Transformers/UserTransformer.php

89 lines
2.5 KiB
PHP
Raw Normal View History

<?php
/**
* UserTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III 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.
*
* Firefly III 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 Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
2018-02-13 11:24:06 -06:00
use FireflyIII\Models\Role;
use FireflyIII\User;
2018-03-03 01:12:18 -06:00
use League\Fractal\Resource\Collection as FractalCollection;
use League\Fractal\TransformerAbstract;
use Log;
use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class UserTransformer
*/
class UserTransformer extends TransformerAbstract
{
/** @var ParameterBag */
protected $parameters;
/**
2018-02-17 03:47:06 -06:00
* UserTransformer constructor.
*
* @codeCoverageIgnore
*
* @param ParameterBag $parameters
*/
public function __construct(ParameterBag $parameters)
{
$this->parameters = $parameters;
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
}
/**
2018-02-17 03:47:06 -06:00
* Transform user.
*
* @param User $user
*
* @return array
*/
public function transform(User $user): array
{
2018-02-13 11:24:06 -06:00
/** @var Role $role */
$role = $user->roles()->first();
2018-04-02 07:50:17 -05:00
if (null !== $role) {
2018-02-13 11:24:06 -06:00
$role = $role->name;
}
return [
2018-02-11 00:46:34 -06:00
'id' => (int)$user->id,
'created_at' => $user->created_at->toAtomString(),
2018-12-12 13:30:25 -06:00
'updated_at' => $user->updated_at->toAtomString(),
2018-02-11 00:46:34 -06:00
'email' => $user->email,
2018-08-06 12:14:30 -05:00
'blocked' => 1 === (int)$user->blocked,
2018-12-03 00:18:05 -06:00
'blocked_code' => '' === $user->blocked_code ? null : $user->blocked_code,
2018-02-13 11:24:06 -06:00
'role' => $role,
2018-02-11 00:46:34 -06:00
'links' => [
[
'rel' => 'self',
2018-02-11 00:46:34 -06:00
'uri' => '/users/' . $user->id,
],
],
];
}
2018-03-05 12:35:58 -06:00
}