From: Andrey Kutejko Date: Tue, 17 Dec 2013 19:24:11 +0000 (+0200) Subject: introduce namespace PFF. version 0.2 X-Git-Tag: 0.2~25 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=27311daeac39ec247ddfd86614f3a73cb6080eec;p=missing-tools.git introduce namespace PFF. version 0.2 --- diff --git a/composer.json b/composer.json index 73108ec..b921b83 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,7 @@ ], "extra": { "branch-alias": { - "dev-master": "0.1-dev" + "dev-master": "0.2-dev" } } } diff --git a/src/array.php b/src/array.php new file mode 100644 index 0000000..e11671c --- /dev/null +++ b/src/array.php @@ -0,0 +1,49 @@ += $columnLengths[$columnIndex]) { + $columns[] = $column; + $column = array(); + $columnIndex++; + } + $column[] = $item; + } + $columns[] = $column; + return $columns; + } + + public static function chunks($collection, $size, $pad=false) + { + $result = array(); + $chunk = array(); + foreach ($collection as $item) { + $chunk[] = $item; + if (count($chunk) === $size) { + $result[] = $chunk; + $chunk = array(); + } + } + $c = count($chunk); + if ($c != 0) { + if ($pad) { + while ($c < $size) { + $chunk[] = null; + ++$c; + } + } + $result[] = $chunk; + } + return $result; + } +} + diff --git a/src/collectiontools.php b/src/collectiontools.php deleted file mode 100644 index fd62b2a..0000000 --- a/src/collectiontools.php +++ /dev/null @@ -1,60 +0,0 @@ -= $columnLengths[$columnIndex]) { - $columns[] = $column; - $column = array(); - $columnIndex++; - } - $column[] = $item; - } - $columns[] = $column; - return $columns; - } - - public static function chunks($collection, $size, $pad=false) - { - $result = array(); - $chunk = array(); - foreach ($collection as $item) { - $chunk[] = $item; - if (count($chunk) === $size) { - $result[] = $chunk; - $chunk = array(); - } - } - $c = count($chunk); - if ($c != 0) { - if ($pad) { - while ($c < $size) { - $chunk[] = null; - ++$c; - } - } - $result[] = $chunk; - } - return $result; - } -} - diff --git a/src/dateformat.php b/src/dateformat.php index 3032fbf..8bb76a2 100644 --- a/src/dateformat.php +++ b/src/dateformat.php @@ -1,5 +1,9 @@ 100, + 'grapefruit' => 400, + 'carrot' => 50, + ); + + $this->assertEquals(400, \PFF\Arr::get($arr, 'grapefruit')); + $this->assertEquals(null, \PFF\Arr::get($arr, 'orange')); + $this->assertEquals('no-oranges', \PFF\Arr::get($arr, 'orange', 'no-oranges')); + } + + public function testEnsureKeyExists() + { + $arr = array( + 'apple' => 100, + 'grapefruit' => 400, + 'carrot' => 50, + ); + + $this->assertEquals(false, array_key_exists('orange', $arr)); + \PFF\Arr::ensureKeyExists($arr, 'orange'); + $this->assertEquals(true, array_key_exists('orange', $arr)); + } + + public function testPushToKey() + { + $arr = array('carrot' => array('orange')); + + \PFF\Arr::pushToKey($arr, 'apple', 'green'); + \PFF\Arr::pushToKey($arr, 'carrot', 'long'); + \PFF\Arr::pushToKey($arr, 'apple', 'sweet'); + + $this->assertEquals(array('green', 'sweet'), $arr['apple']); + $this->assertEquals(array('orange', 'long'), $arr['carrot']); + } + + public function testPop() + { + $arr = array( + 'apple' => 100, + 'grapefruit' => 400, + 'carrot' => 50, + ); + + $apple = \PFF\Arr::pop($arr, 'apple'); + $this->assertEquals(false, array_key_exists('apple', $arr)); + $this->assertEquals(100, $apple); + + $apple = \PFF\Arr::pop($arr, 'apple'); + $this->assertEquals(null, $apple); + + $apple = \PFF\Arr::pop($arr, 'apple', 'what?'); + $this->assertEquals('what?', $apple); + } + + public function testFlatten() + { + $arr = array('a', 'b', array(array(array('x'), 'y', 'z')), array(array('p'))); + $flat = \PFF\Arr::flatten($arr); + $this->assertEquals(array('a', 'b', 'x', 'y', 'z', 'p'), $flat); + } +} + diff --git a/t/ArrayTest.php b/t/ArrayTest.php deleted file mode 100644 index de15380..0000000 --- a/t/ArrayTest.php +++ /dev/null @@ -1,62 +0,0 @@ - 100, - 'grapefruit' => 400, - 'carrot' => 50, - ); - - $this->assertEquals(400, ArrayTools::get($arr, 'grapefruit')); - $this->assertEquals(null, ArrayTools::get($arr, 'orange')); - $this->assertEquals('no-oranges', ArrayTools::get($arr, 'orange', 'no-oranges')); - } - - public function testEnsureKeyExists() - { - $arr = array( - 'apple' => 100, - 'grapefruit' => 400, - 'carrot' => 50, - ); - - $this->assertEquals(false, array_key_exists('orange', $arr)); - ArrayTools::ensureKeyExists($arr, 'orange'); - $this->assertEquals(true, array_key_exists('orange', $arr)); - } - - public function testPushToKey() - { - $arr = array('carrot' => array('orange')); - - ArrayTools::pushToKey($arr, 'apple', 'green'); - ArrayTools::pushToKey($arr, 'carrot', 'long'); - ArrayTools::pushToKey($arr, 'apple', 'sweet'); - - $this->assertEquals(array('green', 'sweet'), $arr['apple']); - $this->assertEquals(array('orange', 'long'), $arr['carrot']); - } - - public function testPop() - { - $arr = array( - 'apple' => 100, - 'grapefruit' => 400, - 'carrot' => 50, - ); - - $apple = ArrayTools::pop($arr, 'apple'); - $this->assertEquals(false, array_key_exists('apple', $arr)); - $this->assertEquals(100, $apple); - - $apple = ArrayTools::pop($arr, 'apple'); - $this->assertEquals(null, $apple); - - $apple = ArrayTools::pop($arr, 'apple', 'what?'); - $this->assertEquals('what?', $apple); - } -} - diff --git a/t/CollectionTest.php b/t/CollectionTest.php index dcf952f..4005b03 100644 --- a/t/CollectionTest.php +++ b/t/CollectionTest.php @@ -1,6 +1,6 @@ assertEquals(3, count($columns)); $this->assertEquals(2, count($columns[0])); $this->assertEquals(1, count($columns[1])); @@ -25,7 +25,7 @@ class CollectionsTest extends PHPUnit_Framework_TestCase 'carrot', 'tomato', ); - $chunks = Collections::chunks($arr, 3); + $chunks = \PFF\Collection::chunks($arr, 3); $this->assertEquals(2, count($chunks)); $this->assertEquals(3, count($chunks[0])); $this->assertEquals(1, count($chunks[1])); @@ -39,7 +39,7 @@ class CollectionsTest extends PHPUnit_Framework_TestCase 'carrot', 'tomato', ); - $chunks = Collections::chunks($arr, 3, true); + $chunks = \PFF\Collection::chunks($arr, 3, true); $this->assertEquals(2, count($chunks)); $this->assertEquals(3, count($chunks[0])); $this->assertEquals(3, count($chunks[1])); diff --git a/t/FormatTest.php b/t/FormatTest.php index 2f6245f..a600516 100644 --- a/t/FormatTest.php +++ b/t/FormatTest.php @@ -1,36 +1,36 @@ assertEquals(DateFormat::DATE_DEFAULT, DateFormat::DATE_BIG_ENDIAN | DateFormat::DATE_HYPHENS); - $this->assertEquals(DateFormat::DATE_US, DateFormat::DATE_MIDDLE_ENDIAN | DateFormat::DATE_SLASHES); - $this->assertEquals(DateFormat::DATE_EUROPEAN, DateFormat::DATE_LITTLE_ENDIAN | DateFormat::DATE_DOTS); + $this->assertEquals(\PFF\DateFormat::DATE_DEFAULT, \PFF\DateFormat::DATE_BIG_ENDIAN | \PFF\DateFormat::DATE_HYPHENS); + $this->assertEquals(\PFF\DateFormat::DATE_US, \PFF\DateFormat::DATE_MIDDLE_ENDIAN | \PFF\DateFormat::DATE_SLASHES); + $this->assertEquals(\PFF\DateFormat::DATE_EUROPEAN, \PFF\DateFormat::DATE_LITTLE_ENDIAN | \PFF\DateFormat::DATE_DOTS); } public function testDateFlagsFromString() { - $this->assertEquals(DateFormat::DATE_DEFAULT, DateFormat::dateFlagsFromString('Default')); - $this->assertEquals(DateFormat::DATE_DEFAULT, DateFormat::dateFlagsFromString('Big-endian hyphEns')); - $this->assertEquals(DateFormat::DATE_EUROPEAN, DateFormat::dateFlagsFromString('European')); - $this->assertEquals(DateFormat::DATE_EUROPEAN, DateFormat::dateFlagsFromString('little-endian dots')); - $this->assertEquals(DateFormat::DATE_US, DateFormat::dateFlagsFromString('US')); - $this->assertEquals(DateFormat::DATE_US, DateFormat::dateFlagsFromString('Middle-Endian Slashes')); - $this->assertEquals(DateFormat::DATE_BIG_ENDIAN | DateFormat::DATE_SPACES, - DateFormat::dateFlagsFromString('big-endian spaces')); + $this->assertEquals(\PFF\DateFormat::DATE_DEFAULT, \PFF\DateFormat::dateFlagsFromString('Default')); + $this->assertEquals(\PFF\DateFormat::DATE_DEFAULT, \PFF\DateFormat::dateFlagsFromString('Big-endian hyphEns')); + $this->assertEquals(\PFF\DateFormat::DATE_EUROPEAN, \PFF\DateFormat::dateFlagsFromString('European')); + $this->assertEquals(\PFF\DateFormat::DATE_EUROPEAN, \PFF\DateFormat::dateFlagsFromString('little-endian dots')); + $this->assertEquals(\PFF\DateFormat::DATE_US, \PFF\DateFormat::dateFlagsFromString('US')); + $this->assertEquals(\PFF\DateFormat::DATE_US, \PFF\DateFormat::dateFlagsFromString('Middle-Endian Slashes')); + $this->assertEquals(\PFF\DateFormat::DATE_BIG_ENDIAN | \PFF\DateFormat::DATE_SPACES, + \PFF\DateFormat::dateFlagsFromString('big-endian spaces')); } public function testDatetimeFlagsFromString() { - $this->assertEquals(DateFormat::DATETIME_DEFAULT, DateFormat::datetimeFlagsFromString('Default')); - $this->assertEquals(DateFormat::DATETIME_DEFAULT, DateFormat::datetimeFlagsFromString('Big-endian hyphEns 24')); - $this->assertEquals(DateFormat::DATETIME_EUROPEAN, DateFormat::datetimeFlagsFromString('European')); - $this->assertEquals(DateFormat::DATETIME_EUROPEAN, DateFormat::datetimeFlagsFromString('little-endian dots 24')); - $this->assertEquals(DateFormat::DATETIME_US, DateFormat::datetimeFlagsFromString('US')); - $this->assertEquals(DateFormat::DATETIME_US, DateFormat::datetimeFlagsFromString('Middle-Endian Slashes 12-upper')); - $this->assertEquals(DateFormat::DATE_BIG_ENDIAN | DateFormat::DATE_SPACES | DateFormat::TIME_12HOURS_LOWER | DateFormat::TIME_SECONDS, - DateFormat::datetimeFlagsFromString("big-endian spaces \n 12-lower Seconds")); + $this->assertEquals(\PFF\DateFormat::DATETIME_DEFAULT, \PFF\DateFormat::datetimeFlagsFromString('Default')); + $this->assertEquals(\PFF\DateFormat::DATETIME_DEFAULT, \PFF\DateFormat::datetimeFlagsFromString('Big-endian hyphEns 24')); + $this->assertEquals(\PFF\DateFormat::DATETIME_EUROPEAN, \PFF\DateFormat::datetimeFlagsFromString('European')); + $this->assertEquals(\PFF\DateFormat::DATETIME_EUROPEAN, \PFF\DateFormat::datetimeFlagsFromString('little-endian dots 24')); + $this->assertEquals(\PFF\DateFormat::DATETIME_US, \PFF\DateFormat::datetimeFlagsFromString('US')); + $this->assertEquals(\PFF\DateFormat::DATETIME_US, \PFF\DateFormat::datetimeFlagsFromString('Middle-Endian Slashes 12-upper')); + $this->assertEquals(\PFF\DateFormat::DATE_BIG_ENDIAN | \PFF\DateFormat::DATE_SPACES | \PFF\DateFormat::TIME_12HOURS_LOWER | \PFF\DateFormat::TIME_SECONDS, + \PFF\DateFormat::datetimeFlagsFromString("big-endian spaces \n 12-lower Seconds")); } } diff --git a/t/HtmlBuilderTest.php b/t/HtmlBuilderTest.php index 3892193..bdef45d 100644 --- a/t/HtmlBuilderTest.php +++ b/t/HtmlBuilderTest.php @@ -4,9 +4,9 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase { public function testTag() { - $tag = HtmlBuilder\Tag::div(array('class' => 'container')) + $tag = \PFF\HtmlBuilder\Tag::div(array('class' => 'container')) ->append('Hello, "World"') - ->append(HtmlBuilder\Tag::a()->append('click ')->raw('me')); + ->append(\PFF\HtmlBuilder\Tag::a()->append('click ')->raw('me')); $this->assertEquals('
Hello, "World"click me
', (string)$tag); } diff --git a/t/MixinsTest.php b/t/MixinsTest.php index d634305..fb0970c 100644 --- a/t/MixinsTest.php +++ b/t/MixinsTest.php @@ -1,6 +1,6 @@ assertTrue(\PFF\Str::startsWith('abcdef', 'abc')); + $this->assertFalse(\PFF\Str::startsWith(' abcdef', 'abc')); + $this->assertFalse(\PFF\Str::startsWith('abc', 'abcdef')); + $this->assertTrue(\PFF\Str::startsWith('abc', 'abc')); + $this->assertTrue(\PFF\Str::startsWith('abc', '')); + $this->assertFalse(\PFF\Str::startsWith('abc', ' ')); + } + + public function testEndsWith() + { + $this->assertTrue(\PFF\Str::endsWith('abcdef', 'def')); + $this->assertFalse(\PFF\Str::endsWith('abcdef ', 'def')); + $this->assertFalse(\PFF\Str::endsWith('abc', 'abcdef')); + $this->assertTrue(\PFF\Str::endsWith('abc', 'abc')); + $this->assertTrue(\PFF\Str::endsWith('abc', '')); + $this->assertFalse(\PFF\Str::endsWith('abc', ' ')); + } +} + diff --git a/t/StringTest.php b/t/StringTest.php deleted file mode 100644 index e2c5eaf..0000000 --- a/t/StringTest.php +++ /dev/null @@ -1,25 +0,0 @@ -assertTrue(StringTools::startsWith('abcdef', 'abc')); - $this->assertFalse(StringTools::startsWith(' abcdef', 'abc')); - $this->assertFalse(StringTools::startsWith('abc', 'abcdef')); - $this->assertTrue(StringTools::startsWith('abc', 'abc')); - $this->assertTrue(StringTools::startsWith('abc', '')); - $this->assertFalse(StringTools::startsWith('abc', ' ')); - } - - public function testEndsWith() - { - $this->assertTrue(StringTools::endsWith('abcdef', 'def')); - $this->assertFalse(StringTools::endsWith('abcdef ', 'def')); - $this->assertFalse(StringTools::endsWith('abc', 'abcdef')); - $this->assertTrue(StringTools::endsWith('abc', 'abc')); - $this->assertTrue(StringTools::endsWith('abc', '')); - $this->assertFalse(StringTools::endsWith('abc', ' ')); - } -} -