Coder Social home page Coder Social logo

Comments (6)

Gerych1984 avatar Gerych1984 commented on June 12, 2024 1

If now explained in that way it makes sense, can you do the PR?

Yes, tomorrow morning - I'm at the phone now. Thanks

from form.

terabytesoftw avatar terabytesoftw commented on June 12, 2024

Can you give an example of what you want to do since collectAttributes() returns all the attributes of the formModel:

return $attributes;

from form.

Gerych1984 avatar Gerych1984 commented on June 12, 2024

Can you give an example of what you want to do since collectAttributes() returns all the attributes of the formModel:

return $attributes;

My current realization (writed today) in abstract class AbstractSearchForm extends FormModel. I'm using this to send as data in Vue form component. Or store in DB - when user save selected filters to quick access in future

public function toArray(bool $hideEmpty = true): array
{
    $array = [];
    /**
     *I think need $this->attributes or sometihng like $this->getAttributeNames() instead of ReflctionClass/ReflectionProperty
    */
    $class = new ReflectionClass($this);
    $properties = $class->getProperties(ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC);

    foreach ($properties as $property) {
        if ($property->isStatic()) {
            continue;
        }

        $name = $property->getName();
        $value = $this->getAttributeValue($name);

        if ($value === null || is_scalar($value)) {
            if ($hideEmpty && ($value === '' || $value === null)) {
                continue;
            }

            $array[$name] = $value;
        } elseif (is_array($value)) {
            if ($hideEmpty && count($value) === 0) {
                continue;
            }

            $array[$name] = $value;
        } elseif ($value instanceof FormModel) {
            $nested = $value->toArray($hideEmpty);

            if ($hideEmpty && count($nested) === 0) {
                continue;
            }

            $array[$name] = $nested;
        }
    }

    return $array;
}

from form.

terabytesoftw avatar terabytesoftw commented on June 12, 2024

Can you give an example of what you want to do since collectAttributes() returns all the attributes of the formModel:

return $attributes;

My current realization (writed today) in abstract class AbstractSearchForm extends FormModel. I'm using this to send as data in Vue form component. Or store in DB - when user save selected filters to quick access in future

public function toArray(bool $hideEmpty = true): array
{
    $array = [];
    /**
     *I think need $this->attributes or sometihng like $this->getAttributeNames() instead of ReflctionClass/ReflectionProperty
    */
    $class = new ReflectionClass($this);
    $properties = $class->getProperties(ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PUBLIC);

    foreach ($properties as $property) {
        if ($property->isStatic()) {
            continue;
        }

        $name = $property->getName();
        $value = $this->getAttributeValue($name);

        if ($value === null || is_scalar($value)) {
            if ($hideEmpty && ($value === '' || $value === null)) {
                continue;
            }

            $array[$name] = $value;
        } elseif (is_array($value)) {
            if ($hideEmpty && count($value) === 0) {
                continue;
            }

            $array[$name] = $value;
        } elseif ($value instanceof FormModel) {
            $nested = $value->toArray($hideEmpty);

            if ($hideEmpty && count($nested) === 0) {
                continue;
            }

            $array[$name] = $nested;
        }
    }

    return $array;
}

Why do a getAttributes(), if calling collectAttributes() has the same attributes, i don't understand.

$this->attributes = $this->collectAttributes();

from form.

Gerych1984 avatar Gerych1984 commented on June 12, 2024

Why do a getAttributes(), if calling collectAttributes() has the same attributes, i don't understand.

$this->attributes = $this->collectAttributes();


It will be call twice (or N times) and unnecessary recalculate attribute types just for get names. I can call collectAttributes in AbstractSearchModel::__construct, but for what? We already have this array, maybe just add method like in AR

public function attributes(): array
{  
    return array_keys($this->attributes);
}

from form.

terabytesoftw avatar terabytesoftw commented on June 12, 2024

Why do a getAttributes(), if calling collectAttributes() has the same attributes, i don't understand.

$this->attributes = $this->collectAttributes();

It will be call twice (or N times) and unnecessary recalculate attribute types just for get names. I can call collectAttributes in AbstractSearchModel::__construct, but for what? We already have this array, maybe just add method like in AR

public function attributes(): array
{  
    return array_keys($this->attributes);
}

If now explained in that way it makes sense, can you do the PR?

from form.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.