<?php declare(strict_types=1);
/*
 * irstea/ng-model-generator-bundle generates Typescript interfaces for Angular using api-platform metadata.
 * Copyright (C) 2018 IRSTEA
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License and the GNU
 * Lesser General Public License along with this program. If not, see
 * <https://www.gnu.org/licenses/>.
 */

namespace Irstea\NgModelGeneratorBundle\Models;

use Irstea\NgModelGeneratorBundle\Exceptions\InvalidArgumentException;

/**
 * Class PHPClass.
 */
final class PHPClass implements \JsonSerializable
{
    use MultitonTrait;

    /** @var string */
    private $namespace;

    /** @var string */
    private $baseName;

    /**
     * ClassName constructor.
     *
     * @param string $name
     */
    private function __construct(string $name)
    {
        $groups = [];
        if (!preg_match('/^\\\\?((?:\w+\\\\)*)(\w+)$/i', $name, $groups)) {
            throw new InvalidArgumentException("Invalid PHP class name: $name");
        }
        [, $this->namespace, $this->baseName] = $groups;
    }

    /**
     * Get namespace.
     *
     * @return string
     */
    public function getNamespace(): string
    {
        return $this->namespace;
    }

    /**
     * Get baseName.
     *
     * @return string
     */
    public function getBaseName(): string
    {
        return $this->baseName;
    }

    /**
     * @return string
     */
    public function getFullName(): string
    {
        return $this->namespace . $this->baseName;
    }

    /**
     * @return bool
     */
    public function exists(): bool
    {
        return class_exists($this->getFullName());
    }

    /**
     * @throws \ReflectionException
     *
     * @return \ReflectionClass
     */
    public function getReflection(): \ReflectionClass
    {
        return new \ReflectionClass($this->getFullName());
    }

    /**
     * @return string
     */
    public function __toString()
    {
        return $this->getFullName();
    }

    /**
     * {@inheritdoc}
     */
    public function jsonSerialize()
    {
        return $this->getFullName();
    }

    /**
     * @param PHPClass $a
     * @param PHPClass $b
     *
     * @return bool
     */
    public static function baseNameOrdering(PHPClass $a, PHPClass $b): bool
    {
        return $a->getBaseName() > $b->getBaseName();
    }
}