public function toDTO($filterByKey = null)
    {
        $dtos = [];
        foreach ($this->getAttributes() as $attrib => $data) {

            $attribDTO = $data; // default (unparsed or scalar)

            // we may need to parse the attribute relation or use
            // the model to get the subordinated Data Transfer Object.
            if ($data instanceof DataTransferObject || $data instanceof ModelCollection) {
                // sub DTO convert to array
                $attribDTO = $data->toDTO();
            }
            elseif (in_array($attrib, $this->relations) || method_exists($this, $attrib)) {
                // unparsed sub DTO passed - parse the DTO to make sure
                // we are working with NEM *NIS compliant* objects *always*.
                $related   = $this->resolveRelationship($attrib, $data);
                $attribDTO = $related->toDTO();
            }

            $dtos[$attrib] = $attribDTO;
        }

        if ($filterByKey && isset($dtos[$filterByKey]))
            return $dtos[$filterByKey];

        return $dtos;
    }
public function toDTO($filterByKey = null)
    {
        $dtos = [];
        foreach ($this->getAttributes() as $attrib => $data) {

            $attribDTO = $data; // default (unparsed or scalar)

            // we may need to parse the attribute relation or use
            // the model to get the subordinated Data Transfer Object.
            if ($data instanceof DataTransferObject || $data instanceof ModelCollection) {
                // sub DTO convert to array
                $attribDTO = $data->toDTO();
            }
            elseif (in_array($attrib, $this->relations) || method_exists($this, $attrib)) {
                // unparsed sub DTO passed - parse the DTO to make sure
                // we are working with NEM *NIS compliant* objects *always*.
                $related   = $this->resolveRelationship($attrib, $data);
                $attribDTO = $related->toDTO();
            }

            $dtos[$attrib] = $attribDTO;
        }

        if ($filterByKey && isset($dtos[$filterByKey]))
            return $dtos[$filterByKey];

        return $dtos;
    }
public function toDTO ( $ filterByKey = null ) { $ dtos = [ ] ; foreach ( $ this -> getAttributes ( ) as $ attrib => $ data ) { $ attribDTO = $ data ; // default (unparsed or scalar) // we may need to parse the attribute relation or use // the model to get the subordinated Data Transfer Object. if ( $ data instanceof DataTransferObject || $ data instanceof ModelCollection ) { // sub DTO convert to array $ attribDTO = $ data -> toDTO ( ) ; } elseif ( in_array ( $ attrib , $ this -> relations ) || method_exists ( $ this , $ attrib ) ) { // unparsed sub DTO passed - parse the DTO to make sure // we are working with NEM *NIS compliant* objects *always*. $ related = $ this -> resolveRelationship ( $ attrib , $ data ) ; $ attribDTO = $ related -> toDTO ( ) ; } $ dtos [ $ attrib ] = $ attribDTO ; } if ( $ filterByKey && isset ( $ dtos [ $ filterByKey ] ) ) return $ dtos [ $ filterByKey ] ; return $ dtos ; }
Generic helper to convert a Model instance to a Data Transfer Object.

This will make it easy to bridge implemented models to NEM *NIS compliant*
objects.

More complicated objects may overload this method to provide with finer
grained data transfer objects.

@see http://bob.nem.ninja/docs/  NIS API Documentation
@param   null|string $filterByKey    non-null will return only the named sub-dtos.
@return  array       Associative     array representation of the object *compliable* with NIS definition.
Generic helper to convert a Model instance to a Data Transfer Object .