Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,22 @@
}
],
"require": {
"php": ">=5.4.0",
"php": "~8",
"ext-json":"*",
"regex-guard/regex-guard": "~1.0",
"nikic/php-parser" : ">=2.0 <5.0.0"
"regex-guard/regex-guard": "~1.1",
"nikic/php-parser" : ">=4.0 <5.0.0"
},
"require-dev": {
"mockery/mockery": "^0.9",
"phpunit/phpunit": ">=4.8 <6.0.0"
"mockery/mockery": "~1.6",
"phpunit/phpunit": "~10"
},
"autoload": {
"psr-4": { "Minime\\Annotations\\": "src/" }
},
"autoload-dev": {
"psr-4": { "Minime\\Annotations\\Fixtures\\": "test/fixtures/" }
},
"scripts": {
"test": "phpunit"
}
}
40 changes: 4 additions & 36 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,18 @@

<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
backupStaticProperties="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">

<testsuites>
<testsuite>
<testsuite name="unit">
<directory>test/suite/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>src/</directory>
</whitelist>
</filter>

<logging>
<log
type="tap"
target="build/logs/report.tap"
/>
<log
type="junit"
target="build/logs/report.junit.xml"
/>
<log
type="coverage-html"
target="build/logs/coverage"
charset="UTF-8"
yui="true"
highlight="true"
/>
<log
type="coverage-text"
target="build/logs/coverage.txt"
/>
<log
type="coverage-clover"
target="build/logs/clover.xml"
/>
<junit outputFile="build/logs/report.junit.xml"/>
</logging>
</phpunit>
16 changes: 7 additions & 9 deletions src/AnnotationsBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,57 +153,55 @@ public function union(AnnotationsBagInterface $bag)
/**
* Countable
*/
public function count()
public function count(): int
{
return count($this->attributes);
}

/**
* JsonSerializable
*/
public function jsonSerialize()
public function jsonSerialize(): array
{
return $this->toArray();
}

/**
* IteratorAggregate
*/
public function getIterator()
public function getIterator(): \Traversable
{
return new ArrayIterator($this->attributes);
}

/**
* ArrayAccess - Whether or not an offset exists.
*/
public function offsetExists($key)
public function offsetExists($key): bool
{
return $this->has($key);
}

/**
* ArrayAccess - Returns the value at specified offset.
*/
public function offsetGet($key)
public function offsetGet($key): mixed
{
return $this->get($key);
}

/**
* ArrayAccess - Assigns a value to the specified offset.
*/
public function offsetSet($key, $value)
public function offsetSet($key, $value): void
{
$this->set($key, $value);

return true;
}

/**
* ArrayAccess - Unsets an offset.
*/
public function offsetUnset($key)
public function offsetUnset($key): void
{
unset($this->attributes[$key]);
}
Expand Down
5 changes: 5 additions & 0 deletions src/DynamicParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,9 @@ protected function sanitizeKey($key)
{
return $key;
}

public function registerConcreteNamespaceLookup(array $namespaces)
{
}

}
7 changes: 7 additions & 0 deletions src/Interfaces/ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,11 @@ interface ParserInterface
* @return array
*/
public function parse($docBlock);

/**
* Register set of namespaces for ConcreteType to autolookup.
*
* @param array $namespaces
*/
public function registerConcreteNamespaceLookup(array $namespaces);
}
5 changes: 5 additions & 0 deletions src/Interfaces/TypeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
*/
interface TypeInterface
{
/**
* @return TypeInterface
*/
public static function getType();

/**
* Parses a type
* @param string $value value to be processed
Expand Down
41 changes: 34 additions & 7 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,60 @@ class Parser extends DynamicParser
* @var array
*/
protected $types = [
'\Minime\Annotations\Types\IntegerType' => 'integer',
'\Minime\Annotations\Types\StringType' => 'string',
'\Minime\Annotations\Types\FloatType' => 'float',
'\Minime\Annotations\Types\JsonType' => 'json',
'\Minime\Annotations\Types\ConcreteType' => '->'
'integerType' => 'integer',
'stringType' => 'string',
'floatType' => 'float',
'jsonType' => 'json',
'concreteType' => '->'
];

/**
* A fallback type if no strong type declaration found.
*
* @var string
*/
protected $typeFallback = 'dynamicType';

/**
* The regex equivalent of $types
*
* @var string
*/
protected $typesPattern;

/**
* @var TypeContainer
*/
private $typeContainer;

/**
* Parser constructor
*
*/
public function __construct()
{
$this->typeContainer = new TypeContainer();
$this->typeContainer->add($this->typeFallback);

foreach ($this->types as $key => $value) {
$this->typeContainer->add($key);
}

$this->buildTypesPattern();
parent::__construct();
}

public function registerType($class, $token)
{
$this->types[$class] = $token;
$this->typeContainer->add($class);
$this->buildTypesPattern();
}

public function unregisterType($class)
{
unset($this->types[$class]);
$this->typeContainer->remove($class);
$this->buildTypesPattern();
}

Expand All @@ -65,16 +86,18 @@ public function unregisterType($class)
protected function parseValue($value, $key = null)
{
$value = trim($value);
$type = '\Minime\\Annotations\\Types\\DynamicType';
$type = $this->typeFallback;

if (preg_match($this->typesPattern, $value, $found)) { // strong typed
$type = $found[1];
$value = trim(substr($value, strlen($type)));

if (in_array($type, $this->types)) {
$type = array_search($type, $this->types);
}
}

return (new $type)->parse($value, $key);
return $this->typeContainer->{$type}->parse($value, $key);
}

/**
Expand All @@ -96,4 +119,8 @@ protected function buildTypesPattern()
{
$this->typesPattern = '/^('.implode('|', $this->types).')(\s+)/';
}

public function registerConcreteNamespaceLookup(array $namespaces) {
$this->typeContainer->concreteType->setNamespaces($namespaces);
}
}
77 changes: 77 additions & 0 deletions src/TypeContainer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Minime\Annotations;

/**
* @property Types\IntegerType $integerType
* @property Types\StringType $stringType
* @property Types\FloatType $floatType
* @property Types\JsonType $jsonType
* @property Types\ConcreteType $concreteType
*/
class TypeContainer
{
/**
* Stores the lambda functions for each type.
*
* @var array
*/
private $builders;

/**
* Stores built types.
*
* @var array
*/
private $types;

/**
* Add a lambda function.
*
* @param string $name
*/
public function add($name)
{
$this->builders[$name] = function() use($name) {
$type = 'Minime\\Annotations\\Types\\' . ucfirst($name);

// do we have in default configuration setup?
if (!class_exists($type)) {
$type = $name;
}

if (is_callable($type . '::getType')) {
$typeClass = call_user_func($type . '::getType');
} else {
$typeClass = new $type;
}

return new $typeClass;
};
}

/**
* Remove a lambda function.
*
* @param string $name
*/
public function remove($name)
{
unset($this->builders[$name]);
unset($this->types[$name]);
}

/**
* @param string $name
*
* @return TypeInterface
*/
public function __get($name)
{
if (!isset($this->types[$name]) && isset($this->builders[$name])) {
$this->types[$name] = $this->builders[$name]();
}

return $this->types[$name];
}
}
Loading