-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 03376dc
Showing
6 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |