laravel-6 support

This commit is contained in:
RafficMohammed
2023-01-08 01:17:22 +05:30
parent 1a5c16ae4b
commit 774eed8b0e
4962 changed files with 279380 additions and 297961 deletions

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,22 +7,14 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
use PharIo\Version\Exception as VersionException;
use PharIo\Version\Version;
use PharIo\Version\VersionConstraintParser;
class ManifestDocumentMapper {
/**
* @param ManifestDocument $document
*
* @returns Manifest
*
* @throws ManifestDocumentMapperException
*/
public function map(ManifestDocument $document) {
public function map(ManifestDocument $document): Manifest {
try {
$contains = $document->getContainsElement();
$type = $this->mapType($contains);
@@ -39,20 +31,13 @@ class ManifestDocumentMapper {
$bundledComponents
);
} catch (VersionException $e) {
throw new ManifestDocumentMapperException($e->getMessage(), $e->getCode(), $e);
throw new ManifestDocumentMapperException($e->getMessage(), (int)$e->getCode(), $e);
} catch (Exception $e) {
throw new ManifestDocumentMapperException($e->getMessage(), $e->getCode(), $e);
throw new ManifestDocumentMapperException($e->getMessage(), (int)$e->getCode(), $e);
}
}
/**
* @param ContainsElement $contains
*
* @return Type
*
* @throws ManifestDocumentMapperException
*/
private function mapType(ContainsElement $contains) {
private function mapType(ContainsElement $contains): Type {
switch ($contains->getType()) {
case 'application':
return Type::application();
@@ -63,22 +48,14 @@ class ManifestDocumentMapper {
}
throw new ManifestDocumentMapperException(
sprintf('Unsupported type %s', $contains->getType())
\sprintf('Unsupported type %s', $contains->getType())
);
}
/**
* @param CopyrightElement $copyright
*
* @return CopyrightInformation
*
* @throws InvalidUrlException
* @throws InvalidEmailException
*/
private function mapCopyright(CopyrightElement $copyright) {
private function mapCopyright(CopyrightElement $copyright): CopyrightInformation {
$authors = new AuthorCollection();
foreach($copyright->getAuthorElements() as $authorElement) {
foreach ($copyright->getAuthorElements() as $authorElement) {
$authors->add(
new Author(
$authorElement->getName(),
@@ -99,14 +76,7 @@ class ManifestDocumentMapper {
);
}
/**
* @param RequiresElement $requires
*
* @return RequirementCollection
*
* @throws ManifestDocumentMapperException
*/
private function mapRequirements(RequiresElement $requires) {
private function mapRequirements(RequiresElement $requires): RequirementCollection {
$collection = new RequirementCollection();
$phpElement = $requires->getPHPElement();
$parser = new VersionConstraintParser;
@@ -115,8 +85,8 @@ class ManifestDocumentMapper {
$versionConstraint = $parser->parse($phpElement->getVersion());
} catch (VersionException $e) {
throw new ManifestDocumentMapperException(
sprintf('Unsupported version constraint - %s', $e->getMessage()),
$e->getCode(),
\sprintf('Unsupported version constraint - %s', $e->getMessage()),
(int)$e->getCode(),
$e
);
}
@@ -131,7 +101,7 @@ class ManifestDocumentMapper {
return $collection;
}
foreach($phpElement->getExtElements() as $extElement) {
foreach ($phpElement->getExtElements() as $extElement) {
$collection->add(
new PhpExtensionRequirement($extElement->getName())
);
@@ -140,19 +110,14 @@ class ManifestDocumentMapper {
return $collection;
}
/**
* @param ManifestDocument $document
*
* @return BundledComponentCollection
*/
private function mapBundledComponents(ManifestDocument $document) {
private function mapBundledComponents(ManifestDocument $document): BundledComponentCollection {
$collection = new BundledComponentCollection();
if (!$document->hasBundlesElement()) {
return $collection;
}
foreach($document->getBundlesElement()->getComponentElements() as $componentElement) {
foreach ($document->getBundlesElement()->getComponentElements() as $componentElement) {
$collection->add(
new BundledComponent(
$componentElement->getName(),
@@ -166,17 +131,9 @@ class ManifestDocumentMapper {
return $collection;
}
/**
* @param ExtensionElement $extension
*
* @return Extension
*
* @throws ManifestDocumentMapperException
*/
private function mapExtension(ExtensionElement $extension) {
private function mapExtension(ExtensionElement $extension): Extension {
try {
$parser = new VersionConstraintParser;
$versionConstraint = $parser->parse($extension->getCompatible());
$versionConstraint = (new VersionConstraintParser)->parse($extension->getCompatible());
return Type::extension(
new ApplicationName($extension->getFor()),
@@ -184,8 +141,8 @@ class ManifestDocumentMapper {
);
} catch (VersionException $e) {
throw new ManifestDocumentMapperException(
sprintf('Unsupported version constraint - %s', $e->getMessage()),
$e->getCode(),
\sprintf('Unsupported version constraint - %s', $e->getMessage()),
(int)$e->getCode(),
$e
);
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,50 +7,28 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ManifestLoader {
/**
* @param string $filename
*
* @return Manifest
*
* @throws ManifestLoaderException
*/
public static function fromFile($filename) {
public static function fromFile(string $filename): Manifest {
try {
return (new ManifestDocumentMapper())->map(
ManifestDocument::fromFile($filename)
);
} catch (Exception $e) {
throw new ManifestLoaderException(
sprintf('Loading %s failed.', $filename),
$e->getCode(),
\sprintf('Loading %s failed.', $filename),
(int)$e->getCode(),
$e
);
}
}
/**
* @param string $filename
*
* @return Manifest
*
* @throws ManifestLoaderException
*/
public static function fromPhar($filename) {
public static function fromPhar(string $filename): Manifest {
return self::fromFile('phar://' . $filename . '/manifest.xml');
}
/**
* @param string $manifest
*
* @return Manifest
*
* @throws ManifestLoaderException
*/
public static function fromString($manifest) {
public static function fromString(string $manifest): Manifest {
try {
return (new ManifestDocumentMapper())->map(
ManifestDocument::fromString($manifest)
@@ -58,7 +36,7 @@ class ManifestLoader {
} catch (Exception $e) {
throw new ManifestLoaderException(
'Processing string failed',
$e->getCode(),
(int)$e->getCode(),
$e
);
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\AnyVersionConstraint;
@@ -15,20 +14,19 @@ use PharIo\Version\Version;
use PharIo\Version\VersionConstraint;
use XMLWriter;
/** @psalm-suppress MissingConstructor */
class ManifestSerializer {
/**
* @var XMLWriter
*/
/** @var XMLWriter */
private $xmlWriter;
public function serializeToFile(Manifest $manifest, $filename) {
file_put_contents(
public function serializeToFile(Manifest $manifest, string $filename): void {
\file_put_contents(
$filename,
$this->serializeToString($manifest)
);
}
public function serializeToString(Manifest $manifest) {
public function serializeToString(Manifest $manifest): string {
$this->startDocument();
$this->addContains($manifest->getName(), $manifest->getVersion(), $manifest->getType());
@@ -39,11 +37,11 @@ class ManifestSerializer {
return $this->finishDocument();
}
private function startDocument() {
private function startDocument(): void {
$xmlWriter = new XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->setIndent(true);
$xmlWriter->setIndentString(str_repeat(' ', 4));
$xmlWriter->setIndentString(\str_repeat(' ', 4));
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->startElement('phar');
$xmlWriter->writeAttribute('xmlns', 'https://phar.io/xml/manifest/1.0');
@@ -51,33 +49,39 @@ class ManifestSerializer {
$this->xmlWriter = $xmlWriter;
}
private function finishDocument() {
private function finishDocument(): string {
$this->xmlWriter->endElement();
$this->xmlWriter->endDocument();
return $this->xmlWriter->outputMemory();
}
private function addContains($name, Version $version, Type $type) {
private function addContains(ApplicationName $name, Version $version, Type $type): void {
$this->xmlWriter->startElement('contains');
$this->xmlWriter->writeAttribute('name', $name);
$this->xmlWriter->writeAttribute('name', $name->asString());
$this->xmlWriter->writeAttribute('version', $version->getVersionString());
switch (true) {
case $type->isApplication(): {
$this->xmlWriter->writeAttribute('type', 'application');
break;
}
case $type->isLibrary(): {
$this->xmlWriter->writeAttribute('type', 'library');
break;
}
case $type->isExtension(): {
/* @var $type Extension */
$this->xmlWriter->writeAttribute('type', 'extension');
$this->addExtension($type->getApplicationName(), $type->getVersionConstraint());
/* @var $type Extension */
$this->addExtension(
$type->getApplicationName(),
$type->getVersionConstraint()
);
break;
}
@@ -89,13 +93,13 @@ class ManifestSerializer {
$this->xmlWriter->endElement();
}
private function addCopyright(CopyrightInformation $copyrightInformation) {
private function addCopyright(CopyrightInformation $copyrightInformation): void {
$this->xmlWriter->startElement('copyright');
foreach($copyrightInformation->getAuthors() as $author) {
foreach ($copyrightInformation->getAuthors() as $author) {
$this->xmlWriter->startElement('author');
$this->xmlWriter->writeAttribute('name', $author->getName());
$this->xmlWriter->writeAttribute('email', (string) $author->getEmail());
$this->xmlWriter->writeAttribute('email', $author->getEmail()->asString());
$this->xmlWriter->endElement();
}
@@ -103,24 +107,25 @@ class ManifestSerializer {
$this->xmlWriter->startElement('license');
$this->xmlWriter->writeAttribute('type', $license->getName());
$this->xmlWriter->writeAttribute('url', $license->getUrl());
$this->xmlWriter->writeAttribute('url', $license->getUrl()->asString());
$this->xmlWriter->endElement();
$this->xmlWriter->endElement();
}
private function addRequirements(RequirementCollection $requirementCollection) {
private function addRequirements(RequirementCollection $requirementCollection): void {
$phpRequirement = new AnyVersionConstraint();
$extensions = [];
foreach($requirementCollection as $requirement) {
foreach ($requirementCollection as $requirement) {
if ($requirement instanceof PhpVersionRequirement) {
$phpRequirement = $requirement->getVersionConstraint();
continue;
}
if ($requirement instanceof PhpExtensionRequirement) {
$extensions[] = (string) $requirement;
$extensions[] = $requirement->asString();
}
}
@@ -128,7 +133,7 @@ class ManifestSerializer {
$this->xmlWriter->startElement('php');
$this->xmlWriter->writeAttribute('version', $phpRequirement->asString());
foreach($extensions as $extension) {
foreach ($extensions as $extension) {
$this->xmlWriter->startElement('ext');
$this->xmlWriter->writeAttribute('name', $extension);
$this->xmlWriter->endElement();
@@ -138,13 +143,13 @@ class ManifestSerializer {
$this->xmlWriter->endElement();
}
private function addBundles(BundledComponentCollection $bundledComponentCollection) {
if (count($bundledComponentCollection) === 0) {
private function addBundles(BundledComponentCollection $bundledComponentCollection): void {
if (\count($bundledComponentCollection) === 0) {
return;
}
$this->xmlWriter->startElement('bundles');
foreach($bundledComponentCollection as $bundledComponent) {
foreach ($bundledComponentCollection as $bundledComponent) {
$this->xmlWriter->startElement('component');
$this->xmlWriter->writeAttribute('name', $bundledComponent->getName());
$this->xmlWriter->writeAttribute('version', $bundledComponent->getVersion()->getVersionString());
@@ -154,9 +159,9 @@ class ManifestSerializer {
$this->xmlWriter->endElement();
}
private function addExtension($application, VersionConstraint $versionConstraint) {
private function addExtension(ApplicationName $applicationName, VersionConstraint $versionConstraint): void {
$this->xmlWriter->startElement('extension');
$this->xmlWriter->writeAttribute('for', $application);
$this->xmlWriter->writeAttribute('for', $applicationName->asString());
$this->xmlWriter->writeAttribute('compatible', $versionConstraint->asString());
$this->xmlWriter->endElement();
}

View File

@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
* (c) Arne Blankerts <arne@blankerts.de>, Sebastian Heuer <sebastian@phpeople.de>, Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ElementCollectionException extends \InvalidArgumentException implements Exception {
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,8 +7,7 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
interface Exception {
interface Exception extends \Throwable {
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,10 +7,8 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class InvalidApplicationNameException extends \InvalidArgumentException implements Exception {
const NotAString = 1;
const InvalidFormat = 2;
public const InvalidFormat = 2;
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class InvalidEmailException extends \InvalidArgumentException implements Exception {

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class InvalidUrlException extends \InvalidArgumentException implements Exception {

View File

@@ -1,5 +1,4 @@
<?php
<?php declare(strict_types = 1);
namespace PharIo\Manifest;
class ManifestDocumentException extends \RuntimeException implements Exception {

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,15 +7,12 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use LibXMLError;
class ManifestDocumentLoadingException extends \Exception implements Exception {
/**
* @var LibXMLError[]
*/
/** @var LibXMLError[] */
private $libxmlErrors;
/**
@@ -28,7 +25,7 @@ class ManifestDocumentLoadingException extends \Exception implements Exception {
$first = $this->libxmlErrors[0];
parent::__construct(
sprintf(
\sprintf(
'%s (Line: %d / Column: %d / File: %s)',
$first->message,
$first->line,
@@ -42,7 +39,7 @@ class ManifestDocumentLoadingException extends \Exception implements Exception {
/**
* @return LibXMLError[]
*/
public function getLibxmlErrors() {
public function getLibxmlErrors(): array {
return $this->libxmlErrors;
}
}

View File

@@ -1,5 +1,4 @@
<?php
<?php declare(strict_types = 1);
namespace PharIo\Manifest;
class ManifestDocumentMapperException extends \RuntimeException implements Exception {

View File

@@ -1,5 +1,4 @@
<?php
<?php declare(strict_types = 1);
namespace PharIo\Manifest;
class ManifestElementException extends \RuntimeException implements Exception {

View File

@@ -1,5 +1,4 @@
<?php
<?php declare(strict_types = 1);
namespace PharIo\Manifest;
class ManifestLoaderException extends \Exception implements Exception {

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,14 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Application extends Type {
/**
* @return bool
*/
public function isApplication() {
public function isApplication(): bool {
return true;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,59 +7,31 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ApplicationName {
/**
* @var string
*/
/** @var string */
private $name;
/**
* ApplicationName constructor.
*
* @param string $name
*
* @throws InvalidApplicationNameException
*/
public function __construct($name) {
$this->ensureIsString($name);
public function __construct(string $name) {
$this->ensureValidFormat($name);
$this->name = $name;
}
/**
* @return string
*/
public function __toString() {
public function asString(): string {
return $this->name;
}
public function isEqual(ApplicationName $name) {
public function isEqual(ApplicationName $name): bool {
return $this->name === $name->name;
}
/**
* @param string $name
*
* @throws InvalidApplicationNameException
*/
private function ensureValidFormat($name) {
if (!preg_match('#\w/\w#', $name)) {
private function ensureValidFormat(string $name): void {
if (!\preg_match('#\w/\w#', $name)) {
throw new InvalidApplicationNameException(
sprintf('Format of name "%s" is not valid - expected: vendor/packagename', $name),
\sprintf('Format of name "%s" is not valid - expected: vendor/packagename', $name),
InvalidApplicationNameException::InvalidFormat
);
}
}
private function ensureIsString($name) {
if (!is_string($name)) {
throw new InvalidApplicationNameException(
'Name must be a string',
InvalidApplicationNameException::NotAString
);
}
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,51 +7,33 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Author {
/**
* @var string
*/
/** @var string */
private $name;
/**
* @var Email
*/
/** @var Email */
private $email;
/**
* @param string $name
* @param Email $email
*/
public function __construct($name, Email $email) {
public function __construct(string $name, Email $email) {
$this->name = $name;
$this->email = $email;
}
/**
* @return string
*/
public function getName() {
public function asString(): string {
return \sprintf(
'%s <%s>',
$this->name,
$this->email->asString()
);
}
public function getName(): string {
return $this->name;
}
/**
* @return Email
*/
public function getEmail() {
public function getEmail(): Email {
return $this->email;
}
/**
* @return string
*/
public function __toString() {
return sprintf(
'%s <%s>',
$this->name,
$this->email
);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,37 +7,28 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorCollection implements \Countable, \IteratorAggregate {
/**
* @var Author[]
*/
/** @var Author[] */
private $authors = [];
public function add(Author $author) {
public function add(Author $author): void {
$this->authors[] = $author;
}
/**
* @return Author[]
*/
public function getAuthors() {
public function getAuthors(): array {
return $this->authors;
}
/**
* @return int
*/
public function count() {
return count($this->authors);
public function count(): int {
return \count($this->authors);
}
/**
* @return AuthorCollectionIterator
*/
public function getIterator() {
public function getIterator(): AuthorCollectionIterator {
return new AuthorCollectionIterator($this);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,50 +7,36 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorCollectionIterator implements \Iterator {
/**
* @var Author[]
*/
private $authors = [];
/** @var Author[] */
private $authors;
/**
* @var int
*/
private $position;
/** @var int */
private $position = 0;
public function __construct(AuthorCollection $authors) {
$this->authors = $authors->getAuthors();
}
public function rewind() {
public function rewind(): void {
$this->position = 0;
}
/**
* @return bool
*/
public function valid() {
return $this->position < count($this->authors);
public function valid(): bool {
return $this->position < \count($this->authors);
}
/**
* @return int
*/
public function key() {
public function key(): int {
return $this->position;
}
/**
* @return Author
*/
public function current() {
public function current(): Author {
return $this->authors[$this->position];
}
public function next() {
public function next(): void {
$this->position++;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,42 +7,27 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
class BundledComponent {
/**
* @var string
*/
/** @var string */
private $name;
/**
* @var Version
*/
/** @var Version */
private $version;
/**
* @param string $name
* @param Version $version
*/
public function __construct($name, Version $version) {
public function __construct(string $name, Version $version) {
$this->name = $name;
$this->version = $version;
}
/**
* @return string
*/
public function getName() {
public function getName(): string {
return $this->name;
}
/**
* @return Version
*/
public function getVersion() {
public function getVersion(): Version {
return $this->version;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,37 +7,28 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class BundledComponentCollection implements \Countable, \IteratorAggregate {
/**
* @var BundledComponent[]
*/
/** @var BundledComponent[] */
private $bundledComponents = [];
public function add(BundledComponent $bundledComponent) {
public function add(BundledComponent $bundledComponent): void {
$this->bundledComponents[] = $bundledComponent;
}
/**
* @return BundledComponent[]
*/
public function getBundledComponents() {
public function getBundledComponents(): array {
return $this->bundledComponents;
}
/**
* @return int
*/
public function count() {
return count($this->bundledComponents);
public function count(): int {
return \count($this->bundledComponents);
}
/**
* @return BundledComponentCollectionIterator
*/
public function getIterator() {
public function getIterator(): BundledComponentCollectionIterator {
return new BundledComponentCollectionIterator($this);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,50 +7,36 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class BundledComponentCollectionIterator implements \Iterator {
/**
* @var BundledComponent[]
*/
private $bundledComponents = [];
/** @var BundledComponent[] */
private $bundledComponents;
/**
* @var int
*/
private $position;
/** @var int */
private $position = 0;
public function __construct(BundledComponentCollection $bundledComponents) {
$this->bundledComponents = $bundledComponents->getBundledComponents();
}
public function rewind() {
public function rewind(): void {
$this->position = 0;
}
/**
* @return bool
*/
public function valid() {
return $this->position < count($this->bundledComponents);
public function valid(): bool {
return $this->position < \count($this->bundledComponents);
}
/**
* @return int
*/
public function key() {
public function key(): int {
return $this->position;
}
/**
* @return BundledComponent
*/
public function current() {
public function current(): BundledComponent {
return $this->bundledComponents[$this->position];
}
public function next() {
public function next(): void {
$this->position++;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,18 +7,13 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class CopyrightInformation {
/**
* @var AuthorCollection
*/
/** @var AuthorCollection */
private $authors;
/**
* @var License
*/
/** @var License */
private $license;
public function __construct(AuthorCollection $authors, License $license) {
@@ -26,17 +21,11 @@ class CopyrightInformation {
$this->license = $license;
}
/**
* @return AuthorCollection
*/
public function getAuthors() {
public function getAuthors(): AuthorCollection {
return $this->authors;
}
/**
* @return License
*/
public function getLicense() {
public function getLicense(): License {
return $this->license;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,40 +7,24 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Email {
/**
* @var string
*/
/** @var string */
private $email;
/**
* @param string $email
*
* @throws InvalidEmailException
*/
public function __construct($email) {
public function __construct(string $email) {
$this->ensureEmailIsValid($email);
$this->email = $email;
}
/**
* @return string
*/
public function __toString() {
public function asString(): string {
return $this->email;
}
/**
* @param string $url
*
* @throws InvalidEmailException
*/
private function ensureEmailIsValid($url) {
if (filter_var($url, \FILTER_VALIDATE_EMAIL) === false) {
private function ensureEmailIsValid(string $url): void {
if (\filter_var($url, \FILTER_VALIDATE_EMAIL) === false) {
throw new InvalidEmailException;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,69 +7,40 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
use PharIo\Version\VersionConstraint;
class Extension extends Type {
/**
* @var ApplicationName
*/
/** @var ApplicationName */
private $application;
/**
* @var VersionConstraint
*/
/** @var VersionConstraint */
private $versionConstraint;
/**
* @param ApplicationName $application
* @param VersionConstraint $versionConstraint
*/
public function __construct(ApplicationName $application, VersionConstraint $versionConstraint) {
$this->application = $application;
$this->versionConstraint = $versionConstraint;
}
/**
* @return ApplicationName
*/
public function getApplicationName() {
public function getApplicationName(): ApplicationName {
return $this->application;
}
/**
* @return VersionConstraint
*/
public function getVersionConstraint() {
public function getVersionConstraint(): VersionConstraint {
return $this->versionConstraint;
}
/**
* @return bool
*/
public function isExtension() {
public function isExtension(): bool {
return true;
}
/**
* @param ApplicationName $name
*
* @return bool
*/
public function isExtensionFor(ApplicationName $name) {
public function isExtensionFor(ApplicationName $name): bool {
return $this->application->isEqual($name);
}
/**
* @param ApplicationName $name
* @param Version $version
*
* @return bool
*/
public function isCompatibleWith(ApplicationName $name, Version $version) {
public function isCompatibleWith(ApplicationName $name, Version $version): bool {
return $this->isExtensionFor($name) && $this->versionConstraint->complies($version);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,14 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Library extends Type {
/**
* @return bool
*/
public function isLibrary() {
public function isLibrary(): bool {
return true;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,36 +7,25 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class License {
/**
* @var string
*/
/** @var string */
private $name;
/**
* @var Url
*/
/** @var Url */
private $url;
public function __construct($name, Url $url) {
public function __construct(string $name, Url $url) {
$this->name = $name;
$this->url = $url;
}
/**
* @return string
*/
public function getName() {
public function getName(): string {
return $this->name;
}
/**
* @return Url
*/
public function getUrl() {
public function getUrl(): Url {
return $this->url;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,40 +7,27 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\Version;
class Manifest {
/**
* @var ApplicationName
*/
/** @var ApplicationName */
private $name;
/**
* @var Version
*/
/** @var Version */
private $version;
/**
* @var Type
*/
/** @var Type */
private $type;
/**
* @var CopyrightInformation
*/
/** @var CopyrightInformation */
private $copyrightInformation;
/**
* @var RequirementCollection
*/
/** @var RequirementCollection */
private $requirements;
/**
* @var BundledComponentCollection
*/
/** @var BundledComponentCollection */
private $bundledComponents;
public function __construct(ApplicationName $name, Version $version, Type $type, CopyrightInformation $copyrightInformation, RequirementCollection $requirements, BundledComponentCollection $bundledComponents) {
@@ -52,76 +39,43 @@ class Manifest {
$this->bundledComponents = $bundledComponents;
}
/**
* @return ApplicationName
*/
public function getName() {
public function getName(): ApplicationName {
return $this->name;
}
/**
* @return Version
*/
public function getVersion() {
public function getVersion(): Version {
return $this->version;
}
/**
* @return Type
*/
public function getType() {
public function getType(): Type {
return $this->type;
}
/**
* @return CopyrightInformation
*/
public function getCopyrightInformation() {
public function getCopyrightInformation(): CopyrightInformation {
return $this->copyrightInformation;
}
/**
* @return RequirementCollection
*/
public function getRequirements() {
public function getRequirements(): RequirementCollection {
return $this->requirements;
}
/**
* @return BundledComponentCollection
*/
public function getBundledComponents() {
public function getBundledComponents(): BundledComponentCollection {
return $this->bundledComponents;
}
/**
* @return bool
*/
public function isApplication() {
public function isApplication(): bool {
return $this->type->isApplication();
}
/**
* @return bool
*/
public function isLibrary() {
public function isLibrary(): bool {
return $this->type->isLibrary();
}
/**
* @return bool
*/
public function isExtension() {
public function isExtension(): bool {
return $this->type->isExtension();
}
/**
* @param ApplicationName $application
* @param Version|null $version
*
* @return bool
*/
public function isExtensionFor(ApplicationName $application, Version $version = null) {
public function isExtensionFor(ApplicationName $application, Version $version = null): bool {
if (!$this->isExtension()) {
return false;
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,26 +7,17 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class PhpExtensionRequirement implements Requirement {
/**
* @var string
*/
/** @var string */
private $extension;
/**
* @param string $extension
*/
public function __construct($extension) {
public function __construct(string $extension) {
$this->extension = $extension;
}
/**
* @return string
*/
public function __toString() {
public function asString(): string {
return $this->extension;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,25 +7,19 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\VersionConstraint;
class PhpVersionRequirement implements Requirement {
/**
* @var VersionConstraint
*/
/** @var VersionConstraint */
private $versionConstraint;
public function __construct(VersionConstraint $versionConstraint) {
$this->versionConstraint = $versionConstraint;
}
/**
* @return VersionConstraint
*/
public function getVersionConstraint() {
public function getVersionConstraint(): VersionConstraint {
return $this->versionConstraint;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,7 +7,6 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
interface Requirement {

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,37 +7,28 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class RequirementCollection implements \Countable, \IteratorAggregate {
/**
* @var Requirement[]
*/
/** @var Requirement[] */
private $requirements = [];
public function add(Requirement $requirement) {
public function add(Requirement $requirement): void {
$this->requirements[] = $requirement;
}
/**
* @return Requirement[]
*/
public function getRequirements() {
public function getRequirements(): array {
return $this->requirements;
}
/**
* @return int
*/
public function count() {
return count($this->requirements);
public function count(): int {
return \count($this->requirements);
}
/**
* @return RequirementCollectionIterator
*/
public function getIterator() {
public function getIterator(): RequirementCollectionIterator {
return new RequirementCollectionIterator($this);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,50 +7,36 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class RequirementCollectionIterator implements \Iterator {
/**
* @var Requirement[]
*/
private $requirements = [];
/** @var Requirement[] */
private $requirements;
/**
* @var int
*/
private $position;
/** @var int */
private $position = 0;
public function __construct(RequirementCollection $requirements) {
$this->requirements = $requirements->getRequirements();
}
public function rewind() {
public function rewind(): void {
$this->position = 0;
}
/**
* @return bool
*/
public function valid() {
return $this->position < count($this->requirements);
public function valid(): bool {
return $this->position < \count($this->requirements);
}
/**
* @return int
*/
public function key() {
public function key(): int {
return $this->position;
}
/**
* @return Requirement
*/
public function current() {
public function current(): Requirement {
return $this->requirements[$this->position];
}
public function next() {
public function next(): void {
$this->position++;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,54 +7,35 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use PharIo\Version\VersionConstraint;
abstract class Type {
/**
* @return Application
*/
public static function application() {
public static function application(): Application {
return new Application;
}
/**
* @return Library
*/
public static function library() {
public static function library(): Library {
return new Library;
}
/**
* @param ApplicationName $application
* @param VersionConstraint $versionConstraint
*
* @return Extension
*/
public static function extension(ApplicationName $application, VersionConstraint $versionConstraint) {
public static function extension(ApplicationName $application, VersionConstraint $versionConstraint): Extension {
return new Extension($application, $versionConstraint);
}
/**
* @return bool
*/
public function isApplication() {
/** @psalm-assert-if-true Application $this */
public function isApplication(): bool {
return false;
}
/**
* @return bool
*/
public function isLibrary() {
/** @psalm-assert-if-true Library $this */
public function isLibrary(): bool {
return false;
}
/**
* @return bool
*/
public function isExtension() {
/** @psalm-assert-if-true Extension $this */
public function isExtension(): bool {
return false;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,30 +7,19 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class Url {
/**
* @var string
*/
/** @var string */
private $url;
/**
* @param string $url
*
* @throws InvalidUrlException
*/
public function __construct($url) {
public function __construct(string $url) {
$this->ensureUrlIsValid($url);
$this->url = $url;
}
/**
* @return string
*/
public function __toString() {
public function asString(): string {
return $this->url;
}
@@ -39,8 +28,8 @@ class Url {
*
* @throws InvalidUrlException
*/
private function ensureUrlIsValid($url) {
if (filter_var($url, \FILTER_VALIDATE_URL) === false) {
private function ensureUrlIsValid($url): void {
if (\filter_var($url, \FILTER_VALIDATE_URL) === false) {
throw new InvalidUrlException;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,15 +7,14 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorElement extends ManifestElement {
public function getName() {
public function getName(): string {
return $this->getAttributeValue('name');
}
public function getEmail() {
public function getEmail(): string {
return $this->getAttributeValue('email');
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,11 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class AuthorElementCollection extends ElementCollection {
public function current() {
public function current(): AuthorElement {
return new AuthorElement(
$this->getCurrentElement()
);

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,11 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class BundlesElement extends ManifestElement {
public function getComponentElements() {
public function getComponentElements(): ComponentElementCollection {
return new ComponentElementCollection(
$this->getChildrenByName('component')
);

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,15 +7,14 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ComponentElement extends ManifestElement {
public function getName() {
public function getName(): string {
return $this->getAttributeValue('name');
}
public function getVersion() {
public function getVersion(): string {
return $this->getAttributeValue('version');
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,11 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ComponentElementCollection extends ElementCollection {
public function current() {
public function current(): ComponentElement {
return new ComponentElement(
$this->getCurrentElement()
);

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,23 +7,22 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ContainsElement extends ManifestElement {
public function getName() {
public function getName(): string {
return $this->getAttributeValue('name');
}
public function getVersion() {
public function getVersion(): string {
return $this->getAttributeValue('version');
}
public function getType() {
public function getType(): string {
return $this->getAttributeValue('type');
}
public function getExtensionElement() {
public function getExtensionElement(): ExtensionElement {
return new ExtensionElement(
$this->getChildByName('extension')
);

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,17 +7,16 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class CopyrightElement extends ManifestElement {
public function getAuthorElements() {
public function getAuthorElements(): AuthorElementCollection {
return new AuthorElementCollection(
$this->getChildrenByName('author')
);
}
public function getLicenseElement() {
public function getLicenseElement(): LicenseElement {
return new LicenseElement(
$this->getChildByName('license')
);

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,52 +7,55 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use DOMElement;
use DOMNodeList;
abstract class ElementCollection implements \Iterator {
/**
* @var DOMNodeList
*/
private $nodeList;
/** @var DOMElement[] */
private $nodes = [];
/** @var int */
private $position;
/**
* ElementCollection constructor.
*
* @param DOMNodeList $nodeList
*/
public function __construct(DOMNodeList $nodeList) {
$this->nodeList = $nodeList;
$this->position = 0;
$this->importNodes($nodeList);
}
#[\ReturnTypeWillChange]
abstract public function current();
/**
* @return DOMElement
*/
protected function getCurrentElement() {
return $this->nodeList->item($this->position);
}
public function next() {
public function next(): void {
$this->position++;
}
public function key() {
public function key(): int {
return $this->position;
}
public function valid() {
return $this->position < $this->nodeList->length;
public function valid(): bool {
return $this->position < \count($this->nodes);
}
public function rewind() {
public function rewind(): void {
$this->position = 0;
}
protected function getCurrentElement(): DOMElement {
return $this->nodes[$this->position];
}
private function importNodes(DOMNodeList $nodeList): void {
foreach ($nodeList as $node) {
if (!$node instanceof DOMElement) {
throw new ElementCollectionException(
\sprintf('\DOMElement expected, got \%s', \get_class($node))
);
}
$this->nodes[] = $node;
}
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,11 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ExtElement extends ManifestElement {
public function getName() {
public function getName(): string {
return $this->getAttributeValue('name');
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,14 +7,12 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ExtElementCollection extends ElementCollection {
public function current() {
public function current(): ExtElement {
return new ExtElement(
$this->getCurrentElement()
);
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,15 +7,14 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class ExtensionElement extends ManifestElement {
public function getFor() {
public function getFor(): string {
return $this->getAttributeValue('for');
}
public function getCompatible() {
public function getCompatible(): string {
return $this->getAttributeValue('compatible');
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,15 +7,14 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class LicenseElement extends ManifestElement {
public function getType() {
public function getType(): string {
return $this->getAttributeValue('type');
}
public function getUrl() {
public function getUrl(): string {
return $this->getAttributeValue('url');
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,89 +7,81 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use DOMDocument;
use DOMElement;
class ManifestDocument {
const XMLNS = 'https://phar.io/xml/manifest/1.0';
public const XMLNS = 'https://phar.io/xml/manifest/1.0';
/**
* @var DOMDocument
*/
/** @var DOMDocument */
private $dom;
/**
* ManifestDocument constructor.
*
* @param DOMDocument $dom
*/
private function __construct(DOMDocument $dom) {
$this->ensureCorrectDocumentType($dom);
$this->dom = $dom;
}
public static function fromFile($filename) {
if (!file_exists($filename)) {
public static function fromFile(string $filename): ManifestDocument {
if (!\file_exists($filename)) {
throw new ManifestDocumentException(
sprintf('File "%s" not found', $filename)
\sprintf('File "%s" not found', $filename)
);
}
return self::fromString(
file_get_contents($filename)
\file_get_contents($filename)
);
}
public static function fromString($xmlString) {
$prev = libxml_use_internal_errors(true);
libxml_clear_errors();
public static function fromString(string $xmlString): ManifestDocument {
$prev = \libxml_use_internal_errors(true);
\libxml_clear_errors();
$dom = new DOMDocument();
$dom->loadXML($xmlString);
$errors = libxml_get_errors();
libxml_use_internal_errors($prev);
$errors = \libxml_get_errors();
\libxml_use_internal_errors($prev);
if (count($errors) !== 0) {
if (\count($errors) !== 0) {
throw new ManifestDocumentLoadingException($errors);
}
return new self($dom);
}
public function getContainsElement() {
private function __construct(DOMDocument $dom) {
$this->ensureCorrectDocumentType($dom);
$this->dom = $dom;
}
public function getContainsElement(): ContainsElement {
return new ContainsElement(
$this->fetchElementByName('contains')
);
}
public function getCopyrightElement() {
public function getCopyrightElement(): CopyrightElement {
return new CopyrightElement(
$this->fetchElementByName('copyright')
);
}
public function getRequiresElement() {
public function getRequiresElement(): RequiresElement {
return new RequiresElement(
$this->fetchElementByName('requires')
);
}
public function hasBundlesElement() {
public function hasBundlesElement(): bool {
return $this->dom->getElementsByTagNameNS(self::XMLNS, 'bundles')->length === 1;
}
public function getBundlesElement() {
public function getBundlesElement(): BundlesElement {
return new BundlesElement(
$this->fetchElementByName('bundles')
);
}
private function ensureCorrectDocumentType(DOMDocument $dom) {
private function ensureCorrectDocumentType(DOMDocument $dom): void {
$root = $dom->documentElement;
if ($root->localName !== 'phar' || $root->namespaceURI !== self::XMLNS) {
@@ -97,19 +89,12 @@ class ManifestDocument {
}
}
/**
* @param $elementName
*
* @return DOMElement
*
* @throws ManifestDocumentException
*/
private function fetchElementByName($elementName) {
private function fetchElementByName(string $elementName): DOMElement {
$element = $this->dom->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0);
if (!$element instanceof DOMElement) {
throw new ManifestDocumentException(
sprintf('Element %s missing', $elementName)
\sprintf('Element %s missing', $elementName)
);
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,40 +7,25 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
use DOMElement;
use DOMNodeList;
class ManifestElement {
const XMLNS = 'https://phar.io/xml/manifest/1.0';
public const XMLNS = 'https://phar.io/xml/manifest/1.0';
/**
* @var DOMElement
*/
/** @var DOMElement */
private $element;
/**
* ContainsElement constructor.
*
* @param DOMElement $element
*/
public function __construct(DOMElement $element) {
$this->element = $element;
}
/**
* @param string $name
*
* @return string
*
* @throws ManifestElementException
*/
protected function getAttributeValue($name) {
protected function getAttributeValue(string $name): string {
if (!$this->element->hasAttribute($name)) {
throw new ManifestElementException(
sprintf(
\sprintf(
'Attribute %s not set on element %s',
$name,
$this->element->localName
@@ -51,50 +36,31 @@ class ManifestElement {
return $this->element->getAttribute($name);
}
/**
* @param $elementName
*
* @return DOMElement
*
* @throws ManifestElementException
*/
protected function getChildByName($elementName) {
protected function getChildByName(string $elementName): DOMElement {
$element = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->item(0);
if (!$element instanceof DOMElement) {
throw new ManifestElementException(
sprintf('Element %s missing', $elementName)
\sprintf('Element %s missing', $elementName)
);
}
return $element;
}
/**
* @param $elementName
*
* @return DOMNodeList
*
* @throws ManifestElementException
*/
protected function getChildrenByName($elementName) {
protected function getChildrenByName(string $elementName): DOMNodeList {
$elementList = $this->element->getElementsByTagNameNS(self::XMLNS, $elementName);
if ($elementList->length === 0) {
throw new ManifestElementException(
sprintf('Element(s) %s missing', $elementName)
\sprintf('Element(s) %s missing', $elementName)
);
}
return $elementList;
}
/**
* @param string $elementName
*
* @return bool
*/
protected function hasChild($elementName) {
protected function hasChild(string $elementName): bool {
return $this->element->getElementsByTagNameNS(self::XMLNS, $elementName)->length !== 0;
}
}

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,19 +7,18 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class PhpElement extends ManifestElement {
public function getVersion() {
public function getVersion(): string {
return $this->getAttributeValue('version');
}
public function hasExtElements() {
public function hasExtElements(): bool {
return $this->hasChild('ext');
}
public function getExtElements() {
public function getExtElements(): ExtElementCollection {
return new ExtElementCollection(
$this->getChildrenByName('ext')
);

View File

@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types = 1);
/*
* This file is part of PharIo\Manifest.
*
@@ -7,11 +7,10 @@
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PharIo\Manifest;
class RequiresElement extends ManifestElement {
public function getPHPElement() {
public function getPHPElement(): PhpElement {
return new PhpElement(
$this->getChildByName('php')
);