Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Commit

Permalink
more tests, more stringproperty functions, test extensions of objecta…
Browse files Browse the repository at this point in the history
…rray and arraylist
  • Loading branch information
perryflynn committed Apr 11, 2017
1 parent 3a34f2b commit cbeccc8
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 26 deletions.
9 changes: 0 additions & 9 deletions src/PerrysLambda/ScalarProperty.php
Expand Up @@ -63,15 +63,6 @@ public function isBool()
return is_bool($this->getData());
}

/**
* Is value null
* @return boolean
*/
public function isNull()
{
return is_null($this->getData());
}

/**
* Cast to numeric
* @return numeric
Expand Down
134 changes: 131 additions & 3 deletions src/PerrysLambda/StringProperty.php
Expand Up @@ -116,11 +116,92 @@ public function length()
return mb_strlen($this->toString(), $this->getEncoding());
}

/**
* Check for empty string
* @return boolean
*/
public function isEmpty()
{
return $this->getData()==="";
}

/**
* Check for null value
* @return boolean
*/
public function isNull()
{
return $this->getData()===null;
}

/**
* Check for empty string or null value
* @return boolean
*/
public function isNullOrEmpty()
{
return $this->isEmpty() || $this->isNull();
}

/**
* Check string is null, empty or contains only whitespaces
* @return boolean
*/
public function isNullOrWhitespace()
{
return $this->isNull() || mb_strlen(trim($this->toString()), $this->getEncoding())===0;
}

/**
* Trim whitespaces
* @return \PerrysLambda\StringProperty
*/
public function trim()
{
return $this->newInstance(trim($this->toString()));
}

/**
* Trim whitespaces on the left side
* @return \PerrysLambda\StringProperty
*/
public function ltrim()
{
return $this->newInstance(ltrim($this->toString()));
}

/**
* Trim whitespaces on the right side
* @return \PerrysLambda\StringProperty
*/
public function rtrim()
{
return $this->newInstance(rtrim($this->toString()));
}

/**
* Convert to lower chars
* @return \PerrysLambda\StringProperty
*/
public function toLower()
{
return $this->newInstance(strtolower($this->toString()));
}

/**
* Convert to upper chars
* @return \PerrysLambda\StringProperty
*/
public function toUpper()
{
return $this->newInstance(strtoupper($this->toString()));
}

/**
* Return a substring of value
* @param int $start
* @param int $length
* @return string
* @return \PerrysLambda\StringProperty
*/
public function substr($start, $length=null)
{
Expand All @@ -130,11 +211,58 @@ public function substr($start, $length=null)
/**
* Split string by separator
* @param string $separator
* @return array
* @return \PerrysLambda\ArrayList
*/
public function split($separator)
{
return explode($separator, $this->toString());
$temp = explode($separator, $this->toString());
$list = ArrayList::asType($this->getClassName(), $temp);
return $list;
}

/**
* Helper function for string padding
* @param string $padstr
* @param int $length
* @param int $type
* @return \PerrysLambda\StringProperty
*/
protected function padDynamic($padstr, $length, $type)
{
return $this->newInstance(str_pad($this->toString(), $length, $padstr, $type));
}

/**
* Add a padding to the beginning of the string
* @param inr $length
* @param string $padstr
* @return \PerrysLambda\StringProperty
*/
public function padLeft($length, $padstr=" ")
{
return $this->padDynamic($padstr, $length, STR_PAD_LEFT);
}

/**
* Add a padding to the end of the string
* @param int $length
* @param string $padstr
* @return \PerrysLambda\StringProperty
*/
public function padRight($length, $padstr=" ")
{
return $this->padDynamic($padstr, $length, STR_PAD_RIGHT);
}

/**
* Add a padding to the end and the beginning of the string
* @param int $length
* @param string $padstr
* @return \PerrysLambda\StringProperty
*/
public function padBoth($length, $padstr=" ")
{
return $this->padDynamic($padstr, $length, STR_PAD_BOTH);
}

/**
Expand Down
37 changes: 27 additions & 10 deletions test/CustomTypeTest.php
@@ -1,32 +1,49 @@
<?php

use PerrysLambda\Converter\ListConverter;
use PerrysLambda\ArrayList;
use PerrysLambda\ObjectArray;
use PerrysLambda\Serializer\Serializer;
use PerrysLambda\IArrayable;
use PerrysLambda\ArrayBase;
use PerrysLambda\Converter\ItemConverter;
use PerrysLambda\IItemConverter;
use PerrysLambda\Serializer\DateTimeSerializer;
use PerrysLambda\Serializer\BooleanSerializer;

class CustomTypeTest extends PHPUnit_Framework_TestCase
{

public function testCustomTypes()
{
$this->assertSame(true, true);
$data = array(
array('name' => 'anemone', 'ranking'=>5),
array('name' => 'steak', 'ranking'=>10),
array('name' => 'crocus', 'ranking'=>5.5),
array('name' => 'super mario', 'ranking'=>8),
array('name' => 'daisy', 'ranking'=>6),
array('name' => 'money', 'ranking'=>7),
);

$list = MyArrayList::asType('MyObjectArray', $data);
$this->assertSame(true, $list->first() instanceof MyObjectArray);

$flowers = $list->getFlowers();
$this->assertSame(true, $flowers instanceof MyArrayList);
$this->assertSame(3, $flowers->length());
$this->assertSame('daisy', $flowers->last()->name);
$this->assertSame(true, $flowers->last()->isFlower());
}

}

class MyArrayList extends ArrayList
{
public function getFlowers()
{
return $this->where(function($v) { return $v->isFlower(); });
}
}

class MyObjectArray extends ObjectArray
{

public function isFlower()
{
//return in_array($this->getScalar('flower')->, array(''));
$flowers = new ArrayList(array('anemone', 'crocus', 'daisy', 'hibiscus'));
return $flowers->any(function($v) { return $v==$this->nameScalar->toLower()->toString(); });
}

}
54 changes: 50 additions & 4 deletions test/LambdaTest.php
Expand Up @@ -18,26 +18,72 @@ public function testProperty()

public function testScalar()
{
$s = new ScalarProperty("Zähn € zahme Ziegen zögen zwei Zentner Zücker zum Zoö!", 'UTF-8');
$s = new ScalarProperty("Zähn € zahme Ziegen zögen zwei Zentner Zücker zum Zoö!", 'UTF-8');

$this->assertSame(true, $s->startsWith(""));
$this->assertSame(false, $s->startsWith(""));
$this->assertSame(true, $s->startsWithI(""));
$this->assertSame(true, $s->endsWith(' Zoö!'));
$this->assertSame(false, $s->endsWith(' zoö!'));
$this->assertSame(true, $s->endsWithI(' zoö!'));
$this->assertSame(54, $s->length());
$this->assertSame(55, $s->length());
$this->assertSame(3, count($s->split('ö')));
$this->assertSame(' Zoö!', $s->substr(-5)->toString());
$this->assertSame(1, $s->indexOf('ä'));
$this->assertSame(-1, $s->indexOf('Ä'));
$this->assertSame(1, $s->indexOfI('Ä'));
$this->assertSame(53, $s->lastIndexOf('!'));
$this->assertSame(50, $s->lastIndexOfI('zoö!'));
$this->assertSame(54, $s->lastIndexOf('!'));
$this->assertSame(51, $s->lastIndexOfI('zoö!'));
$this->assertSame(false, $s->contains('asdf'));
$this->assertSame(true, $s->contains('zwei'));
$this->assertSame(false, $s->contains('ZWEI'));
$this->assertSame(true, $s->containsI('ZWEI'));
$this->assertSame(false, $s->isNullOrWhitespace());

$split = $s->split(' ');
$this->assertSame(11, $split->length());
$split = $split->where(function($v) { return !$v->isNullOrWhitespace(); });
$this->assertSame(10, $split->length());
$this->assertSame(true, $split->first() instanceof \PerrysLambda\StringProperty);

$str = new \PerrysLambda\StringProperty("");
$this->assertSame(true, $str->isEmpty());
$this->assertSame(false, $str->isNull());
$this->assertSame(true, $str->isNullOrEmpty());
$this->assertSame(true, $str->isNullOrWhitespace());

$str->setData(null);
$this->assertSame(false, $str->isEmpty());
$this->assertSame(true, $str->isNull());
$this->assertSame(true, $str->isNullOrEmpty());
$this->assertSame(true, $str->isNullOrWhitespace());

$str->setData(" ");
$this->assertSame(true, $str->isNullOrWhitespace());
$this->assertSame(true, $str->trim()->isEmpty());

$str->setData(" a ");
$this->assertSame(true, $str->startsWith(" "));
$this->assertSame(true, $str->ltrim()->startsWith("a"));
$this->assertSame(8, $str->ltrim()->length());
$this->assertSame(true, $str->ltrim()->endsWith(' '));
$this->assertSame(true, $str->rtrim()->endsWith("a"));
$this->assertSame(6, $str->rtrim()->length());
$this->assertSame(true, $str->rtrim()->startsWith(' '));
$this->assertNotSame('a', $str->trim()->toUpper()->toString());
$this->assertSame('A', $str->trim()->toUpper()->toString());

$str->setData(" A ");
$this->assertNotSame('A', $str->trim()->toLower()->toString());
$this->assertSame('a', $str->trim()->toLower()->toString());

$str->setData('12');
$this->assertSame(' 12', $str->padLeft(5)->toString());
$this->assertSame('00012', $str->padLeft(5, '0')->toString());
$this->assertSame('12 ', $str->padRight(5)->toString());
$this->assertSame('12000', $str->padRight(5, '0')->toString());
$this->assertSame(' 12 ', $str->padBoth(5)->toString());
$this->assertSame('01200', $str->padBoth(5, '0')->toString());

$integer = new ScalarProperty('4211');
$this->assertSame(4211, $integer->toInt());
Expand Down

0 comments on commit cbeccc8

Please sign in to comment.