<commit_before><?php
/*
 *  Copyright 2014 Opendi Software AG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 *  either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 */
namespace Opendi\Lang\Object;

trait CloneProperties
{
    public static function fromJson($json)
    {
        return static::fromObject(json_decode($json));
    }

    public static function fromArray($array)
    {
        $model = new static();

        foreach ($array as $key => $value) {
            if (property_exists($model, $key)) {
                $model->$key = $value;
            }
        }

        return $model;
    }

    /**
     * This method guarantees to first set scalar values and public properties,
     * then to set complex objects using set-methods.
     *
     * set-methods may need additional information which is only set
     * when using the right order.
     *
     * @param $object
     * @param null $before
     * @return static
     */
    public static function fromObject($object, $before = null)
    {
        $model = new static();

        /**
         * In some cases the model needs to be manipulated before
         * any properties are set. Example: Yext ECL models.
         */
        if (is_callable($before)) {
            $before($model);
        }

        $backlog = [];
        $members = get_object_vars($object);
        foreach ($members as $key => $value) {
            // TODO: determine if is_scalar is required here
            if (is_scalar($value) && property_exists($model, $key)) {
                $model->$key = $value;
            } else {
                // First set all properties, then set methods
                $backlog[$key] = $value;
            }
        }

        foreach ($backlog as $key => $value) {
            /**
             * Some objects don't provide public access to properties.
             * If we can't find the property, we try to find a setter method instead.
             *
             * Methods need to follow the usual setValue schema.
             *
             * @var $key string name of the property
             * @var $value string the value to set
             */
            $setter = 'set' . ucfirst($key);
            if (method_exists($model, $setter)) {
                $model->{$setter}($value);
            }
        }

        return $model;
    }
}
<commit_msg>Improved error handling in CloneProperties trait<commit_after><?php
/*
 *  Copyright 2014 Opendi Software AG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed
 *  on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 *  either express or implied. See the License for the specific
 *  language governing permissions and limitations under the License.
 */
namespace Opendi\Lang\Object;

use Closure;
use InvalidArgumentException;

trait CloneProperties
{
    public static function fromJson($json)
    {
        return static::fromObject(json_decode($json));
    }

    /**
     * Returns a new object with properties set from
     * matching array keys
     *
     * @param $array
     * @throws InvalidArgumentException
     * @return static
     */
    public static function fromArray($array)
    {
        if (!is_array($array)) {
            throw new InvalidArgumentException("Expected array, got " . gettype($array));
        }

        $model = new static();

        foreach ($array as $key => $value) {
            if (property_exists($model, $key)) {
                $model->$key = $value;
            }
        }

        return $model;
    }

    /**
     * This method guarantees to first set scalar values and public properties,
     * then to set complex objects using set-methods.
     *
     * set-methods may need additional information which is only set
     * when using the right order.
     *
     * @param $object
     * @param Closure $before
     * @throws InvalidArgumentException
     * @return static
     */
    public static function fromObject($object, $before = null)
    {
        if (!is_object($object)) {
            throw new InvalidArgumentException("Expected object, got " . gettype($object));
        }

        $model = new static();

        /**
         * In some cases the model needs to be manipulated before
         * any properties are set. Example: Yext ECL models.
         */
        if (is_callable($before)) {
            $before($model);
        }

        $backlog = [];
        $members = get_object_vars($object);
        foreach ($members as $key => $value) {
            // TODO: determine if is_scalar is required here
            if (is_scalar($value) && property_exists($model, $key)) {
                $model->$key = $value;
            } else {
                // First set all properties, then set methods
                $backlog[$key] = $value;
            }
        }

        foreach ($backlog as $key => $value) {
            /**
             * Some objects don't provide public access to properties.
             * If we can't find the property, we try to find a setter method instead.
             *
             * Methods need to follow the usual setValue schema.
             *
             * @var $key string name of the property
             * @var $value string the value to set
             */
            $setter = 'set' . ucfirst($key);
            if (method_exists($model, $setter)) {
                $model->{$setter}($value);
            }
        }

        return $model;
    }
}

{'fraction_non_alphanumeric': 0.09828921885087152, 'fraction_numerical': 0.0025823111684958036, 'mean_word_length': 1.899859616284511, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3197609', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1579, 'n_words': 586}