Skip to content

Commit

Permalink
Add first version of library
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierozi committed Sep 14, 2016
1 parent 03376dc commit 9991a88
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 67 deletions.
36 changes: 36 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/vendor/
/composer.lock

# PhpStorm
.idea


# Created by https://www.gitignore.io/api/osx

### OSX ###
*.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
34 changes: 0 additions & 34 deletions api.php

This file was deleted.

20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "plab/parameter",
"license": "BSD-3-Clause",
"authors": [
{ "name": "Pierre Tomasina" }
],
"require": {
"php": ">=7.0.0",
"hoa/ruler": "~2.0",
"hoa/ustring": "~4.0"
},
"require-dev": {
"psy/psysh": "^0.6.1"
},
"autoload": {
"psr-4": {
"Plab\\Parameter\\": "src/"
}
}
}
5 changes: 5 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
php:
image: plab/docker-php:7.0-fpm
volumes:
- .:/app
command: 'php-fpm'
9 changes: 7 additions & 2 deletions src/Checker/Scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

trait Scalar
{
public function isString($value)
public static function isString($value)
{
return is_string($value);
}

public function isBoolean($value)
public static function isBoolean($value)
{
return is_bool($value);
}

public static function isInteger($value)
{
return ctype_digit(strval($value));
}
}
36 changes: 34 additions & 2 deletions src/Converter/Scalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,36 @@

namespace Plab\Parameter\Converter;

use Hoa\Ustring\Ustring;

trait Scalar
{
public function toString($value) : string
/**
* Cast to native php string
* @param $value
* @return string
*/
public static function toString($value) : string
{
return '' . $value;
}

public function toBoolean($value) : bool
/**
* Cast value to Unicode String object served by Hoa\Ustring
* @param $value
* @return Ustring
*/
public static function toUString($value) : Ustring
{
return new Ustring($value);
}

/**
* Cast to native boolean string as web standard
* @param $value
* @return bool
*/
public static function toBoolean($value) : bool
{
if ('true' === $value || 1 === $value || 'on' === $value) {
return true;
Expand All @@ -21,4 +43,14 @@ public function toBoolean($value) : bool

return (bool)($value);
}

/**
* Cast to native integer
* @param $value
* @return int
*/
public static function toInteger($value) : int
{
return (int)$value;
}
}
16 changes: 16 additions & 0 deletions src/Operator/Scalar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Plab\Parameter\Operator;

trait Scalar
{
public static function opNoSpace($value) : bool
{
return 0 === preg_match('/\s+/', $value);
}

public static function opLen($value) : int
{
return strlen($value);
}
}
117 changes: 95 additions & 22 deletions src/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,128 @@

namespace Plab\Parameter;

/**
* Class Parameter
* @package Plab\Parameter
*/
abstract class Parameter
{
const checkersRules = [];
const converters = [];

protected $key;
protected $value;

/**
* Map assertions callback, must be pure method
* @param $key
* @return string
*/
abstract protected function mapAssertion($key) : string;
use Checker\Scalar;
use Converter\Scalar;
use Operator\Scalar;

/**
* Return true if parameter must be readonly and not edited from user space
* @param $key
* @return bool
*/
abstract protected function isReadOnly($key) : boolean;
//TODO implement constant for tolerance if checker missing ? throw notice, error, nothing

/**
* Map converter callback, must be pure method
* Parameter constructor.
* @param $key
* @return string
* @param $value
* @throws \Exception
*/
abstract protected function mapConverter($key) : string;

public function __construct($key, $value)
{
$this->key = $key;
$this->key = $key;
$this->value = $value;

if (empty(static::checkersRules)) {
throw new \Exception('You must implement constant checkersRules in ' . __CLASS__);
}
}

/**
* @return bool
* @throws \Exception
*/
public function isValid()
{
$checker = $this->mapAssertion($this->key);
$rule = static::checkersRules[$this->key] ?? null;

if (null === $rule) {
throw new \Exception('Checker rules not found', E_NOTICE);
}

$ruler = new \Hoa\Ruler\Ruler();
$context = new \Hoa\Ruler\Context();

$context['key'] = $this->key;
$context['value'] = $this->value;

$checkerMethods = $this->getMethods('is');
$converterMethods = $this->getMethods('to');
$operatorMethods = $this->getMethods('op');

$asserter = $ruler->getDefaultAsserter();

foreach ($checkerMethods as $method) {
$asserter->setOperator(mb_strtolower($method), xcallable($this, $method));
}

foreach ($converterMethods as $method) {
$asserter->setOperator(mb_strtolower($method), xcallable($this, $method));
}

if (null === $checker) {
//TODO assertion missing for this parameter
foreach ($operatorMethods as $method) {
$operatorName = mb_strtolower(substr($method, 2));
$asserter->setOperator($operatorName, xcallable($this, $method));
}

return $ruler->assert($rule, $context);
}

/**
* @return mixed
* @throws \Exception
*/
public function value()
{
$conveter = $this->mapConverter($this->key);
$converter = static::converters[$this->key] ?? null;

if (null === $converter) {
return $this->value;
}

//TODO call converter
if (false === in_array(
$converter,
$this->getMethods('to')
)) {
throw new \Exception('Converter method not found', E_ERROR);
}

return (xcallable($this, $converter))($this->value);
}

/**
* @return mixed
*/
public function key()
{
return $this->key;
}

/**
* @param $lexeme
* @return array
*/
public function getMethods($lexeme)
{
$reflection = new \ReflectionClass($this);

$methods = array_map(function($method) use($lexeme) {
if ($lexeme === substr($method->name, 0, 2)) {
return $method->name;
}

return null;
}, $reflection->getMethods());

return array_filter($methods, function($method) {
return $method !== null;
});
}
}
Loading

0 comments on commit 9991a88

Please sign in to comment.