Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierozi committed Sep 14, 2016
0 parents commit 03376dc
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
34 changes: 34 additions & 0 deletions api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Checker {
trait Scalar {
function isString() {
}
}
}

namespace {

class Parameter
{
use Checker\Scalar;
}


$phone = new Parameter('phone', '0648099915');


if ('0648099915' === $Parameter->value()) {
echo '1';
}

if (true === $Parameter->isValid()) {
echo '2';
}

$Parameters = new Parameters(['name' => 'pierre', 'phone', '0648099915']);

true === $Parameters->isValid();
//[...] === $Parameters->values();

}
Empty file added composer.json
Empty file.
16 changes: 16 additions & 0 deletions src/Checker/Scalar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Plab\Parameter\Checker;

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

public function isBoolean($value)
{
return is_bool($value);
}
}
24 changes: 24 additions & 0 deletions src/Converter/Scalar.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Plab\Parameter\Converter;

trait Scalar
{
public function toString($value) : string
{
return '' . $value;
}

public function toBoolean($value) : bool
{
if ('true' === $value || 1 === $value || 'on' === $value) {
return true;
}

if ('false' === $value || 0 === $value || 'off' === $value) {
return false;
}

return (bool)($value);
}
}
56 changes: 56 additions & 0 deletions src/Parameter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Plab\Parameter;

abstract class Parameter
{
protected $key;
protected $value;

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

/**
* Return true if parameter must be readonly and not edited from user space
* @param $key
* @return bool
*/
abstract protected function isReadOnly($key) : boolean;

/**
* Map converter callback, must be pure method
* @param $key
* @return string
*/
abstract protected function mapConverter($key) : string;

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

public function isValid()
{
$checker = $this->mapAssertion($this->key);

if (null === $checker) {
//TODO assertion missing for this parameter
}
}

public function value()
{
$conveter = $this->mapConverter($this->key);

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

//TODO call converter
}
}
40 changes: 40 additions & 0 deletions src/Parameters.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Plab\Parameter;

class Parameters implements Iterator
{
protected $iterator = [];

public function __construct(Iterator $parameters)
{

foreach ($parameters as $key => $value) {

if ($value instanceof Parameter) {
$this->iterator = $value;
continue;
}

$this->iterator[] = new Parameter($key, $value);
}
}

public function isValid()
{
foreach ($this->iterator as $parameter) {
//TODO we dont need stop if we want group hoa exception
}
}

public function values()
{
$result = [];

foreach ($this->iterator as $parameter) {
$result[$parameter->key()] = $parameter->value();
}

return $result;
}
}

0 comments on commit 03376dc

Please sign in to comment.