Skip to content
Permalink
Browse files

Added ToStringBuilder.

  • Loading branch information...
piotrooo committed Mar 11, 2019
1 parent 0e0f857 commit 7cdab564e39748cfc8df9dc9c5f67aa5af5b2c6b
@@ -14,6 +14,7 @@ Enhancements:
* [DI] Implemented injection through factory class.
* [Utilities] Added equalsIgnoreCase to Functions and FluentFunctions (issue #263).
* [Utilities] Fixed Clock to support DST changes when adding hours, minutes or seconds.
* [Utilities] Added ToStringBuilder in the apache-commons style.

Release 1.6.1
--------
@@ -3,6 +3,7 @@
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities;
/**
@@ -11,6 +12,8 @@
*/
class Strings
{
const EMPTY = '';
/**
* Changes underscored string to the camel case.
*
@@ -0,0 +1,11 @@
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities\ToString;
class DefaultToStringStyle extends ToStringStyle
{
}
@@ -0,0 +1,18 @@
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities\ToString;
class MultiLineToStringStyle extends ToStringStyle
{
public function __construct()
{
$this->setContentStart($this->getContentStart());
$this->setContentEnd(PHP_EOL . $this->getContentEnd());
$this->setFieldSeparator(PHP_EOL . " ");
$this->setFieldSeparatorAtStart(true);
}
}
@@ -0,0 +1,15 @@
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities\ToString;
class NoClassNameToStringStyle extends ToStringStyle
{
public function __construct()
{
$this->setUseClassName(false);
}
}
@@ -0,0 +1,15 @@
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities\ToString;
class NoFieldNameToStringStyle extends ToStringStyle
{
public function __construct()
{
$this->setUseFieldNames(false);
}
}
@@ -0,0 +1,15 @@
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities\ToString;
class ShortPrefixToStringStyle extends ToStringStyle
{
public function __construct()
{
$this->setUseShortClassName(true);
}
}
@@ -0,0 +1,20 @@
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities\ToString;
use Ouzo\Utilities\Strings;
class SimpleToStringStyle extends ToStringStyle
{
public function __construct()
{
$this->setUseClassName(false);
$this->setUseFieldNames(false);
$this->setContentStart(Strings::EMPTY);
$this->setContentEnd(Strings::EMPTY);
}
}
@@ -0,0 +1,53 @@
<?php
/*
* Copyright (c) Ouzo contributors, http://ouzoframework.org
* This file is made available under the MIT License (view the LICENSE file for more information).
*/
namespace Ouzo\Utilities\ToString;
use ReflectionClass;
class ToStringBuilder
{
/** @var object */
private $object;
/** @var ToStringStyle */
private $style;
/** @var string */
private $buffer = '';
/**
* @param object $object
* @param ToStringStyle|null $object
*/
public function __construct($object, ToStringStyle $style = null)
{
$this->object = $object;
$this->style = $style ?: ToStringStyle::defaultStyle();
$this->style->appendStart($this->buffer, new ReflectionClass($object));
}
/**
* @param string $string
* @param mixed $value
* @return $this
*/
public function append($string, $value)
{
$this->style->append($this->buffer, $string, $value);
return $this;
}
/**
* @return string
*/
public function toString()
{
$this->style->appendEnd($this->buffer);
return $this->buffer;
}
}
Oops, something went wrong.

0 comments on commit 7cdab56

Please sign in to comment.
You can’t perform that action at this time.