如何比较一个对象数组,并在匹配时合并共享属性的值?

问题描述:

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?

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;
    }
}