Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim DeLois committed Jun 26, 2015
1 parent 1a408bc commit d83426e
Show file tree
Hide file tree
Showing 22 changed files with 1,208 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
@@ -0,0 +1,14 @@
# Vendor Definitions
/composer.lock

# Vendor Directories
/vendor

# Editor/IDE Artifacts
/.idea

# User-Specific Overrides
/phpunit.xml

# Build artifacts
/build
38 changes: 38 additions & 0 deletions README.md
@@ -1,2 +1,40 @@
# raml-generator
An object-oriented, PHP library intended to generate RAML. Useful for authoring custom parsers.

## Working example (of functionality so far)

```php

use DeLois\Raml\Generator\Model\Api;
use DeLois\Raml\Generator\Model\Parameter\StringParameter;
use DeLois\Raml\Generator\Model\Resource;
use DeLois\Raml\Generator\Model\Resource\Request;
use Improv\Http\Request\Method;

$api = ( new Api() )
->setTitle( 'The Best API Known to Man' )
->setVersion( 'v1' )
->setBaseUri( new BaseUri( 'https://example.com/{version}' ) )
->addProtocol( Api::PROTOCOL_HTTPS )
->setDefaultMediaType( Api::MEDIA_TYPE_JSON );

$resource_members = ( new Resource( new UriSegment( '/members' ) ) )
->displayAs( 'Member Profiles' )
->describeAs( 'This endpoint provides the ability to search for member profiles by ID' );

$api->addResource( $resource_users );


$request_get = ( new Request( Method::GET ) )
->describeAs( 'Get Member Profile(s)' )
->addParameter( new StringParameter( 'profile_id', 'The Profile ID of the user whose profile is desired', true ) )
;

$resource_members->addRequest( $request_get );

//$request_get->addResponse();
// etc

// Obviously still need to add the rendering/output...

```
47 changes: 47 additions & 0 deletions composer.json
@@ -0,0 +1,47 @@
{
"name" : "jimdelois/raml-generator" ,
"description": "An object-oriented, PHP library intended to generate RAML. Useful for authoring custom parsers.",
"type": "library",
"keywords": [
"improv",
"improvframework",
"jimdelois",
"delois",
"raml",
"rest",
"api",
"documentation"
],
"homepage": "https://github.com/jimdelois/raml-generator/",
"license": "BSD-3-Clause",
"authors": [
{
"name": "Jim DeLois",
"email": "jim.delois@improvframework.org",
"homepage": "http://www.improvframework.org/"
}
],
"minimum-stability": "stable",
"require": {
"improvframework/http-constants": "~0.0.1",
"php": ">=5.6.0"
},
"require-dev" : {
"phpunit/phpunit": "~4.7.5",
"phing/phing": "~2.10.0",
"phpdocumentor/phpdocumentor": "~2.8.0",
"fiunchinho/phpunit-randomizer": "~2.0.0",
"php": ">=5.6.0"
},
"autoload": {
"psr-4": {
"Improv\\Http\\": "src/Http",
"DeLois\\Raml\\": "src/Raml"
}
},
"autoload-dev": {
"psr-4": {
"DeLois\\Raml\\Test\\": "tests/lib"
}
}
}
39 changes: 39 additions & 0 deletions phpunit.xml.dist
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
colors="true"
forceCoversAnnotation="true"
verbose="true"
>

<php>
<ini name="memory_limit" value="-1"/>
</php>

<testsuites>
<testsuite name="All Tests">
<directory suffix=".php">tests</directory>
</testsuite>
<testsuite name="Unit Tests">
<directory suffix=".php">tests/unit</directory>
</testsuite>
</testsuites>

<filter>
<blacklist>
<directory>vendor</directory>
<!--<directory>tests</directory>-->
</blacklist>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>

<logging>
<log type="junit" target="build/reports/phpunit-log-junit.xml" />
<log type="tap" target="build/reports/phpunit-log-tap.tap" />
<log type="coverage-clover" target="build/reports/phpunit-coverage-clover.xml" />
<log type="coverage-html" target="build/reports/phpunit-coverage-html" title="Improv.ArgParse" />
<log type="coverage-text" target="build/reports/phpunit-coverage-text.txt" title="Improv.ArgParse" />
</logging>

</phpunit>
156 changes: 156 additions & 0 deletions src/Raml/Generator/Model/AbstractNamedParameter.php
@@ -0,0 +1,156 @@
<?php

namespace DeLois\Raml\Generator\Model;


abstract class AbstractNamedParameter {

protected $_name;

protected $_display_name;

protected $_description;

protected $_type;

protected $_example;

protected $_repeat = false;

protected $_required = false;

protected $_default;

const TYPE_STRING = 'string';
const TYPE_NUMBER = 'float';
const TYPE_INTEGER = 'integer';
const TYPE_DATE = 'date'; // Must be UTC
const TYPE_BOOLEAN = 'boolean';
const TYPE_FILE = 'file';

public function __construct( $type, $name, /*SomeClass*/ $description = null, $required = false ) {

$this->_name = $name;
$this->_type = $type;
$this->_description = $description;
$this->_required = $required;

}

public function getName() {

return $this->_name;

}

public function getType() {

return $this->_type;

}

public function displayAs( $display ) {

$this->_display_name = $display;
return $this;

}

public function getDisplayName() {

return $this->_display_name;

}

public function describeAs( $description ) {

$this->_description = $description;
return $this;

}

public function getDescription() {

return $this->_description;

}

public function addExample( $example ) {

$this->_example = $example;
return $this;

}

public function getExample() {

return $this->_example;

}

public function repeats( $bool = null ) {

if ( $bool !== null ) {
$this->_repeat = $bool;
return $this;
}

return $this->_repeat;

}

public function required( $bool = null ) {

if ( $bool !== null ) {
$this->_required = $bool;
return $this;
}

return $this->_required;

}

public function defaultsTo( $default ) {

$this->_default = $default;
return $this;

}

public function getDefault() {

return $this->_default;

}


public static function isValidType( $type ) {

static $cache_map = null;

if ( $cache_map === null ) {

$class = new \ReflectionClass( get_called_class() );
$constants = new \ArrayIterator( $class->getConstants() );
$filtered = new \CallbackFilterIterator( $constants, function ( $value, $key ) {
return strpos( $key, 'TYPE_' ) === 0;
} );

foreach ( $filtered as $value ) {
$cache_map[] = $value;
}

}

return in_array( $type, $cache_map );

}

public function isType( $type ) {

return $this->getType() === $type;

}

}

68 changes: 68 additions & 0 deletions src/Raml/Generator/Model/AbstractUri.php
@@ -0,0 +1,68 @@
<?php

namespace DeLois\Raml\Generator\Model;

abstract class AbstractUri {

protected $_uri;

protected $_parameters = [];

public function __construct( $uri ) {

if ( !$this->_validate( $uri ) ) {
throw new \InvalidArgumentException( sprintf( 'Invalid URI "%s": %s.', $uri, $this->_getFormatDescripton() ) );
}
$this->_uri = $uri;

}

public function __toString() {

return $this->_uri;

}

public function addParameter( AbstractNamedParameter $parameter ) {

$key = $parameter->getName();

$token = $this->_tokenizeParameterKey( $key );

if ( strpos( $this->_uri, $token ) === false ) {
throw new \InvalidArgumentException( sprintf( 'No param "%s" found in URI "%s"', $token, $this->_uri ) );
}

$this->_parameters[ $key ] = $parameter;

return $this;

}

public function addParameters( array $parameters ) {

foreach( $parameters as $parameter ) {
$this->addParameter( $parameter );
}

return $this;

}

public function getParameters() {

return $this->_parameters;

}

private function _tokenizeParameterKey( $key ) {

return "{{$key}}";

}

abstract protected function _validate( $uri );

abstract protected function _getFormatDescripton();

}

0 comments on commit d83426e

Please sign in to comment.