0

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
  • You've spent a lot of time setting up your question, but your actual question is a bit terse and hard to understand. I for one am very unclear on what it is that you're asking. Commented Feb 13, 2015 at 2:32
  • 1
    I 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. Commented Feb 13, 2015 at 2:38
  • I've edited unlockedLetters() to unlockableLetters(). Is more clear. Thank you Commented Feb 13, 2015 at 4:36

3 Answers 3

2

It's not completely clear what you want. As I said in my comment:

agree with @deceze, it's not clear what you want to merge... are you saying you want to 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);
    }, []);
}
Sign up to request clarification or add additional context in comments.

Comments

2

As long as I understand your question, you are looking for this

public final function unlockableLetters() {
    $unlockables = [
        [],
        ['A', 'B'],
        ['C']
    ];
    return array_reduce(array_slice($unlockables, 0, $this->level + 1), function($carry, $item){
        return array_merge($carry, $item);
    }, []);
}

Comments

1

I agree with other developers here. Question is not clear. But here is my guess:

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

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

1 Comment

Ok, sorry. I've updated the question. The point is I do not want to change both method if something change. I want to leave $arr only inside unlockableLetters().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.