XeroPHP\Application::saveAll PHP Method

saveAll() public method

public saveAll ( Collection | array $objects ) : Response
$objects XeroPHP\Remote\Collection | array
return XeroPHP\Remote\Response
    public function saveAll($objects)
    {
        //Just get one type to compare with, doesn't matter which.
        $current_object = current($objects);
        /**
         * @var Object $type
         */
        $type = get_class($current_object);
        $has_guid = $current_object->hasGUID();
        $object_arrays = [];
        foreach ($objects as $object) {
            if ($type !== get_class($object)) {
                throw new Exception('Array passed to ->saveAll() must be homogeneous.');
            }
            // Check if we have a GUID
            if ($object->hasGUID() && $has_guid === false) {
                $has_guid = true;
            }
            $object_arrays[] = $object->toStringArray();
        }
        $request_method = $has_guid ? Request::METHOD_POST : Request::METHOD_PUT;
        $url = new URL($this, $type::getResourceURI());
        $request = new Request($this, $url, $request_method);
        //This might need to be parsed and stored some day.
        $root_node_name = Helpers::pluralize($type::getRootNodeName());
        $data = [$root_node_name => $object_arrays];
        $request->setBody(Helpers::arrayToXML($data));
        $request->setParameter('SummarizeErrors', 'false');
        $request->send();
        $response = $request->getResponse();
        foreach ($response->getElements() as $element_index => $element) {
            if ($response->getErrorsForElement($element_index) === null) {
                $objects[$element_index]->fromStringArray($element);
                $objects[$element_index]->setClean();
            }
        }
        return $response;
    }