0

Background

In a project I am working on, I have created a Builder class with a few methods that builds its objects in different ways. Let's call it CompanyBuilder.

CompanyBuilder has three objects its in charge of building: Company, Employee and Skill. Inside each assembly method it injects some dependencies inside each object.

Objects

These are the objects its in charge of building, I realize there's four here, but there are three main objects.

class Company {
    public $name;
    public $employees = array();

    public function __construct($name) {
        $this->name = $name;
    }
}

class Person {
    public $name;
}

class Employee extends Person {
    public $job;
    public $skills = array();
}

class Skill {
    public $name;

    public function __construct($name) {
        $this->name = $name;
    }
}

Builder

There are three methods in the builder aimed to provide flexibility when building the objects. The one that generated this question looks like this:

$company = $companyBuilder->buildCompany(
    'Acme',
    $dependency,
    $employee,
    array(
        $skillOne,
        $skillTwo,
        $skillThree
    )
);

The result of this would be one company object, with one employee which has one or many skills. This approach meets a business goal. However, there is a problem.

The Problem

The problem arises when I have different Company objects, that contain the same name property. I want to merge the employees into one Company object, if the Company names match.

I had the thought I could create a mergeEmployees method, that could take in an indeterminate amount of Company objects as arguments, that are captured using func_get_args(), which turns them into an array of objects. I've tried this solution and it hasn't worked for me.

I want to find a company, in that array, with the same name and merge it's employees. So if the args array looks like this:

Company Object
(
    [name] => Acme
    [employees] => Array
        (
            [0] => Employee Object
                (
                    [job] => Designer
                    [skills] => Array
                        (
                            [0] => Skill Object
                                (
                                    [name] => web
                                )

                            [1] => Skill Object
                                (
                                    [name] => ui
                                )

                        )

                    [name] => Jacob
                )

        )

)
Company Object
(
    [name] => Scholastic
    [employees] => Array
        (
            [0] => Employee Object
                (
                    [job] => Developer
                    [skills] => Array
                        (
                            [0] => Skill Object
                                (
                                    [name] => java
                                )

                            [1] => Skill Object
                                (
                                    [name] => c#
                                )

                        )

                    [name] => Steve
                )

        )

)
Company Object
(
    [name] => Acme
    [employees] => Array
        (
            [0] => Employee Object
                (
                    [job] => Designer
                    [skills] => Array
                        (
                            [0] => Skill Object
                                (
                                    [name] => ux
                                )

                            [1] => Skill Object
                                (
                                    [name] => css
                                )

                        )

                    [name] => Jacob
                )

        )

)

How would I do comparisons on this array of objects, find that the Employee Jacob should have his skills of 'UX' and 'CSS' and merge it (reliably) with those skills of his previous Employee object?

I have tried using array_reduce() but it wasn't working for me --- could definitely have been misusing it since it's intended to:

Iteratively reduce the array to a single value using a callback function

I would want to use array_filter() but it only passes me one item in the callback function, which isn't enough for a comparison. Unless I'm missing something.

What's the best way to do something like this?

8
  • two objects that are the same but have a different properties Surely that is the definition of not the same Commented Apr 12, 2017 at 19:18
  • I had realized that shortly after posting, and made an edit to make the intent clearer. Commented Apr 12, 2017 at 19:19
  • What about the same employee in two different companies? Commented Apr 12, 2017 at 19:21
  • 1
    You should prevent the creation of a new object and just update the first one. The object reference will do the rest of the job to you. Commented Apr 12, 2017 at 19:22
  • @Luka In that case the Employee's skills wouldn't need to be merged since they're utilized in a different company. Commented Apr 12, 2017 at 19:23

1 Answer 1

1

I would suggest an implementation already in the buildCompany method:

function buildCompany($companyName, $employeeName, $skills){
    //let's say existing companies are in $companies array
    $existingCompany = false;

    foreach($companies as $key => $company){
        if($company->name === $companyName){
            $existingCompany = true;

            $existingEmployee = false;
            foreach($company->employees as $keyE => $employee){
                if($employee->name === $employeeName){
                    $existingEmployee = true;
                    $employee->skills = array_unique(array_merge($employee->skills, $skills), SORT_REGULAR); //http://stackoverflow.com/questions/13469803/php-merging-two-array-into-one-array-also-remove-duplicates
                    break;
                }
            }

            if(!$existingEmployee){
                $company->employees[] = new Employee($employeeName, $skills); // add a constructor to the Employee, i see it's missing (or just not provided)
            }
        }
    }
    if(!$existingCompany){
        $temp = new Company($companyName);
        $temp->employees[] = new Employee($employeeName, $skills); // add a constructor to the Employee, i see it's missing (or just not provided)
        $companies[] = $temp;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

What's the [count($companies) - 1]->employees bit doing? Is it simply just adding the employee to the last added company? It seems like it'd be better to: $company = new Company($companyName); $company->employees[] = new Employee($employeeName, $skills); $companies[] = $company; or something, otherwise that looks like what I'm looking for. I added a check for empty on the companies array before the first foreach.
Ok, i agree with both of your improvements, nice to see you understand the code :)
However, checking for an empty array isn't necessary because foreach does that for you already
True, if it's initialized properly. ;)

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.