ApplicationName.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?php declare(strict_types = 1);
  2. /*
  3. * This file is part of PharIo\Manifest.
  4. *
  5. * Copyright (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de> and contributors
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. *
  10. */
  11. namespace PharIo\Manifest;
  12. use function preg_match;
  13. use function sprintf;
  14. class ApplicationName {
  15. /** @var string */
  16. private $name;
  17. public function __construct(string $name) {
  18. $this->ensureValidFormat($name);
  19. $this->name = $name;
  20. }
  21. public function asString(): string {
  22. return $this->name;
  23. }
  24. public function isEqual(ApplicationName $name): bool {
  25. return $this->name === $name->name;
  26. }
  27. private function ensureValidFormat(string $name): void {
  28. if (!preg_match('#\w/\w#', $name)) {
  29. throw new InvalidApplicationNameException(
  30. sprintf('Format of name "%s" is not valid - expected: vendor/packagename', $name),
  31. InvalidApplicationNameException::InvalidFormat
  32. );
  33. }
  34. }
  35. }