Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
serkin committed May 4, 2015
0 parents commit 882f504
Show file tree
Hide file tree
Showing 12 changed files with 268 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
/nbproject/
/.idea/
/vendor/
/build/
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Serkin Alexander <serkin.alexander@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
26 changes: 26 additions & 0 deletions composer.json
@@ -0,0 +1,26 @@
{
"name": "serkin/volan",
"type": "library",
"description": "PHP array schema validator",
"keywords": ["php", "validation"],
"homepage": "https://github.com/serkin/volan",
"license": "MIT",
"authors": [
{
"name": "Serkin Alexander",
"email": "serkin.alexander@gmail.com",
"homepage": "http://linkedin.com/in/SerkinAlexander",
"role": "Developer"
}
],
"require": {
"php": ">=5.5",
"symfony/yaml": "~2.6"
},

"autoload": {
"psr-4": {
"Volan\\": "src/"
}
}
}
70 changes: 70 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions phpunit.xml
@@ -0,0 +1,16 @@
<phpunit
bootstrap="vendor/autoload.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
colors="true"
verbose="true">
<php>
<var name="fixture_path" value="tests/fixtures"/>
</php>
<testsuites>
<testsuite name="string">
<directory suffix='.php'>tests/unit/String</directory>
</testsuite>
</testsuites>
</phpunit>
6 changes: 6 additions & 0 deletions src/CustomRecursiveArrayIterator.php
@@ -0,0 +1,6 @@
<?php

namespace Volan;

class CustomRecursiveArrayIterator extends \RecursiveArrayIterator
{}
17 changes: 17 additions & 0 deletions src/Validator/AbstractValidator.php
@@ -0,0 +1,17 @@
<?php

namespace Volan\Validator;


abstract class AbstractValidator
{

public function isRequired()
{
return false;
}

abstract function isValid($arrNode, $schemaNode);


}
16 changes: 16 additions & 0 deletions src/Validator/required_string.php
@@ -0,0 +1,16 @@
<?php

namespace Volan\Validator;

class required_string extends AbstractValidator
{

public function isRequired() {
return true;
}

public function isValid($arrNode, $schemaNode)
{
return false;
}
}
33 changes: 33 additions & 0 deletions src/Volan.php
@@ -0,0 +1,33 @@
<?php

namespace Volan;

class Volan
{

private $schema;


public function __construct($schema) {
$this->schema = $schema;
}

public function validate($arr)
{

foreach ($arr as $node):
if(empty($node['_type']) or class_exists($node['_type'], true)):
return 0;
endif;

$nodeTypeClassString = "\Volan\Validator\\" . $node['_type'];

$nodeTypeClass = new $nodeTypeClassString;

unset($node['_type']);
$keys = array_keys($node);
endforeach;

}

}
19 changes: 19 additions & 0 deletions tests/fixtures/schema/schema.yml
@@ -0,0 +1,19 @@
root:
books:
_type: strict_array
title:
_type: required_string
price:
_type: float
comments:
_type: strict_nested_array

author:
_type: required_string
id_user:
_type: required_mongoid
hide:
_type: boolean
comment:
_type: string

3 changes: 3 additions & 0 deletions tests/fixtures/schema/string/required_string.yml
@@ -0,0 +1,3 @@
root:
title:
_type: required_string
37 changes: 37 additions & 0 deletions tests/unit/String/required_string.php
@@ -0,0 +1,37 @@
<?php

use Symfony\Component\Yaml\Parser;

class required_string extends PHPUnit_Framework_TestCase
{
public $schema;


public function setUp()
{
$yaml = new Parser();
$this->schema = $yaml->parse(file_get_contents($GLOBALS['fixture_path'] . '/schema/string/required_string.yml'));
}

public function additionProvider()
{
return [
['title' => null],
['title' => []],
['title' => ''],
['title' => [1,2,3]]
];
}

/**
* @dataProvider additionProvider
*/
public function testInvalidData($arr)
{

$validator = new \Volan\Volan($this->schema);
$result = $validator->validate($arr);

$this->assertTrue($result);
}
}

0 comments on commit 882f504

Please sign in to comment.