Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

First Scalar primitives wrap up: Strings #1

Open
wants to merge 6 commits into from
This page is out of date. Refresh to see the latest.
View
2  .atoum.bootstrap.php
@@ -0,0 +1,2 @@
+<?php
+require_once __DIR__.'/vendor/autoload.php';
View
20 .atoum.php
@@ -0,0 +1,20 @@
+<?php
+use \mageekguy\atoum;
+
+$runner->addTestsFromDirectory(__DIR__.'/Tests/Units');
+
+$cloverWriter = new atoum\writers\file(__DIR__.'/data/coverage.clover');
+$cloverReport = new atoum\reports\asynchronous\clover();
+$cloverReport->addWriter($cloverWriter);
+$runner->addReport($cloverReport);
+
+$script
+ ->addDefaultReport()
+ ->addField(new atoum\report\fields\runner\result\logo())
+ ->addField(new atoum\report\fields\runner\coverage\html(
+ 'Code Coverage',
+ __DIR__.'/web/code-coverage'
+ ));
+
+$script->noCodeCoverageForNamespaces('mageekguy');
+$script->bootstrapFile(__DIR__ . DIRECTORY_SEPARATOR . '.atoum.bootstrap.php');
View
6 .gitignore
@@ -0,0 +1,6 @@
+composer.phar
+composer.lock
+vendor/*
+web/*
+data/*
+.idea/*
View
12 .scrutinizer.yml
@@ -0,0 +1,12 @@
+tools:
+ external_code_coverage:
+ timeout: 600
+ php_code_sniffer: true
+ php_cpd: true
+ php_loc: true
+ php_mess_detector: true
+ php_pdepend: true
+ php_analyzer: true
+ sensiolabs_security_checker: true
+filter:
+ paths: [src/*]
View
25 .travis.yml
@@ -0,0 +1,25 @@
+language: php
+
+php:
+ - 5.4
+ - 5.5
+ - 5.6
+
+before_script:
+ - wget http://getcomposer.org/composer.phar
+ - mkdir -p web/code-coverage data
+ - php composer.phar install --dev
+ - wget https://scrutinizer-ci.com/ocular.phar
+
+script:
+ - bin/atoum
+
+after_script:
+ - php ocular.phar code-coverage:upload --format=php-clover data/coverage.clover
+
+notifications:
+ email:
+ recipients:
+ - ci@verylastroom.com
+ on_success: change
+ on_failure: change
View
25 Tests/Units/MultibyteString.php
@@ -0,0 +1,25 @@
+<?php
+/*
+ * This file is part of the valueobjects-primitive package.
+ *
+ * (c) Sébastien HOUZÉ <sebastien@verylastroom.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PhpLabs\ValueObjects\Primitive\Tests\Units;
+
+use mageekguy\atoum;
+
+class MultibyteString extends atoum\test
+{
+ public function testLength()
+ {
+ $this
+ ->given($string = new \PhpLabs\ValueObjects\Primitive\MultibyteString('東京', 'UTF-8'))
+ ->variable($string->length())
+ ->isEqualTo(2);
+ }
+}
+
View
65 Tests/Units/String.php
@@ -0,0 +1,65 @@
+<?php
+/*
+ * This file is part of the valueobjects-primitive package.
+ *
+ * (c) Sébastien HOUZÉ <sebastien@verylastroom.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PhpLabs\ValueObjects\Primitive\Tests\Units;
+
+use mageekguy\atoum;
+
+class String extends atoum\test
+{
+ public function testToString()
+ {
+ $this
+ ->given($string = new \PhpLabs\ValueObjects\Primitive\String('test'))
+ ->variable($string->trim())
+ ->isEqualTo('test');
+ }
+
+ public function testObjectString()
+ {
+ $this
+ ->given($string = new \PhpLabs\ValueObjects\Primitive\String(new Foo()))
+ ->variable($string->trim())
+ ->isEqualTo('test');
+ }
+
+ public function testEmpty()
+ {
+ $this
+ ->given($string = new \PhpLabs\ValueObjects\Primitive\String(''))
+ ->variable($string->isEmpty())
+ ->isEqualTo(true);
+ }
+
+ public function testTrim()
+ {
+ $this
+ ->given($string = new \PhpLabs\ValueObjects\Primitive\String(' test '))
+ ->variable($string->trim())
+ ->isEqualTo('test');
+ }
+
+ public function testLength()
+ {
+ $this
+ ->given($string = new \PhpLabs\ValueObjects\Primitive\String('test'))
+ ->variable($string->length())
+ ->isEqualTo(4);
+ }
+}
+
+class Foo
+{
+ function __toString()
+ {
+ return 'test';
+ }
+}
+
View
1  bin/atoum
View
34 composer.json
@@ -0,0 +1,34 @@
+{
+ "name": "php-labs/valueobjects-primitive",
+ "description": "Useful yet very simple php objects about things like String, Number, ...",
+ "keywords": ["value objects", "basic object", "address", "coordinates", "street", "country"],
+ "authors": [
+ {
+ "name": "php-labs guys",
+ "homepage": "https://github.com/php-labs/valueobjects-primitive/contributors"
+ }
+ ],
+ "type": "library",
+ "license": "MIT",
+ "autoload": {
+ "psr-4": {
+ "PhpLabs\\ValueObjects\\Primitive\\": "src"
+ }
+ },
+ "require": {
+ "php": ">=5.4.0",
+ "php-labs/valueobjects-utils": "1.0.*@dev"
+ },
+ "require-dev": {
+ "atoum/atoum": "*@dev"
+ },
+ "config": {
+ "bin-dir": "bin"
+ },
+ "minimum-stability": "dev",
+ "extra": {
+ "branch-alias": {
+ "dev-master": "1.0-dev"
+ }
+ }
+}
View
39 src/MultibyteString.php
@@ -0,0 +1,39 @@
+<?php
+/**
+ * User: romain
+ * Date: 28/02/14
+ * Time: 15:14
+ */
+
+namespace PhpLabs\ValueObjects\Primitive;
+
+class MultibyteString extends String
+{
+ /**
+ * @var null|string
+ */
+ protected $encoding;
+
+ /**
+ * @param mixed $value
+ * @param null|string $encoding
+ */
+ public function __construct($value, $encoding = null)
+ {
+ parent::__construct($value);
+
+ if(is_null($encoding)) {
+ $this->encoding = mb_internal_encoding();
+ } else {
+ $this->encoding = $encoding;
+ }
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function length()
+ {
+ return mb_strlen($this->value, $this->encoding);
+ }
+}
View
23 src/Raw/String.php
@@ -0,0 +1,23 @@
+<?php
+/*
+ * This file is part of the valueobjects-primitive package.
+ *
+ * (c) Sébastien HOUZÉ <sebastien@verylastroom.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PhpLabs\ValueObjects\Primitive\Raw;
+
+/**
+ * @author Sébastien HOUZÉ <sebastien@verylastroom.com>
+ */
+class String extends \PhpLabs\ValueObjects\Primitive\String
+{
+ protected function ensurePrerequisites($value)
+ {
+ // we assume for performance considerations that passed value is
+ // a php native string type by contract
+ }
+}
View
80 src/Scalar.php
@@ -0,0 +1,80 @@
+<?php
+/*
+ * This file is part of the valueobjects-primitive package.
+ *
+ * (c) Sébastien HOUZÉ <sebastien@verylastroom.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PhpLabs\ValueObjects\Primitive;
+
+/**
+ * @author Sébastien HOUZÉ <sebastien@verylastroom.com>
+ */
+abstract class Scalar
+{
+ /**
+ * @param mixed
+ */
+ protected $value;
+
+ /**
+ * @param mixed $value
+ */
+ public function __construct($value)
+ {
+ static::ensurePrerequisites($value);
+ static::assignValue($value);
+ }
+
+ /**
+ * @param mixed $value
+ * @throws \InvalidArgumentException
+ */
+ protected function ensurePrerequisites($value)
+ {
+ if (!is_scalar($value)) {
+ throw new \InvalidArgumentException('Passed value must be scalar type');
+ }
+ }
+
+ /**
+ * @param mixed $value
+ */
+ abstract protected function assignValue($value);
+
+ /**
+ * @return string current String object representation
+ */
+ public function __toString()
+ {
+ return strval($this->value);
+ }
+
+ /**
+ * @return mixed
+ */
+ public function getValue()
+ {
+ return $this->value;
+ }
+
+ /**
+ * Test equality between two Scalar object
+ *
+ * @param Scalar $a
+ * @param Scalar $b
+ * @param bool $strict
+ * @return bool
+ */
+ public function equal(Scalar $a, Scalar $b, $strict = false)
+ {
+ if ($strict) {
+ return ($a->getValue() === $b->getValue());
+ } else {
+ return ($a->getValue() == $b->getValue());
+ }
+ }
+}
View
220 src/String.php
@@ -0,0 +1,220 @@
+<?php
+/*
+ * This file is part of the valueobjects-primitive package.
+ *
+ * (c) Sébastien HOUZÉ <sebastien@verylastroom.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace PhpLabs\ValueObjects\Primitive;
+
+use PhpLabs\ValueObjects\Primitive\Raw;
+use PhpLabs\ValueObjects\Primitive\Scalar;
+
+/**
+ * @author Sébastien HOUZÉ <sebastien@verylastroom.com>
+ */
+class String extends Scalar implements \ArrayAccess
+{
+ protected function ensurePrerequisites($value)
+ {
+ if (is_object($value)) {
+ if (!method_exists($value, '__toString')) {
+ throw new \InvalidArgumentException(
+ sprintf('Passed object class "%s" must implement __toString method', get_class($value))
+ );
+ }
+
+ $value = $value->__toString();
+ }
+
+ Scalar::ensurePrerequisites($value);
+ }
+
+ protected function assignValue($value)
+ {
+ $this->value = (string) $value;
+ }
+
+ /**
+ * @return boolean
+ */
+ public function isEmpty()
+ {
+ return 0 == strlen($this->value);
+ }
+
+ /**
+ * Quote string with slashes
+ *
+ * @param null|string $charList
+ * @return static
+ */
+ public function addSlashes($charList = null)
+ {
+ if (is_null($charList)) {
+ $value = addslashes($this->value);
+ } else {
+ $value = addcslashes($this->value, $charList);
+ }
+
+ return new static($value);
+ }
+
+ /**
+ * Split a string into smaller chunks
+ *
+ * @param int $chunkLen
+ * @param string $end
+ * @return static
+ */
+ public function chunkSplit($chunkLen = null , $end = null)
+ {
+ if (is_null($end)) {
+ if (is_null($chunkLen)) {
+ $value = chunk_split($this->value);
+ } else {
+ $value = chunk_split($this->value, $chunkLen);
+ }
+ } else {
+ $value = chunk_split($this->value, $chunkLen, $end);
+ }
+
+ return new static($value);
+ }
+
+ /**
+ * Calculates the crc32 polynomial of a string
+ *
+ * @param bool $unsigned
+ * @return int
+ */
+ public function crc32($unsigned = true)
+ {
+ $crc32 = crc32($this->value);
+ if ($unsigned) {
+ return intval(sprintf("%u", $crc32));
+ } else {
+ return $crc32;
+ }
+ }
+
+ /**
+ * One-way string hashing
+ *
+ * @param string $salt
+ * @return string
+ */
+ public function crypt($salt = null)
+ {
+ if (is_null($salt)) {
+ $value = crypt($this->value);
+ } else {
+ $value = crypt($this->value, $salt);
+ }
+
+ return new static($value);
+ }
+
+ /**
+ * Split a string by string
+ *
+ * @param string $delimiter
+ * @param null $limit
+ * @return array
+ */
+ public function explode($delimiter, $limit = null)
+ {
+ if (is_null($limit)) {
+ return explode($this->value, $delimiter);
+ } else {
+ return explode($this->value, $delimiter, $limit);
+ }
+ }
+
+ public function subString($start, $length = null)
+ {
+ if (is_null($length)) {
+ $value = substr($this->value, $start);
+ } else {
+ $value = substr($this->value, $start, $length);
+ }
+
+ return new static($value);
+ }
+
+ /**
+ * Strip whitespace (or other characters) from the beginning and end of a string
+ * @return static
+ */
+ public function trim()
+ {
+ return new static(trim($this->value));
+ }
+
+ /**
+ * Get the String length
+ * @return int
+ */
+ public function length()
+ {
+ return strlen($this->value);
+ }
+
+ /**
+ * ArrayAccess
+ */
+
+ /**
+ * Whether a offset exists
+ * @link http://php.net/manual/en/arrayaccess.offsetexists.php
+ *
+ * @param mixed $offset
+ * @return boolean true on success or false on failure.
+ */
+ public function offsetExists($offset)
+ {
+ return ($offset >= 0 && $offset < $this->length());
+ }
+
+ /**
+ * Offset to retrieve
+ * @link http://php.net/manual/en/arrayaccess.offsetget.php
+ *
+ * @param mixed $offset
+ * @return mixed Can return all value types.
+ */
+ public function offsetGet($offset)
+ {
+ return substr($this->value, $offset);
+ }
+
+ /**
+ * Offset to set
+ * @link http://php.net/manual/en/arrayaccess.offsetset.php
+ *
+ * @param mixed $offset
+ * @param mixed $value
+ * @throws \BadMethodCallException
+ * @return void
+ */
+ public function offsetSet($offset, $value)
+ {
+ throw new \BadMethodCallException('Value is Immutable');
+ }
+
+ /**
+ * Offset to unset
+ * @link http://php.net/manual/en/arrayaccess.offsetunset.php
+ *
+ * @param mixed $offset
+ * @throws \BadMethodCallException
+ * @return void
+ */
+ public function offsetUnset($offset)
+ {
+ throw new \BadMethodCallException('Value is Immutable');
+ }
+}
Something went wrong with that request. Please try again.