Skip to content

Commit

Permalink
Add tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Milan Matejcek committed Jan 15, 2015
1 parent f175877 commit 3a0364a
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 195 deletions.
141 changes: 141 additions & 0 deletions src/Basic/Arrays.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

namespace h4kuna\DataType\Basic;

use h4kuna\DataType\DataTypeException;

/**
* @author Milan Matějček
*/
final class Arrays
{

private function __construct()
{

}

/**
* Better array_combine where values array does not need same size.
*
* @param array $keys
* @param array $values
* @param mixed $value
* @return array
*/
static function combine(array $keys, array $values, $value = NULL)
{
$diff = count($keys) - count($values);

if ($diff > 0) {
$values = array_merge($values, array_fill(0, $diff, $value));
} elseif ($diff < 0) {
throw new DataTypeException('Array of values can\'t be bigger than keys.');
}

return array_combine($keys, $values);
}

/**
* Implode only values where strlen > 0 and you can define keys.
*
* @param string $glue
* @param array $array
* @param string $keys
* @return string
*/
static function concatWs($glue, $array /* , ... keys */)
{
$keys = array();
if (func_num_args() > 2) {
$keys = array_slice(func_get_args(), 2);
}
$out = '';
foreach ($keys ? self::intesectKeys($array, $keys) : $array as $value) {
if (!strlen($value)) {
continue;
} elseif ($out !== '') {
$out .= $glue;
}
$out .= $value;
}
return $out;
}

/**
* COALESCE similar behavior database
*
* @param array $array
* @return string|NULL
*/
static function coalesce($array /* , $keys, ... */)
{
if (func_num_args() > 1) {
$keys = array_slice(func_get_args(), 1);
$array = self::intesectKeys($array, $keys);
}
foreach ($array as $v) {
if (strlen($v)) {
return $v;
}
}
return NULL;
}

/**
* Unset keys from array
*
* @param array $array
* @param string $key
* @return array Removed keys
*/
static function keysUnset(array & $array, $key /* , ... keys */)
{
$out = array();
$args = array_slice(func_get_args(), 1);
foreach ($args as $key) {
if (array_key_exists($key, $array)) {
$out[$key] = $array[$key];
unset($array[$key]);
}
}
return $out;
}

public static function intesectKeys(array $values, array $keys)
{
return array_intersect_key($values, array_flip($keys));
}

/**
* @see array_column
* @param array $array
* @param string $columnName
* @param string $key
* @return array
*/
static function column($array, $columnName = NULL, $key = NULL)
{
if (PHP_VERSION_ID >= 50500) {
return array_column($array, $columnName, $key);
} elseif ($key === NULL && $key === $columnName) {
return $array;
}
$out = array();
if ($columnName !== NULL && $key !== NULL) {
foreach ($array as $v) {
$out[$v[$key]] = $v[$columnName];
}
} elseif ($columnName !== NULL) {
foreach ($array as $v) {
$out[] = $v[$columnName];
}
} else {
foreach ($array as $v) {
$out[$v[$key]] = $v;
}
}
return $out;
}

}
12 changes: 7 additions & 5 deletions src/Validator/Float.php → src/Basic/Float.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace h4kuna\DataType\Validator;
namespace h4kuna\DataType\Basic;

use h4kuna\DataType\DataTypeException;

/**
* @author Milan Matějček
Expand Down Expand Up @@ -33,9 +35,9 @@ public static function fromString($value)
return self::fromHour($value);
}

$out = preg_replace_callback('/(?:^-?)|([^\,\.\d]+|,)/', function ($found) {
$out = preg_replace_callback('/(?:^\s?-?)|([^\,\.\w]+|,)/i', function ($found) {
if (!isset($found[1])) {
return $found[0];
return trim($found[0]);
}
if (isset($found[1]) && $found[1] === ',') {
return '.';
Expand All @@ -44,11 +46,11 @@ public static function fromString($value)
}, $value);


if (!is_numeric($value)) {
if (!is_numeric($out)) {
throw new DataTypeException('This value is not float: ' . $value);
}

return floatval($value);
return floatval($out);
}

/**
Expand Down
39 changes: 19 additions & 20 deletions src/Validator/Gps.php → src/Basic/Gps.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace h4kuna\DataType\Validator;
namespace h4kuna\DataType\Basic;

use h4kuna\DataType\DataTypeException;

/**
* @author Milan Matějček
Expand All @@ -21,26 +23,26 @@ private function __construct()
*/
public static function fromString($value)
{
$found = array();
$out = $found = array();
if (preg_match('~^(\d{1,3}\.\d+?)(N|S), ?(\d{1,3}\.\d+?)(E|W)$~i', $value, $found)) {
//50.4113628N, 14.9032000E
$this->setCoordinate(self::checkCoordinate($found[3], $found[4]), self::checkCoordinate($found[1], $found[2]));
// 50.4113628N, 14.9032000E
$out = self::setCoordinate(self::checkCoordinate($found[1], $found[2]), self::checkCoordinate($found[3], $found[4]));
} elseif (preg_match('~(-?\d{1,3}\.\d+), ?(-?\d{1,3}\.\d+)$~', $value, $found)) {
//50.4113628, 14.9032000
$this->setCoordinate($found[2], $found[1]);
// 50.4113628, 14.9032000
$out = self::setCoordinate($found[1], $found[2]);
} elseif (preg_match('~^(N|S) ?(\d{1,3})°(\d{1,2}\.\d+?)\',? ?(W|E) ?(\d{1,3})°(\d{1,2}\.\d+?)\'$~i', $value, $found)) {
//N 50°24.68177', E 14°54.19200'
$this->setCoordinate(self::checkCoordinate(self::degToDec($found[5], $found[6]), $found[4]), self::checkCoordinate(self::degToDec($found[2], $found[3]), $found[1]));
// N 50°24.68177', E 14°54.19200'
$out = self::setCoordinate(self::checkCoordinate(self::degToDec($found[2], $found[3]), $found[1]), self::checkCoordinate(self::degToDec($found[5], $found[6]), $found[4]));
} elseif (preg_match('~^(\d{1,3})°(\d{1,2})\'(\d{1,2}\.\d+?)"(N|S), ?(\d{1,3})°(\d{1,2})\'(\d{1,2}\.\d+?)"(W|E)$~i', $value, $found)) {
//50°24'40.906"N, 14°54'11.520"E
$this->setCoordinate(self::checkCoordinate(self::degToDec($found[5], $found[6], $found[7]), $found[8]), self::checkCoordinate(self::degToDec($found[1], $found[2], $found[3]), $found[4]));
// 50°24'40.906"N, 14°54'11.520"E
$out = self::setCoordinate(self::checkCoordinate(self::degToDec($found[1], $found[2], $found[3]), $found[4]), self::checkCoordinate(self::degToDec($found[5], $found[6], $found[7]), $found[8]));
} elseif (preg_match('~^(N|S)(\d{1,3}\.\d+?)° ?(E|W)(\d{1,3}\.\d+?)°$~i', $value, $found)) {
//N49.20811° E19.04247°
$this->setCoordinate(self::checkCoordinate($found[4], $found[3]), self::checkCoordinate($found[2], $found[1]));
// N49.20811° E19.04247°
$out = self::setCoordinate(self::checkCoordinate($found[2], $found[1]), self::checkCoordinate($found[4], $found[3]));
} else {
throw new DataTypeException('Unsupported coordinate. ' . $value);
}
return $this;
return $out;
}

/**
Expand Down Expand Up @@ -85,22 +87,19 @@ private static function checkCoordinate($num, $pole)
* @param float $seconds
* @return float
*/
public static function degToDec($degrees, $minutes, $seconds = 0)
private static function degToDec($degrees, $minutes, $seconds = 0)
{
return $degrees + $minutes / 60 + $seconds / 3600;
}

/**
* @param float $x
* @param float $y
* @return float[]
*/
private function setCoordinate($x, $y)
private static function setCoordinate($x, $y)
{
$setup = $this->getSetup();
return array(
$setup->getXName() => round($x, $setup->getRound()),
$setup->getYName() => round($y, $setup->getRound())
);
return array($x, $y);
}

}
6 changes: 4 additions & 2 deletions src/Validator/Int.php → src/Basic/Int.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace h4kuna\DataType\Validator;
namespace h4kuna\DataType\Basic;

use h4kuna\DataType\DataTypeException;

/**
* @author Milan Matějček
Expand Down Expand Up @@ -28,7 +30,7 @@ public static function fromString($value)
if (is_numeric($value) && $value == intval($value)) {
return intval($value);
}
$out = preg_replace('~\W~', '', $value);
$out = preg_replace('~\s~', '', $value);
$int = intval($out);
if ($out != $int) {
throw new DataTypeException('Input value is not integer. ' . $out);
Expand Down
64 changes: 64 additions & 0 deletions src/Basic/Set.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace h4kuna\DataType\Basic;

use h4kuna\DataType\DataTypeException;
use Iterator;

/**
* Transform set from string to array and vice versa
* MySQL data type SET to checkboxlist
*
* @example
* array(
* foo => TRUE,
* bar => TRUE,
* joe => FALSE
* )
*
* string: foo,bar
* array: [foo => TRUE, bar => TRUE]
*
* @author Milan Matějček
*/
final class Set
{

private function __construct()
{

}

/**
*
* @param string $value
* @return array
*/
public static function fromString($value)
{
if (!is_string($value)) {
throw new DataTypeException('Set must be string and delimited by comma. For example: foo,bar,joe');
}
return array_fill_keys(explode(',', $value), TRUE);
}

/**
*
* @param array|Iterator $set
* @return string
*/
public static function toString($set)
{
$out = '';
foreach ($set as $k => $v) {
if ($v) {
if ($out !== '') {
$out .= ',';
}
$out .= $k;
}
}
return $out;
}

}
21 changes: 14 additions & 7 deletions src/Set.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,25 @@
class Set extends DataType
{

private $set = array();
/** @var array */
private $list = array();

const KEYS = 1;
const AS_STRING = 3;

/**
* @param array $set
*/
public function __construct(array $set = array())
public function __construct($list = array())
{
$this->clean();
$this->set = $set;
if ($list !== array()) {
$this->setList($list);
}
}

public function setList($list)
{
if ($this->lock) {
throw new DataTypeException('List is defined and you can\'t change.');
}
$this->lock = TRUE;
}

/**
Expand Down
Loading

0 comments on commit 3a0364a

Please sign in to comment.