Covivo/mobicoop

View on GitHub
api/src/I18n/Entity/Language.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

/**
 * Copyright (c) 2021, MOBICOOP. All rights reserved.
 * This project is dual licensed under AGPL and proprietary licence.
 ***************************
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU Affero General Public License as
 *    published by the Free Software Foundation, either version 3 of the
 *    License, or (at your option) any later version.
 *
 *    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 Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <gnu.org/licenses>.
 ***************************
 *    Licence MOBICOOP described in the file
 *    LICENSE
 */

namespace App\I18n\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use App\User\Entity\User;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\MaxDepth;

/**
 * A Language.
 *
 * @ORM\Entity()
 *
 * @ORM\HasLifecycleCallbacks
 *
 * @ApiResource(
 *      attributes={
 *          "normalization_context"={"groups"={"read"}, "enable_max_depth"="true"},
 *          "denormalization_context"={"groups"={"write"}}
 *      },
 *      collectionOperations={
 *          "get"={
 *              "swagger_context" = {
 *                  "tags"={"I18n"}
 *              }
 *          },
 *          "post"={
 *              "swagger_context" = {
 *                  "tags"={"I18n"}
 *              }
 *          },
 *      },
 *      itemOperations={
 *          "get"={
 *              "path"="/languages/{id}",
 *              "swagger_context" = {
 *                  "tags"={"I18n"}
 *              }
 *          },
 *          "put"={
 *              "swagger_context" = {
 *                  "tags"={"I18n"}
 *              }
 *          },
 *          "delete"={
 *              "swagger_context" = {
 *                  "tags"={"I18n"}
 *              }
 *          }
 *      }
 * )
 */
class Language
{
    public const LANGUAGES = [
        ['id' => 1, 'code' => 'fr'],
        ['id' => 2, 'code' => 'en'],
        ['id' => 3, 'code' => 'eu'],
        ['id' => 4, 'code' => 'it'],
        ['id' => 5, 'code' => 'de'],
        ['id' => 6, 'code' => 'es'],
        ['id' => 7, 'code' => 'nl'],
    ];

    /**
     * @var int the id of this language
     *
     * @ORM\Id
     *
     * @ORM\GeneratedValue
     *
     * @ORM\Column(type="integer")
     *
     * @ApiProperty(identifier=true)
     *
     * @Groups({"aRead","read", "readUser"})
     */
    private $id;

    /**
     * @var string the code of the language
     *
     * @ORM\Column(type="string", length=255)
     *
     * @Groups({"aRead","read","write", "readUser"})
     */
    private $code;

    /**
     * @var null|ArrayCollection the users of the section
     *
     * @ORM\OneToMany(targetEntity="\App\User\Entity\User", mappedBy="language")
     *
     * @Groups({"write"})
     *
     * @MaxDepth(1)
     */
    private $users;

    /**
     * @var null|ArrayCollection A Language can have multiple entry in Translate
     *
     * @ORM\OneToMany(targetEntity="\App\I18n\Entity\Translate", mappedBy="language", cascade={"persist"})
     *
     * @MaxDepth(1)
     */
    private $translates;

    public function __construct()
    {
        $this->users = new ArrayCollection();
        $this->translates = new ArrayCollection();
    }

    public function getId(): int
    {
        return $this->id;
    }

    public function getCode(): string
    {
        return $this->code;
    }

    public function setCode(string $code): self
    {
        $this->code = $code;

        return $this;
    }

    /**
     * @return ArrayCollection|User[]
     */
    public function getUsers(): ArrayCollection
    {
        return $this->users;
    }

    public function addUser(User $user): self
    {
        if (!$this->users->contains($user)) {
            $this->users[] = $user;
            $user->setLanguage($this);
        }

        return $this;
    }

    public function removeUser(User $user): self
    {
        if ($this->users->contains($user)) {
            $this->users->removeElement($user);
            // set the owning side to null (unless already changed)
            if ($user->getLanguage() === $this) {
                $user->setLanguage(null);
            }
        }

        return $this;
    }

    /**
     * @return ArrayCollection|Translate[]
     */
    public function getTranslates(): ArrayCollection
    {
        return $this->translates;
    }

    public function addTranslate(Translate $translate): self
    {
        if (!$this->translates->contains($translate)) {
            $this->translates[] = $translate;
            $translate->setLanguage($this);
        }

        return $this;
    }

    public function removeTranslate(Translate $translate): self
    {
        if ($this->translates->contains($translate)) {
            $this->translates->removeElement($translate);
            // set the owning side to null (unless already changed)
            if ($translate->getLanguage() === $this) {
                $translate->setLanguage(null);
            }
        }

        return $this;
    }
}