Is there a way to create a php class (or function) that "simplifies" this
ucfirst(str_replace('_',' ',html_entity_decode(trim($variable), ENT_QUOTES))));
The $variable could "come" from anywhere e.g a global from another function or just a "standard" variable
If you want to have this in a class (as the question title implies), then you should create Filter classes. This is a common thing to do. However, compared to the simple function nesting, it will be much more code to do it properly. The advantage is, you can easily extend and combine filters to virtually any filtering needs you have.
I have whipped up something for you quickly.
interface IFilter {
/**
* #param Mixed $value The value to be filtered
* #return Mixed The filtered value
*/
public function filter($value);
}
All filters must implement the IFilter interface. This is to make sure that whenever you are using a Filter, it has a filter() method that accepts a single $value argument. We cannot enforce return values, but the doc block indicated we expect it to return the filtered value. Two very simple filters would look like this:
class ucFirstFilter implements IFilter
{
public function filter($value) {
return ucfirst($value);
}
}
class TrimFilter implements IFilter
{
public function filter($value) {
return trim($value);
}
}
This is nothing but an object wrapper around two of PHP's native functions. You use it like this:
$trimFilter = new TrimFilter;
echo trimFilter->filter(' trim me ');
// returns 'trim me'
The other two filters are somewhat more complex, because they can be passed more than one argument:
class SeparatorToSeparatorFilter implements IFilter
{
protected $_separator;
protected $_replacement;
public function __construct($separator = '_', $replacement = ' ')
{
$this->_separator = $separator;
$this->_replacement = $replacement;
}
public function filter($value) {
return str_replace($this->_separator, $this->_replacement, $value);
}
}
class HtmlEntityDecodeFilter implements IFilter
{
protected $_quoteStyle;
protected $_charset;
public function __construct($quoteStyle=ENT_COMPAT, $charset='ISO-8859-1')
{
$this->_quoteStyle = $quoteStyle;
$this->_charset = $charset;
}
public function filter($value) {
return html_entity_decode($value, $this->_quoteStyle, $this->_charset);
}
}
As you can see, the configuration of the additional arguments is done through the constructor. I have used some default values, so you only have to supply them when you need to deviate from those. In the case of the second filter, I have used the native function's default settings. This is how you use them:
$trimFilter = new TrimFilter;
$separatorFilter = new SeparatorToSeparatorFilter('-');
echo $separatorFilter->filter($trimFilter->filter(' trim-me '));
// returns 'trim me';
Now you might be tempted to add multiple filterings into a single Filter class. Dont. Each Filter should do exactly one thing only. There is a better way to combine filters. All you need is a Filter that aggregates multiple other filters aka a FilterChain:
class FilterChain implements IFilter
{
protected $_filters;
public function __construct()
{
$this->_filters = new SplObjectStorage;
}
public function chain(IFilter $filter)
{
$this->_filters->attach($filter);
return $this;
}
public function remove(IFilter $filter)
{
$this->_filters->detach($filter);
return $this;
}
public function filter($value) {
foreach($this->_filters as $filter) {
$value = $filter->filter($value);
}
return $value;
}
}
The FilterChain accepts any object that implements IFilter and if you call it's filter() method, it will iterate over all chained Filters in the order you chain()ed them and return the passed in $value:
$filterChain = new FilterChain;
$filterChain->chain(new ucFirstFilter)
->chain(new SeparatorToSeparatorFilter)
->chain(new HtmlEntityDecodeFilter(ENT_QUOTES, 'UTF-8'))
->chain(new TrimFilter);
echo $filterChain->filter(' i am a "string_to_be_filtered" ');
// outputs 'i am a "string to be filtered"'
Because the FilterChain also implements IFilter itself, you can also add it to other FilterChains. This is a Composite Pattern. The filter above could be written as
$chain1 = new FilterChain;
$chain1->chain(new ucFirstFilter)
->chain(new SeparatorToSeparatorFilter);
$chain2 = new FilterChain;
$chain2->chain($chain1);
$chain2->chain(new HtmlEntityDecodeFilter(ENT_QUOTES, 'UTF-8'))
->chain(new TrimFilter);
As you can see, it is much more code, but it is also very extensible. The main advantage over having a single function that wraps all native functions into one function is you can combine anything any way you want. If you decided you need another function that does not utilize the trim() function, you'd have to write a completely new function and you'll inadvertently end up with a lot of functions and redundant code for any possible combination of filters. With a FilterChain you simply add the Filters and FilterChains together as needed. And since a FilterChain is an object, you can pass it around freely.
Fortunately, Filter libraries like this already exist, for instance Zend_Filter offers a number of premade filters and can used standalone (e.g. without having to migrate your app to ZF).
If you are using it more than once, then I would definitely put it into a function. That way you won't be repeating all the code.
function functionName($input){
return ucfirst(str_replace('_',' ',html_entity_decode(trim($input), ENT_QUOTES)));
}
echo functionName($variable);
Related
I have a class that takes in lines of text and uses about 15 different "criteria" (individual preg_match statements) to check if the line should qualify to be saved in an array.
How does one best handle such a situation with clean, maintainable code?
Initially I had a crazy long if statement which had all the criteria, for example:
if (
preg_match($criteria1,$line) &&
preg_match($criteria2, $line) &&
...
...
...
preg_match($criteriaN,$line)
) {
//do something, e.g. save the line to an array.
}
I've since put each preg_match statement into a different function within a separate class and call each function in a row, checking if it's true... But now I have 15 separate functions that are only subtly different from each other and it still doesn't feel like I'm writing good code. How is it best to handle this situation?
Well, if you want to keep things object oriented, you could use a class like this...
class Validator{
/* Any criteria needing to be met must exist in this array */
public static $criterias = array($criteria1,
$criteria2,
... ,
$criteriaN);
public static function validate($line){
/* Make sure this line meets each criteria */
foreach(Validator::$criterias as $criteria){
if(!preg_match($criteria, $line))
return false;
}
return true;
}
It makes sense to have the methods and properties static because they are unique to no instance. Then you can simply check to see if a line meets the criteria by calling
Validator::validate($line)
Simple example:
$rgCriterias = [$sCriteria0, $sCriteria1, $sCriteria2];
if(!count(array_filter($rgCriterias, function($sCriteria) use ($line)
{
return !preg_match($sCriteria, $line);
})))
{
//do the stuff
}
Might be an overkill, but you can use the Specification Pattern. a very simplified example would be:
abstract class Specification
{
abstract public function isSatisfiedBy($obj);
}
class RegExSpecification extends Specification
{
private $_pattern;
public function __construct($pattern)
{
$this->_pattern = $pattern;
}
public function isSatisfiedBy($obj)
{
return preg_match($this->_pattern, $obj);
}
}
Usage:
class YourLinesProcessor
{
private $_specs;
public function setSpecs($specs)
{
$this->_specs = $specs;
}
public function check($line)
{
foreach ($this->_specs as $spec)
$satisfied = $spec->isSatisfiedBy($line);
if ($satisfied)
// do something
}
}
$specs = array();
$specs[] = new RegExSpecification("\some\pattern");
$specs[] = new RegExSpecification("\another\pattern");
$processor = new YourLinesProcessor();
$processor->setSpecs($specs);
$processor->check("line_data");
I have a Display object that handles HTML output of the script. It has a mode property, which defines many aspects of how the output is generated. There are several modes, and I will probably add more later. Each mode implies it's own parameters. For example, an author mode would imply an authorID. A search mode would imply a Search object as a parameter, which would contain query information. A comment mode would imply a postID and commentID parameters.
So, a mode has a name and some number of parameters, depending on the name.
I decided to create a Display_Mode object, which has two properties: name and parameters. But how do I assign parameters to names? So that if I have a comment mode, it would neccessarily mean that there are postID and commentID parameters?
My current solution is this:
class display_mode{
public $name;
public $params;
public function __construct($name)
{
$this->name = $name;
switch ($this->name){
case 'comment':
$this->params = array('postID', `commentID`);
break;
case 'author':
$this->params = array('authorID');
}
//etc..
}
}
This seems a bit messy. Is there a better way?
UPD: given answers led me to ask another question, concerning the whole design, which also provides context for this one.
You're better off abstracting the common functionality of the Display_Mode class into an abstract base class or interface and then defining each individual mode as a class that inherits from the base class (or implements the interface).
These specialised mode classes would then explicitly define the parameters they require in their constructors and/or methods, and would be responsible for producing the output that each "mode" requires.
It's difficult to give more specific advice than this without knowing exactly what your Display_Mode class is supposed to do, however.
The idea here is that you should avoid do-it-all classes in OOP, preferring small, self-contained classes each with a single purpose. By giving each mode its own class that can independently render its content, you're making it easy to change the way display modes work or add new ones. All you have to do is add a new class.
This is known as loose coupling.
The description is kinda confusing, but I would create an adapter for each possible "parameter" of Display. This adapter could provide single interface, independently of the the resource that you want to display.
$parameter = new UnknownPrameter;
$adapterFactory = new AdapterFactory;
$parameter = $adapterFactory->build( $parameter );
$display->present( $parameter );
Where AdapterFactory::build() create a wrapper for the specific type of parameter, that you supplied. The returned instance is a container for that parameter.
This approach would also prevent the computation from accumulating in the constructor, which would make the code harder to test/expand.
#Will Vousden already gave you the answer. This is a quick example of how to approach your problem.
abstract class DisplayMode {
protected $_params = array();
public function __construct(array $params) {
$this->_params = $params;
}
public function hasParameter($key) {
if (array_key_exists($key, $this->_params)) {
return true;
}
return false;
}
public function setParameters(array $params) {
$this->_params = $params;
return $this;
}
public function getParameters() {
return $this->_params;
}
}
class CommentMode extends DisplayMode {
public function getCommentId() {
if ($this->hasParameter('comment_id')) {
return $this->_params['comment_id'];
}
return null;
}
public function getPostId() {
if ($this->hasParameter('post_id')) {
return $this->_params['post_id'];
}
return null;
}
}
class AuthorMode extends DisplayMode {
public function getAuthorId() {
if ($this->hasParameter('author_id')) {
return $this->_params['author_id'];
}
return null;
}
}
$comment = new CommentMode(array('post_id' => 4, 'comment_id' => 2));
$author = new AuthorMode(array('author_id' => 3));
// example
print $comment->getCommentId() . ' - ' . $comment->getPostId() . ' - ' . $author->getAuthorId();
I'm working on a test in phpunit and I'm running into an issue. I have a public function on my class that I am trying to test. Depending on the parameters passed in to the method, a protected function also in my test class will be called one or two times. I currently have a test in place to check that the return data is correct, but I would also like to make sure the protected method is being called the correct number of times.
I know that a mock object will allow me to count the number of times a function is called, but it will also override the value returned by the protected function. I tried using a mock object with no "will" section, but it would just return null, not the actual value for the protected method.
ExampleClass
public function do_stuff($runTwice){
$results = do_cool_stuff();
if($runTwice){
$results = 2 * do_cool_stuff();
}
return $results;
}
protected function do_cool_stuff()
{
return 2;
}
In my test, I want to check whether do_cool_stuff() was called once or twice, but I still want the return values of both functions to be the same so I can test those as well in my unit test.
tl;dr
I want to count the number of times a protected method in my test object is called (like you can do with a mock object) but I still want all the methods in my test method to return their normal values (not like a mock object).
Alternatively, revert back to rolling your own testable stand-in. The following aint pretty, but you get the idea:
class ExampleClass {
public function do_stuff($runTwice) {
$results = $this->do_cool_stuff();
if ($runTwice) {
$results = 2 * $this->do_cool_stuff();
}
return $results;
}
protected function do_cool_stuff() {
return 2;
}
}
class TestableExampleClass extends ExampleClass {
/** Stores how many times the do_cool_stuff method is called */
protected $callCount;
function __construct() {
$this->callCount = 0;
}
function getCallCount() {
return $this->callCount;
}
/** Increment the call counter, and then use the base class's functionality */
protected function do_cool_stuff() {
$this->callCount++;
return parent::do_cool_stuff();
}
}
class ExampleClassTest extends PHPUnit_Framework_TestCase {
public function test_do_stuff() {
$example = new ExampleClass();
$this->assertEquals(2, $example->do_stuff(false));
$this->assertEquals(4, $example->do_stuff(true));
}
public function test_do_cool_stuff_is_called_correctly() {
// Try it out the first way
$firstExample = new TestableExampleClass();
$this->assertEquals(0, $firstExample->getCallCount());
$firstExample->do_stuff(false);
$this->assertEquals(1, $firstExample->getCallCount());
// Now test the other code path
$secondExample = new TestableExampleClass();
$this->assertEquals(0, $secondExample->getCallCount());
$secondExample->do_stuff(true);
$this->assertEquals(2, $secondExample->getCallCount());
}
}
I wonder though whether counting the number of times a protected method has been called is really a good test. It's coupling your test to the implementation pretty hard. Does it really matter whether it is called twice, or are you more interested in the interactions with other objects? Or maybe this is pointing towards do_cool_stuff needing a refactor into two separate methods:
class ExampleClass {
public function do_stuff($runTwice) {
if ($runTwice) {
return $this->do_cool_stuff_twice();
} else {
return $this->do_cool_stuff_once();
}
}
//...
}
Try setting a global variable prior to utilizing the class.
$IAmDeclaredOutsideOfTheFunction;
then use it to store the count and simply check it after your functions and classes have been called.
Well,
I have a problem (ok, no real problem, but I wanna try out something new) with creating objects. Actually I have some orders, which contains a list of orderitems.
These orderitems are used and so spreaded in the whole application, and I need a way to create them. The main problem is, I want to be able to create these objects in many different ways.
Actually I do this in the class constructor and check if the argument which is given.
(I'm using php, so there is no overloading support from the language as you surely know :))
A simple and quick Example
class foo {
protected $_data=null;
public function __contruct($bar){
if (is_array($bar)){
$this->_data=$bar;
}
else {
$dataFromDb=getDataFromDatabase
$this->_data=$dataFromDb;
}
}
}
Anyway, if I want to create my object by giving another type of parameter, lets say a xml-document encapsulated in a string I need to put all this stuff in my constructor.
If the process for creating an object is more complicated, I eventually need to create a seperate method for each type, I want to initiate. But this method is only called when this special type is created. (I think you got the problem :))
Another problem comes to mind, if I need more parameters in the constructor to create a concrete object, I have modify all my code, cause the contructor changed. (Ok, I can give him more and more parameters and work with default values, but that is not what I really want).
So my Question is, which pattern fits this problem to solve my creation of a concrete object. I thought about creating a factory for each way I want to create the concrete object. But I'm not sure if this is a common solution to solve such a problem.
IF its only the signature of the constructor changing i would do it like so (a la the Zend Framework universal constructor):
class foo {
// params
public function __construct($options = null)
{
if(null !== $options)
{
$this->setOptions($options);
}
}
public function setOptions(array $options){
foreach ($options as $name => $value){
$method = 'set' . $name;
if(method_exists($this, $method)
{
$this->$method($value);
}
}
return $this;
}
}
And this essntially means all your constructor parameters are array elements with named keys, and anything you want used in this array during initialization you create a setter for and then its automatically called. The down side is the lack of effective hinting in IDEs.
On the otherhand if you want to have specific constructors then i might go with a factory but still use much the same approach:
class foo {
public static function create($class, $options)
{
if(class_exists($class))
{
$obj = new $class($options);
}
}
}
Of course you could alternatively use PHP's reflection to determine how to call the constructor instead of just injecting an arbitrary array argument.
you could simply make it a factory with optional params :)
class Example_Factory
{
public static function factory($mandatoryParam, $optionalParam = null)
{
$instance = new self;
$instance->setMandatory($mandatoryParam);
if ($optionalParam !== null) {
$instance->setOptional($optionalParam);
}
return $instance;
}
public function setMandatory($in)
{
// do something....
}
public function setOptional($in)
{
// do some more...
}
}
Im thinking of making a custom datatypes / prototypes for a project im working on but im wondering if its such a good idea?
For example
class String
{
var $Value;
private $escaped = false;
function __construct($String)
{
$this->Value = $String;
}
function escape()
{
if($escaped === false)
{
$this->Value = Registry::get('Database')->escape($this->Value);
}
return $this;
}
function trim()
{
$this->Value = trim($this->Value);
return $this;
}
function __toString()
{
return $this->__toString();
}
}
$myValue = new String('Hello World')->trim()->escape();
//$myValue is now prepared for DB insert
There will be prototypes for Array, Object, String, Resource etc..
with arrays there will implement Iterator and such
Some benefits i have in mind is specific data types to objects for example
interface Insert
{
public function Insert(String $Value); //Array / Object / Resource
}
The custom prototypes would be useful for all strings.
But do you think that the amount of resource usage will out way the benefits ?
updated for POC
$String = new String('ValueText');
sprintf('Test %s',$String); //Works
trim($String); //Works
base64_encode($String); //Works
Also for arrays the SPL Library would be perfect.
class Array implements ArrayAccess, Iterator, Countable
{
public function __construct(){}
public function offsetSet($offset,$value){}
public function offsetExists($offset){}
public function offsetUnset($offset){}
public function offsetGet($offset){}
public function rewind(){}
public function current(){}
public function key(){}
public function next(){}
public function valid(){}
public function count(){}
}
Another idea would be the extendible entities
class DatabaseVariable extends String
{
function __construct($string)
{
parent::__constrcut($string);
}
public function escape()
{
//Blah
}
}
Having a new entity extend a data-type will make it inherit available methods for that data-type.
As discussed about autoboxing, this is the exact system im looking for but as its not passed discussions yet, for my new project (Forum System) witch I started the other day, do you think that I should go ahead and use my idea?, the user will be able to do faster interactions with datatypes, and if there is a function that does not support an object being passed, we can also do
$RawResource = $Resourtce->Raw();
//...
$Resource->Set($RawResource);
In my opinion, the time you spend writing this code, fixing this code, and cursing the fact that you can't use hundreds of PHP functions with your classes will outweigh any advantage this code may have.
Also, the developer who inherits your project will hate you.
Sounds like way too much trouble for... seemingly no benefit.
If you're worried about forgetting to escape stuff, stop escaping things altogether and start using parametrized queries.