PHP Classes

Lorem::layout() and Lorem::execute()

Recommend this page to a friend!

      Flex Lorem  >  All threads  >  Lorem::layout() and Lorem::execute()  >  (Un) Subscribe thread alerts  
Subject:Lorem::layout() and Lorem::execute()
Summary:discussion
Messages:1
Author:smori
Date:2012-08-19 10:27:01
 

  1. Lorem::layout() and Lorem::execute()   Reply   Report abuse  
Picture of smori smori - 2012-08-19 10:27:01
Hi, I downloaded latest version (2012-08-18).

I wrote simple script but it didn't work:
<?php
include_once 'element.php';
include_once 'lorem.php';
echo Lorem::layout('{Lorem.foo}'),PHP_EOL;
// should return '--not found--'

I checked the code and noticed some points.

diff file is on https://gist.github.com/3393917

1. Lorem::layout()
I think Lorem::layout() can be implemented a little more simply.

We don't need to call str_replace("}", "}\n", $source) by changing
regex of preg_match_all from '/\{(.*)\}/i' to '/\{([^}]+)\}/i'.

If we use flag PREG_SET_ORDER, the results array will be like this:
php > $regex = '/\{([^}]+)\}/i';
php > $str = '{Lorem.foo}{Lorem.bar}';
php > preg_match_all($regex, $str, $matches, PREG_SET_ORDER);
php > print_r($matches);
Array
(
[0] => Array
(
[0] => {Lorem.foo}
[1] => Lorem.foo
)

[1] => Array
(
[0] => {Lorem.bar}
[1] => Lorem.bar
)

)
This will simplify the code (no need to use $index in foreach).

preg_match returns empty array if nothing matched,
and so isset($matches[1]) will return true always.
We can write foreach without checking it.

2. Lorem::execute()
explode(",", $str) returns array with at least 1 element.
So we can remove if statement of count($strgs).

$func checks $f is available, but not used to getting plugin.
As a result, Lorem::execute() never returns '--not found--'.

We don't need to check count($strgs) for call_user_func_array().