dongpao5127 2015-02-13 02:28
浏览 13
已采纳

使用PHP的数组函数进行值对象优化

I have these two methods inside a Value Object:

public final function unlockableLetters()
{
    return [
        [],
        ['A', 'B'],
        ['C']
    ][$this->level];
}

public final function unlockedLetters()
{
    return [
        [],
        ['A', 'B'],
        ['A', 'B', 'C'],
    ][$this->level];
}

The first one returns unlockable elements of each level.

  • At level 0, returns [].
  • At level 1 returns A and B.
  • At level 2 returns C.

The second one returns unlocked elements.

  • At level 0, returns [].
  • At level 1 returns A and B. // NULL + A + B
  • At level 2 returns A, B and C. // A + B + C

There is a way to use array functions of php that create an merge of array returned by unlockLetters() from index 0 to current $this->level?

EDIT I want to leave letters only inside unlockableLetters() method. So, unlockedLetters() can "build" its own return value from first method. If something change in the former, change will occur in the latter.

  • 写回答

3条回答 默认 最新

  • doupingtang9627 2015-02-13 03:10
    关注

    Its not completely clear what you want. As I said in my comment:

    agree with @deceze, its not clear what you want to merge... are you saying you wantto generate the return of unlockedLetters() from unlockLetters() using array functions instead of hard coding it? Also i think better method names are in order here because that also makes it confusing... i would use getUnlockableLetters and getUnlockedLetters() or something along those lines depending on how these are used.

    However, what i think you mean might look something like this:

    // add a property so we can reference it - you might want to adjust
    // the visibility to private or public depending on its nature
    protected $unlockableLetters = [
        [],
        ['A', 'B'],
        ['C']
    ];
    
    public final function unlockLetters()
    {
        // just return the reference to the letters for the level
        return $this->unlockableLetters[$this->level];
    }
    
    public final function unlockedLetters()
    {
        // slice off the array of latters from the lowest level 
        // to the current one - note the +1 since array_slice
        // wants a length and our offsets are 0-based and thus length-1
        $data = array_slice($this->unlockableLetters, 0, $this->level+1);
    
        // use array reduce to build a single array that has
        // all the letters for levels 0 --> $this->level
        return array_reduce($data, function ($merged, $datum) {
           return array_merge($merged, $datum);
        }, []);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄